learnpath.class.php 447 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  202. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  203. Database::query($sql);
  204. $this->lp_view_id = Database::insert_id();
  205. if ($this->debug > 2) {
  206. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  207. }
  208. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  209. Database::query($sql);
  210. }
  211. // Initialise items.
  212. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  213. $sql = "SELECT * FROM $lp_item_table
  214. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  215. ORDER BY parent_item_id, display_order";
  216. $res = Database::query($sql);
  217. if ($this->debug > 2) {
  218. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  219. error_log('-- Start while--', 0);
  220. }
  221. $lp_item_id_list = array();
  222. while ($row = Database::fetch_array($res)) {
  223. $lp_item_id_list[] = $row['id'];
  224. switch ($this->type) {
  225. case 3: //aicc
  226. $oItem = new aiccItem('db', $row['id'], $course_id);
  227. if (is_object($oItem)) {
  228. $my_item_id = $oItem->get_id();
  229. $oItem->set_lp_view($this->lp_view_id, $course_id);
  230. $oItem->set_prevent_reinit($this->prevent_reinit);
  231. // Don't use reference here as the next loop will make the pointed object change.
  232. $this->items[$my_item_id] = $oItem;
  233. $this->refs_list[$oItem->ref] = $my_item_id;
  234. if ($this->debug > 2) {
  235. error_log(
  236. 'New LP - learnpath::__construct() - ' .
  237. 'aicc object with id ' . $my_item_id .
  238. ' set in items[]', 0);
  239. }
  240. }
  241. break;
  242. case 2:
  243. $oItem = new scormItem('db', $row['id'], $course_id);
  244. if (is_object($oItem)) {
  245. $my_item_id = $oItem->get_id();
  246. $oItem->set_lp_view($this->lp_view_id, $course_id);
  247. $oItem->set_prevent_reinit($this->prevent_reinit);
  248. // Don't use reference here as the next loop will make the pointed object change.
  249. $this->items[$my_item_id] = $oItem;
  250. $this->refs_list[$oItem->ref] = $my_item_id;
  251. if ($this->debug > 2) {
  252. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  253. }
  254. }
  255. break;
  256. case 1:
  257. default:
  258. if ($this->debug > 2) {
  259. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  260. }
  261. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  262. if ($this->debug > 2) {
  263. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  264. }
  265. if (is_object($oItem)) {
  266. $my_item_id = $oItem->get_id();
  267. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  268. $oItem->set_prevent_reinit($this->prevent_reinit);
  269. // Don't use reference here as the next loop will make the pointed object change.
  270. $this->items[$my_item_id] = $oItem;
  271. $this->refs_list[$my_item_id] = $my_item_id;
  272. if ($this->debug > 2) {
  273. error_log(
  274. 'New LP - learnpath::__construct() ' . __LINE__ .
  275. ' - object with id ' . $my_item_id . ' set in items[]',
  276. 0);
  277. }
  278. }
  279. break;
  280. }
  281. // Setting the object level with variable $this->items[$i][parent]
  282. foreach ($this->items as $itemLPObject) {
  283. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  284. $itemLPObject->level = $level;
  285. }
  286. // Setting the view in the item object.
  287. if (is_object($this->items[$row['id']])) {
  288. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  289. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  290. $this->items[$row['id']]->current_start_time = 0;
  291. $this->items[$row['id']]->current_stop_time = 0;
  292. }
  293. }
  294. }
  295. if ($this->debug > 2) {
  296. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  297. }
  298. if (!empty($lp_item_id_list)) {
  299. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  300. if (!empty($lp_item_id_list_to_string)) {
  301. // Get last viewing vars.
  302. $lp_item_view_table = Database:: get_course_table(
  303. TABLE_LP_ITEM_VIEW
  304. );
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $lp_item_view_table
  308. WHERE
  309. c_id = $course_id AND
  310. lp_view_id = ".$this->lp_view_id." AND
  311. lp_item_id IN ('".$lp_item_id_list_to_string."')
  312. ORDER BY view_count DESC ";
  313. if ($this->debug > 2) {
  314. error_log(
  315. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  316. 0
  317. );
  318. }
  319. $status_list = array();
  320. $res = Database::query($sql);
  321. while ($row = Database:: fetch_array($res)) {
  322. $status_list[$row['lp_item_id']] = $row['status'];
  323. }
  324. foreach ($lp_item_id_list as $item_id) {
  325. if (isset($status_list[$item_id])) {
  326. $status = $status_list[$item_id];
  327. if (is_object($this->items[$item_id])) {
  328. $this->items[$item_id]->set_status($status);
  329. if (empty($status)) {
  330. $this->items[$item_id]->set_status(
  331. $this->default_status
  332. );
  333. }
  334. }
  335. } else {
  336. if (!api_is_invitee()) {
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  343. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  344. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  345. if ($this->debug > 2) {
  346. error_log(
  347. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  348. 0
  349. );
  350. }
  351. $this->items[$item_id]->set_lp_view(
  352. $this->lp_view_id,
  353. $course_id
  354. );
  355. Database::query($sql);
  356. $insertId = Database::insert_id();
  357. $sql = "UPDATE $lp_item_view_table SET id = iid
  358. WHERE iid = $insertId";
  359. Database::query($sql);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. $this->ordered_items = self::get_flat_ordered_items_list(
  366. $this->get_id(),
  367. 0,
  368. $course_id
  369. );
  370. $this->max_ordered_items = 0;
  371. foreach ($this->ordered_items as $index => $dummy) {
  372. if ($index > $this->max_ordered_items && !empty($dummy)) {
  373. $this->max_ordered_items = $index;
  374. }
  375. }
  376. // TODO: Define the current item better.
  377. $this->first();
  378. if ($this->debug > 2) {
  379. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  380. }
  381. return true;
  382. }
  383. /**
  384. * @return string
  385. */
  386. public function getCourseCode()
  387. {
  388. return $this->cc;
  389. }
  390. /**
  391. * @return int
  392. */
  393. public function get_course_int_id()
  394. {
  395. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  396. }
  397. /**
  398. * @param $course_id
  399. * @return int
  400. */
  401. public function set_course_int_id($course_id)
  402. {
  403. return $this->course_int_id = intval($course_id);
  404. }
  405. /**
  406. * Get the depth level of LP item
  407. * @param $in_tab_items
  408. * @param $in_current_item_id
  409. * @return int
  410. */
  411. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  412. {
  413. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  414. if ($parent_item_id == 0) {
  415. return 0;
  416. } else {
  417. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  418. }
  419. }
  420. /**
  421. * Function rewritten based on old_add_item() from Yannick Warnier.
  422. * Due the fact that users can decide where the item should come, I had to overlook this function and
  423. * I found it better to rewrite it. Old function is still available.
  424. * Added also the possibility to add a description.
  425. *
  426. * @param int $parent
  427. * @param int $previous
  428. * @param string $type
  429. * @param int resource ID (ref)
  430. * @param string $title
  431. * @param string $description
  432. * @param int $prerequisites
  433. * @param int $max_time_allowed
  434. * @return int
  435. */
  436. public function add_item(
  437. $parent,
  438. $previous,
  439. $type = 'dokeos_chapter',
  440. $id,
  441. $title,
  442. $description,
  443. $prerequisites = 0,
  444. $max_time_allowed = 0
  445. ) {
  446. $course_id = api_get_course_int_id();
  447. if ($this->debug > 0) {
  448. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  449. }
  450. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  451. $_course = api_get_course_info();
  452. $parent = intval($parent);
  453. $previous = intval($previous);
  454. $id = intval($id);
  455. $max_time_allowed = htmlentities($max_time_allowed);
  456. if (empty($max_time_allowed)) {
  457. $max_time_allowed = 0;
  458. }
  459. $sql = "SELECT COUNT(id) AS num
  460. FROM $tbl_lp_item
  461. WHERE
  462. c_id = $course_id AND
  463. lp_id = " . $this->get_id() . " AND
  464. parent_item_id = " . $parent;
  465. $res_count = Database::query($sql);
  466. $row = Database :: fetch_array($res_count);
  467. $num = $row['num'];
  468. if ($num > 0) {
  469. if ($previous == 0) {
  470. $sql = "SELECT id, next_item_id, display_order
  471. FROM " . $tbl_lp_item . "
  472. WHERE
  473. c_id = $course_id AND
  474. lp_id = " . $this->get_id() . " AND
  475. parent_item_id = " . $parent . " AND
  476. previous_item_id = 0 OR
  477. previous_item_id=" . $parent;
  478. $result = Database::query($sql);
  479. $row = Database :: fetch_array($result);
  480. $tmp_previous = 0;
  481. $next = $row['id'];
  482. $display_order = 0;
  483. } else {
  484. $previous = (int) $previous;
  485. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  486. FROM $tbl_lp_item
  487. WHERE
  488. c_id = $course_id AND
  489. lp_id = " . $this->get_id() . " AND
  490. id = " . $previous;
  491. $result = Database::query($sql);
  492. $row = Database:: fetch_array($result);
  493. $tmp_previous = $row['id'];
  494. $next = $row['next_item_id'];
  495. $display_order = $row['display_order'];
  496. }
  497. } else {
  498. $tmp_previous = 0;
  499. $next = 0;
  500. $display_order = 0;
  501. }
  502. $id = intval($id);
  503. $typeCleaned = Database::escape_string($type);
  504. if ($type == 'quiz') {
  505. $sql = 'SELECT SUM(ponderation)
  506. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  507. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  508. ON
  509. quiz_question.id = quiz_rel_question.question_id AND
  510. quiz_question.c_id = quiz_rel_question.c_id
  511. WHERE
  512. quiz_rel_question.exercice_id = '.$id." AND
  513. quiz_question.c_id = $course_id AND
  514. quiz_rel_question.c_id = $course_id ";
  515. $rsQuiz = Database::query($sql);
  516. $max_score = Database :: result($rsQuiz, 0, 0);
  517. // Disabling the exercise if we add it inside a LP
  518. $exercise = new Exercise();
  519. $exercise->read($id);
  520. $exercise->disable();
  521. $exercise->save();
  522. } else {
  523. $max_score = 100;
  524. }
  525. $params = array(
  526. "c_id" => $course_id,
  527. "lp_id" => $this->get_id(),
  528. "item_type" =>$typeCleaned ,
  529. "ref" => '',
  530. "title" =>$title ,
  531. "description" => $description,
  532. "path" => $id,
  533. "max_score" => $max_score,
  534. "parent_item_id" => $parent,
  535. "previous_item_id" => $previous,
  536. "next_item_id" => $next,
  537. "display_order" => $display_order +1,
  538. "prerequisite" => $prerequisites,
  539. "max_time_allowed" => $max_time_allowed
  540. );
  541. if ($prerequisites != 0) {
  542. $params['prerequisite'] = $prerequisites;
  543. }
  544. $new_item_id = Database::insert($tbl_lp_item, $params);
  545. if ($this->debug > 2) {
  546. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  547. }
  548. if ($new_item_id) {
  549. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  550. Database::query($sql);
  551. // Update the item that should come after the new item.
  552. $sql = " UPDATE $tbl_lp_item SET
  553. previous_item_id = $new_item_id,
  554. next_item_id = $new_item_id,
  555. id = $new_item_id
  556. WHERE iid = $new_item_id";
  557. Database::query($sql);
  558. // Update all the items after the new item.
  559. $sql = "UPDATE " . $tbl_lp_item . "
  560. SET display_order = display_order + 1
  561. WHERE
  562. c_id = $course_id AND
  563. lp_id = " . $this->get_id() . " AND
  564. id <> " . $new_item_id . " AND
  565. parent_item_id = " . $parent . " AND
  566. display_order > " . $display_order;
  567. Database::query($sql);
  568. // Update the item that should come after the new item.
  569. $sql = "UPDATE " . $tbl_lp_item . "
  570. SET ref = " . $new_item_id . "
  571. WHERE c_id = $course_id AND id = " . $new_item_id;
  572. Database::query($sql);
  573. // Upload audio.
  574. if (!empty($_FILES['mp3']['name'])) {
  575. // Create the audio folder if it does not exist yet.
  576. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  577. if (!is_dir($filepath . 'audio')) {
  578. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  579. $audio_id = add_document(
  580. $_course,
  581. '/audio',
  582. 'folder',
  583. 0,
  584. 'audio'
  585. );
  586. api_item_property_update(
  587. $_course,
  588. TOOL_DOCUMENT,
  589. $audio_id,
  590. 'FolderCreated',
  591. api_get_user_id(),
  592. null,
  593. null,
  594. null,
  595. null,
  596. api_get_session_id()
  597. );
  598. api_item_property_update(
  599. $_course,
  600. TOOL_DOCUMENT,
  601. $audio_id,
  602. 'invisible',
  603. api_get_user_id(),
  604. null,
  605. null,
  606. null,
  607. null,
  608. api_get_session_id()
  609. );
  610. }
  611. $file_path = handle_uploaded_document(
  612. $_course,
  613. $_FILES['mp3'],
  614. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  615. '/audio',
  616. api_get_user_id(),
  617. '',
  618. '',
  619. '',
  620. '',
  621. false
  622. );
  623. // Getting the filename only.
  624. $file_components = explode('/', $file_path);
  625. $file = $file_components[count($file_components) - 1];
  626. // Store the mp3 file in the lp_item table.
  627. $sql = "UPDATE $tbl_lp_item SET
  628. audio = '" . Database::escape_string($file) . "'
  629. WHERE id = '" . intval($new_item_id) . "'";
  630. Database::query($sql);
  631. }
  632. }
  633. return $new_item_id;
  634. }
  635. /**
  636. * Static admin function allowing addition of a learnpath to a course.
  637. * @param string Course code
  638. * @param string Learnpath name
  639. * @param string Learnpath description string, if provided
  640. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  641. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  642. * @param string Zip file containing the learnpath or directory containing the learnpath
  643. * @return integer The new learnpath ID on success, 0 on failure
  644. */
  645. public static function add_lp(
  646. $course,
  647. $name,
  648. $description = '',
  649. $learnpath = 'guess',
  650. $origin = 'zip',
  651. $zipname = '',
  652. $publicated_on = '',
  653. $expired_on = '',
  654. $categoryId = 0
  655. ) {
  656. global $charset;
  657. $course_id = api_get_course_int_id();
  658. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  659. // Check course code exists.
  660. // Check lp_name doesn't exist, otherwise append something.
  661. $i = 0;
  662. $name = Database::escape_string($name);
  663. $categoryId = intval($categoryId);
  664. // Session id.
  665. $session_id = api_get_session_id();
  666. $check_name = "SELECT * FROM $tbl_lp
  667. WHERE c_id = $course_id AND name = '$name'";
  668. $res_name = Database::query($check_name);
  669. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  670. //by default the publication date is the same that the creation date
  671. //The behaviour above was changed due BT#2800
  672. global $_custom;
  673. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  674. $publicated_on = '';
  675. } else {
  676. $publicated_on = api_get_utc_datetime();
  677. }
  678. } else {
  679. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  680. }
  681. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  682. $expired_on = '';
  683. } else {
  684. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  685. }
  686. while (Database :: num_rows($res_name)) {
  687. // There is already one such name, update the current one a bit.
  688. $i++;
  689. $name = $name . ' - ' . $i;
  690. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  691. $res_name = Database::query($check_name);
  692. }
  693. // New name does not exist yet; keep it.
  694. // Escape description.
  695. // Kevin: added htmlentities().
  696. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  697. $type = 1;
  698. switch ($learnpath) {
  699. case 'guess':
  700. break;
  701. case 'dokeos':
  702. case 'chamilo':
  703. $type = 1;
  704. break;
  705. case 'aicc':
  706. break;
  707. }
  708. switch ($origin) {
  709. case 'zip':
  710. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  711. break;
  712. case 'manual':
  713. default:
  714. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  715. $res_max = Database::query($get_max);
  716. if (Database :: num_rows($res_max) < 1) {
  717. $dsp = 1;
  718. } else {
  719. $row = Database :: fetch_array($res_max);
  720. $dsp = $row[0] + 1;
  721. }
  722. $params = [
  723. 'c_id' => $course_id,
  724. 'lp_type' => $type,
  725. 'name' => $name,
  726. 'description' => $description,
  727. 'path' => '',
  728. 'default_view_mod' => 'embedded',
  729. 'default_encoding' => 'UTF-8',
  730. 'display_order' => $dsp,
  731. 'content_maker' => 'Chamilo',
  732. 'content_local' => 'local',
  733. 'js_lib' => '',
  734. 'session_id' => $session_id,
  735. 'created_on' => api_get_utc_datetime(),
  736. 'publicated_on' => $publicated_on,
  737. 'expired_on' => $expired_on,
  738. 'category_id' => $categoryId
  739. ];
  740. $id = Database::insert($tbl_lp, $params);
  741. if ($id > 0) {
  742. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  743. Database::query($sql);
  744. $course_info = api_get_course_info();
  745. // Insert into item_property.
  746. api_item_property_update(
  747. $course_info,
  748. TOOL_LEARNPATH,
  749. $id,
  750. 'LearnpathAdded',
  751. api_get_user_id()
  752. );
  753. api_set_default_visibility($id, TOOL_LEARNPATH);
  754. return $id;
  755. }
  756. break;
  757. }
  758. }
  759. /**
  760. * Auto completes the parents of an item in case it's been completed or passed
  761. * @param integer $item Optional ID of the item from which to look for parents
  762. */
  763. public function autocomplete_parents($item)
  764. {
  765. $debug = $this->debug;
  766. if ($debug) {
  767. error_log('Learnpath::autocomplete_parents()', 0);
  768. }
  769. if (empty($item)) {
  770. $item = $this->current;
  771. }
  772. $currentItem = $this->getItem($item);
  773. if ($currentItem) {
  774. $parent_id = $currentItem->get_parent();
  775. $parent = $this->getItem($parent_id);
  776. if ($parent) {
  777. // if $item points to an object and there is a parent.
  778. if ($debug) {
  779. error_log(
  780. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  781. 0
  782. );
  783. }
  784. // New experiment including failed and browsed in completed status.
  785. //$current_status = $currentItem->get_status();
  786. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  787. // Fixes chapter auto complete
  788. if (true) {
  789. // If the current item is completed or passes or succeeded.
  790. $updateParentStatus = true;
  791. if ($debug) {
  792. error_log('Status of current item is alright', 0);
  793. }
  794. foreach ($parent->get_children() as $childItemId) {
  795. $childItem = $this->getItem($childItemId);
  796. // If children was not set try to get the info
  797. if (empty($childItem->db_item_view_id)) {
  798. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  799. }
  800. // Check all his brothers (parent's children) for completion status.
  801. if ($childItemId != $item) {
  802. if ($debug) {
  803. error_log(
  804. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  805. 0
  806. );
  807. }
  808. // Trying completing parents of failed and browsed items as well.
  809. if ($childItem->status_is(
  810. array(
  811. 'completed',
  812. 'passed',
  813. 'succeeded',
  814. 'browsed',
  815. 'failed'
  816. )
  817. )
  818. ) {
  819. // Keep completion status to true.
  820. continue;
  821. } else {
  822. if ($debug > 2) {
  823. error_log(
  824. '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,
  825. 0
  826. );
  827. }
  828. $updateParentStatus = false;
  829. break;
  830. }
  831. }
  832. }
  833. if ($updateParentStatus) {
  834. // If all the children were completed:
  835. $parent->set_status('completed');
  836. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  837. // Force the status to "completed"
  838. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  839. $this->update_queue[$parent->get_id()] = 'completed';
  840. if ($debug) {
  841. error_log(
  842. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  843. print_r($this->update_queue, 1),
  844. 0
  845. );
  846. }
  847. // Recursive call.
  848. $this->autocomplete_parents($parent->get_id());
  849. }
  850. }
  851. } else {
  852. if ($debug) {
  853. error_log("Parent #$parent_id does not exists");
  854. }
  855. }
  856. } else {
  857. if ($debug) {
  858. error_log("#$item is an item that doesn't have parents");
  859. }
  860. }
  861. }
  862. /**
  863. * Auto saves the current results into the database for the whole learnpath
  864. */
  865. public function autosave()
  866. {
  867. if ($this->debug > 0) {
  868. error_log('New LP - In learnpath::autosave()', 0);
  869. }
  870. // TODO: Add save operations for the learnpath itself.
  871. }
  872. /**
  873. * Closes the current resource
  874. *
  875. * Stops the timer
  876. * Saves into the database if required
  877. * Clears the current resource data from this object
  878. * @return boolean True on success, false on failure
  879. */
  880. public function close()
  881. {
  882. if ($this->debug > 0) {
  883. error_log('New LP - In learnpath::close()', 0);
  884. }
  885. if (empty ($this->lp_id)) {
  886. $this->error = 'Trying to close this learnpath but no ID is set';
  887. return false;
  888. }
  889. $this->current_time_stop = time();
  890. $this->ordered_items = array();
  891. $this->index = 0;
  892. unset ($this->lp_id);
  893. //unset other stuff
  894. return true;
  895. }
  896. /**
  897. * Static admin function allowing removal of a learnpath
  898. * @param string Course code
  899. * @param integer Learnpath ID
  900. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  901. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  902. */
  903. public function delete($course = null, $id = null, $delete = 'keep')
  904. {
  905. $course_id = api_get_course_int_id();
  906. // TODO: Implement a way of getting this to work when the current object is not set.
  907. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  908. // If an ID is specifically given and the current LP is not the same, prevent delete.
  909. if (!empty ($id) && ($id != $this->lp_id)) {
  910. return false;
  911. }
  912. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  913. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  914. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  915. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  916. // Delete lp item id.
  917. foreach ($this->items as $id => $dummy) {
  918. $sql = "DELETE FROM $lp_item_view
  919. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  920. Database::query($sql);
  921. }
  922. // Proposed by Christophe (nickname: clefevre)
  923. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  924. Database::query($sql);
  925. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  926. Database::query($sql);
  927. self::toggle_publish($this->lp_id, 'i');
  928. if ($this->type == 2 || $this->type == 3) {
  929. // This is a scorm learning path, delete the files as well.
  930. $sql = "SELECT path FROM $lp
  931. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  932. $res = Database::query($sql);
  933. if (Database :: num_rows($res) > 0) {
  934. $row = Database :: fetch_array($res);
  935. $path = $row['path'];
  936. $sql = "SELECT id FROM $lp
  937. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  938. $res = Database::query($sql);
  939. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  940. if ($this->debug > 2) {
  941. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  942. }
  943. } else {
  944. // No other LP uses that directory, delete it.
  945. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  946. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  947. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  948. if ($this->debug > 2) {
  949. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  950. }
  951. // Proposed by Christophe (clefevre).
  952. if (strcmp(substr($path, -2), "/.") == 0) {
  953. $path = substr($path, 0, -1); // Remove "." at the end.
  954. }
  955. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  956. rmdirr($course_scorm_dir . $path);
  957. }
  958. }
  959. }
  960. }
  961. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  962. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  963. // Delete tools
  964. $sql = "DELETE FROM $tbl_tool
  965. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  966. Database::query($sql);
  967. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  968. Database::query($sql);
  969. // Updates the display order of all lps.
  970. $this->update_display_order();
  971. api_item_property_update(
  972. api_get_course_info(),
  973. TOOL_LEARNPATH,
  974. $this->lp_id,
  975. 'delete',
  976. api_get_user_id()
  977. );
  978. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  979. if ($link_info !== false) {
  980. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  981. }
  982. if (api_get_setting('search_enabled') == 'true') {
  983. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  984. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  985. }
  986. }
  987. /**
  988. * Removes all the children of one item - dangerous!
  989. * @param integer $id Element ID of which children have to be removed
  990. * @return integer Total number of children removed
  991. */
  992. public function delete_children_items($id)
  993. {
  994. $course_id = api_get_course_int_id();
  995. if ($this->debug > 0) {
  996. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  997. }
  998. $num = 0;
  999. if (empty ($id) || $id != strval(intval($id))) {
  1000. return false;
  1001. }
  1002. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1003. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1004. $res = Database::query($sql);
  1005. while ($row = Database :: fetch_array($res)) {
  1006. $num += $this->delete_children_items($row['id']);
  1007. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1008. Database::query($sql_del);
  1009. $num++;
  1010. }
  1011. return $num;
  1012. }
  1013. /**
  1014. * Removes an item from the current learnpath
  1015. * @param integer $id Elem ID (0 if first)
  1016. * @param integer $remove Whether to remove the resource/data from the
  1017. * system or leave it (default: 'keep', others 'remove')
  1018. * @return integer Number of elements moved
  1019. * @todo implement resource removal
  1020. */
  1021. public function delete_item($id, $remove = 'keep')
  1022. {
  1023. $course_id = api_get_course_int_id();
  1024. if ($this->debug > 0) {
  1025. error_log('New LP - In learnpath::delete_item()', 0);
  1026. }
  1027. // TODO: Implement the resource removal.
  1028. if (empty ($id) || $id != strval(intval($id))) {
  1029. return false;
  1030. }
  1031. // First select item to get previous, next, and display order.
  1032. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1033. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1034. $res_sel = Database::query($sql_sel);
  1035. if (Database :: num_rows($res_sel) < 1) {
  1036. return false;
  1037. }
  1038. $row = Database :: fetch_array($res_sel);
  1039. $previous = $row['previous_item_id'];
  1040. $next = $row['next_item_id'];
  1041. $display = $row['display_order'];
  1042. $parent = $row['parent_item_id'];
  1043. $lp = $row['lp_id'];
  1044. // Delete children items.
  1045. $num = $this->delete_children_items($id);
  1046. if ($this->debug > 2) {
  1047. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1048. }
  1049. // Now delete the item.
  1050. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1051. if ($this->debug > 2) {
  1052. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1053. }
  1054. Database::query($sql_del);
  1055. // Now update surrounding items.
  1056. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1057. WHERE c_id = ".$course_id." AND id = $previous";
  1058. Database::query($sql_upd);
  1059. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1060. WHERE c_id = ".$course_id." AND id = $next";
  1061. Database::query($sql_upd);
  1062. // Now update all following items with new display order.
  1063. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1064. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1065. Database::query($sql_all);
  1066. //Removing prerequisites since the item will not longer exist
  1067. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1068. Database::query($sql_all);
  1069. // Remove from search engine if enabled.
  1070. if (api_get_setting('search_enabled') == 'true') {
  1071. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1072. $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';
  1073. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1074. $res = Database::query($sql);
  1075. if (Database :: num_rows($res) > 0) {
  1076. $row2 = Database :: fetch_array($res);
  1077. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1078. $di = new ChamiloIndexer();
  1079. $di->remove_document((int) $row2['search_did']);
  1080. }
  1081. $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';
  1082. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1083. Database::query($sql);
  1084. }
  1085. }
  1086. /**
  1087. * Updates an item's content in place
  1088. * @param integer $id Element ID
  1089. * @param integer $parent Parent item ID
  1090. * @param integer $previous Previous item ID
  1091. * @param string $title Item title
  1092. * @param string $description Item description
  1093. * @param string $prerequisites Prerequisites (optional)
  1094. * @param array $audio The array resulting of the $_FILES[mp3] element
  1095. * @param int $max_time_allowed
  1096. * @param string $url
  1097. * @return boolean True on success, false on error
  1098. */
  1099. public function edit_item(
  1100. $id,
  1101. $parent,
  1102. $previous,
  1103. $title,
  1104. $description,
  1105. $prerequisites = '0',
  1106. $audio = array(),
  1107. $max_time_allowed = 0,
  1108. $url = ''
  1109. ) {
  1110. $course_id = api_get_course_int_id();
  1111. $_course = api_get_course_info();
  1112. if ($this->debug > 0) {
  1113. error_log('New LP - In learnpath::edit_item()', 0);
  1114. }
  1115. if (empty ($max_time_allowed)) {
  1116. $max_time_allowed = 0;
  1117. }
  1118. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1119. return false;
  1120. }
  1121. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1122. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1123. $res_select = Database::query($sql_select);
  1124. $row_select = Database :: fetch_array($res_select);
  1125. $audio_update_sql = '';
  1126. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1127. // Create the audio folder if it does not exist yet.
  1128. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1129. if (!is_dir($filepath . 'audio')) {
  1130. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1131. $audio_id = add_document(
  1132. $_course,
  1133. '/audio',
  1134. 'folder',
  1135. 0,
  1136. 'audio'
  1137. );
  1138. api_item_property_update(
  1139. $_course,
  1140. TOOL_DOCUMENT,
  1141. $audio_id,
  1142. 'FolderCreated',
  1143. api_get_user_id(),
  1144. null,
  1145. null,
  1146. null,
  1147. null,
  1148. api_get_session_id()
  1149. );
  1150. api_item_property_update(
  1151. $_course,
  1152. TOOL_DOCUMENT,
  1153. $audio_id,
  1154. 'invisible',
  1155. api_get_user_id(),
  1156. null,
  1157. null,
  1158. null,
  1159. null,
  1160. api_get_session_id()
  1161. );
  1162. }
  1163. // Upload file in documents.
  1164. $pi = pathinfo($audio['name']);
  1165. if ($pi['extension'] == 'mp3') {
  1166. $c_det = api_get_course_info($this->cc);
  1167. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1168. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1169. $path = substr($path, 7);
  1170. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1171. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1172. }
  1173. }
  1174. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1175. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1176. // TODO: htmlspecialchars to be checked for encoding related problems.
  1177. if ($same_parent && $same_previous) {
  1178. // Only update title and description.
  1179. $sql = "UPDATE " . $tbl_lp_item . "
  1180. SET title = '" . Database::escape_string($title) . "',
  1181. prerequisite = '" . $prerequisites . "',
  1182. description = '" . Database::escape_string($description) . "'
  1183. " . $audio_update_sql . ",
  1184. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1185. WHERE c_id = ".$course_id." AND id = " . $id;
  1186. Database::query($sql);
  1187. } else {
  1188. $old_parent = $row_select['parent_item_id'];
  1189. $old_previous = $row_select['previous_item_id'];
  1190. $old_next = $row_select['next_item_id'];
  1191. $old_order = $row_select['display_order'];
  1192. $old_prerequisite = $row_select['prerequisite'];
  1193. $old_max_time_allowed = $row_select['max_time_allowed'];
  1194. /* BEGIN -- virtually remove the current item id */
  1195. /* for the next and previous item it is like the current item doesn't exist anymore */
  1196. if ($old_previous != 0) {
  1197. // Next
  1198. $sql = "UPDATE " . $tbl_lp_item . "
  1199. SET next_item_id = " . $old_next . "
  1200. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1201. Database::query($sql);
  1202. }
  1203. if ($old_next != 0) {
  1204. // Previous
  1205. $sql = "UPDATE " . $tbl_lp_item . "
  1206. SET previous_item_id = " . $old_previous . "
  1207. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1208. Database::query($sql);
  1209. }
  1210. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1211. $sql = "UPDATE " . $tbl_lp_item . "
  1212. SET display_order = display_order - 1
  1213. WHERE
  1214. c_id = ".$course_id." AND
  1215. display_order > " . $old_order . " AND
  1216. lp_id = " . $this->lp_id . " AND
  1217. parent_item_id = " . $old_parent;
  1218. Database::query($sql);
  1219. /* END -- virtually remove the current item id */
  1220. /* BEGIN -- update the current item id to his new location */
  1221. if ($previous == 0) {
  1222. // Select the data of the item that should come after the current item.
  1223. $sql = "SELECT id, display_order
  1224. FROM " . $tbl_lp_item . "
  1225. WHERE
  1226. c_id = ".$course_id." AND
  1227. lp_id = " . $this->lp_id . " AND
  1228. parent_item_id = " . $parent . " AND
  1229. previous_item_id = " . $previous;
  1230. $res_select_old = Database::query($sql);
  1231. $row_select_old = Database::fetch_array($res_select_old);
  1232. // If the new parent didn't have children before.
  1233. if (Database :: num_rows($res_select_old) == 0) {
  1234. $new_next = 0;
  1235. $new_order = 1;
  1236. } else {
  1237. $new_next = $row_select_old['id'];
  1238. $new_order = $row_select_old['display_order'];
  1239. }
  1240. } else {
  1241. // Select the data of the item that should come before the current item.
  1242. $sql = "SELECT next_item_id, display_order
  1243. FROM " . $tbl_lp_item . "
  1244. WHERE c_id = ".$course_id." AND id = " . $previous;
  1245. $res_select_old = Database::query($sql);
  1246. $row_select_old = Database :: fetch_array($res_select_old);
  1247. $new_next = $row_select_old['next_item_id'];
  1248. $new_order = $row_select_old['display_order'] + 1;
  1249. }
  1250. // TODO: htmlspecialchars to be checked for encoding related problems.
  1251. // Update the current item with the new data.
  1252. $sql = "UPDATE " . $tbl_lp_item . "
  1253. SET
  1254. title = '" . Database::escape_string($title) . "',
  1255. description = '" . Database::escape_string($description) . "',
  1256. parent_item_id = " . $parent . ",
  1257. previous_item_id = " . $previous . ",
  1258. next_item_id = " . $new_next . ",
  1259. display_order = " . $new_order . "
  1260. " . $audio_update_sql . "
  1261. WHERE c_id = ".$course_id." AND id = " . $id;
  1262. Database::query($sql);
  1263. if ($previous != 0) {
  1264. // Update the previous item's next_item_id.
  1265. $sql = "UPDATE " . $tbl_lp_item . "
  1266. SET next_item_id = " . $id . "
  1267. WHERE c_id = ".$course_id." AND id = " . $previous;
  1268. Database::query($sql);
  1269. }
  1270. if ($new_next != 0) {
  1271. // Update the next item's previous_item_id.
  1272. $sql = "UPDATE " . $tbl_lp_item . "
  1273. SET previous_item_id = " . $id . "
  1274. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1275. Database::query($sql);
  1276. }
  1277. if ($old_prerequisite != $prerequisites) {
  1278. $sql = "UPDATE " . $tbl_lp_item . "
  1279. SET prerequisite = '" . $prerequisites . "'
  1280. WHERE c_id = ".$course_id." AND id = " . $id;
  1281. Database::query($sql);
  1282. }
  1283. if ($old_max_time_allowed != $max_time_allowed) {
  1284. // update max time allowed
  1285. $sql = "UPDATE " . $tbl_lp_item . "
  1286. SET max_time_allowed = " . $max_time_allowed . "
  1287. WHERE c_id = ".$course_id." AND id = " . $id;
  1288. Database::query($sql);
  1289. }
  1290. // Update all the items with the same or a bigger display_order than the current item.
  1291. $sql = "UPDATE " . $tbl_lp_item . "
  1292. SET display_order = display_order + 1
  1293. WHERE
  1294. c_id = ".$course_id." AND
  1295. lp_id = " . $this->get_id() . " AND
  1296. id <> " . $id . " AND
  1297. parent_item_id = " . $parent . " AND
  1298. display_order >= " . $new_order;
  1299. Database::query($sql);
  1300. }
  1301. if ($row_select['item_type'] == 'link') {
  1302. $link = new Link();
  1303. $linkId = $row_select['path'];
  1304. $link->updateLink($linkId, $url);
  1305. }
  1306. }
  1307. /**
  1308. * Updates an item's prereq in place
  1309. * @param integer $id Element ID
  1310. * @param string $prerequisite_id Prerequisite Element ID
  1311. * @param int $mastery_score Prerequisite min score
  1312. * @param int $max_score Prerequisite max score
  1313. *
  1314. * @return boolean True on success, false on error
  1315. */
  1316. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1317. {
  1318. $course_id = api_get_course_int_id();
  1319. if ($this->debug > 0) {
  1320. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1321. }
  1322. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1323. return false;
  1324. }
  1325. $prerequisite_id = intval($prerequisite_id);
  1326. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1327. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1328. $mastery_score = 0;
  1329. }
  1330. if (!is_numeric($max_score) || $max_score < 0) {
  1331. $max_score = 100;
  1332. }
  1333. /*if ($mastery_score > $max_score) {
  1334. $max_score = $mastery_score;
  1335. }*/
  1336. if (!is_numeric($prerequisite_id)) {
  1337. $prerequisite_id = 'NULL';
  1338. }
  1339. $mastery_score = floatval($mastery_score);
  1340. $max_score = floatval($max_score);
  1341. $sql = " UPDATE $tbl_lp_item
  1342. SET
  1343. prerequisite = $prerequisite_id ,
  1344. prerequisite_min_score = $mastery_score ,
  1345. prerequisite_max_score = $max_score
  1346. WHERE c_id = $course_id AND id = $id";
  1347. Database::query($sql);
  1348. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1349. // Will this be enough to ensure unicity?
  1350. /*$sql = " UPDATE $tbl_lp_item
  1351. SET mastery_score = $mastery_score
  1352. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1353. Database::query($sql);*/
  1354. }
  1355. // TODO: Update the item object (can be ignored for now because refreshed).
  1356. return true;
  1357. }
  1358. /**
  1359. * Escapes a string with the available database escape function
  1360. * @param string String to escape
  1361. * @return string String escaped
  1362. * @deprecated use Database::escape_string
  1363. */
  1364. public function escape_string($string)
  1365. {
  1366. return Database::escape_string($string);
  1367. }
  1368. /**
  1369. * Static admin function exporting a learnpath into a zip file
  1370. * @param string Export type (scorm, zip, cd)
  1371. * @param string Course code
  1372. * @param integer Learnpath ID
  1373. * @param string Zip file name
  1374. * @return string Zip file path (or false on error)
  1375. */
  1376. public function export_lp($type, $course, $id, $zipname)
  1377. {
  1378. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1379. return false;
  1380. }
  1381. $url = '';
  1382. switch ($type) {
  1383. case 'scorm':
  1384. break;
  1385. case 'zip':
  1386. break;
  1387. case 'cdrom':
  1388. break;
  1389. }
  1390. return $url;
  1391. }
  1392. /**
  1393. * Gets all the chapters belonging to the same parent as the item/chapter given
  1394. * Can also be called as abstract method
  1395. * @param integer Item ID
  1396. * @return array A list of all the "brother items" (or an empty array on failure)
  1397. */
  1398. public function get_brother_chapters($id)
  1399. {
  1400. $course_id = api_get_course_int_id();
  1401. if ($this->debug > 0) {
  1402. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1403. }
  1404. if (empty($id)|| $id != strval(intval($id))) {
  1405. return array ();
  1406. }
  1407. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1408. $sql_parent = "SELECT * FROM $lp_item
  1409. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1410. $res_parent = Database::query($sql_parent);
  1411. if (Database :: num_rows($res_parent) > 0) {
  1412. $row_parent = Database :: fetch_array($res_parent);
  1413. $parent = $row_parent['parent_item_id'];
  1414. $sql_bros = "SELECT * FROM $lp_item
  1415. WHERE
  1416. c_id = ".$course_id." AND
  1417. parent_item_id = $parent AND
  1418. id = $id AND
  1419. item_type='dokeos_chapter'
  1420. ORDER BY display_order";
  1421. $res_bros = Database::query($sql_bros);
  1422. $list = array ();
  1423. while ($row_bro = Database :: fetch_array($res_bros)) {
  1424. $list[] = $row_bro;
  1425. }
  1426. return $list;
  1427. }
  1428. return array ();
  1429. }
  1430. /**
  1431. * Gets all the items belonging to the same parent as the item given
  1432. * Can also be called as abstract method
  1433. * @param integer Item ID
  1434. * @return array A list of all the "brother items" (or an empty array on failure)
  1435. */
  1436. public function get_brother_items($id)
  1437. {
  1438. $course_id = api_get_course_int_id();
  1439. if ($this->debug > 0) {
  1440. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1441. }
  1442. if (empty ($id) || $id != strval(intval($id))) {
  1443. return array ();
  1444. }
  1445. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1446. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1447. $res_parent = Database::query($sql_parent);
  1448. if (Database :: num_rows($res_parent) > 0) {
  1449. $row_parent = Database :: fetch_array($res_parent);
  1450. $parent = $row_parent['parent_item_id'];
  1451. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1452. ORDER BY display_order";
  1453. $res_bros = Database::query($sql_bros);
  1454. $list = array ();
  1455. while ($row_bro = Database :: fetch_array($res_bros)) {
  1456. $list[] = $row_bro;
  1457. }
  1458. return $list;
  1459. }
  1460. return array ();
  1461. }
  1462. /**
  1463. * Get the specific prefix index terms of this learning path
  1464. * @param string $prefix
  1465. * @return array Array of terms
  1466. */
  1467. public function get_common_index_terms_by_prefix($prefix)
  1468. {
  1469. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1470. $terms = get_specific_field_values_list_by_prefix(
  1471. $prefix,
  1472. $this->cc,
  1473. TOOL_LEARNPATH,
  1474. $this->lp_id
  1475. );
  1476. $prefix_terms = array();
  1477. if (!empty($terms)) {
  1478. foreach ($terms as $term) {
  1479. $prefix_terms[] = $term['value'];
  1480. }
  1481. }
  1482. return $prefix_terms;
  1483. }
  1484. /**
  1485. * Gets the number of items currently completed
  1486. * @return integer The number of items currently completed
  1487. */
  1488. public function get_complete_items_count()
  1489. {
  1490. if ($this->debug > 0) {
  1491. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1492. }
  1493. $i = 0;
  1494. $completedStatusList = array(
  1495. 'completed',
  1496. 'passed',
  1497. 'succeeded',
  1498. 'browsed',
  1499. 'failed'
  1500. );
  1501. foreach ($this->items as $id => $dummy) {
  1502. // Trying failed and browsed considered "progressed" as well.
  1503. if ($this->items[$id]->status_is($completedStatusList) &&
  1504. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1505. $this->items[$id]->get_type() != 'dir'
  1506. ) {
  1507. $i++;
  1508. }
  1509. }
  1510. return $i;
  1511. }
  1512. /**
  1513. * Gets the current item ID
  1514. * @return integer The current learnpath item id
  1515. */
  1516. public function get_current_item_id()
  1517. {
  1518. $current = 0;
  1519. if ($this->debug > 0) {
  1520. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1521. }
  1522. if (!empty($this->current)) {
  1523. $current = $this->current;
  1524. }
  1525. if ($this->debug > 2) {
  1526. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1527. }
  1528. return $current;
  1529. }
  1530. /**
  1531. * Force to get the first learnpath item id
  1532. * @return integer The current learnpath item id
  1533. */
  1534. public function get_first_item_id()
  1535. {
  1536. $current = 0;
  1537. if (is_array($this->ordered_items)) {
  1538. $current = $this->ordered_items[0];
  1539. }
  1540. return $current;
  1541. }
  1542. /**
  1543. * Gets the total number of items available for viewing in this SCORM
  1544. * @return integer The total number of items
  1545. */
  1546. public function get_total_items_count()
  1547. {
  1548. if ($this->debug > 0) {
  1549. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1550. }
  1551. return count($this->items);
  1552. }
  1553. /**
  1554. * Gets the total number of items available for viewing in this SCORM but without chapters
  1555. * @return integer The total no-chapters number of items
  1556. */
  1557. public function get_total_items_count_without_chapters()
  1558. {
  1559. if ($this->debug > 0) {
  1560. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1561. }
  1562. $total = 0;
  1563. $typeListNotToCount = self::getChapterTypes();
  1564. foreach ($this->items as $temp2) {
  1565. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1566. $total++;
  1567. }
  1568. }
  1569. return $total;
  1570. }
  1571. /**
  1572. * Gets the first element URL.
  1573. * @return string URL to load into the viewer
  1574. */
  1575. public function first()
  1576. {
  1577. if ($this->debug > 0) {
  1578. error_log('New LP - In learnpath::first()', 0);
  1579. error_log('$this->last_item_seen '.$this->last_item_seen);
  1580. }
  1581. // Test if the last_item_seen exists and is not a dir.
  1582. if (count($this->ordered_items) == 0) {
  1583. $this->index = 0;
  1584. }
  1585. if ($this->debug > 0) {
  1586. if (isset($this->items[$this->last_item_seen])) {
  1587. $status = $this->items[$this->last_item_seen]->get_status();
  1588. }
  1589. error_log('status '.$status);
  1590. }
  1591. if (!empty($this->last_item_seen) &&
  1592. !empty($this->items[$this->last_item_seen]) &&
  1593. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1594. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1595. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1596. //&& !$this->items[$this->last_item_seen]->is_done()
  1597. ) {
  1598. if ($this->debug > 2) {
  1599. 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);
  1600. }
  1601. $index = -1;
  1602. foreach ($this->ordered_items as $myindex => $item_id) {
  1603. if ($item_id == $this->last_item_seen) {
  1604. $index = $myindex;
  1605. break;
  1606. }
  1607. }
  1608. if ($index == -1) {
  1609. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1610. if ($this->debug > 2) {
  1611. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1612. }
  1613. return false;
  1614. } else {
  1615. $this->last = $this->last_item_seen;
  1616. $this->current = $this->last_item_seen;
  1617. $this->index = $index;
  1618. }
  1619. } else {
  1620. if ($this->debug > 2) {
  1621. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1622. }
  1623. $index = 0;
  1624. // Loop through all ordered items and stop at the first item that is
  1625. // not a directory *and* that has not been completed yet.
  1626. while ( !empty($this->ordered_items[$index]) AND
  1627. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1628. (
  1629. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1630. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1631. $this->items[$this->ordered_items[$index]]->is_done() === true
  1632. ) AND $index < $this->max_ordered_items) {
  1633. $index++;
  1634. }
  1635. $this->last = $this->current;
  1636. // current is
  1637. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1638. $this->index = $index;
  1639. if ($this->debug > 2) {
  1640. error_log('$index ' . $index);
  1641. }
  1642. if ($this->debug > 2) {
  1643. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1644. }
  1645. }
  1646. if ($this->debug > 2) {
  1647. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1648. }
  1649. }
  1650. /**
  1651. * Gets the information about an item in a format usable as JavaScript to update
  1652. * the JS API by just printing this content into the <head> section of the message frame
  1653. * @param int $item_id
  1654. * @return string
  1655. */
  1656. public function get_js_info($item_id = '')
  1657. {
  1658. if ($this->debug > 0) {
  1659. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1660. }
  1661. $info = '';
  1662. $item_id = intval($item_id);
  1663. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1664. //if item is defined, return values from DB
  1665. $oItem = $this->items[$item_id];
  1666. $info .= '<script language="javascript">';
  1667. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1668. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1669. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1670. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1671. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1672. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1673. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1674. $info .= "top.set_flag_synchronized();";
  1675. $info .= '</script>';
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1678. }
  1679. return $info;
  1680. } else {
  1681. // If item_id is empty, just update to default SCORM data.
  1682. $info .= '<script language="javascript">';
  1683. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1684. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1685. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1686. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1687. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1688. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1689. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1690. $info .= "top.set_flag_synchronized();";
  1691. $info .= '</script>';
  1692. if ($this->debug > 2) {
  1693. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1694. }
  1695. return $info;
  1696. }
  1697. }
  1698. /**
  1699. * Gets the js library from the database
  1700. * @return string The name of the javascript library to be used
  1701. */
  1702. public function get_js_lib()
  1703. {
  1704. $lib = '';
  1705. if (!empty ($this->js_lib)) {
  1706. $lib = $this->js_lib;
  1707. }
  1708. return $lib;
  1709. }
  1710. /**
  1711. * Gets the learnpath database ID
  1712. * @return integer Learnpath ID in the lp table
  1713. */
  1714. public function get_id()
  1715. {
  1716. if (!empty ($this->lp_id)) {
  1717. return $this->lp_id;
  1718. } else {
  1719. return 0;
  1720. }
  1721. }
  1722. /**
  1723. * Gets the last element URL.
  1724. * @return string URL to load into the viewer
  1725. */
  1726. public function get_last()
  1727. {
  1728. if ($this->debug > 0) {
  1729. error_log('New LP - In learnpath::get_last()', 0);
  1730. }
  1731. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1732. if ($this->index > 0) {
  1733. $this->index = count($this->ordered_items) - 1;
  1734. return $this->ordered_items[$this->index];
  1735. }
  1736. return false;
  1737. }
  1738. /**
  1739. * Gets the navigation bar for the learnpath display screen
  1740. * @return string The HTML string to use as a navigation bar
  1741. */
  1742. public function get_navigation_bar($idBar = null, $display=null) {
  1743. if ($this->debug > 0) {
  1744. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1745. }
  1746. if(empty($idBar)){
  1747. $idBar='control-top';
  1748. }
  1749. if(empty($display)){
  1750. $display='display:block';
  1751. }
  1752. $navbar = null;
  1753. $lp_id = $this->lp_id;
  1754. $mycurrentitemid = $this->get_current_item_id();
  1755. if ($this->mode == 'fullscreen') {
  1756. $navbar = '
  1757. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1758. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onclick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1759. <a id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1760. <a id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1761. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
  1762. </div>';
  1763. } else {
  1764. $navbar = '
  1765. <span id="'.$idBar.'" class="buttons text-right">
  1766. <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">
  1767. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1768. </a>
  1769. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1770. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1771. </a>
  1772. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1773. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1774. </a>
  1775. </span>';
  1776. }
  1777. return $navbar;
  1778. }
  1779. /**
  1780. * Gets the next resource in queue (url).
  1781. * @return string URL to load into the viewer
  1782. */
  1783. public function get_next_index()
  1784. {
  1785. if ($this->debug > 0) {
  1786. error_log('New LP - In learnpath::get_next_index()', 0);
  1787. }
  1788. // TODO
  1789. $index = $this->index;
  1790. $index++;
  1791. if ($this->debug > 2) {
  1792. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1793. }
  1794. 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) {
  1795. $index++;
  1796. if ($index == $this->max_ordered_items){
  1797. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1798. return $this->index;
  1799. } else {
  1800. return $index;
  1801. }
  1802. }
  1803. }
  1804. if (empty ($this->ordered_items[$index])) {
  1805. return $this->index;
  1806. }
  1807. if ($this->debug > 2) {
  1808. error_log('New LP - index is now ' . $index, 0);
  1809. }
  1810. return $index;
  1811. }
  1812. /**
  1813. * Gets item_id for the next element
  1814. * @return integer Next item (DB) ID
  1815. */
  1816. public function get_next_item_id()
  1817. {
  1818. if ($this->debug > 0) {
  1819. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1820. }
  1821. $new_index = $this->get_next_index();
  1822. if (!empty ($new_index)) {
  1823. if (isset ($this->ordered_items[$new_index])) {
  1824. if ($this->debug > 2) {
  1825. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1826. }
  1827. return $this->ordered_items[$new_index];
  1828. }
  1829. }
  1830. if ($this->debug > 2) {
  1831. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1832. }
  1833. return 0;
  1834. }
  1835. /**
  1836. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1837. *
  1838. * Generally, the package provided is in the form of a zip file, so the function
  1839. * has been written to test a zip file. If not a zip, the function will return the
  1840. * default return value: ''
  1841. * @param string the path to the file
  1842. * @param string the original name of the file
  1843. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1844. */
  1845. public static function get_package_type($file_path, $file_name)
  1846. {
  1847. // Get name of the zip file without the extension.
  1848. $file_info = pathinfo($file_name);
  1849. $filename = $file_info['basename']; // Name including extension.
  1850. $extension = $file_info['extension']; // Extension only.
  1851. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1852. 'dll',
  1853. 'exe'
  1854. ))) {
  1855. return 'oogie';
  1856. }
  1857. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1858. 'dll',
  1859. 'exe'
  1860. ))) {
  1861. return 'woogie';
  1862. }
  1863. // Filename without its extension.
  1864. $file_base_name = str_replace('.' . $extension, '', $filename);
  1865. $zipFile = new PclZip($file_path);
  1866. // Check the zip content (real size and file extension).
  1867. $zipContentArray = $zipFile->listContent();
  1868. $package_type = '';
  1869. $at_root = false;
  1870. $manifest = '';
  1871. $aicc_match_crs = 0;
  1872. $aicc_match_au = 0;
  1873. $aicc_match_des = 0;
  1874. $aicc_match_cst = 0;
  1875. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1876. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1877. foreach ($zipContentArray as $thisContent) {
  1878. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1879. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1880. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1881. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1882. $package_type = 'scorm';
  1883. break; // Exit the foreach loop.
  1884. } elseif (
  1885. preg_match('/aicc\//i', $thisContent['filename']) ||
  1886. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1887. ) {
  1888. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1889. switch ($ext) {
  1890. case 'crs':
  1891. $aicc_match_crs = 1;
  1892. break;
  1893. case 'au':
  1894. $aicc_match_au = 1;
  1895. break;
  1896. case 'des':
  1897. $aicc_match_des = 1;
  1898. break;
  1899. case 'cst':
  1900. $aicc_match_cst = 1;
  1901. break;
  1902. default:
  1903. break;
  1904. }
  1905. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1906. } else {
  1907. $package_type = '';
  1908. }
  1909. }
  1910. }
  1911. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1912. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1913. $package_type = 'aicc';
  1914. }
  1915. return $package_type;
  1916. }
  1917. /**
  1918. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1919. * @return string URL to load into the viewer
  1920. */
  1921. public function get_previous_index()
  1922. {
  1923. if ($this->debug > 0) {
  1924. error_log('New LP - In learnpath::get_previous_index()', 0);
  1925. }
  1926. $index = $this->index;
  1927. if (isset ($this->ordered_items[$index -1])) {
  1928. $index--;
  1929. 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')) {
  1930. $index--;
  1931. if ($index < 0) {
  1932. return $this->index;
  1933. }
  1934. }
  1935. } else {
  1936. if ($this->debug > 2) {
  1937. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1938. }
  1939. // There is no previous item.
  1940. }
  1941. return $index;
  1942. }
  1943. /**
  1944. * Gets item_id for the next element
  1945. * @return integer Previous item (DB) ID
  1946. */
  1947. public function get_previous_item_id()
  1948. {
  1949. if ($this->debug > 0) {
  1950. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1951. }
  1952. $new_index = $this->get_previous_index();
  1953. return $this->ordered_items[$new_index];
  1954. }
  1955. /**
  1956. * Gets the progress value from the progress_db attribute
  1957. * @return integer Current progress value
  1958. */
  1959. public function get_progress()
  1960. {
  1961. if ($this->debug > 0) {
  1962. error_log('New LP - In learnpath::get_progress()', 0);
  1963. }
  1964. if (!empty ($this->progress_db)) {
  1965. return $this->progress_db;
  1966. }
  1967. return 0;
  1968. }
  1969. /**
  1970. * Returns the HTML necessary to print a mediaplayer block inside a page
  1971. * @return string The mediaplayer HTML
  1972. */
  1973. public function get_mediaplayer($autostart = 'true')
  1974. {
  1975. $course_id = api_get_course_int_id();
  1976. $_course = api_get_course_info();
  1977. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1978. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1979. // Getting all the information about the item.
  1980. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  1981. INNER JOIN ".$tbl_lp_item_view." as lp_view
  1982. ON lp.id = lp_view.lp_item_id
  1983. WHERE
  1984. lp.id = '".$_SESSION['oLP']->current."' AND
  1985. lp.c_id = $course_id AND
  1986. lp_view.c_id = $course_id";
  1987. $result = Database::query($sql);
  1988. $row = Database::fetch_assoc($result);
  1989. $output = '';
  1990. if (!empty ($row['audio'])) {
  1991. $list = $_SESSION['oLP']->get_toc();
  1992. $type_quiz = false;
  1993. foreach($list as $toc) {
  1994. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  1995. $type_quiz = true;
  1996. }
  1997. }
  1998. if ($type_quiz) {
  1999. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2000. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2001. } else {
  2002. $autostart_audio = $autostart;
  2003. }
  2004. } else {
  2005. $autostart_audio = 'true';
  2006. }
  2007. $courseInfo = api_get_course_info();
  2008. $audio = $row['audio'];
  2009. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2010. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2011. if (!file_exists($file)) {
  2012. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2013. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2014. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2015. }
  2016. $player = Display::getMediaPlayer(
  2017. $file,
  2018. array(
  2019. 'id' => 'lp_audio_media_player',
  2020. 'url' => $url,
  2021. 'autoplay' => $autostart_audio,
  2022. 'width' => '100%'
  2023. )
  2024. );
  2025. // The mp3 player.
  2026. $output = '<div id="container">';
  2027. $output .= $player;
  2028. $output .= '</div>';
  2029. }
  2030. return $output;
  2031. }
  2032. /**
  2033. * Checks if the learning path is visible for student after the progress
  2034. * of its prerequisite is completed, considering the time availability and
  2035. * the LP visibility.
  2036. * @param int $lp_id
  2037. * @param int $student_id
  2038. * @param string Course code (optional)
  2039. * @param int $sessionId
  2040. * @return bool
  2041. */
  2042. public static function is_lp_visible_for_student(
  2043. $lp_id,
  2044. $student_id,
  2045. $courseCode = null,
  2046. $sessionId = null
  2047. ) {
  2048. $lp_id = (int)$lp_id;
  2049. $courseInfo = api_get_course_info($courseCode);
  2050. $sessionId = intval($sessionId);
  2051. if (empty($sessionId)) {
  2052. $sessionId = api_get_session_id();
  2053. }
  2054. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2055. // Get current prerequisite
  2056. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2057. FROM $tbl_learnpath
  2058. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2059. $itemInfo = api_get_item_property_info(
  2060. $courseInfo['real_id'],
  2061. TOOL_LEARNPATH,
  2062. $lp_id,
  2063. $sessionId
  2064. );
  2065. // If the item was deleted.
  2066. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2067. return false;
  2068. }
  2069. $rs = Database::query($sql);
  2070. $now = time();
  2071. if (Database::num_rows($rs) > 0) {
  2072. $row = Database::fetch_array($rs, 'ASSOC');
  2073. $prerequisite = $row['prerequisite'];
  2074. $is_visible = true;
  2075. if (!empty($prerequisite)) {
  2076. $progress = self::getProgress(
  2077. $prerequisite,
  2078. $student_id,
  2079. $courseInfo['real_id'],
  2080. $sessionId
  2081. );
  2082. $progress = intval($progress);
  2083. if ($progress < 100) {
  2084. $is_visible = false;
  2085. }
  2086. }
  2087. // Also check the time availability of the LP
  2088. if ($is_visible) {
  2089. // Adding visibility restrictions
  2090. if (!empty($row['publicated_on']) &&
  2091. $row['publicated_on'] != '0000-00-00 00:00:00'
  2092. ) {
  2093. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2094. //api_not_allowed();
  2095. $is_visible = false;
  2096. }
  2097. }
  2098. // Blocking empty start times see BT#2800
  2099. global $_custom;
  2100. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2101. $_custom['lps_hidden_when_no_start_date']
  2102. ) {
  2103. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2104. //api_not_allowed();
  2105. $is_visible = false;
  2106. }
  2107. }
  2108. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2109. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2110. //api_not_allowed();
  2111. $is_visible = false;
  2112. }
  2113. }
  2114. }
  2115. // Check if the subscription users/group to a LP is ON
  2116. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2117. // Try group
  2118. $is_visible = false;
  2119. // Checking only the user visibility
  2120. $userVisibility = api_get_item_visibility(
  2121. $courseInfo,
  2122. 'learnpath',
  2123. $row['id'],
  2124. $sessionId,
  2125. $student_id,
  2126. 'LearnpathSubscription'
  2127. );
  2128. if ($userVisibility == 1) {
  2129. $is_visible = true;
  2130. } else {
  2131. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2132. if (!empty($userGroups)) {
  2133. foreach ($userGroups as $groupInfo) {
  2134. $groupId = $groupInfo['iid'];
  2135. $userVisibility = api_get_item_visibility(
  2136. $courseInfo,
  2137. 'learnpath',
  2138. $row['id'],
  2139. $sessionId,
  2140. null,
  2141. 'LearnpathSubscription',
  2142. $groupId
  2143. );
  2144. if ($userVisibility == 1) {
  2145. $is_visible = true;
  2146. break;
  2147. }
  2148. }
  2149. }
  2150. }
  2151. }
  2152. return $is_visible;
  2153. }
  2154. return false;
  2155. }
  2156. /**
  2157. * @param int $lpId
  2158. * @param int $userId
  2159. * @param int $courseId
  2160. * @param int $sessionId
  2161. * @return int
  2162. */
  2163. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2164. {
  2165. $lpId = intval($lpId);
  2166. $userId = intval($userId);
  2167. $courseId = intval($courseId);
  2168. $sessionId = intval($sessionId);
  2169. $progress = 0;
  2170. $sessionCondition = api_get_session_condition($sessionId);
  2171. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2172. $sql = "SELECT * FROM $table
  2173. WHERE
  2174. c_id = ".$courseId." AND
  2175. lp_id = $lpId AND
  2176. user_id = $userId $sessionCondition";
  2177. $res = Database::query($sql);
  2178. if (Database :: num_rows($res) > 0) {
  2179. $row = Database:: fetch_array($res);
  2180. $progress = $row['progress'];
  2181. }
  2182. return $progress;
  2183. }
  2184. /**
  2185. * Displays a progress bar
  2186. * completed so far.
  2187. * @param integer $percentage Progress value to display
  2188. * @param string $text_add Text to display near the progress value
  2189. * @return string HTML string containing the progress bar
  2190. */
  2191. public static function get_progress_bar($percentage = -1, $text_add = '')
  2192. {
  2193. $text = $percentage . $text_add;
  2194. $output = '<div class="progress">
  2195. <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.';">
  2196. '. $text .'
  2197. </div>
  2198. </div>';
  2199. return $output;
  2200. }
  2201. /**
  2202. * @param string $mode can be '%' or 'abs'
  2203. * otherwise this value will be used $this->progress_bar_mode
  2204. * @return string
  2205. */
  2206. public function getProgressBar($mode = null)
  2207. {
  2208. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2209. return self::get_progress_bar($percentage, $text_add);
  2210. }
  2211. /**
  2212. * Gets the progress bar info to display inside the progress bar.
  2213. * Also used by scorm_api.php
  2214. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2215. * we display a number of completed elements per total elements
  2216. * @param integer $add Additional steps to fake as completed
  2217. * @return list array Percentage or number and symbol (% or /xx)
  2218. */
  2219. public function get_progress_bar_text($mode = '', $add = 0)
  2220. {
  2221. if ($this->debug > 0) {
  2222. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2223. }
  2224. if (empty($mode)) {
  2225. $mode = $this->progress_bar_mode;
  2226. }
  2227. $total_items = $this->get_total_items_count_without_chapters();
  2228. if ($this->debug > 2) {
  2229. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2230. }
  2231. $completeItems = $this->get_complete_items_count();
  2232. if ($this->debug > 2) {
  2233. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2234. }
  2235. if ($add != 0) {
  2236. $completeItems += $add;
  2237. if ($this->debug > 2) {
  2238. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2239. }
  2240. }
  2241. $text = '';
  2242. if ($completeItems > $total_items) {
  2243. $completeItems = $total_items;
  2244. }
  2245. $percentage = 0;
  2246. if ($mode == '%') {
  2247. if ($total_items > 0) {
  2248. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2249. } else {
  2250. $percentage = 0;
  2251. }
  2252. $percentage = number_format($percentage, 0);
  2253. $text = '%';
  2254. } elseif ($mode == 'abs') {
  2255. $percentage = $completeItems;
  2256. $text = '/' . $total_items;
  2257. }
  2258. return array(
  2259. $percentage,
  2260. $text
  2261. );
  2262. }
  2263. /**
  2264. * Gets the progress bar mode
  2265. * @return string The progress bar mode attribute
  2266. */
  2267. public function get_progress_bar_mode()
  2268. {
  2269. if ($this->debug > 0) {
  2270. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2271. }
  2272. if (!empty ($this->progress_bar_mode)) {
  2273. return $this->progress_bar_mode;
  2274. } else {
  2275. return '%';
  2276. }
  2277. }
  2278. /**
  2279. * Gets the learnpath proximity (remote or local)
  2280. * @return string Learnpath proximity
  2281. */
  2282. public function get_proximity()
  2283. {
  2284. if ($this->debug > 0) {
  2285. error_log('New LP - In learnpath::get_proximity()', 0);
  2286. }
  2287. if (!empty ($this->proximity)) {
  2288. return $this->proximity;
  2289. } else {
  2290. return '';
  2291. }
  2292. }
  2293. /**
  2294. * Gets the learnpath theme (remote or local)
  2295. * @return string Learnpath theme
  2296. */
  2297. public function get_theme()
  2298. {
  2299. if ($this->debug > 0) {
  2300. error_log('New LP - In learnpath::get_theme()', 0);
  2301. }
  2302. if (!empty ($this->theme)) {
  2303. return $this->theme;
  2304. } else {
  2305. return '';
  2306. }
  2307. }
  2308. /**
  2309. * Gets the learnpath session id
  2310. * @return string Learnpath theme
  2311. */
  2312. public function get_lp_session_id()
  2313. {
  2314. if ($this->debug > 0) {
  2315. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2316. }
  2317. if (!empty ($this->lp_session_id)) {
  2318. return $this->lp_session_id;
  2319. } else {
  2320. return 0;
  2321. }
  2322. }
  2323. /**
  2324. * Gets the learnpath image
  2325. * @return string Web URL of the LP image
  2326. */
  2327. public function get_preview_image()
  2328. {
  2329. if ($this->debug > 0) {
  2330. error_log('New LP - In learnpath::get_preview_image()', 0);
  2331. }
  2332. if (!empty($this->preview_image)) {
  2333. return $this->preview_image;
  2334. } else {
  2335. return '';
  2336. }
  2337. }
  2338. /**
  2339. * @param string $size
  2340. * @param string $path_type
  2341. * @return bool|string
  2342. */
  2343. public function get_preview_image_path($size = null, $path_type = 'web')
  2344. {
  2345. $preview_image = $this->get_preview_image();
  2346. if (isset($preview_image) && !empty($preview_image)) {
  2347. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2348. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2349. if (isset($size)) {
  2350. $info = pathinfo($preview_image);
  2351. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2352. if (file_exists($image_sys_path.$image_custom_size)) {
  2353. if ($path_type == 'web') {
  2354. return $image_path.$image_custom_size;
  2355. } else {
  2356. return $image_sys_path.$image_custom_size;
  2357. }
  2358. }
  2359. } else {
  2360. if ($path_type == 'web') {
  2361. return $image_path.$preview_image;
  2362. } else {
  2363. return $image_sys_path.$preview_image;
  2364. }
  2365. }
  2366. }
  2367. return false;
  2368. }
  2369. /**
  2370. * Gets the learnpath author
  2371. * @return string LP's author
  2372. */
  2373. public function get_author()
  2374. {
  2375. if ($this->debug > 0) {
  2376. error_log('New LP - In learnpath::get_author()', 0);
  2377. }
  2378. if (!empty ($this->author)) {
  2379. return $this->author;
  2380. } else {
  2381. return '';
  2382. }
  2383. }
  2384. /**
  2385. * Gets the learnpath author
  2386. * @return string LP's author
  2387. */
  2388. public function get_hide_toc_frame()
  2389. {
  2390. if ($this->debug > 0) {
  2391. error_log('New LP - In learnpath::get_author()', 0);
  2392. }
  2393. if (!empty ($this->hide_toc_frame)) {
  2394. return $this->hide_toc_frame;
  2395. } else {
  2396. return '';
  2397. }
  2398. }
  2399. /**
  2400. * Generate a new prerequisites string for a given item. If this item was a sco and
  2401. * its prerequisites were strings (instead of IDs), then transform those strings into
  2402. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2403. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2404. * same rule as the scorm_export() method
  2405. * @param integer Item ID
  2406. * @return string Prerequisites string ready for the export as SCORM
  2407. */
  2408. public function get_scorm_prereq_string($item_id)
  2409. {
  2410. if ($this->debug > 0) {
  2411. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2412. }
  2413. if (!is_object($this->items[$item_id])) {
  2414. return false;
  2415. }
  2416. /** @var learnpathItem $oItem */
  2417. $oItem = $this->items[$item_id];
  2418. $prereq = $oItem->get_prereq_string();
  2419. if (empty($prereq)) {
  2420. return '';
  2421. }
  2422. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2423. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2424. // then simply return it (with the ITEM_ prefix).
  2425. //return 'ITEM_' . $prereq;
  2426. return $this->items[$prereq]->ref;
  2427. } else {
  2428. if (isset($this->refs_list[$prereq])) {
  2429. // It's a simple string item from which the ID can be found in the refs list,
  2430. // so we can transform it directly to an ID for export.
  2431. return $this->items[$this->refs_list[$prereq]]->ref;
  2432. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2433. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2434. } else {
  2435. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2436. // and replace them, one by one, by the internal IDs (chamilo db)
  2437. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2438. // by a space as well.
  2439. $find = array (
  2440. '&',
  2441. '|',
  2442. '~',
  2443. '=',
  2444. '<>',
  2445. '{',
  2446. '}',
  2447. '*',
  2448. '(',
  2449. ')'
  2450. );
  2451. $replace = array (
  2452. ' ',
  2453. ' ',
  2454. ' ',
  2455. ' ',
  2456. ' ',
  2457. ' ',
  2458. ' ',
  2459. ' ',
  2460. ' ',
  2461. ' '
  2462. );
  2463. $prereq_mod = str_replace($find, $replace, $prereq);
  2464. $ids = explode(' ', $prereq_mod);
  2465. foreach ($ids as $id) {
  2466. $id = trim($id);
  2467. if (isset ($this->refs_list[$id])) {
  2468. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2469. }
  2470. }
  2471. return $prereq;
  2472. }
  2473. }
  2474. }
  2475. /**
  2476. * Returns the XML DOM document's node
  2477. * @param resource Reference to a list of objects to search for the given ITEM_*
  2478. * @param string The identifier to look for
  2479. * @return mixed The reference to the element found with that identifier. False if not found
  2480. */
  2481. public function get_scorm_xml_node(& $children, $id)
  2482. {
  2483. for ($i = 0; $i < $children->length; $i++) {
  2484. $item_temp = $children->item($i);
  2485. if ($item_temp->nodeName == 'item') {
  2486. if ($item_temp->getAttribute('identifier') == $id) {
  2487. return $item_temp;
  2488. }
  2489. }
  2490. $subchildren = $item_temp->childNodes;
  2491. if ($subchildren->length > 0) {
  2492. $val = $this->get_scorm_xml_node($subchildren, $id);
  2493. if (is_object($val)) {
  2494. return $val;
  2495. }
  2496. }
  2497. }
  2498. return false;
  2499. }
  2500. /**
  2501. * Returns a usable array of stats related to the current learnpath and user
  2502. * @return array Well-formatted array containing status for the current learnpath
  2503. */
  2504. public function get_stats()
  2505. {
  2506. if ($this->debug > 0) {
  2507. error_log('New LP - In learnpath::get_stats()', 0);
  2508. }
  2509. }
  2510. /**
  2511. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2512. * the given course (for all learnpaths and all users)
  2513. * @param string Course code
  2514. * @return array Well-formatted array containing status for the course's learnpaths
  2515. */
  2516. public function get_stats_course($course)
  2517. {
  2518. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2519. // TODO
  2520. }
  2521. /**
  2522. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2523. * the given course and learnpath (for all users)
  2524. * @param string Course code
  2525. * @param integer Learnpath ID
  2526. * @return array Well-formatted array containing status for the specified learnpath
  2527. */
  2528. public function get_stats_lp($course, $lp)
  2529. {
  2530. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2531. // TODO
  2532. }
  2533. /**
  2534. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2535. * the given course, learnpath and user.
  2536. * @param string Course code
  2537. * @param integer Learnpath ID
  2538. * @param integer User ID
  2539. * @return array Well-formatted array containing status for the specified learnpath and user
  2540. */
  2541. public function get_stats_lp_user($course, $lp, $user)
  2542. {
  2543. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2544. // TODO
  2545. }
  2546. /**
  2547. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2548. * the given course and learnpath (for all users)
  2549. * @param string Course code
  2550. * @param integer User ID
  2551. * @return array Well-formatted array containing status for the user's learnpaths
  2552. */
  2553. public function get_stats_user($course, $user) {
  2554. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2555. // TODO
  2556. }
  2557. /**
  2558. * Gets the status list for all LP's items
  2559. * @return array Array of [index] => [item ID => current status]
  2560. */
  2561. public function get_items_status_list()
  2562. {
  2563. if ($this->debug > 0) {
  2564. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2565. }
  2566. $list = array ();
  2567. foreach ($this->ordered_items as $item_id) {
  2568. $list[] = array (
  2569. $item_id => $this->items[$item_id]->get_status()
  2570. );
  2571. }
  2572. return $list;
  2573. }
  2574. /**
  2575. * Return the number of interactions for the given learnpath Item View ID.
  2576. * This method can be used as static.
  2577. * @param integer Item View ID
  2578. * @param integer course id
  2579. * @return integer Number of interactions
  2580. */
  2581. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2582. {
  2583. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2584. $lp_iv_id = intval($lp_iv_id);
  2585. $course_id = intval($course_id);
  2586. $sql = "SELECT count(*) FROM $table
  2587. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2588. $res = Database::query($sql);
  2589. $num = 0;
  2590. if (Database::num_rows($res)) {
  2591. $row = Database::fetch_array($res);
  2592. $num = $row[0];
  2593. }
  2594. return $num;
  2595. }
  2596. /**
  2597. * Return the interactions as an array for the given lp_iv_id.
  2598. * This method can be used as static.
  2599. * @param integer Learnpath Item View ID
  2600. * @return array
  2601. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2602. */
  2603. public static function get_iv_interactions_array($lp_iv_id)
  2604. {
  2605. $course_id = api_get_course_int_id();
  2606. $list = array();
  2607. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2608. if (empty($lp_iv_id)) {
  2609. return array();
  2610. }
  2611. $sql = "SELECT * FROM $table
  2612. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2613. ORDER BY order_id ASC";
  2614. $res = Database::query($sql);
  2615. $num = Database :: num_rows($res);
  2616. if ($num > 0) {
  2617. $list[] = array (
  2618. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2619. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2620. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2621. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2622. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2623. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2624. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2625. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2626. );
  2627. while ($row = Database :: fetch_array($res)) {
  2628. $list[] = array (
  2629. 'order_id' => ($row['order_id'] + 1),
  2630. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2631. 'type' => $row['interaction_type'],
  2632. 'time' => $row['completion_time'],
  2633. //'correct_responses' => $row['correct_responses'],
  2634. 'correct_responses' => '', // Hide correct responses from students.
  2635. 'student_response' => $row['student_response'],
  2636. 'result' => $row['result'],
  2637. 'latency' => $row['latency']
  2638. );
  2639. }
  2640. }
  2641. return $list;
  2642. }
  2643. /**
  2644. * Return the number of objectives for the given learnpath Item View ID.
  2645. * This method can be used as static.
  2646. * @param integer Item View ID
  2647. * @return integer Number of objectives
  2648. */
  2649. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2650. {
  2651. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2652. $course_id = intval($course_id);
  2653. $lp_iv_id = intval($lp_iv_id);
  2654. $sql = "SELECT count(*) FROM $table
  2655. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2656. //@todo seems that this always returns 0
  2657. $res = Database::query($sql);
  2658. $num = 0;
  2659. if (Database::num_rows($res)) {
  2660. $row = Database :: fetch_array($res);
  2661. $num = $row[0];
  2662. }
  2663. return $num;
  2664. }
  2665. /**
  2666. * Return the objectives as an array for the given lp_iv_id.
  2667. * This method can be used as static.
  2668. * @param integer Learnpath Item View ID
  2669. * @return array
  2670. * @todo Translate labels
  2671. */
  2672. public static function get_iv_objectives_array($lp_iv_id = 0)
  2673. {
  2674. $course_id = api_get_course_int_id();
  2675. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2676. $sql = "SELECT * FROM $table
  2677. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2678. ORDER BY order_id ASC";
  2679. $res = Database::query($sql);
  2680. $num = Database :: num_rows($res);
  2681. $list = array();
  2682. if ($num > 0) {
  2683. $list[] = array(
  2684. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2685. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2686. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2687. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2688. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2689. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2690. );
  2691. while ($row = Database :: fetch_array($res)) {
  2692. $list[] = array (
  2693. 'order_id' => ($row['order_id'] + 1),
  2694. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2695. 'score_raw' => $row['score_raw'],
  2696. 'score_max' => $row['score_max'],
  2697. 'score_min' => $row['score_min'],
  2698. 'status' => $row['status']
  2699. );
  2700. }
  2701. }
  2702. return $list;
  2703. }
  2704. /**
  2705. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2706. * used by get_html_toc() to be ready to display
  2707. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2708. */
  2709. public function get_toc()
  2710. {
  2711. if ($this->debug > 0) {
  2712. error_log('learnpath::get_toc()', 0);
  2713. }
  2714. $toc = array();
  2715. foreach ($this->ordered_items as $item_id) {
  2716. if ($this->debug > 2) {
  2717. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2718. }
  2719. // TODO: Change this link generation and use new function instead.
  2720. $toc[] = array (
  2721. 'id' => $item_id,
  2722. 'title' => $this->items[$item_id]->get_title(),
  2723. 'status' => $this->items[$item_id]->get_status(),
  2724. 'level' => $this->items[$item_id]->get_level(),
  2725. 'type' => $this->items[$item_id]->get_type(),
  2726. 'description' => $this->items[$item_id]->get_description(),
  2727. 'path' => $this->items[$item_id]->get_path(),
  2728. );
  2729. }
  2730. if ($this->debug > 2) {
  2731. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2732. }
  2733. return $toc;
  2734. }
  2735. /**
  2736. * Generate and return the table of contents for this learnpath. The JS
  2737. * table returned is used inside of scorm_api.php
  2738. * @return string A JS array vairiable construction
  2739. */
  2740. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2741. {
  2742. if ($this->debug > 0) {
  2743. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2744. }
  2745. $toc = $varname.' = new Array();';
  2746. foreach ($this->ordered_items as $item_id) {
  2747. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2748. }
  2749. if ($this->debug > 2) {
  2750. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2751. }
  2752. return $toc;
  2753. }
  2754. /**
  2755. * Gets the learning path type
  2756. * @param boolean Return the name? If false, return the ID. Default is false.
  2757. * @return mixed Type ID or name, depending on the parameter
  2758. */
  2759. public function get_type($get_name = false)
  2760. {
  2761. $res = false;
  2762. if ($this->debug > 0) {
  2763. error_log('New LP - In learnpath::get_type()', 0);
  2764. }
  2765. if (!empty ($this->type)) {
  2766. if ($get_name) {
  2767. // Get it from the lp_type table in main db.
  2768. } else {
  2769. $res = $this->type;
  2770. }
  2771. }
  2772. if ($this->debug > 2) {
  2773. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2774. }
  2775. return $res;
  2776. }
  2777. /**
  2778. * Gets the learning path type as static method
  2779. * @param boolean Return the name? If false, return the ID. Default is false.
  2780. * @return mixed Type ID or name, depending on the parameter
  2781. */
  2782. public static function get_type_static($lp_id = 0)
  2783. {
  2784. $course_id = api_get_course_int_id();
  2785. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2786. $lp_id = intval($lp_id);
  2787. $sql = "SELECT lp_type FROM $tbl_lp
  2788. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2789. $res = Database::query($sql);
  2790. if ($res === false) {
  2791. return null;
  2792. }
  2793. if (Database :: num_rows($res) <= 0) {
  2794. return null;
  2795. }
  2796. $row = Database :: fetch_array($res);
  2797. return $row['lp_type'];
  2798. }
  2799. /**
  2800. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2801. * This method can be used as abstract and is recursive
  2802. * @param integer Learnpath ID
  2803. * @param integer Parent ID of the items to look for
  2804. * @return mixed Ordered list of item IDs or false on error
  2805. */
  2806. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2807. {
  2808. if (empty($course_id)) {
  2809. $course_id = api_get_course_int_id();
  2810. } else {
  2811. $course_id = intval($course_id);
  2812. }
  2813. $list = array();
  2814. if (empty($lp)) {
  2815. return false;
  2816. }
  2817. $lp = intval($lp);
  2818. $parent = intval($parent);
  2819. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2820. $sql = "SELECT id FROM $tbl_lp_item
  2821. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2822. ORDER BY display_order";
  2823. $res = Database::query($sql);
  2824. while ($row = Database :: fetch_array($res)) {
  2825. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2826. $list[] = $row['id'];
  2827. foreach ($sublist as $item) {
  2828. $list[] = $item;
  2829. }
  2830. }
  2831. return $list;
  2832. }
  2833. /**
  2834. * @return array
  2835. */
  2836. public static function getChapterTypes()
  2837. {
  2838. return array(
  2839. 'dokeos_chapter',
  2840. 'dokeos_module',
  2841. 'chapter',
  2842. 'dir'
  2843. );
  2844. }
  2845. /**
  2846. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2847. * @return string HTML TOC ready to display
  2848. */
  2849. public function get_html_toc($toc_list = null)
  2850. {
  2851. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2852. if ($this->debug > 0) {
  2853. error_log('In learnpath::get_html_toc()', 0);
  2854. }
  2855. if (empty($toc_list)) {
  2856. $toc_list = $this->get_toc();
  2857. }
  2858. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2859. $html = '<div class="scorm-body">';
  2860. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2861. require_once 'resourcelinker.inc.php';
  2862. // Temporary variables.
  2863. $mycurrentitemid = $this->get_current_item_id();
  2864. $color_counter = 0;
  2865. $i = 0;
  2866. foreach ($toc_list as $item) {
  2867. // TODO: Complete this
  2868. $icon_name = array (
  2869. 'not attempted' => '../img/notattempted.gif',
  2870. 'incomplete' => '../img/incomplete.png',
  2871. 'failed' => '../img/delete.png',
  2872. 'completed' => '../img/completed.png',
  2873. 'passed' => '../img/passed.png',
  2874. 'succeeded' => '../img/succeeded.png',
  2875. 'browsed' => '../img/completed.png',
  2876. );
  2877. // Style Status
  2878. $class_name = array (
  2879. 'not attempted' => 'scorm_not_attempted',
  2880. 'incomplete' => 'scorm_not_attempted',
  2881. 'failed' => 'scorm_failed',
  2882. 'completed' => 'scorm_completed',
  2883. 'passed' => 'scorm_completed',
  2884. 'succeeded' => 'scorm_completed',
  2885. 'browsed' => 'scorm_completed',
  2886. );
  2887. $scorm_color_background = 'row_odd';
  2888. $style_item = '';
  2889. if ($color_counter % 2 == 0) {
  2890. $scorm_color_background = 'row_even';
  2891. }
  2892. $dirTypes = self::getChapterTypes();
  2893. if (in_array($item['type'], $dirTypes)) {
  2894. $scorm_color_background ='scorm_item_section ';
  2895. $style_item = '';
  2896. }
  2897. if ($item['id'] == $this->current) {
  2898. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2899. } elseif (!in_array($item['type'], $dirTypes)) {
  2900. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2901. }
  2902. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2903. // Learning path title
  2904. $title = $item['title'];
  2905. if (empty ($title)) {
  2906. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2907. }
  2908. $title = Security::remove_XSS($title);
  2909. // Learning path personalization
  2910. // build the LP tree
  2911. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2912. $description = $item['description'];
  2913. if (empty($description)) {
  2914. $description = $title;
  2915. }
  2916. if (in_array($item['type'], $dirTypes)) {
  2917. // Chapters
  2918. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2919. } else {
  2920. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2921. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2922. }
  2923. if (in_array($item['type'], $dirTypes)) {
  2924. // Chapter
  2925. // if you want to put an image before, you should use css
  2926. $html .= stripslashes($title);
  2927. } else {
  2928. $this->get_link('http', $item['id'], $toc_list);
  2929. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2930. }
  2931. $html .= "</div>";
  2932. if ($scorm_color_background != '') {
  2933. $html .= '</div>';
  2934. }
  2935. $color_counter++;
  2936. }
  2937. $html .= "</div>";
  2938. $html .= "</div>";
  2939. return $html;
  2940. }
  2941. /**
  2942. * Returns an HTML-formatted string ready to display with teacher buttons
  2943. * in LP view menu
  2944. * @return string HTML TOC ready to display
  2945. */
  2946. public function get_teacher_toc_buttons()
  2947. {
  2948. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2949. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2950. $html = '';
  2951. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2952. $gradebook = '';
  2953. if (!empty($_GET['gradebook'])) {
  2954. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2955. }
  2956. if ($this->get_lp_session_id() == api_get_session_id()) {
  2957. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2958. $html .= '<div class="btn-group">';
  2959. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" .
  2960. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2961. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "' target='_parent'>" .
  2962. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2963. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'">' .
  2964. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2965. $html .= '</div>';
  2966. $html .= '</div>';
  2967. }
  2968. }
  2969. return $html;
  2970. }
  2971. /**
  2972. * Gets the learnpath maker name - generally the editor's name
  2973. * @return string Learnpath maker name
  2974. */
  2975. public function get_maker()
  2976. {
  2977. if ($this->debug > 0) {
  2978. error_log('New LP - In learnpath::get_maker()', 0);
  2979. }
  2980. if (!empty ($this->maker)) {
  2981. return $this->maker;
  2982. } else {
  2983. return '';
  2984. }
  2985. }
  2986. /**
  2987. * Gets the learnpath name/title
  2988. * @return string Learnpath name/title
  2989. */
  2990. public function get_name()
  2991. {
  2992. if ($this->debug > 0) {
  2993. error_log('New LP - In learnpath::get_name()', 0);
  2994. }
  2995. if (!empty ($this->name)) {
  2996. return $this->name;
  2997. } else {
  2998. return 'N/A';
  2999. }
  3000. }
  3001. /**
  3002. * Gets a link to the resource from the present location, depending on item ID.
  3003. * @param string $type Type of link expected
  3004. * @param integer $item_id Learnpath item ID
  3005. * @return string $provided_toc Link to the lp_item resource
  3006. */
  3007. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3008. {
  3009. $course_id = $this->get_course_int_id();
  3010. if ($this->debug > 0) {
  3011. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3012. }
  3013. if (empty($item_id)) {
  3014. if ($this->debug > 2) {
  3015. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3016. }
  3017. $item_id = $this->get_current_item_id();
  3018. }
  3019. if (empty($item_id)) {
  3020. if ($this->debug > 2) {
  3021. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3022. }
  3023. //still empty, this means there was no item_id given and we are not in an object context or
  3024. //the object property is empty, return empty link
  3025. $item_id = $this->first();
  3026. return '';
  3027. }
  3028. $file = '';
  3029. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3030. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3031. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3032. $item_id = intval($item_id);
  3033. $sql = "SELECT
  3034. l.lp_type as ltype,
  3035. l.path as lpath,
  3036. li.item_type as litype,
  3037. li.path as lipath,
  3038. li.parameters as liparams
  3039. FROM $lp_table l
  3040. INNER JOIN $lp_item_table li
  3041. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3042. WHERE li.id = $item_id ";
  3043. if ($this->debug > 2) {
  3044. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3045. }
  3046. $res = Database::query($sql);
  3047. if (Database :: num_rows($res) > 0) {
  3048. $row = Database :: fetch_array($res);
  3049. $lp_type = $row['ltype'];
  3050. $lp_path = $row['lpath'];
  3051. $lp_item_type = $row['litype'];
  3052. $lp_item_path = $row['lipath'];
  3053. $lp_item_params = $row['liparams'];
  3054. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3055. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3056. }
  3057. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3058. if ($type == 'http') {
  3059. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3060. } else {
  3061. $course_path = $sys_course_path; //system path
  3062. }
  3063. // 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.
  3064. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3065. $lp_type = 1;
  3066. }
  3067. if ($this->debug > 2) {
  3068. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3069. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3070. }
  3071. // Now go through the specific cases to get the end of the path
  3072. // @todo Use constants instead of int values.
  3073. switch ($lp_type) {
  3074. case 1 :
  3075. if ($lp_item_type == 'dokeos_chapter') {
  3076. $file = 'lp_content.php?type=dir';
  3077. } else {
  3078. require_once 'resourcelinker.inc.php';
  3079. $file = rl_get_resource_link_for_learnpath(
  3080. $course_id,
  3081. $this->get_id(),
  3082. $item_id,
  3083. $this->get_view_id()
  3084. );
  3085. if ($this->debug > 0) {
  3086. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3087. }
  3088. if ($lp_item_type == 'link') {
  3089. if (Link::is_youtube_link($file)) {
  3090. $src = Link::get_youtube_video_id($file);
  3091. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3092. } elseif (Link::isVimeoLink($file)) {
  3093. $src = Link::getVimeoLinkId($file);
  3094. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3095. } else {
  3096. // If the current site is HTTPS and the link is
  3097. // HTTP, browsers will refuse opening the link
  3098. $urlId = api_get_current_access_url_id();
  3099. $url = api_get_access_url($urlId, false);
  3100. $protocol = substr($url['url'], 0, 5);
  3101. if ($protocol === 'https') {
  3102. $linkProtocol = substr($file, 0, 5);
  3103. if ($linkProtocol === 'http:') {
  3104. //this is the special intervention case
  3105. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&source=' . urlencode($file);
  3106. }
  3107. }
  3108. }
  3109. } else {
  3110. // Check how much attempts of a exercise exits in lp
  3111. $lp_item_id = $this->get_current_item_id();
  3112. $lp_view_id = $this->get_view_id();
  3113. $prevent_reinit = null;
  3114. if (isset($this->items[$this->current])) {
  3115. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3116. }
  3117. if (empty($provided_toc)) {
  3118. if ($this->debug > 0) {
  3119. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3120. }
  3121. $list = $this->get_toc();
  3122. } else {
  3123. if ($this->debug > 0) {
  3124. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3125. }
  3126. $list = $provided_toc;
  3127. }
  3128. $type_quiz = false;
  3129. foreach ($list as $toc) {
  3130. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3131. $type_quiz = true;
  3132. }
  3133. }
  3134. if ($type_quiz) {
  3135. $lp_item_id = intval($lp_item_id);
  3136. $lp_view_id = intval($lp_view_id);
  3137. $sql = "SELECT count(*) FROM $lp_item_view_table
  3138. WHERE
  3139. c_id = $course_id AND
  3140. lp_item_id='" . $lp_item_id . "' AND
  3141. lp_view_id ='" . $lp_view_id . "' AND
  3142. status='completed'";
  3143. $result = Database::query($sql);
  3144. $row_count = Database :: fetch_row($result);
  3145. $count_item_view = (int) $row_count[0];
  3146. $not_multiple_attempt = 0;
  3147. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3148. $not_multiple_attempt = 1;
  3149. }
  3150. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3151. }
  3152. $tmp_array = explode('/', $file);
  3153. $document_name = $tmp_array[count($tmp_array) - 1];
  3154. if (strpos($document_name, '_DELETED_')) {
  3155. $file = 'blank.php?error=document_deleted';
  3156. }
  3157. }
  3158. }
  3159. break;
  3160. case 2 :
  3161. if ($this->debug > 2) {
  3162. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3163. }
  3164. if ($lp_item_type != 'dir') {
  3165. // Quite complex here:
  3166. // We want to make sure 'http://' (and similar) links can
  3167. // be loaded as is (withouth the Chamilo path in front) but
  3168. // some contents use this form: resource.htm?resource=http://blablabla
  3169. // which means we have to find a protocol at the path's start, otherwise
  3170. // it should not be considered as an external URL.
  3171. //if ($this->prerequisites_match($item_id)) {
  3172. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3173. if ($this->debug > 2) {
  3174. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3175. }
  3176. // Distant url, return as is.
  3177. $file = $lp_item_path;
  3178. } else {
  3179. if ($this->debug > 2) {
  3180. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3181. }
  3182. // Prevent getting untranslatable urls.
  3183. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3184. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3185. // Prepare the path.
  3186. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3187. // TODO: Fix this for urls with protocol header.
  3188. $file = str_replace('//', '/', $file);
  3189. $file = str_replace(':/', '://', $file);
  3190. if (substr($lp_path, -1) == '/') {
  3191. $lp_path = substr($lp_path, 0, -1);
  3192. }
  3193. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3194. // if file not found.
  3195. $decoded = html_entity_decode($lp_item_path);
  3196. list ($decoded) = explode('?', $decoded);
  3197. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3198. require_once 'resourcelinker.inc.php';
  3199. $file = rl_get_resource_link_for_learnpath(
  3200. $course_id,
  3201. $this->get_id(),
  3202. $item_id,
  3203. $this->get_view_id()
  3204. );
  3205. if (empty($file)) {
  3206. $file = 'blank.php?error=document_not_found';
  3207. } else {
  3208. $tmp_array = explode('/', $file);
  3209. $document_name = $tmp_array[count($tmp_array) - 1];
  3210. if (strpos($document_name, '_DELETED_')) {
  3211. $file = 'blank.php?error=document_deleted';
  3212. } else {
  3213. $file = 'blank.php?error=document_not_found';
  3214. }
  3215. }
  3216. } else {
  3217. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3218. }
  3219. }
  3220. }
  3221. // We want to use parameters if they were defined in the imsmanifest
  3222. if (strpos($file, 'blank.php') === false) {
  3223. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3224. }
  3225. } else {
  3226. $file = 'lp_content.php?type=dir';
  3227. }
  3228. break;
  3229. case 3 :
  3230. if ($this->debug > 2) {
  3231. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3232. }
  3233. // Formatting AICC HACP append URL.
  3234. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3235. if (!empty($lp_item_params)) {
  3236. $aicc_append .= $lp_item_params . '&';
  3237. }
  3238. if ($lp_item_type != 'dir') {
  3239. // Quite complex here:
  3240. // We want to make sure 'http://' (and similar) links can
  3241. // be loaded as is (withouth the Chamilo path in front) but
  3242. // some contents use this form: resource.htm?resource=http://blablabla
  3243. // which means we have to find a protocol at the path's start, otherwise
  3244. // it should not be considered as an external URL.
  3245. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3246. if ($this->debug > 2) {
  3247. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3248. }
  3249. // Distant url, return as is.
  3250. $file = $lp_item_path;
  3251. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3252. /*
  3253. if (stristr($file,'<servername>') !== false) {
  3254. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3255. }
  3256. */
  3257. if (stripos($file, '<servername>') !== false) {
  3258. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3259. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3260. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3261. }
  3262. //
  3263. $file .= $aicc_append;
  3264. } else {
  3265. if ($this->debug > 2) {
  3266. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3267. }
  3268. // Prevent getting untranslatable urls.
  3269. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3270. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3271. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3272. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3273. // TODO: Fix this for urls with protocol header.
  3274. $file = str_replace('//', '/', $file);
  3275. $file = str_replace(':/', '://', $file);
  3276. $file .= $aicc_append;
  3277. }
  3278. } else {
  3279. $file = 'lp_content.php?type=dir';
  3280. }
  3281. break;
  3282. case 4 :
  3283. break;
  3284. default :
  3285. break;
  3286. }
  3287. // Replace &amp; by & because &amp; will break URL with params
  3288. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3289. }
  3290. if ($this->debug > 2) {
  3291. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3292. }
  3293. return $file;
  3294. }
  3295. /**
  3296. * Gets the latest usable view or generate a new one
  3297. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3298. * @return integer DB lp_view id
  3299. */
  3300. public function get_view($attempt_num = 0)
  3301. {
  3302. if ($this->debug > 0) {
  3303. error_log('New LP - In learnpath::get_view()', 0);
  3304. }
  3305. $search = '';
  3306. // Use $attempt_num to enable multi-views management (disabled so far).
  3307. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3308. $search = 'AND view_count = ' . $attempt_num;
  3309. }
  3310. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3311. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3312. $course_id = api_get_course_int_id();
  3313. $sessionId = api_get_session_id();
  3314. $sql = "SELECT id, view_count FROM $lp_view_table
  3315. WHERE
  3316. c_id = " . $course_id . " AND
  3317. lp_id = " . $this->get_id() . " AND
  3318. user_id = " . $this->get_user_id() . " AND
  3319. session_id = $sessionId
  3320. $search
  3321. ORDER BY view_count DESC";
  3322. $res = Database::query($sql);
  3323. if (Database :: num_rows($res) > 0) {
  3324. $row = Database :: fetch_array($res);
  3325. $this->lp_view_id = $row['id'];
  3326. } else if (!api_is_invitee()) {
  3327. // There is no database record, create one.
  3328. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3329. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3330. Database::query($sql);
  3331. $id = Database :: insert_id();
  3332. $this->lp_view_id = $id;
  3333. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3334. Database::query($sql);
  3335. }
  3336. return $this->lp_view_id;
  3337. }
  3338. /**
  3339. * Gets the current view id
  3340. * @return integer View ID (from lp_view)
  3341. */
  3342. public function get_view_id()
  3343. {
  3344. if ($this->debug > 0) {
  3345. error_log('New LP - In learnpath::get_view_id()', 0);
  3346. }
  3347. if (!empty ($this->lp_view_id)) {
  3348. return $this->lp_view_id;
  3349. } else {
  3350. return 0;
  3351. }
  3352. }
  3353. /**
  3354. * Gets the update queue
  3355. * @return array Array containing IDs of items to be updated by JavaScript
  3356. */
  3357. public function get_update_queue()
  3358. {
  3359. if ($this->debug > 0) {
  3360. error_log('New LP - In learnpath::get_update_queue()', 0);
  3361. }
  3362. return $this->update_queue;
  3363. }
  3364. /**
  3365. * Gets the user ID
  3366. * @return integer User ID
  3367. */
  3368. public function get_user_id()
  3369. {
  3370. if ($this->debug > 0) {
  3371. error_log('New LP - In learnpath::get_user_id()', 0);
  3372. }
  3373. if (!empty ($this->user_id)) {
  3374. return $this->user_id;
  3375. } else {
  3376. return false;
  3377. }
  3378. }
  3379. /**
  3380. * Checks if any of the items has an audio element attached
  3381. * @return bool True or false
  3382. */
  3383. public function has_audio()
  3384. {
  3385. if ($this->debug > 1) {
  3386. error_log('New LP - In learnpath::has_audio()', 0);
  3387. }
  3388. $has = false;
  3389. foreach ($this->items as $i => $item) {
  3390. if (!empty ($this->items[$i]->audio)) {
  3391. $has = true;
  3392. break;
  3393. }
  3394. }
  3395. return $has;
  3396. }
  3397. /**
  3398. * Logs a message into a file
  3399. * @param string Message to log
  3400. * @return boolean True on success, false on error or if msg empty
  3401. */
  3402. public function log($msg)
  3403. {
  3404. if ($this->debug > 0) {
  3405. error_log('New LP - In learnpath::log()', 0);
  3406. }
  3407. // TODO
  3408. $this->error .= $msg;
  3409. return true;
  3410. }
  3411. /**
  3412. * Moves an item up and down at its level
  3413. * @param integer Item to move up and down
  3414. * @param string Direction 'up' or 'down'
  3415. * @return integer New display order, or false on error
  3416. */
  3417. public function move_item($id, $direction)
  3418. {
  3419. $course_id = api_get_course_int_id();
  3420. if ($this->debug > 0) {
  3421. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3422. }
  3423. if (empty($id) || empty($direction)) {
  3424. return false;
  3425. }
  3426. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3427. $sql_sel = "SELECT *
  3428. FROM " . $tbl_lp_item . "
  3429. WHERE c_id = ".$course_id." AND id = " . $id;
  3430. $res_sel = Database::query($sql_sel);
  3431. // Check if elem exists.
  3432. if (Database :: num_rows($res_sel) < 1) {
  3433. return false;
  3434. }
  3435. // Gather data.
  3436. $row = Database :: fetch_array($res_sel);
  3437. $previous = $row['previous_item_id'];
  3438. $next = $row['next_item_id'];
  3439. $display = $row['display_order'];
  3440. $parent = $row['parent_item_id'];
  3441. $lp = $row['lp_id'];
  3442. // Update the item (switch with previous/next one).
  3443. switch ($direction) {
  3444. case 'up':
  3445. if ($this->debug > 2) {
  3446. error_log('Movement up detected', 0);
  3447. }
  3448. if ($display <= 1) { /*do nothing*/
  3449. } else {
  3450. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3451. WHERE c_id = ".$course_id." AND id = $previous";
  3452. if ($this->debug > 2) {
  3453. error_log('Selecting previous: ' . $sql_sel2, 0);
  3454. }
  3455. $res_sel2 = Database::query($sql_sel2);
  3456. if (Database :: num_rows($res_sel2) < 1) {
  3457. $previous_previous = 0;
  3458. }
  3459. // Gather data.
  3460. $row2 = Database :: fetch_array($res_sel2);
  3461. $previous_previous = $row2['previous_item_id'];
  3462. // Update previous_previous item (switch "next" with current).
  3463. if ($previous_previous != 0) {
  3464. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3465. next_item_id = $id
  3466. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3467. if ($this->debug > 2) {
  3468. error_log($sql_upd2, 0);
  3469. }
  3470. Database::query($sql_upd2);
  3471. }
  3472. // Update previous item (switch with current).
  3473. if ($previous != 0) {
  3474. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3475. next_item_id = $next,
  3476. previous_item_id = $id,
  3477. display_order = display_order +1
  3478. WHERE c_id = ".$course_id." AND id = $previous";
  3479. if ($this->debug > 2) {
  3480. error_log($sql_upd2, 0);
  3481. }
  3482. Database::query($sql_upd2);
  3483. }
  3484. // Update current item (switch with previous).
  3485. if ($id != 0) {
  3486. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3487. next_item_id = $previous,
  3488. previous_item_id = $previous_previous,
  3489. display_order = display_order-1
  3490. WHERE c_id = ".$course_id." AND id = $id";
  3491. if ($this->debug > 2) {
  3492. error_log($sql_upd2, 0);
  3493. }
  3494. Database::query($sql_upd2);
  3495. }
  3496. // Update next item (new previous item).
  3497. if ($next != 0) {
  3498. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3499. WHERE c_id = ".$course_id." AND id = $next";
  3500. if ($this->debug > 2) {
  3501. error_log($sql_upd2, 0);
  3502. }
  3503. Database::query($sql_upd2);
  3504. }
  3505. $display = $display -1;
  3506. }
  3507. break;
  3508. case 'down':
  3509. if ($this->debug > 2) {
  3510. error_log('Movement down detected', 0);
  3511. }
  3512. if ($next == 0) { /* Do nothing. */
  3513. } else {
  3514. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3515. if ($this->debug > 2) {
  3516. error_log('Selecting next: ' . $sql_sel2, 0);
  3517. }
  3518. $res_sel2 = Database::query($sql_sel2);
  3519. if (Database :: num_rows($res_sel2) < 1) {
  3520. $next_next = 0;
  3521. }
  3522. // Gather data.
  3523. $row2 = Database :: fetch_array($res_sel2);
  3524. $next_next = $row2['next_item_id'];
  3525. // Update previous item (switch with current).
  3526. if ($previous != 0) {
  3527. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3528. WHERE c_id = ".$course_id." AND id = $previous";
  3529. Database::query($sql_upd2);
  3530. }
  3531. // Update current item (switch with previous).
  3532. if ($id != 0) {
  3533. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3534. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3535. WHERE c_id = ".$course_id." AND id = $id";
  3536. Database::query($sql_upd2);
  3537. }
  3538. // Update next item (new previous item).
  3539. if ($next != 0) {
  3540. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3541. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3542. WHERE c_id = ".$course_id." AND id = $next";
  3543. Database::query($sql_upd2);
  3544. }
  3545. // Update next_next item (switch "previous" with current).
  3546. if ($next_next != 0) {
  3547. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3548. previous_item_id = $id
  3549. WHERE c_id = ".$course_id." AND id = $next_next";
  3550. Database::query($sql_upd2);
  3551. }
  3552. $display = $display +1;
  3553. }
  3554. break;
  3555. default :
  3556. return false;
  3557. }
  3558. return $display;
  3559. }
  3560. /**
  3561. * Move a learnpath up (display_order)
  3562. * @param integer $lp_id Learnpath ID
  3563. */
  3564. public static function move_up($lp_id)
  3565. {
  3566. $course_id = api_get_course_int_id();
  3567. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3568. $sql = "SELECT * FROM $lp_table
  3569. WHERE c_id = ".$course_id."
  3570. ORDER BY display_order";
  3571. $res = Database::query($sql);
  3572. if ($res === false)
  3573. return false;
  3574. $lps = array ();
  3575. $lp_order = array ();
  3576. $num = Database :: num_rows($res);
  3577. // First check the order is correct, globally (might be wrong because
  3578. // of versions < 1.8.4)
  3579. if ($num > 0) {
  3580. $i = 1;
  3581. while ($row = Database :: fetch_array($res)) {
  3582. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3583. $need_fix = true;
  3584. $sql_u = "UPDATE $lp_table SET display_order = $i
  3585. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3586. Database::query($sql_u);
  3587. }
  3588. $row['display_order'] = $i;
  3589. $lps[$row['id']] = $row;
  3590. $lp_order[$i] = $row['id'];
  3591. $i++;
  3592. }
  3593. }
  3594. if ($num > 1) { // If there's only one element, no need to sort.
  3595. $order = $lps[$lp_id]['display_order'];
  3596. if ($order > 1) { // If it's the first element, no need to move up.
  3597. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3598. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3599. Database::query($sql_u1);
  3600. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3601. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3602. Database::query($sql_u2);
  3603. }
  3604. }
  3605. }
  3606. /**
  3607. * Move a learnpath down (display_order)
  3608. * @param integer $lp_id Learnpath ID
  3609. */
  3610. public static function move_down($lp_id)
  3611. {
  3612. $course_id = api_get_course_int_id();
  3613. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3614. $sql = "SELECT * FROM $lp_table
  3615. WHERE c_id = ".$course_id."
  3616. ORDER BY display_order";
  3617. $res = Database::query($sql);
  3618. if ($res === false) {
  3619. return false;
  3620. }
  3621. $lps = array ();
  3622. $lp_order = array ();
  3623. $num = Database :: num_rows($res);
  3624. $max = 0;
  3625. // First check the order is correct, globally (might be wrong because
  3626. // of versions < 1.8.4).
  3627. if ($num > 0) {
  3628. $i = 1;
  3629. while ($row = Database :: fetch_array($res)) {
  3630. $max = $i;
  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 < $max) { // 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. * Updates learnpath attributes to point to the next element
  3657. * The last part is similar to set_current_item but processing the other way around
  3658. */
  3659. public function next()
  3660. {
  3661. if ($this->debug > 0) {
  3662. error_log('New LP - In learnpath::next()', 0);
  3663. }
  3664. $this->last = $this->get_current_item_id();
  3665. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3666. $this->autocomplete_parents($this->last);
  3667. $new_index = $this->get_next_index();
  3668. if ($this->debug > 2) {
  3669. error_log('New LP - New index: ' . $new_index, 0);
  3670. }
  3671. $this->index = $new_index;
  3672. if ($this->debug > 2) {
  3673. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3674. }
  3675. $this->current = $this->ordered_items[$new_index];
  3676. if ($this->debug > 2) {
  3677. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3678. }
  3679. }
  3680. /**
  3681. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3682. * class, this might be redefined to allow several behaviours depending on the document type.
  3683. * @param integer Resource ID
  3684. * @return boolean True on success, false otherwise
  3685. */
  3686. public function open($id)
  3687. {
  3688. if ($this->debug > 0) {
  3689. error_log('New LP - In learnpath::open()', 0);
  3690. }
  3691. // TODO:
  3692. // set the current resource attribute to this resource
  3693. // switch on element type (redefine in child class?)
  3694. // set status for this item to "opened"
  3695. // start timer
  3696. // initialise score
  3697. $this->index = 0; //or = the last item seen (see $this->last)
  3698. }
  3699. /**
  3700. * Check that all prerequisites are fulfilled. Returns true and an
  3701. * empty string on succes, returns false
  3702. * and the prerequisite string on error.
  3703. * This function is based on the rules for aicc_script language as
  3704. * described in the SCORM 1.2 CAM documentation page 108.
  3705. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3706. * @return boolean True if prerequisites are matched, false otherwise -
  3707. * Empty string if true returned, prerequisites string otherwise.
  3708. */
  3709. public function prerequisites_match($itemId = null)
  3710. {
  3711. $debug = $this->debug;
  3712. if ($debug > 0) {
  3713. error_log('In learnpath::prerequisites_match()', 0);
  3714. }
  3715. if (empty($itemId)) {
  3716. $itemId = $this->current;
  3717. }
  3718. $currentItem = $this->getItem($itemId);
  3719. if ($currentItem) {
  3720. if ($this->type == 2) {
  3721. // Getting prereq from scorm
  3722. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3723. } else {
  3724. $prereq_string = $currentItem->get_prereq_string();
  3725. }
  3726. if (empty($prereq_string)) {
  3727. if ($debug > 0) {
  3728. error_log('Found prereq_string is empty return true');
  3729. }
  3730. return true;
  3731. }
  3732. // Clean spaces.
  3733. $prereq_string = str_replace(' ', '', $prereq_string);
  3734. if ($debug > 0) {
  3735. error_log('Found prereq_string: ' . $prereq_string, 0);
  3736. }
  3737. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3738. $result = $currentItem->parse_prereq(
  3739. $prereq_string,
  3740. $this->items,
  3741. $this->refs_list,
  3742. $this->get_user_id()
  3743. );
  3744. if ($result === false) {
  3745. $this->set_error_msg($currentItem->prereq_alert);
  3746. }
  3747. } else {
  3748. $result = true;
  3749. if ($debug > 1) {
  3750. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3751. }
  3752. }
  3753. if ($debug > 1) {
  3754. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3755. }
  3756. return $result;
  3757. }
  3758. /**
  3759. * Updates learnpath attributes to point to the previous element
  3760. * The last part is similar to set_current_item but processing the other way around
  3761. */
  3762. public function previous()
  3763. {
  3764. if ($this->debug > 0) {
  3765. error_log('New LP - In learnpath::previous()', 0);
  3766. }
  3767. $this->last = $this->get_current_item_id();
  3768. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3769. $this->autocomplete_parents($this->last);
  3770. $new_index = $this->get_previous_index();
  3771. $this->index = $new_index;
  3772. $this->current = $this->ordered_items[$new_index];
  3773. }
  3774. /**
  3775. * Publishes a learnpath. This basically means show or hide the learnpath
  3776. * to normal users.
  3777. * Can be used as abstract
  3778. * @param integer Learnpath ID
  3779. * @param string New visibility
  3780. */
  3781. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3782. {
  3783. $action = 'visible';
  3784. if ($set_visibility != 1) {
  3785. $action = 'invisible';
  3786. self::toggle_publish($lp_id, 'i');
  3787. }
  3788. return api_item_property_update(
  3789. api_get_course_info(),
  3790. TOOL_LEARNPATH,
  3791. $lp_id,
  3792. $action,
  3793. api_get_user_id()
  3794. );
  3795. }
  3796. /**
  3797. * Publishes a learnpath. This basically means show or hide the learnpath
  3798. * on the course homepage
  3799. * Can be used as abstract
  3800. * @param integer $lp_id Learnpath id
  3801. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3802. * @return bool
  3803. */
  3804. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3805. {
  3806. $course_id = api_get_course_int_id();
  3807. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3808. $lp_id = intval($lp_id);
  3809. $sql = "SELECT * FROM $tbl_lp
  3810. WHERE c_id = ".$course_id." AND id = $lp_id";
  3811. $result = Database::query($sql);
  3812. if (Database::num_rows($result)) {
  3813. $row = Database :: fetch_array($result);
  3814. $name = domesticate($row['name']);
  3815. if ($set_visibility == 'i') {
  3816. $s = $name . " " . get_lang('LearnpathNotPublished');
  3817. $dialogBox = $s;
  3818. $v = 0;
  3819. }
  3820. if ($set_visibility == 'v') {
  3821. $s = $name . " " . get_lang('LearnpathPublished');
  3822. $dialogBox = $s;
  3823. $v = 1;
  3824. }
  3825. } else {
  3826. return false;
  3827. }
  3828. $session_id = api_get_session_id();
  3829. $session_condition = api_get_session_condition($session_id);
  3830. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3831. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3832. $sql = "SELECT * FROM $tbl_tool
  3833. WHERE
  3834. c_id = ".$course_id." AND
  3835. link='$link' and
  3836. image='scormbuilder.gif' and
  3837. link LIKE '$link%'
  3838. $session_condition
  3839. ";
  3840. $result = Database::query($sql);
  3841. $num = Database :: num_rows($result);
  3842. if ($set_visibility == 'i' && $num > 0) {
  3843. $sql = "DELETE FROM $tbl_tool
  3844. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3845. Database::query($sql);
  3846. } elseif ($set_visibility == 'v' && $num == 0) {
  3847. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3848. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3849. Database::query($sql);
  3850. $insertId = Database::insert_id();
  3851. if ($insertId) {
  3852. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3853. Database::query($sql);
  3854. }
  3855. } elseif ($set_visibility == 'v' && $num > 0) {
  3856. $sql = "UPDATE $tbl_tool SET
  3857. c_id = $course_id,
  3858. name = '$name',
  3859. link = '$link',
  3860. image = 'scormbuilder.gif',
  3861. visibility = '$v',
  3862. admin = '0',
  3863. address = 'pastillegris.gif',
  3864. added_tool = 0,
  3865. session_id = $session_id
  3866. WHERE
  3867. c_id = ".$course_id." AND
  3868. (link='$link' and image='scormbuilder.gif' $session_condition)
  3869. ";
  3870. Database::query($sql);
  3871. } else {
  3872. // Parameter and database incompatible, do nothing, exit.
  3873. return false;
  3874. }
  3875. }
  3876. /**
  3877. * Restart the whole learnpath. Return the URL of the first element.
  3878. * Make sure the results are saved with anoter method. This method should probably be
  3879. * redefined in children classes.
  3880. * To use a similar method statically, use the create_new_attempt() method
  3881. * @return string URL to load in the viewer
  3882. */
  3883. public function restart()
  3884. {
  3885. if ($this->debug > 0) {
  3886. error_log('New LP - In learnpath::restart()', 0);
  3887. }
  3888. // TODO
  3889. // Call autosave method to save the current progress.
  3890. //$this->index = 0;
  3891. if (api_is_invitee()) {
  3892. return false;
  3893. }
  3894. $session_id = api_get_session_id();
  3895. $course_id = api_get_course_int_id();
  3896. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3897. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3898. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3899. if ($this->debug > 2) {
  3900. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3901. }
  3902. $res = Database::query($sql);
  3903. $view_id = Database::insert_id();
  3904. if ($view_id) {
  3905. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3906. Database::query($sql);
  3907. $this->lp_view_id = $view_id;
  3908. $this->attempt = $this->attempt + 1;
  3909. } else {
  3910. $this->error = 'Could not insert into item_view table...';
  3911. return false;
  3912. }
  3913. $this->autocomplete_parents($this->current);
  3914. foreach ($this->items as $index => $dummy) {
  3915. $this->items[$index]->restart();
  3916. $this->items[$index]->set_lp_view($this->lp_view_id);
  3917. }
  3918. $this->first();
  3919. return true;
  3920. }
  3921. /**
  3922. * Saves the current item
  3923. * @return boolean
  3924. */
  3925. public function save_current()
  3926. {
  3927. if ($this->debug > 0) {
  3928. error_log('learnpath::save_current()', 0);
  3929. }
  3930. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3931. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3932. if ($this->debug > 2) {
  3933. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3934. }
  3935. if ($this->debug > 2) {
  3936. error_log('' . print_r($this->items, true), 0);
  3937. }
  3938. if (isset($this->items[$this->current]) &&
  3939. is_object($this->items[$this->current])
  3940. ) {
  3941. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3942. $this->autocomplete_parents($this->current);
  3943. $status = $this->items[$this->current]->get_status();
  3944. $this->update_queue[$this->current] = $status;
  3945. return $res;
  3946. }
  3947. return false;
  3948. }
  3949. /**
  3950. * Saves the given item
  3951. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3952. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3953. * @return boolean
  3954. */
  3955. public function save_item($item_id = null, $from_outside = true)
  3956. {
  3957. $debug = $this->debug;
  3958. if ($debug) {
  3959. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3960. }
  3961. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3962. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3963. if (empty($item_id)) {
  3964. $item_id = intval($_REQUEST['id']);
  3965. }
  3966. if (empty($item_id)) {
  3967. $item_id = $this->get_current_item_id();
  3968. }
  3969. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3970. if ($debug) {
  3971. error_log('Object exists');
  3972. }
  3973. // Saving the item.
  3974. $res = $this->items[$item_id]->save(
  3975. $from_outside,
  3976. $this->prerequisites_match($item_id)
  3977. );
  3978. if ($debug) {
  3979. error_log('update_queue before:');
  3980. error_log(print_r($this->update_queue,1));
  3981. }
  3982. $this->autocomplete_parents($item_id);
  3983. $status = $this->items[$item_id]->get_status();
  3984. $this->update_queue[$item_id] = $status;
  3985. if ($debug) {
  3986. error_log('get_status(): ' . $status);
  3987. error_log('update_queue after:');
  3988. error_log(print_r($this->update_queue,1));
  3989. }
  3990. return $res;
  3991. }
  3992. return false;
  3993. }
  3994. /**
  3995. * Saves the last item seen's ID only in case
  3996. */
  3997. public function save_last()
  3998. {
  3999. $course_id = api_get_course_int_id();
  4000. if ($this->debug > 0) {
  4001. error_log('New LP - In learnpath::save_last()', 0);
  4002. }
  4003. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4004. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4005. if (isset($this->current) && !api_is_invitee()) {
  4006. if ($this->debug > 2) {
  4007. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4008. }
  4009. $sql = "UPDATE $table SET
  4010. last_item = " . intval($this->get_current_item_id()). "
  4011. WHERE
  4012. c_id = $course_id AND
  4013. lp_id = " . $this->get_id() . " AND
  4014. user_id = " . $this->get_user_id()." ".$session_condition;
  4015. if ($this->debug > 2) {
  4016. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4017. }
  4018. Database::query($sql);
  4019. }
  4020. if (!api_is_invitee()) {
  4021. // Save progress.
  4022. list($progress, $text) = $this->get_progress_bar_text('%');
  4023. if ($progress >= 0 && $progress <= 100) {
  4024. $progress = (int) $progress;
  4025. $sql = "UPDATE $table SET
  4026. progress = $progress
  4027. WHERE
  4028. c_id = ".$course_id." AND
  4029. lp_id = " . $this->get_id() . " AND
  4030. user_id = " . $this->get_user_id()." ".$session_condition;
  4031. // Ignore errors as some tables might not have the progress field just yet.
  4032. Database::query($sql);
  4033. $this->progress_db = $progress;
  4034. }
  4035. }
  4036. }
  4037. /**
  4038. * Sets the current item ID (checks if valid and authorized first)
  4039. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4040. */
  4041. public function set_current_item($item_id = null)
  4042. {
  4043. if ($this->debug > 0) {
  4044. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4045. }
  4046. if (empty ($item_id)) {
  4047. if ($this->debug > 2) {
  4048. error_log('New LP - No new current item given, ignore...', 0);
  4049. }
  4050. // Do nothing.
  4051. } else {
  4052. if ($this->debug > 2) {
  4053. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4054. }
  4055. if (is_numeric($item_id)) {
  4056. $item_id = intval($item_id);
  4057. // TODO: Check in database here.
  4058. $this->last = $this->current;
  4059. $this->current = $item_id;
  4060. // TODO: Update $this->index as well.
  4061. foreach ($this->ordered_items as $index => $item) {
  4062. if ($item == $this->current) {
  4063. $this->index = $index;
  4064. break;
  4065. }
  4066. }
  4067. if ($this->debug > 2) {
  4068. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4069. }
  4070. } else {
  4071. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4072. }
  4073. }
  4074. }
  4075. /**
  4076. * Sets the encoding
  4077. * @param string New encoding
  4078. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4079. */
  4080. public function set_encoding($enc = 'UTF-8')
  4081. {
  4082. if ($this->debug > 0) {
  4083. error_log('New LP - In learnpath::set_encoding()', 0);
  4084. }
  4085. $course_id = api_get_course_int_id();
  4086. $enc = api_refine_encoding_id($enc);
  4087. if (empty($enc)) {
  4088. $enc = api_get_system_encoding();
  4089. }
  4090. if (api_is_encoding_supported($enc)) {
  4091. $lp = $this->get_id();
  4092. if ($lp != 0) {
  4093. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4094. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4095. $res = Database::query($sql);
  4096. return $res;
  4097. }
  4098. }
  4099. return false;
  4100. }
  4101. /**
  4102. * Sets the JS lib setting in the database directly.
  4103. * This is the JavaScript library file this lp needs to load on startup
  4104. * @param string Proximity setting
  4105. * @return boolean True on update success. False otherwise.
  4106. */
  4107. public function set_jslib($lib = '')
  4108. {
  4109. if ($this->debug > 0) {
  4110. error_log('New LP - In learnpath::set_jslib()', 0);
  4111. }
  4112. $lp = $this->get_id();
  4113. $course_id = api_get_course_int_id();
  4114. if ($lp != 0) {
  4115. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4116. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4117. $res = Database::query($sql);
  4118. return $res;
  4119. } else {
  4120. return false;
  4121. }
  4122. }
  4123. /**
  4124. * Sets the name of the LP maker (publisher) (and save)
  4125. * @param string Optional string giving the new content_maker of this learnpath
  4126. * @return boolean True
  4127. */
  4128. public function set_maker($name = '')
  4129. {
  4130. if ($this->debug > 0) {
  4131. error_log('New LP - In learnpath::set_maker()', 0);
  4132. }
  4133. if (empty ($name))
  4134. return false;
  4135. $this->maker = $name;
  4136. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4137. $course_id = api_get_course_int_id();
  4138. $lp_id = $this->get_id();
  4139. $sql = "UPDATE $lp_table SET
  4140. content_maker = '" . Database::escape_string($this->maker) . "'
  4141. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4142. if ($this->debug > 2) {
  4143. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4144. }
  4145. Database::query($sql);
  4146. return true;
  4147. }
  4148. /**
  4149. * Sets the name of the current learnpath (and save)
  4150. * @param string $name Optional string giving the new name of this learnpath
  4151. * @return boolean True/False
  4152. */
  4153. public function set_name($name = null)
  4154. {
  4155. if ($this->debug > 0) {
  4156. error_log('New LP - In learnpath::set_name()', 0);
  4157. }
  4158. if (empty($name)) {
  4159. return false;
  4160. }
  4161. $this->name = $name;
  4162. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4163. $lp_id = $this->get_id();
  4164. $course_id = api_get_course_int_id();
  4165. $sql = "UPDATE $lp_table SET
  4166. name = '" . Database::escape_string($this->name). "'
  4167. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4168. if ($this->debug > 2) {
  4169. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4170. }
  4171. $result = Database::query($sql);
  4172. // If the lp is visible on the homepage, change his name there.
  4173. if (Database::affected_rows($result)) {
  4174. $session_id = api_get_session_id();
  4175. $session_condition = api_get_session_condition($session_id);
  4176. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4177. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4178. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4179. WHERE
  4180. c_id = $course_id AND
  4181. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4182. Database::query($sql);
  4183. return true;
  4184. } else {
  4185. return false;
  4186. }
  4187. }
  4188. /**
  4189. * Set index specified prefix terms for all items in this path
  4190. * @param string Comma-separated list of terms
  4191. * @param char Xapian term prefix
  4192. * @return boolean False on error, true otherwise
  4193. */
  4194. public function set_terms_by_prefix($terms_string, $prefix)
  4195. {
  4196. $course_id = api_get_course_int_id();
  4197. if (api_get_setting('search_enabled') !== 'true')
  4198. return false;
  4199. if (!extension_loaded('xapian')) {
  4200. return false;
  4201. }
  4202. $terms_string = trim($terms_string);
  4203. $terms = explode(',', $terms_string);
  4204. array_walk($terms, 'trim_value');
  4205. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4206. // Don't do anything if no change, verify only at DB, not the search engine.
  4207. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4208. return false;
  4209. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4210. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4211. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4212. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4213. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4214. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4215. $lp_id = intval($_POST['lp_id']);
  4216. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4217. $result = Database::query($sql);
  4218. $di = new ChamiloIndexer();
  4219. while ($lp_item = Database :: fetch_array($result)) {
  4220. // Get search_did.
  4221. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4222. $sql = 'SELECT * FROM %s
  4223. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4224. LIMIT 1';
  4225. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4226. //echo $sql; echo '<br>';
  4227. $res = Database::query($sql);
  4228. if (Database::num_rows($res) > 0) {
  4229. $se_ref = Database :: fetch_array($res);
  4230. // Compare terms.
  4231. $doc = $di->get_document($se_ref['search_did']);
  4232. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4233. $xterms = array();
  4234. foreach ($xapian_terms as $xapian_term) {
  4235. $xterms[] = substr($xapian_term['name'], 1);
  4236. }
  4237. $dterms = $terms;
  4238. $missing_terms = array_diff($dterms, $xterms);
  4239. $deprecated_terms = array_diff($xterms, $dterms);
  4240. // Save it to search engine.
  4241. foreach ($missing_terms as $term) {
  4242. $doc->add_term($prefix . $term, 1);
  4243. }
  4244. foreach ($deprecated_terms as $term) {
  4245. $doc->remove_term($prefix . $term);
  4246. }
  4247. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4248. $di->getDb()->flush();
  4249. } else {
  4250. //@todo What we should do here?
  4251. }
  4252. }
  4253. return true;
  4254. }
  4255. /**
  4256. * Sets the theme of the LP (local/remote) (and save)
  4257. * @param string Optional string giving the new theme of this learnpath
  4258. * @return bool Returns true if theme name is not empty
  4259. */
  4260. public function set_theme($name = '')
  4261. {
  4262. $course_id = api_get_course_int_id();
  4263. if ($this->debug > 0) {
  4264. error_log('New LP - In learnpath::set_theme()', 0);
  4265. }
  4266. $this->theme = $name;
  4267. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4268. $lp_id = $this->get_id();
  4269. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4270. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4271. if ($this->debug > 2) {
  4272. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4273. }
  4274. Database::query($sql);
  4275. return true;
  4276. }
  4277. /**
  4278. * Sets the image of an LP (and save)
  4279. * @param string Optional string giving the new image of this learnpath
  4280. * @return bool Returns true if theme name is not empty
  4281. */
  4282. public function set_preview_image($name = '')
  4283. {
  4284. $course_id = api_get_course_int_id();
  4285. if ($this->debug > 0) {
  4286. error_log('New LP - In learnpath::set_preview_image()', 0);
  4287. }
  4288. $this->preview_image = $name;
  4289. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4290. $lp_id = $this->get_id();
  4291. $sql = "UPDATE $lp_table SET
  4292. preview_image = '" . Database::escape_string($this->preview_image). "'
  4293. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4294. if ($this->debug > 2) {
  4295. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4296. }
  4297. Database::query($sql);
  4298. return true;
  4299. }
  4300. /**
  4301. * Sets the author of a LP (and save)
  4302. * @param string Optional string giving the new author of this learnpath
  4303. * @return bool Returns true if author's name is not empty
  4304. */
  4305. public function set_author($name = '')
  4306. {
  4307. $course_id = api_get_course_int_id();
  4308. if ($this->debug > 0) {
  4309. error_log('New LP - In learnpath::set_author()', 0);
  4310. }
  4311. $this->author = $name;
  4312. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4313. $lp_id = $this->get_id();
  4314. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4315. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4316. if ($this->debug > 2) {
  4317. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4318. }
  4319. Database::query($sql);
  4320. return true;
  4321. }
  4322. /**
  4323. * Sets the hide_toc_frame parameter of a LP (and save)
  4324. * @param int 1 if frame is hidden 0 then else
  4325. * @return bool Returns true if author's name is not empty
  4326. */
  4327. public function set_hide_toc_frame($hide)
  4328. {
  4329. $course_id = api_get_course_int_id();
  4330. if ($this->debug > 0) {
  4331. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4332. }
  4333. if (intval($hide) == $hide){
  4334. $this->hide_toc_frame = $hide;
  4335. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4336. $lp_id = $this->get_id();
  4337. $sql = "UPDATE $lp_table SET
  4338. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4339. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4340. if ($this->debug > 2) {
  4341. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4342. }
  4343. Database::query($sql);
  4344. return true;
  4345. } else {
  4346. return false;
  4347. }
  4348. }
  4349. /**
  4350. * Sets the prerequisite of a LP (and save)
  4351. * @param int integer giving the new prerequisite of this learnpath
  4352. * @return bool returns true if prerequisite is not empty
  4353. */
  4354. public function set_prerequisite($prerequisite)
  4355. {
  4356. $course_id = api_get_course_int_id();
  4357. if ($this->debug > 0) {
  4358. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4359. }
  4360. $this->prerequisite = intval($prerequisite);
  4361. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4362. $lp_id = $this->get_id();
  4363. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  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 requisite : ' . $this->requisite, 0);
  4367. }
  4368. Database::query($sql);
  4369. return true;
  4370. }
  4371. /**
  4372. * Sets the location/proximity of the LP (local/remote) (and save)
  4373. * @param string Optional string giving the new location of this learnpath
  4374. * @return boolean True on success / False on error
  4375. */
  4376. public function set_proximity($name = '')
  4377. {
  4378. $course_id = api_get_course_int_id();
  4379. if ($this->debug > 0) {
  4380. error_log('New LP - In learnpath::set_proximity()', 0);
  4381. }
  4382. if (empty ($name))
  4383. return false;
  4384. $this->proximity = $name;
  4385. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4386. $lp_id = $this->get_id();
  4387. $sql = "UPDATE $lp_table SET
  4388. content_local = '" . Database::escape_string($name) . "'
  4389. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4390. if ($this->debug > 2) {
  4391. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4392. }
  4393. Database::query($sql);
  4394. return true;
  4395. }
  4396. /**
  4397. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4398. * @param integer DB ID of the item
  4399. */
  4400. public function set_previous_item($id)
  4401. {
  4402. if ($this->debug > 0) {
  4403. error_log('New LP - In learnpath::set_previous_item()', 0);
  4404. }
  4405. $this->last = $id;
  4406. }
  4407. /**
  4408. * Sets use_max_score
  4409. * @param string $use_max_score Optional string giving the new location of this learnpath
  4410. * @return boolean True on success / False on error
  4411. */
  4412. public function set_use_max_score($use_max_score = 1)
  4413. {
  4414. $course_id = api_get_course_int_id();
  4415. if ($this->debug > 0) {
  4416. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4417. }
  4418. $use_max_score = intval($use_max_score);
  4419. $this->use_max_score = $use_max_score;
  4420. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4421. $lp_id = $this->get_id();
  4422. $sql = "UPDATE $lp_table SET
  4423. use_max_score = '" . $this->use_max_score . "'
  4424. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4425. if ($this->debug > 2) {
  4426. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4427. }
  4428. Database::query($sql);
  4429. return true;
  4430. }
  4431. /**
  4432. * Sets and saves the expired_on date
  4433. * @param string $expired_on Optional string giving the new author of this learnpath
  4434. * @return bool Returns true if author's name is not empty
  4435. */
  4436. public function set_expired_on($expired_on)
  4437. {
  4438. $course_id = api_get_course_int_id();
  4439. if ($this->debug > 0) {
  4440. error_log('New LP - In learnpath::set_expired_on()', 0);
  4441. }
  4442. if (!empty($expired_on)) {
  4443. $this->expired_on = api_get_utc_datetime($expired_on);
  4444. } else {
  4445. $this->expired_on = '';
  4446. }
  4447. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4448. $lp_id = $this->get_id();
  4449. $sql = "UPDATE $lp_table SET
  4450. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4451. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4452. if ($this->debug > 2) {
  4453. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4454. }
  4455. Database::query($sql);
  4456. return true;
  4457. }
  4458. /**
  4459. * Sets and saves the publicated_on date
  4460. * @param string $publicated_on Optional string giving the new author of this learnpath
  4461. * @return bool Returns true if author's name is not empty
  4462. */
  4463. public function set_publicated_on($publicated_on)
  4464. {
  4465. $course_id = api_get_course_int_id();
  4466. if ($this->debug > 0) {
  4467. error_log('New LP - In learnpath::set_expired_on()', 0);
  4468. }
  4469. if (!empty($publicated_on)) {
  4470. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4471. } else {
  4472. $this->publicated_on = '';
  4473. }
  4474. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4475. $lp_id = $this->get_id();
  4476. $sql = "UPDATE $lp_table SET
  4477. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4478. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4479. if ($this->debug > 2) {
  4480. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4481. }
  4482. Database::query($sql);
  4483. return true;
  4484. }
  4485. /**
  4486. * Sets and saves the expired_on date
  4487. * @return bool Returns true if author's name is not empty
  4488. */
  4489. public function set_modified_on()
  4490. {
  4491. $course_id = api_get_course_int_id();
  4492. if ($this->debug > 0) {
  4493. error_log('New LP - In learnpath::set_expired_on()', 0);
  4494. }
  4495. $this->modified_on = api_get_utc_datetime();
  4496. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4497. $lp_id = $this->get_id();
  4498. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4499. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4500. if ($this->debug > 2) {
  4501. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4502. }
  4503. Database::query($sql);
  4504. return true;
  4505. }
  4506. /**
  4507. * Sets the object's error message
  4508. * @param string Error message. If empty, reinits the error string
  4509. * @return void
  4510. */
  4511. public function set_error_msg($error = '')
  4512. {
  4513. if ($this->debug > 0) {
  4514. error_log('New LP - In learnpath::set_error_msg()', 0);
  4515. }
  4516. if (empty ($error)) {
  4517. $this->error = '';
  4518. } else {
  4519. $this->error .= $error;
  4520. }
  4521. }
  4522. /**
  4523. * Launches the current item if not 'sco'
  4524. * (starts timer and make sure there is a record ready in the DB)
  4525. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4526. * @return boolean
  4527. */
  4528. public function start_current_item($allow_new_attempt = false)
  4529. {
  4530. if ($this->debug > 0) {
  4531. error_log('New LP - In learnpath::start_current_item()', 0);
  4532. }
  4533. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4534. $type = $this->get_type();
  4535. $item_type = $this->items[$this->current]->get_type();
  4536. if (($type == 2 && $item_type != 'sco') ||
  4537. ($type == 3 && $item_type != 'au') ||
  4538. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4539. ) {
  4540. $this->items[$this->current]->open($allow_new_attempt);
  4541. $this->autocomplete_parents($this->current);
  4542. $prereq_check = $this->prerequisites_match($this->current);
  4543. $this->items[$this->current]->save(false, $prereq_check);
  4544. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4545. } else {
  4546. // If sco, then it is supposed to have been updated by some other call.
  4547. }
  4548. if ($item_type == 'sco') {
  4549. $this->items[$this->current]->restart();
  4550. }
  4551. }
  4552. if ($this->debug > 0) {
  4553. error_log('New LP - End of learnpath::start_current_item()', 0);
  4554. }
  4555. return true;
  4556. }
  4557. /**
  4558. * Stops the processing and counters for the old item (as held in $this->last)
  4559. * @return boolean True/False
  4560. */
  4561. public function stop_previous_item()
  4562. {
  4563. if ($this->debug > 0) {
  4564. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4565. }
  4566. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4567. if ($this->debug > 2) {
  4568. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4569. }
  4570. switch ($this->get_type()) {
  4571. case '3' :
  4572. if ($this->items[$this->last]->get_type() != 'au') {
  4573. if ($this->debug > 2) {
  4574. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4575. }
  4576. $this->items[$this->last]->close();
  4577. //$this->autocomplete_parents($this->last);
  4578. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4579. } else {
  4580. if ($this->debug > 2) {
  4581. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4582. }
  4583. }
  4584. case '2' :
  4585. if ($this->items[$this->last]->get_type() != 'sco') {
  4586. if ($this->debug > 2) {
  4587. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4588. }
  4589. $this->items[$this->last]->close();
  4590. //$this->autocomplete_parents($this->last);
  4591. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4592. } else {
  4593. if ($this->debug > 2) {
  4594. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4595. }
  4596. }
  4597. break;
  4598. case '1' :
  4599. default :
  4600. if ($this->debug > 2) {
  4601. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4602. }
  4603. $this->items[$this->last]->close();
  4604. break;
  4605. }
  4606. } else {
  4607. if ($this->debug > 2) {
  4608. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4609. }
  4610. return false;
  4611. }
  4612. return true;
  4613. }
  4614. /**
  4615. * Updates the default view mode from fullscreen to embedded and inversely
  4616. * @return string The current default view mode ('fullscreen' or 'embedded')
  4617. */
  4618. public function update_default_view_mode()
  4619. {
  4620. $course_id = api_get_course_int_id();
  4621. if ($this->debug > 0) {
  4622. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4623. }
  4624. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4625. $sql = "SELECT * FROM $lp_table
  4626. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4627. $res = Database::query($sql);
  4628. if (Database :: num_rows($res) > 0) {
  4629. $row = Database :: fetch_array($res);
  4630. $default_view_mode = $row['default_view_mod'];
  4631. $view_mode = $default_view_mode;
  4632. switch ($default_view_mode) {
  4633. case 'fullscreen':
  4634. $view_mode = 'embedded';
  4635. break;
  4636. case 'embedded':
  4637. $view_mode = 'embedframe';
  4638. break;
  4639. case 'embedframe':
  4640. $view_mode = 'impress';
  4641. break;
  4642. case 'impress':
  4643. $view_mode = 'fullscreen';
  4644. break;
  4645. }
  4646. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4647. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4648. Database::query($sql);
  4649. $this->mode = $view_mode;
  4650. return $view_mode;
  4651. } else {
  4652. if ($this->debug > 2) {
  4653. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4654. }
  4655. }
  4656. return -1;
  4657. }
  4658. /**
  4659. * Updates the default behaviour about auto-commiting SCORM updates
  4660. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4661. */
  4662. public function update_default_scorm_commit()
  4663. {
  4664. $course_id = api_get_course_int_id();
  4665. if ($this->debug > 0) {
  4666. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4667. }
  4668. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4669. $sql = "SELECT * FROM $lp_table
  4670. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4671. $res = Database::query($sql);
  4672. if (Database :: num_rows($res) > 0) {
  4673. $row = Database :: fetch_array($res);
  4674. $force = $row['force_commit'];
  4675. if ($force == 1) {
  4676. $force = 0;
  4677. $force_return = false;
  4678. } elseif ($force == 0) {
  4679. $force = 1;
  4680. $force_return = true;
  4681. }
  4682. $sql = "UPDATE $lp_table SET force_commit = $force
  4683. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4684. Database::query($sql);
  4685. $this->force_commit = $force_return;
  4686. return $force_return;
  4687. } else {
  4688. if ($this->debug > 2) {
  4689. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4690. }
  4691. }
  4692. return -1;
  4693. }
  4694. /**
  4695. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4696. * @return bool True on success, false on failure
  4697. */
  4698. public function update_display_order()
  4699. {
  4700. $course_id = api_get_course_int_id();
  4701. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4702. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4703. $res = Database::query($sql);
  4704. if ($res === false)
  4705. return false;
  4706. $num = Database :: num_rows($res);
  4707. // First check the order is correct, globally (might be wrong because
  4708. // of versions < 1.8.4).
  4709. if ($num > 0) {
  4710. $i = 1;
  4711. while ($row = Database :: fetch_array($res)) {
  4712. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4713. $need_fix = true;
  4714. $sql = "UPDATE $lp_table SET display_order = $i
  4715. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4716. Database::query($sql);
  4717. }
  4718. $i++;
  4719. }
  4720. }
  4721. return true;
  4722. }
  4723. /**
  4724. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4725. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4726. */
  4727. public function update_reinit()
  4728. {
  4729. $course_id = api_get_course_int_id();
  4730. if ($this->debug > 0) {
  4731. error_log('New LP - In learnpath::update_reinit()', 0);
  4732. }
  4733. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4734. $sql = "SELECT * FROM $lp_table
  4735. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4736. $res = Database::query($sql);
  4737. if (Database :: num_rows($res) > 0) {
  4738. $row = Database :: fetch_array($res);
  4739. $force = $row['prevent_reinit'];
  4740. if ($force == 1) {
  4741. $force = 0;
  4742. } elseif ($force == 0) {
  4743. $force = 1;
  4744. }
  4745. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4746. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4747. Database::query($sql);
  4748. $this->prevent_reinit = $force;
  4749. return $force;
  4750. } else {
  4751. if ($this->debug > 2) {
  4752. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4753. }
  4754. }
  4755. return -1;
  4756. }
  4757. /**
  4758. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4759. *
  4760. * @return string 'single', 'multi' or 'seriousgame'
  4761. * @author ndiechburg <noel@cblue.be>
  4762. **/
  4763. public function get_attempt_mode()
  4764. {
  4765. //Set default value for seriousgame_mode
  4766. if (!isset($this->seriousgame_mode)) {
  4767. $this->seriousgame_mode=0;
  4768. }
  4769. // Set default value for prevent_reinit
  4770. if (!isset($this->prevent_reinit)) {
  4771. $this->prevent_reinit =1;
  4772. }
  4773. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4774. return 'seriousgame';
  4775. }
  4776. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4777. return 'single';
  4778. }
  4779. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4780. return 'multiple';
  4781. }
  4782. return 'single';
  4783. }
  4784. /**
  4785. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4786. *
  4787. * @param string 'seriousgame', 'single' or 'multiple'
  4788. * @return boolean
  4789. * @author ndiechburg <noel@cblue.be>
  4790. **/
  4791. public function set_attempt_mode($mode)
  4792. {
  4793. $course_id = api_get_course_int_id();
  4794. switch ($mode) {
  4795. case 'seriousgame' :
  4796. $sg_mode = 1;
  4797. $prevent_reinit = 1;
  4798. break;
  4799. case 'single' :
  4800. $sg_mode = 0;
  4801. $prevent_reinit = 1;
  4802. break;
  4803. case 'multiple' :
  4804. $sg_mode = 0;
  4805. $prevent_reinit = 0;
  4806. break;
  4807. default :
  4808. $sg_mode = 0;
  4809. $prevent_reinit = 0;
  4810. break;
  4811. }
  4812. $this->prevent_reinit = $prevent_reinit;
  4813. $this->seriousgame_mode = $sg_mode;
  4814. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4815. $sql = "UPDATE $lp_table SET
  4816. prevent_reinit = $prevent_reinit ,
  4817. seriousgame_mode = $sg_mode
  4818. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4819. $res = Database::query($sql);
  4820. if ($res) {
  4821. return true;
  4822. } else {
  4823. return false;
  4824. }
  4825. }
  4826. /**
  4827. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4828. *
  4829. * @return boolean
  4830. * @author ndiechburg <noel@cblue.be>
  4831. **/
  4832. public function switch_attempt_mode()
  4833. {
  4834. if ($this->debug > 0) {
  4835. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4836. }
  4837. $mode = $this->get_attempt_mode();
  4838. switch ($mode) {
  4839. case 'single' :
  4840. $next_mode = 'multiple';
  4841. break;
  4842. case 'multiple' :
  4843. $next_mode = 'seriousgame';
  4844. break;
  4845. case 'seriousgame' :
  4846. $next_mode = 'single';
  4847. break;
  4848. default :
  4849. $next_mode = 'single';
  4850. break;
  4851. }
  4852. $this->set_attempt_mode($next_mode);
  4853. }
  4854. /**
  4855. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4856. * but possibility to do again a completed item.
  4857. *
  4858. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4859. * @author ndiechburg <noel@cblue.be>
  4860. **/
  4861. public function set_seriousgame_mode()
  4862. {
  4863. $course_id = api_get_course_int_id();
  4864. if ($this->debug > 0) {
  4865. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4866. }
  4867. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4868. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4869. $res = Database::query($sql);
  4870. if (Database :: num_rows($res) > 0) {
  4871. $row = Database :: fetch_array($res);
  4872. $force = $row['seriousgame_mode'];
  4873. if ($force == 1) {
  4874. $force = 0;
  4875. } elseif ($force == 0) {
  4876. $force = 1;
  4877. }
  4878. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4879. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4880. Database::query($sql);
  4881. $this->seriousgame_mode = $force;
  4882. return $force;
  4883. } else {
  4884. if ($this->debug > 2) {
  4885. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4886. }
  4887. }
  4888. return -1;
  4889. }
  4890. /**
  4891. * Updates the "scorm_debug" value that shows or hide the debug window
  4892. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4893. */
  4894. public function update_scorm_debug()
  4895. {
  4896. $course_id = api_get_course_int_id();
  4897. if ($this->debug > 0) {
  4898. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4899. }
  4900. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4901. $sql = "SELECT * FROM $lp_table
  4902. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4903. $res = Database::query($sql);
  4904. if (Database :: num_rows($res) > 0) {
  4905. $row = Database :: fetch_array($res);
  4906. $force = $row['debug'];
  4907. if ($force == 1) {
  4908. $force = 0;
  4909. } elseif ($force == 0) {
  4910. $force = 1;
  4911. }
  4912. $sql = "UPDATE $lp_table SET debug = $force
  4913. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4914. $res = Database::query($sql);
  4915. $this->scorm_debug = $force;
  4916. return $force;
  4917. } else {
  4918. if ($this->debug > 2) {
  4919. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4920. }
  4921. }
  4922. return -1;
  4923. }
  4924. /**
  4925. * Function that makes a call to the function sort_tree_array and create_tree_array
  4926. * @author Kevin Van Den Haute
  4927. * @param array
  4928. */
  4929. public function tree_array($array)
  4930. {
  4931. if ($this->debug > 1) {
  4932. error_log('New LP - In learnpath::tree_array()', 0);
  4933. }
  4934. $array = $this->sort_tree_array($array);
  4935. $this->create_tree_array($array);
  4936. }
  4937. /**
  4938. * Creates an array with the elements of the learning path tree in it
  4939. *
  4940. * @author Kevin Van Den Haute
  4941. * @param array $array
  4942. * @param int $parent
  4943. * @param int $depth
  4944. * @param array $tmp
  4945. */
  4946. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4947. {
  4948. if ($this->debug > 1) {
  4949. error_log('New LP - In learnpath::create_tree_array())', 0);
  4950. }
  4951. if (is_array($array)) {
  4952. for ($i = 0; $i < count($array); $i++) {
  4953. if ($array[$i]['parent_item_id'] == $parent) {
  4954. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4955. $tmp[] = $array[$i]['parent_item_id'];
  4956. $depth++;
  4957. }
  4958. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4959. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4960. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4961. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4962. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4963. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4964. $this->arrMenu[] = array(
  4965. 'id' => $array[$i]['id'],
  4966. 'ref' => $ref,
  4967. 'item_type' => $array[$i]['item_type'],
  4968. 'title' => $array[$i]['title'],
  4969. 'path' => $path,
  4970. 'description' => $array[$i]['description'],
  4971. 'parent_item_id' => $array[$i]['parent_item_id'],
  4972. 'previous_item_id' => $array[$i]['previous_item_id'],
  4973. 'next_item_id' => $array[$i]['next_item_id'],
  4974. 'min_score' => $array[$i]['min_score'],
  4975. 'max_score' => $array[$i]['max_score'],
  4976. 'mastery_score' => $array[$i]['mastery_score'],
  4977. 'display_order' => $array[$i]['display_order'],
  4978. 'prerequisite' => $preq,
  4979. 'depth' => $depth,
  4980. 'audio' => $audio,
  4981. 'prerequisite_min_score' => $prerequisiteMinScore,
  4982. 'prerequisite_max_score' => $prerequisiteMaxScore
  4983. );
  4984. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4985. }
  4986. }
  4987. }
  4988. }
  4989. /**
  4990. * Sorts a multi dimensional array by parent id and display order
  4991. * @author Kevin Van Den Haute
  4992. *
  4993. * @param array $array (array with al the learning path items in it)
  4994. *
  4995. * @return array
  4996. */
  4997. public function sort_tree_array($array) {
  4998. foreach ($array as $key => $row) {
  4999. $parent[$key] = $row['parent_item_id'];
  5000. $position[$key] = $row['display_order'];
  5001. }
  5002. if (count($array) > 0)
  5003. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5004. return $array;
  5005. }
  5006. /**
  5007. * Function that creates a html list of learning path items so that we can add audio files to them
  5008. * @author Kevin Van Den Haute
  5009. * @param int $lp_id
  5010. * @return string
  5011. */
  5012. public function overview()
  5013. {
  5014. if ($this->debug > 0) {
  5015. error_log('New LP - In learnpath::overview()', 0);
  5016. }
  5017. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5018. $return = '';
  5019. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5020. // we need to start a form when we want to update all the mp3 files
  5021. if ($update_audio == 'true') {
  5022. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&action=' . Security :: remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5023. }
  5024. $return .= '<div id="message"></div>';
  5025. if (count($this->items) == 0) {
  5026. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5027. } else {
  5028. $return_audio = '<table class="data_table">';
  5029. $return_audio .= '<tr>';
  5030. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5031. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5032. $return_audio .= '</tr>';
  5033. if ($update_audio != 'true') {
  5034. $return .= '<div class="col-md-12">';
  5035. $return .= self::return_new_tree($update_audio);
  5036. $return .='</div>';
  5037. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5038. } else {
  5039. $return_audio .= self::return_new_tree($update_audio);
  5040. $return .= $return_audio.'</table>';
  5041. }
  5042. // We need to close the form when we are updating the mp3 files.
  5043. if ($update_audio == 'true') {
  5044. $return .= '<div class="footer-audio">';
  5045. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5046. $return .= '</div>';
  5047. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5048. }
  5049. }
  5050. // We need to close the form when we are updating the mp3 files.
  5051. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5052. $return .= '</form>';
  5053. }
  5054. return $return;
  5055. }
  5056. /**
  5057. * @param string string $update_audio
  5058. * @param bool $drop_element_here
  5059. * @return string
  5060. */
  5061. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5062. {
  5063. $return = '';
  5064. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5065. $course_id = api_get_course_int_id();
  5066. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5067. $sql = "SELECT * FROM $tbl_lp_item
  5068. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5069. $result = Database::query($sql);
  5070. $arrLP = array();
  5071. while ($row = Database :: fetch_array($result)) {
  5072. $arrLP[] = array(
  5073. 'id' => $row['id'],
  5074. 'item_type' => $row['item_type'],
  5075. 'title' => Security :: remove_XSS($row['title']),
  5076. 'path' => $row['path'],
  5077. 'description' => Security::remove_XSS($row['description']),
  5078. 'parent_item_id' => $row['parent_item_id'],
  5079. 'previous_item_id' => $row['previous_item_id'],
  5080. 'next_item_id' => $row['next_item_id'],
  5081. 'max_score' => $row['max_score'],
  5082. 'min_score' => $row['min_score'],
  5083. 'mastery_score' => $row['mastery_score'],
  5084. 'prerequisite' => $row['prerequisite'],
  5085. 'display_order' => $row['display_order'],
  5086. 'audio' => $row['audio'],
  5087. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5088. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5089. );
  5090. }
  5091. $this->tree_array($arrLP);
  5092. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5093. unset ($this->arrMenu);
  5094. $default_data = null;
  5095. $default_content = null;
  5096. $elements = array();
  5097. $return_audio = null;
  5098. for ($i = 0; $i < count($arrLP); $i++) {
  5099. $title = $arrLP[$i]['title'];
  5100. $title_cut = cut($arrLP[$i]['title'], 25);
  5101. //Link for the documents
  5102. if ($arrLP[$i]['item_type'] == 'document') {
  5103. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5104. $title_cut = Display::url(
  5105. $title_cut,
  5106. $url,
  5107. array(
  5108. 'class' => 'ajax',
  5109. 'data-title' => $title_cut
  5110. )
  5111. );
  5112. }
  5113. if (($i % 2) == 0) {
  5114. $oddClass = 'row_odd';
  5115. } else {
  5116. $oddClass = 'row_even';
  5117. }
  5118. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5119. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5120. $icon = '';
  5121. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5122. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5123. } else {
  5124. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5125. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5126. } else {
  5127. $icon = '<img src="../img/folder_document.gif" />';
  5128. }
  5129. }
  5130. // The audio column.
  5131. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5132. $audio = '';
  5133. if (!$update_audio || $update_audio <> 'true') {
  5134. if (!empty($arrLP[$i]['audio'])) {
  5135. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5136. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5137. $audio .= '<script type="text/javascript">
  5138. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5139. s1.addParam("allowscriptaccess","always");
  5140. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5141. s1.write("container' . $i . '");
  5142. </script>';*/
  5143. } else {
  5144. $audio .= '';
  5145. }
  5146. } else {
  5147. $types = self::getChapterTypes();
  5148. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5149. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5150. if (!empty ($arrLP[$i]['audio'])) {
  5151. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5152. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5153. }
  5154. }
  5155. }
  5156. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5157. $return_audio .= '</td>';
  5158. $move_icon = '';
  5159. $move_item_icon = '';
  5160. $edit_icon = '';
  5161. $delete_icon = '';
  5162. $audio_icon = '';
  5163. $prerequisities_icon = '';
  5164. $forumIcon = '';
  5165. if ($is_allowed_to_edit) {
  5166. if (!$update_audio || $update_audio <> 'true') {
  5167. $move_icon .= '<a class="moved" href="#">';
  5168. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5169. $move_icon .= '</a>';
  5170. }
  5171. // No edit for this item types
  5172. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5173. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5174. $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">';
  5175. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5176. $edit_icon .= '</a>';
  5177. if (
  5178. !in_array($arrLP[$i]['item_type'], ['forum', 'thread'])
  5179. ) {
  5180. if (
  5181. $this->items[$arrLP[$i]['id']]->getForumThread(
  5182. $this->course_int_id,
  5183. $this->lp_session_id
  5184. )
  5185. ) {
  5186. $forumIcon = Display::url(
  5187. Display::return_icon('forum.png', get_lang('CreateForum'), [], ICON_SIZE_TINY),
  5188. '#',
  5189. ['class' => 'btn btn-default disabled']
  5190. );
  5191. } else {
  5192. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5193. 'action' => 'create_forum',
  5194. 'id' => $arrLP[$i]['id'],
  5195. 'lp_id' => $this->lp_id
  5196. ]);
  5197. $forumIcon = Display::url(
  5198. Display::return_icon('forum.png', get_lang('CreateForum'), [], ICON_SIZE_TINY),
  5199. $forumIconUrl,
  5200. ['class' => "btn btn-default"]
  5201. );
  5202. }
  5203. }
  5204. } else {
  5205. $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">';
  5206. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5207. $edit_icon .= '</a>';
  5208. }
  5209. }
  5210. $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">';
  5211. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5212. $delete_icon .= '</a>';
  5213. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5214. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5215. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']);
  5216. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item', ['class' => 'btn btn-default']);
  5217. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio', ['class' => 'btn btn-default']);
  5218. }
  5219. }
  5220. if ($update_audio != 'true') {
  5221. $row = $move_icon . ' ' . $icon .
  5222. Display::span($title_cut) .
  5223. Display::tag(
  5224. 'div',
  5225. "<div class=\"btn-group btn-group-xs\">$audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5226. array('class'=>'btn-toolbar button_actions')
  5227. );
  5228. } else {
  5229. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5230. }
  5231. $parent_id = $arrLP[$i]['parent_item_id'];
  5232. $default_data[$arrLP[$i]['id']] = $row;
  5233. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5234. if (empty($parent_id)) {
  5235. $elements[$arrLP[$i]['id']]['data'] = $row;
  5236. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5237. } else {
  5238. $parent_arrays = array();
  5239. if ($arrLP[$i]['depth'] > 1) {
  5240. //Getting list of parents
  5241. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5242. foreach($arrLP as $item) {
  5243. if ($item['id'] == $parent_id) {
  5244. if ($item['parent_item_id'] == 0) {
  5245. $parent_id = $item['id'];
  5246. break;
  5247. } else {
  5248. $parent_id = $item['parent_item_id'];
  5249. if (empty($parent_arrays)) {
  5250. $parent_arrays[] = intval($item['id']);
  5251. }
  5252. $parent_arrays[] = $parent_id;
  5253. break;
  5254. }
  5255. }
  5256. }
  5257. }
  5258. }
  5259. if (!empty($parent_arrays)) {
  5260. $parent_arrays = array_reverse($parent_arrays);
  5261. $val = '$elements';
  5262. $x = 0;
  5263. foreach($parent_arrays as $item) {
  5264. if ($x != count($parent_arrays) -1) {
  5265. $val .= '["'.$item.'"]["children"]';
  5266. } else {
  5267. $val .= '["'.$item.'"]["children"]';
  5268. }
  5269. $x++;
  5270. }
  5271. $val .= "";
  5272. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5273. eval($code_str);
  5274. } else {
  5275. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5276. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5277. }
  5278. }
  5279. }
  5280. $list = '<ul id="lp_item_list">';
  5281. $tree = self::print_recursive($elements, $default_data, $default_content);
  5282. if (!empty($tree)) {
  5283. $list .= $tree;
  5284. } else {
  5285. if ($drop_element_here) {
  5286. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5287. }
  5288. }
  5289. $list .= '</ul>';
  5290. //$return .= Display::panel($list, $this->name);
  5291. $return .= Display::panelCollapse($this->name, $list, 'scorm-list', null, 'scorm-list-accordion', 'scorm-list-collapse');
  5292. if ($update_audio == 'true') {
  5293. $return = $return_audio;
  5294. }
  5295. return $return;
  5296. }
  5297. /**
  5298. * @param array $elements
  5299. * @param array $default_data
  5300. * @param array $default_content
  5301. * @return string
  5302. */
  5303. public function print_recursive($elements, $default_data, $default_content)
  5304. {
  5305. $return = '';
  5306. foreach ($elements as $key => $item) {
  5307. if (isset($item['load_data']) || empty($item['data'])) {
  5308. $item['data'] = $default_data[$item['load_data']];
  5309. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5310. }
  5311. $sub_list = '';
  5312. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5313. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5314. }
  5315. if (empty($item['children'])) {
  5316. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5317. $active = null;
  5318. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5319. $active = 'active';
  5320. }
  5321. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5322. } else {
  5323. //sections
  5324. if (isset($item['children'])) {
  5325. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5326. }
  5327. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5328. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5329. }
  5330. }
  5331. return $return;
  5332. }
  5333. /**
  5334. * This function builds the action menu
  5335. * @param bool $returnContent
  5336. * @return void
  5337. */
  5338. public function build_action_menu($returnContent = false)
  5339. {
  5340. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5341. $return = '<div class="actions">';
  5342. $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> ';
  5343. $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>';
  5344. $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>';
  5345. $buttons = array(
  5346. array(
  5347. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5348. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5349. ),
  5350. array(
  5351. 'title' => get_lang('ClearAllPrerequisites'),
  5352. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5353. ),
  5354. );
  5355. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5356. $return .= '</div>';
  5357. if ($returnContent) {
  5358. return $return;
  5359. }
  5360. echo $return;
  5361. }
  5362. /**
  5363. * Creates the default learning path folder
  5364. * @param array $course
  5365. * @return bool
  5366. */
  5367. public static function generate_learning_path_folder($course)
  5368. {
  5369. // Creating learning_path folder
  5370. $dir = '/learning_path';
  5371. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5372. $folder = false;
  5373. if (!is_dir($filepath.'/'.$dir)) {
  5374. $folderData = create_unexisting_directory(
  5375. $course,
  5376. api_get_user_id(),
  5377. api_get_session_id(),
  5378. 0,
  5379. 0,
  5380. $filepath,
  5381. $dir,
  5382. get_lang('LearningPaths'),
  5383. 0
  5384. );
  5385. if (!empty($folderData)) {
  5386. $folder = true;
  5387. }
  5388. } else {
  5389. $folder = true;
  5390. }
  5391. return $folder;
  5392. }
  5393. /**
  5394. * @param array $course
  5395. * @param string $lp_name
  5396. * @return array
  5397. */
  5398. public function generate_lp_folder($course, $lp_name = null)
  5399. {
  5400. $filepath = '';
  5401. $dir = '/learning_path/';
  5402. if (empty($lp_name)) {
  5403. $lp_name = $this->name;
  5404. }
  5405. $folder = self::generate_learning_path_folder($course);
  5406. // Creating LP folder
  5407. if ($folder) {
  5408. //Limits title size
  5409. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5410. $dir = $dir.$title;
  5411. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5412. if (!is_dir($filepath.'/'.$dir)) {
  5413. $folderData = create_unexisting_directory(
  5414. $course,
  5415. api_get_user_id(),
  5416. 0,
  5417. 0,
  5418. 0,
  5419. $filepath,
  5420. $dir,
  5421. $lp_name
  5422. );
  5423. if (!empty($folderData)) {
  5424. $folder = true;
  5425. }
  5426. } else {
  5427. $folder = true;
  5428. }
  5429. $dir = $dir.'/';
  5430. if ($folder) {
  5431. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5432. }
  5433. }
  5434. $array = array(
  5435. 'dir' => $dir,
  5436. 'filepath' => $filepath,
  5437. 'folder' => $folder
  5438. );
  5439. return $array;
  5440. }
  5441. /**
  5442. * Create a new document //still needs some finetuning
  5443. * @param array $_course
  5444. * @return string
  5445. */
  5446. public function create_document($_course)
  5447. {
  5448. $course_id = api_get_course_int_id();
  5449. global $charset;
  5450. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5451. // Please, do not modify this dirname formatting.
  5452. if (strstr($dir, '..'))
  5453. $dir = '/';
  5454. if ($dir[0] == '.')
  5455. $dir = substr($dir, 1);
  5456. if ($dir[0] != '/')
  5457. $dir = '/' . $dir;
  5458. if ($dir[strlen($dir) - 1] != '/')
  5459. $dir .= '/';
  5460. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5461. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5462. //Generates folder
  5463. $result = $this->generate_lp_folder($_course);
  5464. $dir = $result['dir'];
  5465. $filepath = $result['filepath'];
  5466. }
  5467. if (!is_dir($filepath)) {
  5468. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5469. $dir = '/';
  5470. }
  5471. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5472. // is already escaped twice when it gets here.
  5473. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5474. $title = disable_dangerous_file($title);
  5475. $filename = $title;
  5476. $content = $_POST['content_lp'];
  5477. $tmp_filename = $filename;
  5478. $i = 0;
  5479. while (file_exists($filepath . $tmp_filename . '.html'))
  5480. $tmp_filename = $filename . '_' . ++ $i;
  5481. $filename = $tmp_filename . '.html';
  5482. $content = stripslashes($content);
  5483. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5484. // Change the path of mp3 to absolute.
  5485. // The first regexp deals with :// urls.
  5486. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5487. // The second regexp deals with audio/ urls.
  5488. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5489. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5490. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5491. if (!file_exists($filepath . $filename)) {
  5492. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5493. fputs($fp, $content);
  5494. fclose($fp);
  5495. $file_size = filesize($filepath . $filename);
  5496. $save_file_path = $dir.$filename;
  5497. $document_id = add_document(
  5498. $_course,
  5499. $save_file_path,
  5500. 'file',
  5501. $file_size,
  5502. $tmp_filename
  5503. );
  5504. if ($document_id) {
  5505. api_item_property_update(
  5506. $_course,
  5507. TOOL_DOCUMENT,
  5508. $document_id,
  5509. 'DocumentAdded',
  5510. api_get_user_id(),
  5511. null,
  5512. null,
  5513. null,
  5514. null,
  5515. api_get_session_id()
  5516. );
  5517. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5518. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5519. if ($new_comment || $new_title) {
  5520. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5521. $ct = '';
  5522. if ($new_comment)
  5523. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5524. if ($new_title)
  5525. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5526. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5527. Database::query($sql_update);
  5528. }
  5529. }
  5530. return $document_id;
  5531. }
  5532. }
  5533. }
  5534. /**
  5535. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5536. * @param array $_course array
  5537. * @return void
  5538. */
  5539. public function edit_document($_course)
  5540. {
  5541. $course_id = api_get_course_int_id();
  5542. global $_configuration;
  5543. // Please, do not modify this dirname formatting.
  5544. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5545. if (strstr($dir, '..'))
  5546. $dir = '/';
  5547. if ($dir[0] == '.')
  5548. $dir = substr($dir, 1);
  5549. if ($dir[0] != '/')
  5550. $dir = '/' . $dir;
  5551. if ($dir[strlen($dir) - 1] != '/')
  5552. $dir .= '/';
  5553. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5554. if (!is_dir($filepath)) {
  5555. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5556. $dir = '/';
  5557. }
  5558. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5559. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5560. $document_id = intval($_POST['path']);
  5561. $sql = "SELECT path FROM " . $table_doc . "
  5562. WHERE c_id = $course_id AND id = " . $document_id;
  5563. $res = Database::query($sql);
  5564. $row = Database :: fetch_array($res);
  5565. $content = stripslashes($_POST['content_lp']);
  5566. $file = $filepath . $row['path'];
  5567. if ($fp = @ fopen($file, 'w')) {
  5568. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5569. // Change the path of mp3 to absolute.
  5570. // The first regexp deals with :// urls.
  5571. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5572. // The second regexp deals with audio/ urls.
  5573. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5574. fputs($fp, $content);
  5575. fclose($fp);
  5576. $sql = "UPDATE " . $table_doc ." SET
  5577. title='".Database::escape_string($_POST['title'])."'
  5578. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5579. Database::query($sql);
  5580. }
  5581. }
  5582. }
  5583. /**
  5584. * Displays the selected item, with a panel for manipulating the item
  5585. * @param int $item_id
  5586. * @param string $msg
  5587. * @return string
  5588. */
  5589. public function display_item($item_id, $msg = null, $show_actions = true)
  5590. {
  5591. $course_id = api_get_course_int_id();
  5592. $return = '';
  5593. if (is_numeric($item_id)) {
  5594. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5595. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5596. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5597. $result = Database::query($sql);
  5598. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5599. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5600. // Prevents wrong parent selection for document, see Bug#1251.
  5601. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5602. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5603. }
  5604. if ($show_actions) {
  5605. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5606. }
  5607. $return .= '<div style="padding:10px;">';
  5608. if ($msg != '')
  5609. $return .= $msg;
  5610. $return .= '<h3>'.$row['title'].'</h3>';
  5611. switch ($row['item_type']) {
  5612. case TOOL_QUIZ:
  5613. if (!empty($row['path'])) {
  5614. $exercise = new Exercise();
  5615. $exercise->read($row['path']);
  5616. $return .= $exercise->description.'<br />';
  5617. }
  5618. break;
  5619. case TOOL_DOCUMENT:
  5620. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5621. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5622. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5623. $result = Database::query($sql_doc);
  5624. $path_file = Database::result($result, 0, 0);
  5625. $path_parts = pathinfo($path_file);
  5626. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5627. if (in_array($path_parts['extension'], array(
  5628. 'html',
  5629. 'txt',
  5630. 'png',
  5631. 'jpg',
  5632. 'JPG',
  5633. 'jpeg',
  5634. 'JPEG',
  5635. 'gif',
  5636. 'swf'
  5637. ))) {
  5638. $return .= $this->display_document($row['path'], true, true);
  5639. }
  5640. break;
  5641. }
  5642. $return .= '</div>';
  5643. }
  5644. }
  5645. return $return;
  5646. }
  5647. /**
  5648. * Shows the needed forms for editing a specific item
  5649. * @param int $item_id
  5650. * @return string
  5651. */
  5652. public function display_edit_item($item_id)
  5653. {
  5654. $course_id = api_get_course_int_id();
  5655. $return = '';
  5656. if (is_numeric($item_id)) {
  5657. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5658. $sql = "SELECT * FROM $tbl_lp_item
  5659. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5660. $res = Database::query($sql);
  5661. $row = Database::fetch_array($res);
  5662. switch ($row['item_type']) {
  5663. case 'dokeos_chapter' :
  5664. case 'dir' :
  5665. case 'asset' :
  5666. case 'sco' :
  5667. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5668. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5669. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5670. } else {
  5671. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5672. }
  5673. break;
  5674. case TOOL_DOCUMENT :
  5675. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5676. $sql = "SELECT lp.*, doc.path as dir
  5677. FROM " . $tbl_lp_item . " as lp
  5678. LEFT JOIN " . $tbl_doc . " as doc
  5679. ON doc.id = lp.path
  5680. WHERE
  5681. lp.c_id = $course_id AND
  5682. doc.c_id = $course_id AND
  5683. lp.id = " . intval($item_id);
  5684. $res_step = Database::query($sql);
  5685. $row_step = Database :: fetch_array($res_step);
  5686. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5687. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5688. break;
  5689. case TOOL_LINK :
  5690. $link_id = (string) $row['path'];
  5691. if (ctype_digit($link_id)) {
  5692. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5693. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5694. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5695. $res_link = Database::query($sql_select);
  5696. $row_link = Database :: fetch_array($res_link);
  5697. if (is_array($row_link)) {
  5698. $row['url'] = $row_link['url'];
  5699. }
  5700. }
  5701. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5702. $return .= $this->display_link_form('edit', $item_id, $row);
  5703. break;
  5704. case 'dokeos_module' :
  5705. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5706. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5707. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5708. } else {
  5709. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5710. }
  5711. break;
  5712. case TOOL_QUIZ :
  5713. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5714. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5715. break;
  5716. case TOOL_HOTPOTATOES :
  5717. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5718. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5719. break;
  5720. case TOOL_STUDENTPUBLICATION :
  5721. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5722. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5723. break;
  5724. case TOOL_FORUM :
  5725. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5726. $return .= $this->display_forum_form('edit', $item_id, $row);
  5727. break;
  5728. case TOOL_THREAD :
  5729. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5730. $return .= $this->display_thread_form('edit', $item_id, $row);
  5731. break;
  5732. }
  5733. }
  5734. return $return;
  5735. }
  5736. /**
  5737. * Function that displays a list with al the resources that
  5738. * could be added to the learning path
  5739. * @return string
  5740. */
  5741. public function display_resources()
  5742. {
  5743. $course_code = api_get_course_id();
  5744. // Get all the docs.
  5745. $documents = $this->get_documents(true);
  5746. // Get all the exercises.
  5747. $exercises = $this->get_exercises();
  5748. // Get all the links.
  5749. $links = $this->get_links();
  5750. // Get al the student publications.
  5751. $works = $this->get_student_publications();
  5752. // Get al the forums.
  5753. $forums = $this->get_forums(null, $course_code);
  5754. $headers = array(
  5755. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5756. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5757. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5758. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5759. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5760. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5761. );
  5762. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5763. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5764. echo Display::tabs(
  5765. $headers,
  5766. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5767. );
  5768. return true;
  5769. }
  5770. /**
  5771. * Returns the extension of a document
  5772. * @param string filename
  5773. * @return string Extension (part after the last dot)
  5774. */
  5775. public function get_extension($filename)
  5776. {
  5777. $explode = explode('.', $filename);
  5778. return $explode[count($explode) - 1];
  5779. }
  5780. /**
  5781. * Displays a document by id
  5782. *
  5783. * @param int $id
  5784. * @return string
  5785. */
  5786. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5787. {
  5788. $_course = api_get_course_info();
  5789. $course_id = api_get_course_int_id();
  5790. $return = '';
  5791. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5792. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5793. WHERE c_id = ".$course_id." AND id = " . $id;
  5794. $res_doc = Database::query($sql_doc);
  5795. $row_doc = Database :: fetch_array($res_doc);
  5796. // TODO: Add a path filter.
  5797. if ($iframe) {
  5798. $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>';
  5799. } else {
  5800. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5801. }
  5802. return $return;
  5803. }
  5804. /**
  5805. * Return HTML form to add/edit a quiz
  5806. * @param string Action (add/edit)
  5807. * @param integer Item ID if already exists
  5808. * @param mixed Extra information (quiz ID if integer)
  5809. * @return string HTML form
  5810. */
  5811. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5812. {
  5813. $course_id = api_get_course_int_id();
  5814. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5815. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5816. if ($id != 0 && is_array($extra_info)) {
  5817. $item_title = $extra_info['title'];
  5818. $item_description = $extra_info['description'];
  5819. } elseif (is_numeric($extra_info)) {
  5820. $sql = "SELECT title, description
  5821. FROM " . $tbl_quiz . "
  5822. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5823. $result = Database::query($sql);
  5824. $row = Database::fetch_array($result);
  5825. $item_title = $row['title'];
  5826. $item_description = $row['description'];
  5827. } else {
  5828. $item_title = '';
  5829. $item_description = '';
  5830. }
  5831. $item_title = Security::remove_XSS($item_title);
  5832. $item_description = Security::remove_XSS($item_description);
  5833. $legend = '<legend>';
  5834. if ($id != 0 && is_array($extra_info))
  5835. $parent = $extra_info['parent_item_id'];
  5836. else
  5837. $parent = 0;
  5838. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5839. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5840. $result = Database::query($sql);
  5841. $arrLP = array ();
  5842. while ($row = Database :: fetch_array($result)) {
  5843. $arrLP[] = array (
  5844. 'id' => $row['id'],
  5845. 'item_type' => $row['item_type'],
  5846. 'title' => $row['title'],
  5847. 'path' => $row['path'],
  5848. 'description' => $row['description'],
  5849. 'parent_item_id' => $row['parent_item_id'],
  5850. 'previous_item_id' => $row['previous_item_id'],
  5851. 'next_item_id' => $row['next_item_id'],
  5852. 'display_order' => $row['display_order'],
  5853. 'max_score' => $row['max_score'],
  5854. 'min_score' => $row['min_score'],
  5855. 'mastery_score' => $row['mastery_score'],
  5856. 'prerequisite' => $row['prerequisite'],
  5857. 'max_time_allowed' => $row['max_time_allowed']
  5858. );
  5859. }
  5860. $this->tree_array($arrLP);
  5861. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5862. unset ($this->arrMenu);
  5863. if ($action == 'add') {
  5864. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5865. } elseif ($action == 'move') {
  5866. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5867. } else {
  5868. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5869. }
  5870. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5871. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5872. }
  5873. $legend .= '</legend>';
  5874. $return = '';
  5875. $return .= '<div class="sectioncomment">';
  5876. $return .= '<form method="POST">';
  5877. $return .= $legend;
  5878. $return .= '<table class="lp_form">';
  5879. if ($action != 'move') {
  5880. $return .= '<tr>';
  5881. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5882. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5883. $return .= '</tr>';
  5884. }
  5885. $return .= '<tr>';
  5886. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5887. $return .= '<td class="input">';
  5888. // Select for Parent item, root or chapter
  5889. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5890. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5891. $arrHide = array (
  5892. $id
  5893. );
  5894. for ($i = 0; $i < count($arrLP); $i++) {
  5895. if ($action != 'add') {
  5896. 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)) {
  5897. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5898. } else {
  5899. $arrHide[] = $arrLP[$i]['id'];
  5900. }
  5901. } else {
  5902. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5903. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5904. }
  5905. }
  5906. if (is_array($arrLP)) {
  5907. reset($arrLP);
  5908. }
  5909. $return .= '</select>';
  5910. $return .= '</td>';
  5911. $return .= '</tr>';
  5912. $return .= '<tr>';
  5913. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5914. $return .= '<td class="input">';
  5915. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5916. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5917. for ($i = 0; $i < count($arrLP); $i++) {
  5918. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5919. if (is_array($extra_info)) {
  5920. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  5921. $selected = 'selected="selected" ';
  5922. }
  5923. } elseif ($action == 'add') {
  5924. $selected = 'selected="selected" ';
  5925. } else {
  5926. $selected = '';
  5927. }
  5928. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5929. }
  5930. }
  5931. $return .= '</select>';
  5932. $return .= '</td>';
  5933. $return .= '</tr>';
  5934. if ($action != 'move') {
  5935. $id_prerequisite = 0;
  5936. if (is_array($arrLP)) {
  5937. foreach ($arrLP as $key => $value) {
  5938. if ($value['id'] == $id) {
  5939. $id_prerequisite = $value['prerequisite'];
  5940. break;
  5941. }
  5942. }
  5943. }
  5944. $arrHide = array ();
  5945. for ($i = 0; $i < count($arrLP); $i++) {
  5946. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5947. if (is_array($extra_info)) {
  5948. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  5949. $s_selected_position = $arrLP[$i]['id'];
  5950. }
  5951. } elseif ($action == 'add') {
  5952. $s_selected_position = 0;
  5953. }
  5954. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5955. }
  5956. }
  5957. /*// Commented the prerequisites, only visible in edit (exercise).
  5958. $return .= '<tr>';
  5959. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5960. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5961. foreach($arrHide as $key => $value){
  5962. if($key==$s_selected_position && $action == 'add'){
  5963. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5964. }
  5965. elseif($key==$id_prerequisite && $action == 'edit'){
  5966. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5967. }
  5968. else{
  5969. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5970. }
  5971. }
  5972. $return .= "</select></td>";
  5973. */
  5974. $return .= '</tr>';
  5975. /*$return .= '<tr>';
  5976. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5977. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5978. // Remove temporarily the test description.
  5979. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5980. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5981. $return .= '</tr>'; */
  5982. }
  5983. $return .= '<tr>';
  5984. if ($action == 'add') {
  5985. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5986. } else {
  5987. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5988. }
  5989. $return .= '</tr>';
  5990. $return .= '</table>';
  5991. if ($action == 'move') {
  5992. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5993. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5994. }
  5995. if (is_numeric($extra_info)) {
  5996. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5997. } elseif (is_array($extra_info)) {
  5998. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5999. }
  6000. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  6001. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6002. $return .= '</form>';
  6003. $return .= '</div>';
  6004. return $return;
  6005. }
  6006. /**
  6007. * Addition of Hotpotatoes tests
  6008. * @param string Action
  6009. * @param integer Internal ID of the item
  6010. * @param mixed Extra information - can be an array with title and description indexes
  6011. * @return string HTML structure to display the hotpotatoes addition formular
  6012. */
  6013. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6014. {
  6015. $course_id = api_get_course_int_id();
  6016. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6017. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6018. if ($id != 0 && is_array($extra_info)) {
  6019. $item_title = stripslashes($extra_info['title']);
  6020. $item_description = stripslashes($extra_info['description']);
  6021. } elseif (is_numeric($extra_info)) {
  6022. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6023. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6024. WHERE
  6025. c_id = ".$course_id." AND
  6026. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6027. id = " . (int) $extra_info . "
  6028. ORDER BY id ASC";
  6029. $res_hot = Database::query($sql);
  6030. $row = Database::fetch_array($res_hot);
  6031. $item_title = $row['title'];
  6032. $item_description = $row['description'];
  6033. if (!empty ($row['comment'])) {
  6034. $item_title = $row['comment'];
  6035. }
  6036. } else {
  6037. $item_title = '';
  6038. $item_description = '';
  6039. }
  6040. if ($id != 0 && is_array($extra_info)) {
  6041. $parent = $extra_info['parent_item_id'];
  6042. } else {
  6043. $parent = 0;
  6044. }
  6045. $sql = "SELECT * FROM $tbl_lp_item
  6046. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6047. $result = Database::query($sql);
  6048. $arrLP = array ();
  6049. while ($row = Database :: fetch_array($result)) {
  6050. $arrLP[] = array (
  6051. 'id' => $row['id'],
  6052. 'item_type' => $row['item_type'],
  6053. 'title' => $row['title'],
  6054. 'path' => $row['path'],
  6055. 'description' => $row['description'],
  6056. 'parent_item_id' => $row['parent_item_id'],
  6057. 'previous_item_id' => $row['previous_item_id'],
  6058. 'next_item_id' => $row['next_item_id'],
  6059. 'display_order' => $row['display_order'],
  6060. 'max_score' => $row['max_score'],
  6061. 'min_score' => $row['min_score'],
  6062. 'mastery_score' => $row['mastery_score'],
  6063. 'prerequisite' => $row['prerequisite'],
  6064. 'max_time_allowed' => $row['max_time_allowed']
  6065. );
  6066. }
  6067. $legend = '<legend>';
  6068. if ($action == 'add')
  6069. $legend .= get_lang('CreateTheExercise');
  6070. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6071. else
  6072. $legend .= get_lang('EditCurrentExecice');
  6073. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6074. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6075. }
  6076. $legend .= '</legend>';
  6077. $return = '<form method="POST">';
  6078. $return .= $legend;
  6079. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6080. $return .= '<tr>';
  6081. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6082. $return .= '<td class="input">';
  6083. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6084. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6085. $arrHide = array (
  6086. $id
  6087. );
  6088. if (count($arrLP) > 0) {
  6089. for ($i = 0; $i < count($arrLP); $i++) {
  6090. if ($action != 'add') {
  6091. 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)) {
  6092. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6093. } else {
  6094. $arrHide[] = $arrLP[$i]['id'];
  6095. }
  6096. } else {
  6097. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6098. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6099. }
  6100. }
  6101. reset($arrLP);
  6102. }
  6103. $return .= '</select>';
  6104. $return .= '</td>';
  6105. $return .= '</tr>';
  6106. $return .= '<tr>';
  6107. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6108. $return .= '<td class="input">';
  6109. $return .= '<select id="previous" name="previous" size="1">';
  6110. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6111. for ($i = 0; $i < count($arrLP); $i++) {
  6112. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6113. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6114. $selected = 'selected="selected" ';
  6115. elseif ($action == 'add') $selected = 'selected="selected" ';
  6116. else
  6117. $selected = '';
  6118. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6119. }
  6120. }
  6121. $return .= '</select>';
  6122. $return .= '</td>';
  6123. $return .= '</tr>';
  6124. if ($action != 'move') {
  6125. $return .= '<tr>';
  6126. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6127. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6128. $return .= '</tr>';
  6129. $id_prerequisite = 0;
  6130. if (is_array($arrLP) && count($arrLP) > 0) {
  6131. foreach ($arrLP as $key => $value) {
  6132. if ($value['id'] == $id) {
  6133. $id_prerequisite = $value['prerequisite'];
  6134. break;
  6135. }
  6136. }
  6137. $arrHide = array ();
  6138. for ($i = 0; $i < count($arrLP); $i++) {
  6139. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6140. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6141. $s_selected_position = $arrLP[$i]['id'];
  6142. elseif ($action == 'add') $s_selected_position = 0;
  6143. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6144. }
  6145. }
  6146. }
  6147. }
  6148. $return .= '<tr>';
  6149. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6150. $return .= '</tr>';
  6151. $return .= '</table>';
  6152. if ($action == 'move') {
  6153. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6154. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6155. }
  6156. if (is_numeric($extra_info)) {
  6157. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6158. } elseif (is_array($extra_info)) {
  6159. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6160. }
  6161. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6162. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6163. $return .= '</form>';
  6164. return $return;
  6165. }
  6166. /**
  6167. * Return the form to display the forum edit/add option
  6168. * @param string Action (add/edit)
  6169. * @param integer ID of the lp_item if already exists
  6170. * @param mixed Forum ID or title
  6171. * @return string HTML form
  6172. */
  6173. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6174. {
  6175. $course_id = api_get_course_int_id();
  6176. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6177. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6178. if ($id != 0 && is_array($extra_info)) {
  6179. $item_title = stripslashes($extra_info['title']);
  6180. } elseif (is_numeric($extra_info)) {
  6181. $sql = "SELECT forum_title as title, forum_comment as comment
  6182. FROM " . $tbl_forum . "
  6183. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6184. $result = Database::query($sql);
  6185. $row = Database :: fetch_array($result);
  6186. $item_title = $row['title'];
  6187. $item_description = $row['comment'];
  6188. } else {
  6189. $item_title = '';
  6190. $item_description = '';
  6191. }
  6192. $legend = '<legend>';
  6193. if ($id != 0 && is_array($extra_info)) {
  6194. $parent = $extra_info['parent_item_id'];
  6195. } else {
  6196. $parent = 0;
  6197. }
  6198. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6199. WHERE
  6200. c_id = ".$course_id." AND
  6201. lp_id = " . $this->lp_id;
  6202. $result = Database::query($sql);
  6203. $arrLP = array();
  6204. while ($row = Database :: fetch_array($result)) {
  6205. $arrLP[] = array (
  6206. 'id' => $row['id'],
  6207. 'item_type' => $row['item_type'],
  6208. 'title' => $row['title'],
  6209. 'path' => $row['path'],
  6210. 'description' => $row['description'],
  6211. 'parent_item_id' => $row['parent_item_id'],
  6212. 'previous_item_id' => $row['previous_item_id'],
  6213. 'next_item_id' => $row['next_item_id'],
  6214. 'display_order' => $row['display_order'],
  6215. 'max_score' => $row['max_score'],
  6216. 'min_score' => $row['min_score'],
  6217. 'mastery_score' => $row['mastery_score'],
  6218. 'prerequisite' => $row['prerequisite']
  6219. );
  6220. }
  6221. $this->tree_array($arrLP);
  6222. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6223. unset($this->arrMenu);
  6224. if ($action == 'add')
  6225. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6226. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6227. else
  6228. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6229. $legend .= '</legend>';
  6230. $return = '<div class="sectioncomment">';
  6231. $return .= '<form method="POST">';
  6232. $return .= $legend;
  6233. $return .= '<table class="lp_form">';
  6234. if ($action != 'move') {
  6235. $return .= '<tr>';
  6236. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6237. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6238. $return .= '</tr>';
  6239. }
  6240. $return .= '<tr>';
  6241. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6242. $return .= '<td class="input">';
  6243. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6244. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6245. $arrHide = array(
  6246. $id
  6247. );
  6248. //$parent_item_id = $_SESSION['parent_item_id'];
  6249. for ($i = 0; $i < count($arrLP); $i++) {
  6250. if ($action != 'add') {
  6251. 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)) {
  6252. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6253. } else {
  6254. $arrHide[] = $arrLP[$i]['id'];
  6255. }
  6256. } else {
  6257. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6258. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6259. }
  6260. }
  6261. if (is_array($arrLP)) {
  6262. reset($arrLP);
  6263. }
  6264. $return .= '</select>';
  6265. $return .= '</td>';
  6266. $return .= '</tr>';
  6267. $return .= '<tr>';
  6268. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6269. $return .= '<td class="input">';
  6270. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6271. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6272. for ($i = 0; $i < count($arrLP); $i++) {
  6273. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6274. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6275. $selected = 'selected="selected" ';
  6276. elseif ($action == 'add') $selected = 'selected="selected" ';
  6277. else
  6278. $selected = '';
  6279. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6280. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6281. }
  6282. }
  6283. $return .= '</select>';
  6284. $return .= '</td>';
  6285. $return .= '</tr>';
  6286. if ($action != 'move') {
  6287. $return .= '<tr>';
  6288. $return .= '</tr>';
  6289. $id_prerequisite = 0;
  6290. if (is_array($arrLP)) {
  6291. foreach ($arrLP as $key => $value) {
  6292. if ($value['id'] == $id) {
  6293. $id_prerequisite = $value['prerequisite'];
  6294. break;
  6295. }
  6296. }
  6297. }
  6298. $arrHide = array();
  6299. for ($i = 0; $i < count($arrLP); $i++) {
  6300. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6301. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6302. $s_selected_position = $arrLP[$i]['id'];
  6303. elseif ($action == 'add') $s_selected_position = 0;
  6304. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6305. }
  6306. }
  6307. $return .= '</tr>';
  6308. }
  6309. $return .= '<tr>';
  6310. if ($action == 'add') {
  6311. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6312. } else {
  6313. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6314. }
  6315. $return .= '</tr>';
  6316. $return .= '</table>';
  6317. if ($action == 'move') {
  6318. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6319. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6320. }
  6321. if (is_numeric($extra_info)) {
  6322. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6323. } elseif (is_array($extra_info)) {
  6324. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6325. }
  6326. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6327. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6328. $return .= '</form>';
  6329. $return .= '</div>';
  6330. return $return;
  6331. }
  6332. /**
  6333. * Return HTML form to add/edit forum threads
  6334. * @param string Action (add/edit)
  6335. * @param integer Item ID if already exists in learning path
  6336. * @param mixed Extra information (thread ID if integer)
  6337. * @return string HTML form
  6338. */
  6339. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6340. {
  6341. $course_id = api_get_course_int_id();
  6342. if (empty($course_id)) {
  6343. return null;
  6344. }
  6345. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6346. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6347. if ($id != 0 && is_array($extra_info)) {
  6348. $item_title = stripslashes($extra_info['title']);
  6349. } elseif (is_numeric($extra_info)) {
  6350. $sql = "SELECT thread_title as title FROM $tbl_forum
  6351. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6352. $result = Database::query($sql);
  6353. $row = Database :: fetch_array($result);
  6354. $item_title = $row['title'];
  6355. $item_description = '';
  6356. } else {
  6357. $item_title = '';
  6358. $item_description = '';
  6359. }
  6360. $return = null;
  6361. if ($id != 0 && is_array($extra_info)) {
  6362. $parent = $extra_info['parent_item_id'];
  6363. } else {
  6364. $parent = 0;
  6365. }
  6366. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6367. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6368. $result = Database::query($sql);
  6369. $arrLP = array ();
  6370. while ($row = Database :: fetch_array($result)) {
  6371. $arrLP[] = array (
  6372. 'id' => $row['id'],
  6373. 'item_type' => $row['item_type'],
  6374. 'title' => $row['title'],
  6375. 'path' => $row['path'],
  6376. 'description' => $row['description'],
  6377. 'parent_item_id' => $row['parent_item_id'],
  6378. 'previous_item_id' => $row['previous_item_id'],
  6379. 'next_item_id' => $row['next_item_id'],
  6380. 'display_order' => $row['display_order'],
  6381. 'max_score' => $row['max_score'],
  6382. 'min_score' => $row['min_score'],
  6383. 'mastery_score' => $row['mastery_score'],
  6384. 'prerequisite' => $row['prerequisite']
  6385. );
  6386. }
  6387. $this->tree_array($arrLP);
  6388. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6389. unset ($this->arrMenu);
  6390. $return .= '<form method="POST">';
  6391. if ($action == 'add')
  6392. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6393. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6394. else
  6395. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6396. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6397. $return .= '<tr>';
  6398. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6399. $return .= '<td class="input">';
  6400. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6401. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6402. $arrHide = array (
  6403. $id
  6404. );
  6405. for ($i = 0; $i < count($arrLP); $i++) {
  6406. if ($action != 'add') {
  6407. 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)) {
  6408. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6409. } else {
  6410. $arrHide[] = $arrLP[$i]['id'];
  6411. }
  6412. } else {
  6413. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6414. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6415. }
  6416. }
  6417. if ($arrLP != null) {
  6418. reset($arrLP);
  6419. }
  6420. $return .= '</select>';
  6421. $return .= '</td>';
  6422. $return .= '</tr>';
  6423. $return .= '<tr>';
  6424. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6425. $return .= '<td class="input">';
  6426. $return .= '<select id="previous" name="previous" size="1">';
  6427. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6428. for ($i = 0; $i < count($arrLP); $i++) {
  6429. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6430. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6431. $selected = 'selected="selected" ';
  6432. elseif ($action == 'add') $selected = 'selected="selected" ';
  6433. else
  6434. $selected = '';
  6435. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6436. }
  6437. }
  6438. $return .= '</select>';
  6439. $return .= '</td>';
  6440. $return .= '</tr>';
  6441. if ($action != 'move') {
  6442. $return .= '<tr>';
  6443. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6444. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6445. $return .= '</tr>';
  6446. $return .= '<tr>';
  6447. $return .= '</tr>';
  6448. $id_prerequisite = 0;
  6449. if ($arrLP != null) {
  6450. foreach ($arrLP as $key => $value) {
  6451. if ($value['id'] == $id) {
  6452. $id_prerequisite = $value['prerequisite'];
  6453. break;
  6454. }
  6455. }
  6456. }
  6457. $arrHide = array();
  6458. for ($i = 0; $i < count($arrLP); $i++) {
  6459. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6460. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6461. $s_selected_position = $arrLP[$i]['id'];
  6462. elseif ($action == 'add') $s_selected_position = 0;
  6463. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6464. }
  6465. }
  6466. $return .= '<tr>';
  6467. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6468. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6469. foreach ($arrHide as $key => $value) {
  6470. if ($key == $s_selected_position && $action == 'add') {
  6471. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6472. }
  6473. elseif ($key == $id_prerequisite && $action == 'edit') {
  6474. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6475. } else {
  6476. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6477. }
  6478. }
  6479. $return .= "</select></td>";
  6480. $return .= '</tr>';
  6481. }
  6482. $return .= '<tr>';
  6483. $return .= '<td></td><td>
  6484. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6485. $return .= '</tr>';
  6486. $return .= '</table>';
  6487. if ($action == 'move') {
  6488. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6489. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6490. }
  6491. if (is_numeric($extra_info)) {
  6492. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6493. }
  6494. elseif (is_array($extra_info)) {
  6495. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6496. }
  6497. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6498. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6499. $return .= '</form>';
  6500. $return .= '</div>';
  6501. return $return;
  6502. }
  6503. /**
  6504. * Return the HTML form to display an item (generally a section/module item)
  6505. * @param string Item type (module/dokeos_module)
  6506. * @param string Title (optional, only when creating)
  6507. * @param string Action ('add'/'edit')
  6508. * @param integer lp_item ID
  6509. * @param mixed Extra info
  6510. * @return string HTML form
  6511. */
  6512. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6513. {
  6514. $course_id = api_get_course_int_id();
  6515. $_course = api_get_course_info();
  6516. global $charset;
  6517. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6518. if ($id != 0 && is_array($extra_info)) {
  6519. $item_title = $extra_info['title'];
  6520. $item_description = $extra_info['description'];
  6521. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6522. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6523. } else {
  6524. $item_title = '';
  6525. $item_description = '';
  6526. $item_path_fck = '';
  6527. }
  6528. if ($id != 0 && is_array($extra_info)) {
  6529. $parent = $extra_info['parent_item_id'];
  6530. } else {
  6531. $parent = 0;
  6532. }
  6533. $id = intval($id);
  6534. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6535. WHERE
  6536. c_id = ".$course_id." AND
  6537. lp_id = " . $this->lp_id . " AND
  6538. id != $id";
  6539. if ($item_type == 'module')
  6540. $sql .= " AND parent_item_id = 0";
  6541. $result = Database::query($sql);
  6542. $arrLP = array ();
  6543. while ($row = Database :: fetch_array($result)) {
  6544. $arrLP[] = array(
  6545. 'id' => $row['id'],
  6546. 'item_type' => $row['item_type'],
  6547. 'title' => $row['title'],
  6548. 'path' => $row['path'],
  6549. 'description' => $row['description'],
  6550. 'parent_item_id' => $row['parent_item_id'],
  6551. 'previous_item_id' => $row['previous_item_id'],
  6552. 'next_item_id' => $row['next_item_id'],
  6553. 'max_score' => $row['max_score'],
  6554. 'min_score' => $row['min_score'],
  6555. 'mastery_score' => $row['mastery_score'],
  6556. 'prerequisite' => $row['prerequisite'],
  6557. 'display_order' => $row['display_order']
  6558. );
  6559. }
  6560. $this->tree_array($arrLP);
  6561. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6562. unset ($this->arrMenu);
  6563. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6564. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6565. $form = new FormValidator('form', 'POST', $url);
  6566. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6567. $defaults['description'] = $item_description;
  6568. $form->addElement('header', $title);
  6569. //$arrHide = array($id);
  6570. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6571. $arrHide[0]['padding'] = 3;
  6572. $charset = api_get_system_encoding();
  6573. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6574. for ($i = 0; $i < count($arrLP); $i++) {
  6575. if ($action != 'add') {
  6576. 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)) {
  6577. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6578. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6579. if ($parent == $arrLP[$i]['id']) {
  6580. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6581. }
  6582. }
  6583. } else {
  6584. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6585. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6586. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6587. if ($parent == $arrLP[$i]['id']) {
  6588. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6589. }
  6590. }
  6591. }
  6592. }
  6593. if ($action != 'move') {
  6594. $form->addElement('text', 'title', get_lang('Title'));
  6595. $form->applyFilter('title', 'html_filter');
  6596. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6597. } else {
  6598. $form->addElement('hidden', 'title');
  6599. }
  6600. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6601. foreach ($arrHide as $key => $value) {
  6602. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6603. }
  6604. if (!empty($s_selected_parent)) {
  6605. $parent_select->setSelected($s_selected_parent);
  6606. }
  6607. }
  6608. if (is_array($arrLP)) {
  6609. reset($arrLP);
  6610. }
  6611. $arrHide = array();
  6612. // POSITION
  6613. for ($i = 0; $i < count($arrLP); $i++) {
  6614. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6615. //this is the same!
  6616. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6617. $s_selected_position = $arrLP[$i]['id'];
  6618. } elseif ($action == 'add') {
  6619. $s_selected_position = $arrLP[$i]['id'];
  6620. }
  6621. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6622. }
  6623. }
  6624. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6625. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6626. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6627. foreach ($arrHide as $key => $value) {
  6628. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6629. }
  6630. if (!empty ($s_selected_position)) {
  6631. $position->setSelected($s_selected_position);
  6632. }
  6633. if (is_array($arrLP)) {
  6634. reset($arrLP);
  6635. }
  6636. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6637. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6638. $form->addElement('hidden', 'parent', '0');
  6639. }
  6640. //fix in order to use the tab
  6641. if ($item_type == 'chapter') {
  6642. $form->addElement('hidden', 'type', 'chapter');
  6643. }
  6644. $extension = null;
  6645. if (!empty($item_path)) {
  6646. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6647. }
  6648. //assets can't be modified
  6649. //$item_type == 'asset' ||
  6650. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6651. if ($item_type == 'sco') {
  6652. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6653. }
  6654. $renderer = $form->defaultRenderer();
  6655. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6656. $relative_prefix = '';
  6657. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6658. 'Width' => '100%',
  6659. 'Height' => '500',
  6660. 'FullPage' => true,
  6661. 'CreateDocumentDir' => $relative_prefix,
  6662. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6663. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6664. );
  6665. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6666. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6667. //$defaults['content_lp'] = file_get_contents($item_path);
  6668. $defaults['content_lp'] = file_get_contents($content_path);
  6669. }
  6670. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6671. $form->addElement('hidden', 'post_time', time());
  6672. $form->setDefaults($defaults);
  6673. return $form->return_form();
  6674. }
  6675. /**
  6676. * Returns the form to update or create a document
  6677. * @param string Action (add/edit)
  6678. * @param integer ID of the lp_item (if already exists)
  6679. * @param mixed Integer if document ID, string if info ('new')
  6680. * @return string HTML form
  6681. */
  6682. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6683. {
  6684. $course_id = api_get_course_int_id();
  6685. $_course = api_get_course_info();
  6686. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6687. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6688. $no_display_edit_textarea = false;
  6689. $item_description = '';
  6690. //If action==edit document
  6691. //We don't display the document form if it's not an editable document (html or txt file)
  6692. if ($action == "edit") {
  6693. if (is_array($extra_info)) {
  6694. $path_parts = pathinfo($extra_info['dir']);
  6695. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6696. $no_display_edit_textarea = true;
  6697. }
  6698. }
  6699. }
  6700. $no_display_add = false;
  6701. // If action==add an existing document
  6702. // We don't display the document form if it's not an editable document (html or txt file).
  6703. if ($action == "add") {
  6704. if (is_numeric($extra_info)) {
  6705. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6706. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6707. $result = Database::query($sql_doc);
  6708. $path_file = Database :: result($result, 0, 0);
  6709. $path_parts = pathinfo($path_file);
  6710. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6711. $no_display_add = true;
  6712. }
  6713. }
  6714. }
  6715. if ($id != 0 && is_array($extra_info)) {
  6716. $item_title = stripslashes($extra_info['title']);
  6717. $item_description = stripslashes($extra_info['description']);
  6718. $item_terms = stripslashes($extra_info['terms']);
  6719. if (empty ($item_title)) {
  6720. $path_parts = pathinfo($extra_info['path']);
  6721. $item_title = stripslashes($path_parts['filename']);
  6722. }
  6723. } elseif (is_numeric($extra_info)) {
  6724. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6725. WHERE
  6726. c_id = ".$course_id." AND
  6727. id = " . intval($extra_info);
  6728. $result = Database::query($sql_doc);
  6729. $row = Database::fetch_array($result);
  6730. $item_title = $row['title'];
  6731. $item_title = str_replace('_', ' ', $item_title);
  6732. if (empty ($item_title)) {
  6733. $path_parts = pathinfo($row['path']);
  6734. $item_title = stripslashes($path_parts['filename']);
  6735. }
  6736. } else {
  6737. $item_title = '';
  6738. $item_description = '';
  6739. }
  6740. $return = '<legend>';
  6741. if ($id != 0 && is_array($extra_info)) {
  6742. $parent = $extra_info['parent_item_id'];
  6743. } else {
  6744. $parent = 0;
  6745. }
  6746. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6747. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6748. $result = Database::query($sql);
  6749. $arrLP = array ();
  6750. while ($row = Database :: fetch_array($result)) {
  6751. $arrLP[] = array(
  6752. 'id' => $row['id'],
  6753. 'item_type' => $row['item_type'],
  6754. 'title' => $row['title'],
  6755. 'path' => $row['path'],
  6756. 'description' => $row['description'],
  6757. 'parent_item_id' => $row['parent_item_id'],
  6758. 'previous_item_id' => $row['previous_item_id'],
  6759. 'next_item_id' => $row['next_item_id'],
  6760. 'display_order' => $row['display_order'],
  6761. 'max_score' => $row['max_score'],
  6762. 'min_score' => $row['min_score'],
  6763. 'mastery_score' => $row['mastery_score'],
  6764. 'prerequisite' => $row['prerequisite']
  6765. );
  6766. }
  6767. $this->tree_array($arrLP);
  6768. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6769. unset ($this->arrMenu);
  6770. if ($action == 'add') {
  6771. $return .= get_lang('CreateTheDocument');
  6772. } elseif ($action == 'move') {
  6773. $return .= get_lang('MoveTheCurrentDocument');
  6774. } else {
  6775. $return .= get_lang('EditTheCurrentDocument');
  6776. }
  6777. $return .= '</legend>';
  6778. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6779. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6780. }
  6781. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6782. $defaults['title'] = Security :: remove_XSS($item_title);
  6783. if (empty($item_title)) {
  6784. $defaults['title'] = Security::remove_XSS($item_title);
  6785. }
  6786. $defaults['description'] = $item_description;
  6787. $form->addElement('html', $return);
  6788. if ($action != 'move') {
  6789. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6790. $form->applyFilter('title', 'html_filter');
  6791. }
  6792. $arrHide[0]['value'] = $this->name;
  6793. $arrHide[0]['padding'] = 3;
  6794. for ($i = 0; $i < count($arrLP); $i++) {
  6795. if ($action != 'add') {
  6796. 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)) {
  6797. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6798. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6799. if ($parent == $arrLP[$i]['id']) {
  6800. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6801. }
  6802. }
  6803. } else {
  6804. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6805. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6806. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6807. if ($parent == $arrLP[$i]['id']) {
  6808. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6809. }
  6810. }
  6811. }
  6812. }
  6813. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="form-control" id="idParent" " onchange="javascript: load_cbo(this.value);"');
  6814. $my_count=0;
  6815. foreach ($arrHide as $key => $value) {
  6816. if ($my_count!=0) {
  6817. // The LP name is also the first section and is not in the same charset like the other sections.
  6818. $value['value'] = Security :: remove_XSS($value['value']);
  6819. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6820. } else {
  6821. $value['value'] = Security :: remove_XSS($value['value']);
  6822. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6823. }
  6824. $my_count++;
  6825. }
  6826. if (!empty($id)) {
  6827. $parent_select->setSelected($parent);
  6828. } else {
  6829. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  6830. $parent_select->setSelected($parent_item_id);
  6831. }
  6832. if (is_array($arrLP)) {
  6833. reset($arrLP);
  6834. }
  6835. $arrHide = array();
  6836. $s_selected_position = null;
  6837. //POSITION
  6838. for ($i = 0; $i < count($arrLP); $i++) {
  6839. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6840. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6841. $s_selected_position = $arrLP[$i]['id'];
  6842. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6843. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6844. }
  6845. }
  6846. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="form-control"');
  6847. $position->addOption(get_lang('FirstPosition'), 0);
  6848. foreach ($arrHide as $key => $value) {
  6849. $padding = isset($value['padding']) ? $value['padding']: 0;
  6850. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6851. }
  6852. $position->setSelected($s_selected_position);
  6853. if (is_array($arrLP)) {
  6854. reset($arrLP);
  6855. }
  6856. if ($action != 'move') {
  6857. $id_prerequisite = 0;
  6858. if (is_array($arrLP)) {
  6859. foreach ($arrLP as $key => $value) {
  6860. if ($value['id'] == $id) {
  6861. $id_prerequisite = $value['prerequisite'];
  6862. break;
  6863. }
  6864. }
  6865. }
  6866. $arrHide = array();
  6867. for ($i = 0; $i < count($arrLP); $i++) {
  6868. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6869. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6870. $s_selected_position = $arrLP[$i]['id'];
  6871. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6872. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6873. }
  6874. }
  6875. if (!$no_display_add) {
  6876. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  6877. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  6878. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $edit == 'true')) {
  6879. if (isset ($_POST['content']))
  6880. $content = stripslashes($_POST['content']);
  6881. elseif (is_array($extra_info)) {
  6882. //If it's an html document or a text file
  6883. if (!$no_display_edit_textarea) {
  6884. $content = $this->display_document($extra_info['path'], false, false);
  6885. }
  6886. } elseif (is_numeric($extra_info))
  6887. $content = $this->display_document($extra_info, false, false);
  6888. else
  6889. $content = '';
  6890. if (!$no_display_edit_textarea) {
  6891. // We need to calculate here some specific settings for the online editor.
  6892. // The calculated settings work for documents in the Documents tool
  6893. // (on the root or in subfolders).
  6894. // For documents in native scorm packages it is unclear whether the
  6895. // online editor should be activated or not.
  6896. // A new document, it is in the root of the repository.
  6897. $relative_path = '';
  6898. $relative_prefix = '';
  6899. if (is_array($extra_info) && $extra_info != 'new') {
  6900. // The document already exists. Whe have to determine its relative path towards the repository root.
  6901. $relative_path = explode('/', $extra_info['dir']);
  6902. $cnt = count($relative_path) - 2;
  6903. if ($cnt < 0) {
  6904. $cnt = 0;
  6905. }
  6906. $relative_prefix = str_repeat('../', $cnt);
  6907. $relative_path = array_slice($relative_path, 1, $cnt);
  6908. $relative_path = implode('/', $relative_path);
  6909. if (strlen($relative_path) > 0) {
  6910. $relative_path = $relative_path . '/';
  6911. }
  6912. } else {
  6913. $result = $this->generate_lp_folder($_course);
  6914. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6915. $relative_prefix = '../../';
  6916. }
  6917. $editor_config = array(
  6918. 'ToolbarSet'=> 'LearningPathDocuments',
  6919. 'Width' => '100%',
  6920. 'Height' => '500',
  6921. 'FullPage' => true,
  6922. 'CreateDocumentDir' => $relative_prefix,
  6923. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6924. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6925. );
  6926. if ($_GET['action'] == 'add_item') {
  6927. $class = 'add';
  6928. $text = get_lang('LPCreateDocument');
  6929. } else {
  6930. if ($_GET['action'] == 'edit_item') {
  6931. $class = 'save';
  6932. $text = get_lang('SaveDocument');
  6933. }
  6934. }
  6935. $form->addButtonSave($text, 'submit_button');
  6936. $renderer = $form->defaultRenderer();
  6937. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  6938. $form->addElement('html', '<div class="editor-lp">');
  6939. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  6940. $form->addElement('html', '</div>');
  6941. $defaults['content_lp'] = $content;
  6942. }
  6943. } elseif (is_numeric($extra_info)) {
  6944. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6945. $return = $this->display_document($extra_info, true, true, true);
  6946. $form->addElement('html', $return);
  6947. }
  6948. }
  6949. }
  6950. if ($action == 'move') {
  6951. $form->addElement('hidden', 'title', $item_title);
  6952. $form->addElement('hidden', 'description', $item_description);
  6953. }
  6954. if (is_numeric($extra_info)) {
  6955. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6956. $form->addElement('hidden', 'path', $extra_info);
  6957. } elseif (is_array($extra_info)) {
  6958. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6959. $form->addElement('hidden', 'path', $extra_info['path']);
  6960. }
  6961. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6962. $form->addElement('hidden', 'post_time', time());
  6963. $form->setDefaults($defaults);
  6964. return $form->return_form();
  6965. }
  6966. /**
  6967. * Return HTML form to add/edit a link item
  6968. * @param string $action (add/edit)
  6969. * @param integer $id Item ID if exists
  6970. * @param mixed $extra_info
  6971. * @return string HTML form
  6972. */
  6973. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6974. {
  6975. $course_id = api_get_course_int_id();
  6976. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6977. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6978. if ($id != 0 && is_array($extra_info)) {
  6979. $item_title = stripslashes($extra_info['title']);
  6980. $item_description = stripslashes($extra_info['description']);
  6981. $item_url = stripslashes($extra_info['url']);
  6982. } elseif (is_numeric($extra_info)) {
  6983. $extra_info = intval($extra_info);
  6984. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  6985. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6986. $result = Database::query($sql);
  6987. $row = Database :: fetch_array($result);
  6988. $item_title = $row['title'];
  6989. $item_description = $row['description'];
  6990. $item_url = $row['url'];
  6991. } else {
  6992. $item_title = '';
  6993. $item_description = '';
  6994. $item_url = '';
  6995. }
  6996. $legend = '<legend>';
  6997. if ($id != 0 && is_array($extra_info)) {
  6998. $parent = $extra_info['parent_item_id'];
  6999. } else {
  7000. $parent = 0;
  7001. }
  7002. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7003. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7004. $result = Database::query($sql);
  7005. $arrLP = array();
  7006. while ($row = Database :: fetch_array($result)) {
  7007. $arrLP[] = array(
  7008. 'id' => $row['id'],
  7009. 'item_type' => $row['item_type'],
  7010. 'title' => $row['title'],
  7011. 'path' => $row['path'],
  7012. 'description' => $row['description'],
  7013. 'parent_item_id' => $row['parent_item_id'],
  7014. 'previous_item_id' => $row['previous_item_id'],
  7015. 'next_item_id' => $row['next_item_id'],
  7016. 'display_order' => $row['display_order'],
  7017. 'max_score' => $row['max_score'],
  7018. 'min_score' => $row['min_score'],
  7019. 'mastery_score' => $row['mastery_score'],
  7020. 'prerequisite' => $row['prerequisite']
  7021. );
  7022. }
  7023. $this->tree_array($arrLP);
  7024. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7025. unset ($this->arrMenu);
  7026. if ($action == 'add')
  7027. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  7028. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  7029. else
  7030. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7031. $legend .= '</legend>';
  7032. $return = '<div class="sectioncomment">';
  7033. $return .= '<form method="POST">';
  7034. $return .= $legend;
  7035. $return .= '<table>';
  7036. if ($action != 'move') {
  7037. $return .= '<tr>';
  7038. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7039. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7040. $return .= '</tr>';
  7041. }
  7042. $return .= '<tr>';
  7043. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7044. $return .= '<td class="input">';
  7045. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7046. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7047. $arrHide = array(
  7048. $id
  7049. );
  7050. $parent_item_id = $_SESSION['parent_item_id'];
  7051. for ($i = 0; $i < count($arrLP); $i++) {
  7052. if ($action != 'add') {
  7053. 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)) {
  7054. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7055. } else {
  7056. $arrHide[] = $arrLP[$i]['id'];
  7057. }
  7058. } else {
  7059. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7060. $return .= '<option ' . (($parent_item_id == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7061. }
  7062. }
  7063. if (is_array($arrLP)) {
  7064. reset($arrLP);
  7065. }
  7066. $return .= '</select>';
  7067. $return .= '</td>';
  7068. $return .= '</tr>';
  7069. $return .= '<tr>';
  7070. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7071. $return .= '<td class="input">';
  7072. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7073. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7074. for ($i = 0; $i < count($arrLP); $i++) {
  7075. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7076. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7077. $selected = 'selected="selected" ';
  7078. elseif ($action == 'add')
  7079. $selected = 'selected="selected" ';
  7080. else
  7081. $selected = '';
  7082. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7083. }
  7084. }
  7085. $return .= '</select>';
  7086. $return .= '</td>';
  7087. $return .= '</tr>';
  7088. if ($action != 'move') {
  7089. $return .= '<tr>';
  7090. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7091. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  7092. $return .= '</tr>';
  7093. $id_prerequisite = 0;
  7094. if (is_array($arrLP)) {
  7095. foreach ($arrLP as $key => $value) {
  7096. if ($value['id'] == $id) {
  7097. $id_prerequisite = $value['prerequisite'];
  7098. break;
  7099. }
  7100. }
  7101. }
  7102. $arrHide = array();
  7103. for ($i = 0; $i < count($arrLP); $i++) {
  7104. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7105. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7106. $s_selected_position = $arrLP[$i]['id'];
  7107. elseif ($action == 'add') $s_selected_position = 0;
  7108. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7109. }
  7110. }
  7111. $return .= '</tr>';
  7112. }
  7113. $return .= '<tr>';
  7114. if ($action == 'add') {
  7115. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7116. } else {
  7117. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7118. }
  7119. $return .= '</tr>';
  7120. $return .= '</table>';
  7121. if ($action == 'move') {
  7122. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7123. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7124. }
  7125. if (is_numeric($extra_info)) {
  7126. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7127. } elseif (is_array($extra_info)) {
  7128. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7129. }
  7130. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7131. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7132. $return .= '</form>';
  7133. $return .= '</div>';
  7134. return $return;
  7135. }
  7136. /**
  7137. * Return HTML form to add/edit a student publication (work)
  7138. * @param string Action (add/edit)
  7139. * @param integer Item ID if already exists
  7140. * @param mixed Extra info (work ID if integer)
  7141. * @return string HTML form
  7142. */
  7143. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7144. {
  7145. $course_id = api_get_course_int_id();
  7146. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7147. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7148. if ($id != 0 && is_array($extra_info)) {
  7149. $item_title = stripslashes($extra_info['title']);
  7150. $item_description = stripslashes($extra_info['description']);
  7151. } elseif (is_numeric($extra_info)) {
  7152. $extra_info = intval($extra_info);
  7153. $sql = "SELECT title, description
  7154. FROM " . $tbl_publication . "
  7155. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7156. $result = Database::query($sql);
  7157. $row = Database :: fetch_array($result);
  7158. $item_title = $row['title'];
  7159. } else {
  7160. $item_title = get_lang('Student_publication');
  7161. }
  7162. if ($id != 0 && is_array($extra_info)) {
  7163. $parent = $extra_info['parent_item_id'];
  7164. } else {
  7165. $parent = 0;
  7166. }
  7167. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7168. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7169. $result = Database::query($sql);
  7170. $arrLP = array();
  7171. while ($row = Database :: fetch_array($result)) {
  7172. $arrLP[] = array (
  7173. 'id' => $row['id'],
  7174. 'item_type' => $row['item_type'],
  7175. 'title' => $row['title'],
  7176. 'path' => $row['path'],
  7177. 'description' => $row['description'],
  7178. 'parent_item_id' => $row['parent_item_id'],
  7179. 'previous_item_id' => $row['previous_item_id'],
  7180. 'next_item_id' => $row['next_item_id'],
  7181. 'display_order' => $row['display_order'],
  7182. 'max_score' => $row['max_score'],
  7183. 'min_score' => $row['min_score'],
  7184. 'mastery_score' => $row['mastery_score'],
  7185. 'prerequisite' => $row['prerequisite']
  7186. );
  7187. }
  7188. $this->tree_array($arrLP);
  7189. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7190. unset ($this->arrMenu);
  7191. $form = new FormValidator('frm_student_publication', 'post', '#');
  7192. if ($action == 'add') {
  7193. $form->addHeader(get_lang('Student_publication'));
  7194. } elseif ($action == 'move') {
  7195. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7196. } else {
  7197. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7198. }
  7199. if ($action != 'move') {
  7200. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7201. }
  7202. $parentSelect = $form->addSelect(
  7203. 'parent',
  7204. get_lang('Parent'),
  7205. ['0' => $this->name],
  7206. [
  7207. 'onchange' => 'javascript: load_cbo(this.value);',
  7208. 'class' => 'learnpath_item_form',
  7209. 'id' => 'idParent'
  7210. ]
  7211. );
  7212. $arrHide = array (
  7213. $id
  7214. );
  7215. for ($i = 0; $i < count($arrLP); $i++) {
  7216. if ($action != 'add') {
  7217. if (
  7218. (
  7219. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7220. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7221. $arrLP[$i]['item_type'] == 'dir'
  7222. ) &&
  7223. !in_array($arrLP[$i]['id'], $arrHide) &&
  7224. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7225. ) {
  7226. $parentSelect->addOption(
  7227. $arrLP[$i]['title'],
  7228. $arrLP[$i]['id'],
  7229. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7230. );
  7231. if ($parent == $arrLP[$i]['id']) {
  7232. $parentSelect->setSelected($arrLP[$i]['id']);
  7233. }
  7234. } else {
  7235. $arrHide[] = $arrLP[$i]['id'];
  7236. }
  7237. } else {
  7238. if (
  7239. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7240. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7241. ) {
  7242. $parentSelect->addOption(
  7243. $arrLP[$i]['title'],
  7244. $arrLP[$i]['id'],
  7245. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7246. );
  7247. if ($parent == $arrLP[$i]['id']) {
  7248. $parentSelect->setSelected($arrLP[$i]['id']);
  7249. }
  7250. }
  7251. }
  7252. }
  7253. if (is_array($arrLP)) {
  7254. reset($arrLP);
  7255. }
  7256. $previousSelect = $form->addSelect(
  7257. 'previous',
  7258. get_lang('Position'),
  7259. ['0' => get_lang('FirstPosition')],
  7260. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7261. );
  7262. for ($i = 0; $i < count($arrLP); $i++) {
  7263. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7264. $previousSelect->addOption(
  7265. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7266. $arrLP[$i]['id']
  7267. );
  7268. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7269. $previousSelect->setSelected($arrLP[$i]['id']);
  7270. } elseif ($action == 'add') {
  7271. $previousSelect->setSelected($arrLP[$i]['id']);
  7272. }
  7273. }
  7274. }
  7275. if ($action != 'move') {
  7276. $id_prerequisite = 0;
  7277. if (is_array($arrLP)) {
  7278. foreach ($arrLP as $key => $value) {
  7279. if ($value['id'] == $id) {
  7280. $id_prerequisite = $value['prerequisite'];
  7281. break;
  7282. }
  7283. }
  7284. }
  7285. $arrHide = array ();
  7286. for ($i = 0; $i < count($arrLP); $i++) {
  7287. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7288. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7289. $s_selected_position = $arrLP[$i]['id'];
  7290. elseif ($action == 'add') $s_selected_position = 0;
  7291. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7292. }
  7293. }
  7294. }
  7295. if ($action == 'add') {
  7296. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7297. } else {
  7298. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7299. }
  7300. if ($action == 'move') {
  7301. $form->addHidden('title', $item_title);
  7302. $form->addHidden('description', $item_description);
  7303. }
  7304. if (is_numeric($extra_info)) {
  7305. $form->addHidden('path', $extra_info);
  7306. } elseif (is_array($extra_info)) {
  7307. $form->addHidden('path', $extra_info['path']);
  7308. }
  7309. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7310. $form->addHidden('post_time', time());
  7311. $form->setDefaults(['title' => $item_title]);
  7312. $return = '<div class="sectioncomment">';
  7313. $return .= $form->returnForm();
  7314. $return .= '</div>';
  7315. return $return;
  7316. }
  7317. /**
  7318. * Displays the menu for manipulating a step
  7319. *
  7320. * @param $item_id
  7321. * @param string $item_type
  7322. * @return string
  7323. */
  7324. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7325. {
  7326. $_course = api_get_course_info();
  7327. $course_id = api_get_course_int_id();
  7328. $course_code = api_get_course_id();
  7329. $return = '<div class="actions">';
  7330. switch ($item_type) {
  7331. case 'dokeos_chapter' :
  7332. case 'chapter' :
  7333. // Commented the message cause should not show it.
  7334. //$lang = get_lang('TitleManipulateChapter');
  7335. break;
  7336. case 'dokeos_module' :
  7337. case 'module' :
  7338. // Commented the message cause should not show it.
  7339. //$lang = get_lang('TitleManipulateModule');
  7340. break;
  7341. case TOOL_DOCUMENT :
  7342. // Commented the message cause should not show it.
  7343. //$lang = get_lang('TitleManipulateDocument');
  7344. break;
  7345. case TOOL_LINK :
  7346. case 'link' :
  7347. // Commented the message cause should not show it.
  7348. //$lang = get_lang('TitleManipulateLink');
  7349. break;
  7350. case TOOL_QUIZ :
  7351. // Commented the message cause should not show it.
  7352. //$lang = get_lang('TitleManipulateQuiz');
  7353. break;
  7354. case TOOL_STUDENTPUBLICATION :
  7355. // Commented the message cause should not show it.
  7356. //$lang = get_lang('TitleManipulateStudentPublication');
  7357. break;
  7358. }
  7359. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7360. $item_id = intval($item_id);
  7361. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7362. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7363. $result = Database::query($sql);
  7364. $row = Database::fetch_assoc($result);
  7365. $audio_player = null;
  7366. // We display an audio player if needed.
  7367. if (!empty($row['audio'])) {
  7368. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7369. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7370. </div>';
  7371. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7372. $audio_player .= '<script>
  7373. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7374. s1.addParam("allowscriptaccess","always");
  7375. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7376. s1.write("container");
  7377. </script>';
  7378. }
  7379. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7380. $return .= Display::url(
  7381. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7382. $url.'&action=edit_item&path_item=' . $row['path']
  7383. );
  7384. $return .= Display::url(
  7385. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7386. $url.'&action=move_item'
  7387. );
  7388. // Commented for now as prerequisites cannot be added to chapters.
  7389. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7390. $return .= Display::url(
  7391. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7392. $url.'&action=edit_item_prereq'
  7393. );
  7394. }
  7395. $return .= Display::url(
  7396. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7397. $url.'&action=delete_item'
  7398. );
  7399. if ($item_type == TOOL_HOTPOTATOES ) {
  7400. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7401. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7402. }
  7403. if ($item_type == TOOL_DOCUMENT ) {
  7404. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7405. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7406. }
  7407. $return .= '</div>';
  7408. if (!empty($audio_player)) {
  7409. $return .= '<br />'.$audio_player;
  7410. }
  7411. return $return;
  7412. }
  7413. /**
  7414. * Creates the javascript needed for filling up the checkboxes without page reload
  7415. * @return string
  7416. */
  7417. public function get_js_dropdown_array()
  7418. {
  7419. $course_id = api_get_course_int_id();
  7420. $return = 'var child_name = new Array();' . "\n";
  7421. $return .= 'var child_value = new Array();' . "\n\n";
  7422. $return .= 'child_name[0] = new Array();' . "\n";
  7423. $return .= 'child_value[0] = new Array();' . "\n\n";
  7424. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7425. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7426. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7427. ORDER BY display_order ASC";
  7428. $res_zero = Database::query($sql_zero);
  7429. $i = 0;
  7430. while ($row_zero = Database :: fetch_array($res_zero)) {
  7431. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7432. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7433. }
  7434. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7435. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7436. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7437. }
  7438. $return .= "\n";
  7439. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7440. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7441. $res = Database::query($sql);
  7442. while ($row = Database :: fetch_array($res)) {
  7443. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7444. WHERE
  7445. c_id = ".$course_id." AND
  7446. parent_item_id = " . $row['id'] . "
  7447. ORDER BY display_order ASC";
  7448. $res_parent = Database::query($sql_parent);
  7449. $i = 0;
  7450. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7451. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7452. while ($row_parent = Database :: fetch_array($res_parent)) {
  7453. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7454. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7455. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7456. }
  7457. $return .= "\n";
  7458. }
  7459. return $return;
  7460. }
  7461. /**
  7462. * Display the form to allow moving an item
  7463. * @param integer Item ID
  7464. * @return string HTML form
  7465. */
  7466. public function display_move_item($item_id)
  7467. {
  7468. $course_id = api_get_course_int_id();
  7469. $return = '';
  7470. if (is_numeric($item_id)) {
  7471. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7472. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7473. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7474. $res = Database::query($sql);
  7475. $row = Database :: fetch_array($res);
  7476. switch ($row['item_type']) {
  7477. case 'dokeos_chapter' :
  7478. case 'dir' :
  7479. case 'asset' :
  7480. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7481. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7482. break;
  7483. case 'dokeos_module' :
  7484. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7485. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7486. break;
  7487. case TOOL_DOCUMENT :
  7488. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7489. $return .= $this->display_document_form('move', $item_id, $row);
  7490. break;
  7491. case TOOL_LINK :
  7492. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7493. $return .= $this->display_link_form('move', $item_id, $row);
  7494. break;
  7495. case TOOL_HOTPOTATOES :
  7496. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7497. $return .= $this->display_link_form('move', $item_id, $row);
  7498. break;
  7499. case TOOL_QUIZ :
  7500. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7501. $return .= $this->display_quiz_form('move', $item_id, $row);
  7502. break;
  7503. case TOOL_STUDENTPUBLICATION :
  7504. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7505. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7506. break;
  7507. case TOOL_FORUM :
  7508. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7509. $return .= $this->display_forum_form('move', $item_id, $row);
  7510. break;
  7511. case TOOL_THREAD :
  7512. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7513. $return .= $this->display_forum_form('move', $item_id, $row);
  7514. break;
  7515. }
  7516. }
  7517. return $return;
  7518. }
  7519. /**
  7520. * Displays a basic form on the overview page for changing the item title and the item description.
  7521. * @param string $item_type
  7522. * @param string $title
  7523. * @param array $data
  7524. * @return string
  7525. */
  7526. public function display_item_small_form($item_type, $title = '', $data = array())
  7527. {
  7528. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7529. $form = new FormValidator('small_form', 'post', $url);
  7530. $form->addElement('header', $title);
  7531. $form->addElement('text', 'title', get_lang('Title'));
  7532. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7533. $form->addElement('hidden', 'id', $data['id']);
  7534. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7535. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7536. $form->setDefaults(array('title' => $data['title']));
  7537. return $form->toHtml();
  7538. }
  7539. /**
  7540. * Return HTML form to allow prerequisites selection
  7541. * @todo use FormValidator
  7542. * @param integer Item ID
  7543. * @return string HTML form
  7544. */
  7545. public function display_item_prerequisites_form($item_id)
  7546. {
  7547. $course_id = api_get_course_int_id();
  7548. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7549. $item_id = intval($item_id);
  7550. /* Current prerequisite */
  7551. $sql = "SELECT * FROM $tbl_lp_item
  7552. WHERE c_id = $course_id AND id = " . $item_id;
  7553. $result = Database::query($sql);
  7554. $row = Database::fetch_array($result);
  7555. $prerequisiteId = $row['prerequisite'];
  7556. $return = '<legend>';
  7557. $return .= get_lang('AddEditPrerequisites');
  7558. $return .= '</legend>';
  7559. $return .= '<form method="POST">';
  7560. $return .= '<table class="data_table">';
  7561. $return .= '<tr>';
  7562. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7563. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7564. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7565. $return .= '</tr>';
  7566. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7567. $return .= '<tr >';
  7568. $return .= '<td colspan="3" class="radio">';
  7569. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7570. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7571. $return .= '</tr>';
  7572. $sql = "SELECT * FROM $tbl_lp_item
  7573. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7574. $result = Database::query($sql);
  7575. $arrLP = array();
  7576. $selectedMinScore = array();
  7577. $selectedMaxScore = array();
  7578. while ($row = Database :: fetch_array($result)) {
  7579. if ($row['id'] == $item_id) {
  7580. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7581. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7582. }
  7583. $arrLP[] = array(
  7584. 'id' => $row['id'],
  7585. 'item_type' => $row['item_type'],
  7586. 'title' => $row['title'],
  7587. 'ref' => $row['ref'],
  7588. 'description' => $row['description'],
  7589. 'parent_item_id' => $row['parent_item_id'],
  7590. 'previous_item_id' => $row['previous_item_id'],
  7591. 'next_item_id' => $row['next_item_id'],
  7592. 'max_score' => $row['max_score'],
  7593. 'min_score' => $row['min_score'],
  7594. 'mastery_score' => $row['mastery_score'],
  7595. 'prerequisite' => $row['prerequisite'],
  7596. 'next_item_id' => $row['next_item_id'],
  7597. 'display_order' => $row['display_order'],
  7598. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7599. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7600. );
  7601. }
  7602. $this->tree_array($arrLP);
  7603. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7604. unset($this->arrMenu);
  7605. for ($i = 0; $i < count($arrLP); $i++) {
  7606. $item = $arrLP[$i];
  7607. if ($item['id'] == $item_id) {
  7608. break;
  7609. }
  7610. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7611. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7612. $return .= '<tr>';
  7613. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7614. $return .= '<label for="id' . $item['id'] . '">';
  7615. $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'] . '" />';
  7616. $icon_name = str_replace(' ', '', $item['item_type']);
  7617. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7618. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7619. } else {
  7620. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7621. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7622. } else {
  7623. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7624. }
  7625. }
  7626. $return .= $item['title'] . '</label>';
  7627. $return .= '</td>';
  7628. if ($item['item_type'] == TOOL_QUIZ) {
  7629. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7630. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7631. $tmp_obj_exercice = new Exercise();
  7632. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7633. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7634. $tmp_obj_lp_item->update_in_bdd();
  7635. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7636. $return .= '<td class="exercise">';
  7637. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7638. $return .= '</td>';
  7639. $return .= '<td class="exercise">';
  7640. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7641. $return .= '</td>';
  7642. }
  7643. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7644. $return .= '<td class="exercise">';
  7645. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7646. $return .= '</td>';
  7647. $return .= '<td class="exercise"">';
  7648. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7649. $return .= '</td>';
  7650. }
  7651. $return .= '</tr>';
  7652. }
  7653. $return .= '<tr>';
  7654. $return .= '</tr>';
  7655. $return .= '</table>';
  7656. $return .= '<div style="padding-top:3px;">';
  7657. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7658. $return .= '</form>';
  7659. return $return;
  7660. }
  7661. /**
  7662. * Return HTML list to allow prerequisites selection for lp
  7663. * @param integer Item ID
  7664. * @return string HTML form
  7665. */
  7666. public function display_lp_prerequisites_list()
  7667. {
  7668. $course_id = api_get_course_int_id();
  7669. $lp_id = $this->lp_id;
  7670. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7671. // get current prerequisite
  7672. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7673. $result = Database::query($sql);
  7674. $row = Database :: fetch_array($result);
  7675. $prerequisiteId = $row['prerequisite'];
  7676. $session_id = api_get_session_id();
  7677. $session_condition = api_get_session_condition($session_id);
  7678. $sql = "SELECT * FROM $tbl_lp
  7679. WHERE c_id = $course_id $session_condition
  7680. ORDER BY display_order ";
  7681. $rs = Database::query($sql);
  7682. $return = '';
  7683. $return .= '<select name="prerequisites" class="form-control">';
  7684. $return .= '<option value="0">'.get_lang('None').'</option>';
  7685. if (Database::num_rows($rs) > 0) {
  7686. while ($row = Database::fetch_array($rs)) {
  7687. if ($row['id'] == $lp_id) {
  7688. continue;
  7689. }
  7690. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7691. }
  7692. }
  7693. $return .= '</select>';
  7694. return $return;
  7695. }
  7696. /**
  7697. * Creates a list with all the documents in it
  7698. * @param bool $showInvisibleFiles
  7699. * @return string
  7700. */
  7701. public function get_documents($showInvisibleFiles = false)
  7702. {
  7703. $course_info = api_get_course_info();
  7704. $sessionId = api_get_session_id();
  7705. $document_tree = DocumentManager::get_document_preview(
  7706. $course_info,
  7707. $this->lp_id,
  7708. null,
  7709. $sessionId,
  7710. true,
  7711. null,
  7712. null,
  7713. $showInvisibleFiles,
  7714. true
  7715. );
  7716. return $document_tree;
  7717. }
  7718. /**
  7719. * Creates a list with all the exercises (quiz) in it
  7720. * @return string
  7721. */
  7722. public function get_exercises()
  7723. {
  7724. $course_id = api_get_course_int_id();
  7725. // New for hotpotatoes.
  7726. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7727. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7728. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7729. $session_id = api_get_session_id();
  7730. $condition_session = api_get_session_condition($session_id);
  7731. $sql_quiz = "SELECT * FROM $tbl_quiz
  7732. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7733. ORDER BY title ASC";
  7734. $sql_hot = "SELECT * FROM $tbl_doc
  7735. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7736. ORDER BY id ASC";
  7737. $res_quiz = Database::query($sql_quiz);
  7738. $res_hot = Database::query($sql_hot);
  7739. $return = '<ul class="lp_resource">';
  7740. $return .= '<li class="lp_resource_element">';
  7741. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7742. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7743. get_lang('NewExercise') . '</a>';
  7744. $return .= '</li>';
  7745. // Display hotpotatoes
  7746. while ($row_hot = Database :: fetch_array($res_hot)) {
  7747. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7748. $return .= '<a class="moved" href="#">';
  7749. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7750. $return .= '</a> ';
  7751. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7752. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7753. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7754. $return .= '</li>';
  7755. }
  7756. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7757. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7758. $return .= '<a class="moved" href="#">';
  7759. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7760. $return .= '</a> ';
  7761. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7762. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7763. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7764. '</a>';
  7765. $return .= '</li>';
  7766. }
  7767. $return .= '</ul>';
  7768. return $return;
  7769. }
  7770. /**
  7771. * Creates a list with all the links in it
  7772. * @return string
  7773. */
  7774. public function get_links()
  7775. {
  7776. $selfUrl = api_get_self();
  7777. $courseIdReq = api_get_cidreq();
  7778. $course = api_get_course_info();
  7779. $course_id = $course['real_id'];
  7780. $tbl_link = Database::get_course_table(TABLE_LINK);
  7781. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7782. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7783. $session_id = api_get_session_id();
  7784. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7785. $sql = "SELECT link.id as link_id,
  7786. link.title as link_title,
  7787. link.category_id as category_id,
  7788. link_category.category_title as category_title
  7789. FROM $tbl_link as link
  7790. LEFT JOIN $linkCategoryTable as link_category
  7791. ON link.category_id = link_category.id
  7792. WHERE link.c_id = ".$course_id." $condition_session
  7793. ORDER BY link_category.category_title ASC, link.title ASC";
  7794. $links = Database::query($sql);
  7795. $categorizedLinks = array();
  7796. $categories = array();
  7797. while ($link = Database :: fetch_array($links)) {
  7798. if (!$link['category_id']) {
  7799. $link['category_title'] = get_lang('Uncategorized');
  7800. }
  7801. $categories[$link['category_id']] = $link['category_title'];
  7802. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7803. }
  7804. $linksHtmlCode =
  7805. '<script>
  7806. function toggle_tool(tool, id){
  7807. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7808. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7809. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7810. } else {
  7811. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7812. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7813. }
  7814. }
  7815. </script>
  7816. <ul class="lp_resource">
  7817. <li class="lp_resource_element">
  7818. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7819. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7820. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7821. </li>';
  7822. foreach ($categorizedLinks as $categoryId => $links) {
  7823. $linkNodes = null;
  7824. foreach ($links as $key => $title) {
  7825. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7826. $linkNodes .=
  7827. '<li class="lp_resource_element" data_id="'.$key.
  7828. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7829. <a class="moved" href="#">'.
  7830. $moveEverywhereIcon.
  7831. '</a>
  7832. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7833. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7834. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7835. Security::remove_XSS($title).
  7836. '</a>
  7837. </li>';
  7838. }
  7839. }
  7840. $linksHtmlCode .=
  7841. '<li>
  7842. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7843. style="vertical-align:middle">
  7844. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7845. align="absbottom" />
  7846. </a>
  7847. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7848. </li>
  7849. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7850. }
  7851. $linksHtmlCode .= '</ul>';
  7852. return $linksHtmlCode;
  7853. }
  7854. /**
  7855. * Creates a list with all the student publications in it
  7856. * @return unknown
  7857. */
  7858. public function get_student_publications()
  7859. {
  7860. $return = '<div class="lp_resource" >';
  7861. $return .= '<div class="lp_resource_element">';
  7862. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7863. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7864. $return .= '</div>';
  7865. $return .= '</div>';
  7866. return $return;
  7867. }
  7868. /**
  7869. * Creates a list with all the forums in it
  7870. * @return string
  7871. */
  7872. public function get_forums()
  7873. {
  7874. require_once '../forum/forumfunction.inc.php';
  7875. require_once '../forum/forumconfig.inc.php';
  7876. $a_forums = get_forums();
  7877. $return = '<ul class="lp_resource">';
  7878. //First add link
  7879. $return .= '<li class="lp_resource_element">';
  7880. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7881. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&content=forum&origin=learnpath&lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7882. $return .= '</li>';
  7883. $return .= '<script>
  7884. function toggle_forum(forum_id){
  7885. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7886. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7887. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7888. } else {
  7889. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7890. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7891. }
  7892. }
  7893. </script>';
  7894. foreach ($a_forums as $forum) {
  7895. if (!empty($forum['forum_id'])) {
  7896. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7897. $return .= '<a class="moved" href="#">';
  7898. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7899. $return .= ' </a>';
  7900. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7901. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7902. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7903. </a>
  7904. <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">' .
  7905. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7906. $return .= '</li>';
  7907. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7908. $a_threads = get_threads($forum['forum_id']);
  7909. if (is_array($a_threads)) {
  7910. foreach ($a_threads as $thread) {
  7911. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7912. $return .= '&nbsp;<a class="moved" href="#">';
  7913. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7914. $return .= ' </a>';
  7915. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7916. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  7917. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7918. $return .= '</li>';
  7919. }
  7920. }
  7921. $return .= '</div>';
  7922. }
  7923. }
  7924. $return .= '</ul>';
  7925. return $return;
  7926. }
  7927. /**
  7928. * // TODO: The output encoding should be equal to the system encoding.
  7929. *
  7930. * Exports the learning path as a SCORM package. This is the main function that
  7931. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7932. * whole thing and returns the zip.
  7933. *
  7934. * This method needs to be called in PHP5, as it will fail with non-adequate
  7935. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7936. * you need to call it on a learnpath object.
  7937. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7938. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7939. * path has been modified, it should use the generic method here below.
  7940. * @TODO link this function with the export_lp() function in the same class
  7941. * @param string Optional name of zip file. If none, title of learnpath is
  7942. * domesticated and trailed with ".zip"
  7943. * @return string Returns the zip package string, or null if error
  7944. */
  7945. public function scorm_export()
  7946. {
  7947. $_course = api_get_course_info();
  7948. $course_id = $_course['real_id'];
  7949. // Remove memory and time limits as much as possible as this might be a long process...
  7950. if (function_exists('ini_set')) {
  7951. api_set_memory_limit('128M');
  7952. ini_set('max_execution_time', 600);
  7953. }
  7954. // Create the zip handler (this will remain available throughout the method).
  7955. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7956. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7957. $temp_dir_short = uniqid();
  7958. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7959. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7960. $zip_folder = new PclZip($temp_zip_file);
  7961. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7962. $root_path = $main_path = api_get_path(SYS_PATH);
  7963. $files_cleanup = array();
  7964. // Place to temporarily stash the zip file.
  7965. // create the temp dir if it doesn't exist
  7966. // or do a cleanup before creating the zip file.
  7967. if (!is_dir($temp_zip_dir)) {
  7968. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7969. } else {
  7970. // Cleanup: Check the temp dir for old files and delete them.
  7971. $handle = opendir($temp_zip_dir);
  7972. while (false !== ($file = readdir($handle))) {
  7973. if ($file != '.' && $file != '..') {
  7974. unlink("$temp_zip_dir/$file");
  7975. }
  7976. }
  7977. closedir($handle);
  7978. }
  7979. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7980. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7981. // Remove the possible . at the end of the path.
  7982. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7983. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7984. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7985. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7986. }
  7987. // Build a dummy imsmanifest structure.
  7988. // Do not add to the zip yet (we still need it).
  7989. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7990. // Aggregation Model official document, section "2.3 Content Packaging".
  7991. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7992. $xmldoc = new DOMDocument('1.0');
  7993. $root = $xmldoc->createElement('manifest');
  7994. $root->setAttribute('identifier', 'SingleCourseManifest');
  7995. $root->setAttribute('version', '1.1');
  7996. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7997. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7998. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7999. $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');
  8000. // Build mandatory sub-root container elements.
  8001. $metadata = $xmldoc->createElement('metadata');
  8002. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8003. $metadata->appendChild($md_schema);
  8004. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8005. $metadata->appendChild($md_schemaversion);
  8006. $root->appendChild($metadata);
  8007. $organizations = $xmldoc->createElement('organizations');
  8008. $resources = $xmldoc->createElement('resources');
  8009. // Build the only organization we will use in building our learnpaths.
  8010. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8011. $organization = $xmldoc->createElement('organization');
  8012. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8013. // To set the title of the SCORM entity (=organization), we take the name given
  8014. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8015. // learning path charset) as it is the encoding that defines how it is stored
  8016. // in the database. Then we convert it to HTML entities again as the "&" character
  8017. // alone is not authorized in XML (must be &amp;).
  8018. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8019. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8020. $organization->appendChild($org_title);
  8021. $folder_name = 'document';
  8022. // Removes the learning_path/scorm_folder path when exporting see #4841
  8023. $path_to_remove = null;
  8024. $result = $this->generate_lp_folder($_course);
  8025. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8026. $path_to_remove = 'document'.$result['dir'];
  8027. $path_to_replace = $folder_name.'/';
  8028. }
  8029. //Fixes chamilo scorm exports
  8030. if ($this->ref == 'chamilo_scorm_export') {
  8031. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8032. }
  8033. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8034. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8035. $link_updates = array();
  8036. $links_to_create = array();
  8037. foreach ($this->items as $index => $item) {
  8038. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8039. // Get included documents from this item.
  8040. if ($item->type == 'sco') {
  8041. $inc_docs = $item->get_resources_from_source(
  8042. null,
  8043. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8044. );
  8045. } else {
  8046. $inc_docs = $item->get_resources_from_source();
  8047. }
  8048. // Give a child element <item> to the <organization> element.
  8049. $my_item_id = $item->get_id();
  8050. $my_item = $xmldoc->createElement('item');
  8051. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8052. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8053. $my_item->setAttribute('isvisible', 'true');
  8054. // Give a child element <title> to the <item> element.
  8055. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8056. $my_item->appendChild($my_title);
  8057. // Give a child element <adlcp:prerequisites> to the <item> element.
  8058. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8059. $my_prereqs->setAttribute('type', 'aicc_script');
  8060. $my_item->appendChild($my_prereqs);
  8061. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8062. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8063. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8064. //$xmldoc->createElement('adlcp:timelimitaction','');
  8065. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8066. //$xmldoc->createElement('adlcp:datafromlms','');
  8067. // Give a child element <adlcp:masteryscore> to the <item> element.
  8068. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8069. $my_item->appendChild($my_masteryscore);
  8070. // Attach this item to the organization element or hits parent if there is one.
  8071. if (!empty($item->parent) && $item->parent != 0) {
  8072. $children = $organization->childNodes;
  8073. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8074. if (is_object($possible_parent)) {
  8075. $possible_parent->appendChild($my_item);
  8076. } else {
  8077. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8078. }
  8079. } else {
  8080. if ($this->debug > 0) { error_log('No parent'); }
  8081. $organization->appendChild($my_item);
  8082. }
  8083. // Get the path of the file(s) from the course directory root.
  8084. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8085. if (!empty($path_to_remove)) {
  8086. //From docs
  8087. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8088. //From quiz
  8089. if ($this->ref == 'chamilo_scorm_export') {
  8090. $path_to_remove = 'scorm/'.$this->path.'/';
  8091. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8092. }
  8093. } else {
  8094. $my_xml_file_path = $my_file_path;
  8095. }
  8096. $my_sub_dir = dirname($my_file_path);
  8097. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8098. $my_xml_sub_dir = $my_sub_dir;
  8099. // Give a <resource> child to the <resources> element
  8100. $my_resource = $xmldoc->createElement('resource');
  8101. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8102. $my_resource->setAttribute('type', 'webcontent');
  8103. $my_resource->setAttribute('href', $my_xml_file_path);
  8104. // adlcp:scormtype can be either 'sco' or 'asset'.
  8105. if ($item->type == 'sco') {
  8106. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8107. } else {
  8108. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8109. }
  8110. // xml:base is the base directory to find the files declared in this resource.
  8111. $my_resource->setAttribute('xml:base', '');
  8112. // Give a <file> child to the <resource> element.
  8113. $my_file = $xmldoc->createElement('file');
  8114. $my_file->setAttribute('href', $my_xml_file_path);
  8115. $my_resource->appendChild($my_file);
  8116. // Dependency to other files - not yet supported.
  8117. $i = 1;
  8118. foreach ($inc_docs as $doc_info) {
  8119. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8120. $my_dep = $xmldoc->createElement('resource');
  8121. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8122. $my_dep->setAttribute('identifier', $res_id);
  8123. $my_dep->setAttribute('type', 'webcontent');
  8124. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8125. $my_dep_file = $xmldoc->createElement('file');
  8126. // Check type of URL.
  8127. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8128. if ($doc_info[1] == 'remote') {
  8129. // Remote file. Save url as is.
  8130. $my_dep_file->setAttribute('href', $doc_info[0]);
  8131. $my_dep->setAttribute('xml:base', '');
  8132. } elseif ($doc_info[1] == 'local') {
  8133. switch ($doc_info[2]) {
  8134. case 'url': // Local URL - save path as url for now, don't zip file.
  8135. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8136. $current_dir = dirname($abs_path);
  8137. $current_dir = str_replace('\\', '/', $current_dir);
  8138. $file_path = realpath($abs_path);
  8139. $file_path = str_replace('\\', '/', $file_path);
  8140. $my_dep_file->setAttribute('href', $file_path);
  8141. $my_dep->setAttribute('xml:base', '');
  8142. if (strstr($file_path, $main_path) !== false) {
  8143. // The calculated real path is really inside Chamilo's root path.
  8144. // Reduce file path to what's under the DocumentRoot.
  8145. $file_path = substr($file_path, strlen($root_path) - 1);
  8146. //echo $file_path;echo '<br /><br />';
  8147. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8148. $zip_files_abs[] = $file_path;
  8149. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8150. $my_dep_file->setAttribute('href', $file_path);
  8151. $my_dep->setAttribute('xml:base', '');
  8152. } elseif (empty($file_path)) {
  8153. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8154. if (strpos($document_root, -1) == '/') {
  8155. $document_root = substr(0, -1, $document_root);
  8156. }*/
  8157. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8158. $file_path = str_replace('//', '/', $file_path);
  8159. if (file_exists($file_path)) {
  8160. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8161. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8162. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8163. $my_dep_file->setAttribute('href', $file_path);
  8164. $my_dep->setAttribute('xml:base', '');
  8165. }
  8166. }
  8167. break;
  8168. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8169. $my_dep_file->setAttribute('href', $doc_info[0]);
  8170. $my_dep->setAttribute('xml:base', '');
  8171. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8172. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8173. $abs_img_path_without_subdir = $doc_info[0];
  8174. $relp = api_get_path(REL_PATH); // The url-append config param.
  8175. $pos = strpos($abs_img_path_without_subdir, $relp);
  8176. if ($pos === 0) {
  8177. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8178. }
  8179. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8180. $file_path = str_replace('\\', '/', $file_path);
  8181. $file_path = str_replace('//', '/', $file_path);
  8182. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8183. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8184. // Check if the current document is in that path.
  8185. if (strstr($file_path, $cur_path) !== false) {
  8186. // The document is in that path, now get the relative path
  8187. // to the containing document.
  8188. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8189. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8190. $relative_path = '';
  8191. if (strstr($file_path, $cur_path) !== false) {
  8192. //$relative_path = substr($file_path, strlen($orig_file_path));
  8193. $relative_path = str_replace($cur_path, '', $file_path);
  8194. $file_path = substr($file_path, strlen($cur_path));
  8195. } else {
  8196. // This case is still a problem as it's difficult to calculate a relative path easily
  8197. // might still generate wrong links.
  8198. //$file_path = substr($file_path,strlen($cur_path));
  8199. // Calculate the directory path to the current file (without trailing slash).
  8200. $my_relative_path = dirname($file_path);
  8201. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8202. $my_relative_file = basename($file_path);
  8203. // Calculate the directory path to the containing file (without trailing slash).
  8204. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8205. $dotdot = '';
  8206. $subdir = '';
  8207. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8208. $my_relative_path2 = dirname($my_relative_path);
  8209. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8210. $my_orig_file_path = dirname($my_orig_file_path);
  8211. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8212. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8213. $dotdot += '../';
  8214. $my_relative_path = $my_relative_path2;
  8215. }
  8216. $relative_path = $dotdot.$subdir.$my_relative_file;
  8217. }
  8218. // Put the current document in the zip (this array is the array
  8219. // that will manage documents already in the course folder - relative).
  8220. $zip_files[] = $file_path;
  8221. // Update the links to the current document in the containing document (make them relative).
  8222. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8223. $my_dep_file->setAttribute('href', $file_path);
  8224. $my_dep->setAttribute('xml:base', '');
  8225. } elseif (strstr($file_path, $main_path) !== false) {
  8226. // The calculated real path is really inside Chamilo's root path.
  8227. // Reduce file path to what's under the DocumentRoot.
  8228. $file_path = substr($file_path, strlen($root_path));
  8229. //echo $file_path;echo '<br /><br />';
  8230. //error_log('Reduced path: '.$file_path, 0);
  8231. $zip_files_abs[] = $file_path;
  8232. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8233. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8234. $my_dep->setAttribute('xml:base', '');
  8235. } elseif (empty($file_path)) {
  8236. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8237. if(strpos($document_root,-1) == '/') {
  8238. $document_root = substr(0, -1, $document_root);
  8239. }*/
  8240. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8241. $file_path = str_replace('//', '/', $file_path);
  8242. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8243. $current_dir = dirname($abs_path);
  8244. $current_dir = str_replace('\\', '/', $current_dir);
  8245. if (file_exists($file_path)) {
  8246. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8247. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8248. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8249. $my_dep_file->setAttribute('href','document/'.$file_path);
  8250. $my_dep->setAttribute('xml:base', '');
  8251. }
  8252. }
  8253. break;
  8254. case 'rel':
  8255. // Path relative to the current document.
  8256. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8257. if (substr($doc_info[0], 0, 2) == '..') {
  8258. // Relative path going up.
  8259. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8260. $current_dir = str_replace('\\', '/', $current_dir);
  8261. $file_path = realpath($current_dir.$doc_info[0]);
  8262. $file_path = str_replace('\\', '/', $file_path);
  8263. //error_log($file_path.' <-> '.$main_path,0);
  8264. if (strstr($file_path, $main_path) !== false) {
  8265. // The calculated real path is really inside Chamilo's root path.
  8266. // Reduce file path to what's under the DocumentRoot.
  8267. $file_path = substr($file_path, strlen($root_path));
  8268. //error_log('Reduced path: '.$file_path, 0);
  8269. $zip_files_abs[] = $file_path;
  8270. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8271. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8272. $my_dep->setAttribute('xml:base', '');
  8273. }
  8274. } else {
  8275. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8276. $my_dep_file->setAttribute('href', $doc_info[0]);
  8277. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8278. }
  8279. break;
  8280. default:
  8281. $my_dep_file->setAttribute('href', $doc_info[0]);
  8282. $my_dep->setAttribute('xml:base', '');
  8283. break;
  8284. }
  8285. }
  8286. $my_dep->appendChild($my_dep_file);
  8287. $resources->appendChild($my_dep);
  8288. $dependency = $xmldoc->createElement('dependency');
  8289. $dependency->setAttribute('identifierref', $res_id);
  8290. $my_resource->appendChild($dependency);
  8291. $i++;
  8292. }
  8293. $resources->appendChild($my_resource);
  8294. $zip_files[] = $my_file_path;
  8295. } else {
  8296. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8297. switch ($item->type) {
  8298. case TOOL_LINK:
  8299. $my_item = $xmldoc->createElement('item');
  8300. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8301. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8302. $my_item->setAttribute('isvisible', 'true');
  8303. // Give a child element <title> to the <item> element.
  8304. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8305. $my_item->appendChild($my_title);
  8306. // Give a child element <adlcp:prerequisites> to the <item> element.
  8307. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8308. $my_prereqs->setAttribute('type', 'aicc_script');
  8309. $my_item->appendChild($my_prereqs);
  8310. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8311. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8312. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8313. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8314. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8315. //$xmldoc->createElement('adlcp:datafromlms', '');
  8316. // Give a child element <adlcp:masteryscore> to the <item> element.
  8317. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8318. $my_item->appendChild($my_masteryscore);
  8319. // Attach this item to the organization element or its parent if there is one.
  8320. if (!empty($item->parent) && $item->parent != 0) {
  8321. $children = $organization->childNodes;
  8322. for ($i = 0; $i < $children->length; $i++) {
  8323. $item_temp = $children->item($i);
  8324. if ($item_temp -> nodeName == 'item') {
  8325. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8326. $item_temp -> appendChild($my_item);
  8327. }
  8328. }
  8329. }
  8330. } else {
  8331. $organization->appendChild($my_item);
  8332. }
  8333. $my_file_path = 'link_'.$item->get_id().'.html';
  8334. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8335. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8336. $rs = Database::query($sql);
  8337. if ($link = Database :: fetch_array($rs)) {
  8338. $url = $link['url'];
  8339. $title = stripslashes($link['title']);
  8340. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8341. $my_xml_file_path = $my_file_path;
  8342. $my_sub_dir = dirname($my_file_path);
  8343. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8344. $my_xml_sub_dir = $my_sub_dir;
  8345. // Give a <resource> child to the <resources> element.
  8346. $my_resource = $xmldoc->createElement('resource');
  8347. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8348. $my_resource->setAttribute('type', 'webcontent');
  8349. $my_resource->setAttribute('href', $my_xml_file_path);
  8350. // adlcp:scormtype can be either 'sco' or 'asset'.
  8351. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8352. // xml:base is the base directory to find the files declared in this resource.
  8353. $my_resource->setAttribute('xml:base', '');
  8354. // give a <file> child to the <resource> element.
  8355. $my_file = $xmldoc->createElement('file');
  8356. $my_file->setAttribute('href', $my_xml_file_path);
  8357. $my_resource->appendChild($my_file);
  8358. $resources->appendChild($my_resource);
  8359. }
  8360. break;
  8361. case TOOL_QUIZ:
  8362. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8363. $exe = new Exercise();
  8364. $exe->read($exe_id);
  8365. $my_item = $xmldoc->createElement('item');
  8366. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8367. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8368. $my_item->setAttribute('isvisible', 'true');
  8369. // Give a child element <title> to the <item> element.
  8370. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8371. $my_item->appendChild($my_title);
  8372. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8373. //$my_item->appendChild($my_max_score);
  8374. // Give a child element <adlcp:prerequisites> to the <item> element.
  8375. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8376. $my_prereqs->setAttribute('type','aicc_script');
  8377. $my_item->appendChild($my_prereqs);
  8378. // Give a child element <adlcp:masteryscore> to the <item> element.
  8379. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8380. $my_item->appendChild($my_masteryscore);
  8381. // Attach this item to the organization element or hits parent if there is one.
  8382. if (!empty($item->parent) && $item->parent != 0) {
  8383. $children = $organization->childNodes;
  8384. for ($i = 0; $i < $children->length; $i++) {
  8385. $item_temp = $children->item($i);
  8386. if ($item_temp -> nodeName == 'item') {
  8387. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8388. $item_temp -> appendChild($my_item);
  8389. }
  8390. }
  8391. }
  8392. } else {
  8393. $organization->appendChild($my_item);
  8394. }
  8395. // Get the path of the file(s) from the course directory root
  8396. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8397. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8398. // Write the contents of the exported exercise into a (big) html file
  8399. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8400. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8401. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8402. $res = file_put_contents($tmp_file_path, $contents);
  8403. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8404. $files_cleanup[] = $tmp_file_path;
  8405. //error_log($tmp_path); die();
  8406. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8407. $my_xml_file_path = $my_file_path;
  8408. $my_sub_dir = dirname($my_file_path);
  8409. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8410. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8411. $my_xml_sub_dir = $my_sub_dir;
  8412. // Give a <resource> child to the <resources> element.
  8413. $my_resource = $xmldoc->createElement('resource');
  8414. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8415. $my_resource->setAttribute('type', 'webcontent');
  8416. $my_resource->setAttribute('href', $my_xml_file_path);
  8417. // adlcp:scormtype can be either 'sco' or 'asset'.
  8418. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8419. // xml:base is the base directory to find the files declared in this resource.
  8420. $my_resource->setAttribute('xml:base', '');
  8421. // Give a <file> child to the <resource> element.
  8422. $my_file = $xmldoc->createElement('file');
  8423. $my_file->setAttribute('href', $my_xml_file_path);
  8424. $my_resource->appendChild($my_file);
  8425. // Get included docs.
  8426. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8427. // Dependency to other files - not yet supported.
  8428. $i = 1;
  8429. foreach ($inc_docs as $doc_info) {
  8430. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8431. $my_dep = $xmldoc->createElement('resource');
  8432. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8433. $my_dep->setAttribute('identifier', $res_id);
  8434. $my_dep->setAttribute('type', 'webcontent');
  8435. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8436. $my_dep_file = $xmldoc->createElement('file');
  8437. // Check type of URL.
  8438. if ($doc_info[1] == 'remote') {
  8439. // Remote file. Save url as is.
  8440. $my_dep_file->setAttribute('href', $doc_info[0]);
  8441. $my_dep->setAttribute('xml:base', '');
  8442. } elseif ($doc_info[1] == 'local') {
  8443. switch ($doc_info[2]) {
  8444. case 'url': // Local URL - save path as url for now, don't zip file.
  8445. // Save file but as local file (retrieve from URL).
  8446. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8447. $current_dir = dirname($abs_path);
  8448. $current_dir = str_replace('\\', '/', $current_dir);
  8449. $file_path = realpath($abs_path);
  8450. $file_path = str_replace('\\', '/', $file_path);
  8451. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8452. $my_dep->setAttribute('xml:base', '');
  8453. if (strstr($file_path, $main_path) !== false) {
  8454. // The calculated real path is really inside the chamilo root path.
  8455. // Reduce file path to what's under the DocumentRoot.
  8456. $file_path = substr($file_path, strlen($root_path));
  8457. //echo $file_path;echo '<br /><br />';
  8458. //error_log('Reduced path: '.$file_path, 0);
  8459. $zip_files_abs[] = $file_path;
  8460. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8461. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8462. $my_dep->setAttribute('xml:base', '');
  8463. } elseif (empty($file_path)) {
  8464. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8465. if (strpos($document_root,-1) == '/') {
  8466. $document_root = substr(0, -1, $document_root);
  8467. }*/
  8468. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8469. $file_path = str_replace('//', '/', $file_path);
  8470. if (file_exists($file_path)) {
  8471. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8472. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8473. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8474. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8475. $my_dep->setAttribute('xml:base', '');
  8476. }
  8477. }
  8478. break;
  8479. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8480. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8481. $current_dir = str_replace('\\', '/', $current_dir);
  8482. $file_path = realpath($doc_info[0]);
  8483. $file_path = str_replace('\\', '/', $file_path);
  8484. $my_dep_file->setAttribute('href', $file_path);
  8485. $my_dep->setAttribute('xml:base', '');
  8486. if (strstr($file_path,$main_path) !== false) {
  8487. // The calculated real path is really inside the chamilo root path.
  8488. // Reduce file path to what's under the DocumentRoot.
  8489. $file_path = substr($file_path, strlen($root_path));
  8490. //echo $file_path;echo '<br /><br />';
  8491. //error_log('Reduced path: '.$file_path, 0);
  8492. $zip_files_abs[] = $file_path;
  8493. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8494. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8495. $my_dep->setAttribute('xml:base', '');
  8496. } elseif (empty($file_path)) {
  8497. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8498. if (strpos($document_root,-1) == '/') {
  8499. $document_root = substr(0, -1, $document_root);
  8500. }*/
  8501. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8502. $file_path = str_replace('//', '/', $file_path);
  8503. if (file_exists($file_path)) {
  8504. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8505. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8506. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8507. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8508. $my_dep->setAttribute('xml:base', '');
  8509. }
  8510. }
  8511. break;
  8512. 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
  8513. if (substr($doc_info[0], 0, 2) == '..') {
  8514. // Relative path going up.
  8515. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8516. $current_dir = str_replace('\\', '/', $current_dir);
  8517. $file_path = realpath($current_dir.$doc_info[0]);
  8518. $file_path = str_replace('\\', '/', $file_path);
  8519. //error_log($file_path.' <-> '.$main_path, 0);
  8520. if (strstr($file_path, $main_path) !== false) {
  8521. // The calculated real path is really inside Chamilo's root path.
  8522. // Reduce file path to what's under the DocumentRoot.
  8523. $file_path = substr($file_path, strlen($root_path));
  8524. $file_path_dest = $file_path;
  8525. // File path is courses/CHAMILO/document/....
  8526. $info_file_path = explode('/', $file_path);
  8527. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8528. $file_path_dest = 'document/'.$file_path;
  8529. }
  8530. //error_log('Reduced path: '.$file_path, 0);
  8531. $zip_files_abs[] = $file_path;
  8532. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8533. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8534. $my_dep->setAttribute('xml:base', '');
  8535. }
  8536. } else {
  8537. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8538. $my_dep_file->setAttribute('href', $doc_info[0]);
  8539. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8540. }
  8541. break;
  8542. default:
  8543. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8544. $my_dep->setAttribute('xml:base', '');
  8545. break;
  8546. }
  8547. }
  8548. $my_dep->appendChild($my_dep_file);
  8549. $resources->appendChild($my_dep);
  8550. $dependency = $xmldoc->createElement('dependency');
  8551. $dependency->setAttribute('identifierref', $res_id);
  8552. $my_resource->appendChild($dependency);
  8553. $i++;
  8554. }
  8555. $resources->appendChild($my_resource);
  8556. $zip_files[] = $my_file_path;
  8557. break;
  8558. default:
  8559. // Get the path of the file(s) from the course directory root
  8560. $my_file_path = 'non_exportable.html';
  8561. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8562. $my_xml_file_path = $my_file_path;
  8563. $my_sub_dir = dirname($my_file_path);
  8564. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8565. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8566. $my_xml_sub_dir = $my_sub_dir;
  8567. // Give a <resource> child to the <resources> element.
  8568. $my_resource = $xmldoc->createElement('resource');
  8569. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8570. $my_resource->setAttribute('type', 'webcontent');
  8571. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8572. // adlcp:scormtype can be either 'sco' or 'asset'.
  8573. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8574. // xml:base is the base directory to find the files declared in this resource.
  8575. $my_resource->setAttribute('xml:base', '');
  8576. // Give a <file> child to the <resource> element.
  8577. $my_file = $xmldoc->createElement('file');
  8578. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8579. $my_resource->appendChild($my_file);
  8580. $resources->appendChild($my_resource);
  8581. break;
  8582. }
  8583. }
  8584. }
  8585. $organizations->appendChild($organization);
  8586. $root->appendChild($organizations);
  8587. $root->appendChild($resources);
  8588. $xmldoc->appendChild($root);
  8589. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8590. // then add the file to the zip, then destroy the file (this is done automatically).
  8591. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8592. foreach ($zip_files as $file_path) {
  8593. if (empty($file_path)) {
  8594. continue;
  8595. }
  8596. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8597. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8598. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8599. }
  8600. $this->create_path($dest_file);
  8601. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8602. // Check if the file needs a link update.
  8603. if (in_array($file_path, array_keys($link_updates))) {
  8604. $string = file_get_contents($dest_file);
  8605. unlink($dest_file);
  8606. foreach ($link_updates[$file_path] as $old_new) {
  8607. // This is an ugly hack that allows .flv files to be found by the flv player that
  8608. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8609. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8610. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8611. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8612. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8613. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8614. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8615. }
  8616. //Fix to avoid problems with default_course_document
  8617. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8618. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8619. $newDestination = $old_new['dest'];
  8620. } else {
  8621. $newDestination = str_replace('document/', '', $old_new['dest']);
  8622. }
  8623. $string = str_replace($old_new['orig'], $newDestination, $string);
  8624. //Add files inside the HTMLs
  8625. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8626. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8627. if (file_exists($sys_course_path.$new_path)) {
  8628. copy($sys_course_path.$new_path, $destinationFile);
  8629. }
  8630. }
  8631. file_put_contents($dest_file, $string);
  8632. }
  8633. }
  8634. foreach ($zip_files_abs as $file_path) {
  8635. if (empty($file_path)) {
  8636. continue;
  8637. }
  8638. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8639. continue;
  8640. }
  8641. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8642. $this->create_path($dest_file);
  8643. copy($main_path.$file_path, $dest_file);
  8644. // Check if the file needs a link update.
  8645. if (in_array($file_path, array_keys($link_updates))) {
  8646. $string = file_get_contents($dest_file);
  8647. unlink($dest_file);
  8648. foreach ($link_updates[$file_path] as $old_new) {
  8649. // This is an ugly hack that allows .flv files to be found by the flv player that
  8650. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8651. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8652. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8653. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8654. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8655. }
  8656. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8657. }
  8658. file_put_contents($dest_file, $string);
  8659. }
  8660. }
  8661. if (is_array($links_to_create)) {
  8662. foreach ($links_to_create as $file => $link) {
  8663. $file_content = '<!DOCTYPE html><head>
  8664. <meta charset="'.api_get_language_isocode().'" />
  8665. <title>'.$link['title'].'</title>
  8666. </head>
  8667. <body dir="'.api_get_text_direction().'">
  8668. <div style="text-align:center">
  8669. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8670. </body>
  8671. </html>';
  8672. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8673. }
  8674. }
  8675. // Add non exportable message explanation.
  8676. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8677. $file_content = '<!DOCTYPE html><head>
  8678. <meta charset="'.api_get_language_isocode().'" />
  8679. <title>'.$lang_not_exportable.'</title>
  8680. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8681. </head>
  8682. <body dir="'.api_get_text_direction().'">';
  8683. $file_content .=
  8684. <<<EOD
  8685. <style>
  8686. .error-message {
  8687. font-family: arial, verdana, helvetica, sans-serif;
  8688. border-width: 1px;
  8689. border-style: solid;
  8690. left: 50%;
  8691. margin: 10px auto;
  8692. min-height: 30px;
  8693. padding: 5px;
  8694. right: 50%;
  8695. width: 500px;
  8696. background-color: #FFD1D1;
  8697. border-color: #FF0000;
  8698. color: #000;
  8699. }
  8700. </style>
  8701. <body>
  8702. <div class="error-message">
  8703. $lang_not_exportable
  8704. </div>
  8705. </body>
  8706. </html>
  8707. EOD;
  8708. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8709. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8710. }
  8711. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8712. // Add the extra files that go along with a SCORM package.
  8713. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8714. $extra_files = scandir($main_code_path);
  8715. foreach ($extra_files as $extra_file) {
  8716. if (strpos($extra_file, '.') === 0)
  8717. continue;
  8718. else {
  8719. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8720. $this->create_path($dest_file);
  8721. copy($main_code_path.$extra_file, $dest_file);
  8722. }
  8723. }
  8724. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8725. $manifest = @$xmldoc->saveXML();
  8726. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8727. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8728. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8729. // Clean possible temporary files.
  8730. foreach ($files_cleanup as $file) {
  8731. $res = unlink($file);
  8732. if ($res === false) {
  8733. error_log(
  8734. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8735. 0
  8736. );
  8737. }
  8738. }
  8739. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8740. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8741. }
  8742. /**
  8743. * @param int $lp_id
  8744. * @return bool
  8745. */
  8746. public function scorm_export_to_pdf($lp_id)
  8747. {
  8748. $lp_id = intval($lp_id);
  8749. $files_to_export = array();
  8750. $course_data = api_get_course_info($this->cc);
  8751. if (!empty($course_data)) {
  8752. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8753. $list = self::get_flat_ordered_items_list($lp_id);
  8754. if (!empty($list)) {
  8755. foreach ($list as $item_id) {
  8756. $item = $this->items[$item_id];
  8757. switch ($item->type) {
  8758. case 'document':
  8759. //Getting documents from a LP with chamilo documents
  8760. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8761. // Try loading document from the base course.
  8762. if (empty($file_data) && !empty($sessionId)) {
  8763. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8764. }
  8765. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8766. if (file_exists($file_path)) {
  8767. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8768. }
  8769. break;
  8770. case 'asset': //commes from a scorm package generated by chamilo
  8771. case 'sco':
  8772. $file_path = $scorm_path.'/'.$item->path;
  8773. if (file_exists($file_path)) {
  8774. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8775. }
  8776. break;
  8777. case 'dokeos_chapter':
  8778. case 'dir':
  8779. case 'chapter':
  8780. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8781. break;
  8782. }
  8783. }
  8784. }
  8785. $pdf = new PDF();
  8786. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8787. return $result;
  8788. }
  8789. return false;
  8790. }
  8791. /**
  8792. * Temp function to be moved in main_api or the best place around for this.
  8793. * Creates a file path if it doesn't exist
  8794. * @param string $path
  8795. */
  8796. public function create_path($path)
  8797. {
  8798. $path_bits = explode('/', dirname($path));
  8799. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8800. $path_built = IS_WINDOWS_OS ? '' : '/';
  8801. foreach ($path_bits as $bit) {
  8802. if (!empty ($bit)) {
  8803. $new_path = $path_built . $bit;
  8804. if (is_dir($new_path)) {
  8805. $path_built = $new_path . '/';
  8806. } else {
  8807. mkdir($new_path, api_get_permissions_for_new_directories());
  8808. $path_built = $new_path . '/';
  8809. }
  8810. }
  8811. }
  8812. }
  8813. /**
  8814. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8815. * @return boolean The results of the unlink function, or false if there was no image to start with
  8816. */
  8817. public function delete_lp_image()
  8818. {
  8819. $img = $this->get_preview_image();
  8820. if ($img != '') {
  8821. $del_file = $this->get_preview_image_path(null, 'sys');
  8822. if (isset($del_file) && file_exists($del_file)) {
  8823. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8824. if (file_exists($del_file_2)) {
  8825. unlink($del_file_2);
  8826. }
  8827. $this->set_preview_image('');
  8828. return @unlink($del_file);
  8829. }
  8830. }
  8831. return false;
  8832. }
  8833. /**
  8834. * Uploads an author image to the upload/learning_path/images directory
  8835. * @param array The image array, coming from the $_FILES superglobal
  8836. * @return boolean True on success, false on error
  8837. */
  8838. public function upload_image($image_array)
  8839. {
  8840. $image_moved = false;
  8841. if (!empty ($image_array['name'])) {
  8842. $upload_ok = process_uploaded_file($image_array);
  8843. $has_attachment = true;
  8844. } else {
  8845. $image_moved = true;
  8846. }
  8847. if ($upload_ok) {
  8848. if ($has_attachment) {
  8849. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8850. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8851. $updir = $sys_course_path . $courseDir;
  8852. // Try to add an extension to the file if it hasn't one.
  8853. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8854. if (!filter_extension($new_file_name)) {
  8855. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8856. $image_moved = false;
  8857. } else {
  8858. $file_extension = explode('.', $image_array['name']);
  8859. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8860. $filename = uniqid('');
  8861. $new_file_name = $filename.'.'.$file_extension;
  8862. $new_path = $updir.'/'.$new_file_name;
  8863. // Resize the image.
  8864. $temp = new Image($image_array['tmp_name']);
  8865. $temp->resize(104);
  8866. $result = $temp->send_image($new_path);
  8867. // Storing the image filename.
  8868. if ($result) {
  8869. $image_moved = true;
  8870. $this->set_preview_image($new_file_name);
  8871. //Resize to 64px to use on course homepage
  8872. $temp->resize(64);
  8873. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8874. return true;
  8875. }
  8876. }
  8877. }
  8878. }
  8879. return false;
  8880. }
  8881. /**
  8882. * @param int $lp_id
  8883. * @param string $status
  8884. */
  8885. public function set_autolaunch($lp_id, $status)
  8886. {
  8887. $course_id = api_get_course_int_id();
  8888. $lp_id = intval($lp_id);
  8889. $status = intval($status);
  8890. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8891. // Setting everything to autolaunch = 0
  8892. $attributes['autolaunch'] = 0;
  8893. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8894. Database::update($lp_table, $attributes, $where);
  8895. if ($status == 1) {
  8896. //Setting my lp_id to autolaunch = 1
  8897. $attributes['autolaunch'] = 1;
  8898. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8899. Database::update($lp_table, $attributes, $where );
  8900. }
  8901. }
  8902. /**
  8903. * Gets previous_item_id for the next element of the lp_item table
  8904. * @author Isaac flores paz
  8905. * @return integer Previous item ID
  8906. */
  8907. public function select_previous_item_id()
  8908. {
  8909. $course_id = api_get_course_int_id();
  8910. if ($this->debug > 0) {
  8911. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8912. }
  8913. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8914. // Get the max order of the items
  8915. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8916. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8917. $rs_max_order = Database::query($sql_max_order);
  8918. $row_max_order = Database::fetch_object($rs_max_order);
  8919. $max_order = $row_max_order->display_order;
  8920. // Get the previous item ID
  8921. $sql = "SELECT id as previous FROM $table_lp_item
  8922. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8923. $rs_max = Database::query($sql);
  8924. $row_max = Database::fetch_object($rs_max);
  8925. // Return the previous item ID
  8926. return $row_max->previous;
  8927. }
  8928. /**
  8929. * Copies an LP
  8930. */
  8931. public function copy()
  8932. {
  8933. $main_path = api_get_path(SYS_CODE_PATH);
  8934. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8935. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8936. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8937. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8938. //Course builder
  8939. $cb = new CourseBuilder();
  8940. //Setting tools that will be copied
  8941. $cb->set_tools_to_build(array('learnpaths'));
  8942. //Setting elements that will be copied
  8943. $cb->set_tools_specific_id_list(
  8944. array('learnpaths' => array($this->lp_id))
  8945. );
  8946. $course = $cb->build();
  8947. //Course restorer
  8948. $course_restorer = new CourseRestorer($course);
  8949. $course_restorer->set_add_text_in_items(true);
  8950. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8951. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8952. }
  8953. public function verify_document_size($s)
  8954. {
  8955. $post_max = ini_get('post_max_size');
  8956. if (substr($post_max, -1, 1) == 'M') {
  8957. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  8958. } elseif (substr($post_max, -1, 1) == 'G') {
  8959. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  8960. }
  8961. $upl_max = ini_get('upload_max_filesize');
  8962. if (substr($upl_max, -1, 1) == 'M') {
  8963. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  8964. } elseif (substr($upl_max, -1, 1) == 'G') {
  8965. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  8966. }
  8967. $documents_total_space = DocumentManager::documents_total_space();
  8968. $course_max_space = DocumentManager::get_course_quota();
  8969. $total_size = filesize($s) + $documents_total_space;
  8970. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8971. return true;
  8972. } else{
  8973. return false;
  8974. }
  8975. }
  8976. /**
  8977. * Clear LP prerequisites
  8978. */
  8979. public function clear_prerequisites()
  8980. {
  8981. $course_id = $this->get_course_int_id();
  8982. if ($this->debug > 0) {
  8983. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8984. }
  8985. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8986. $lp_id = $this->get_id();
  8987. //Cleaning prerequisites
  8988. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8989. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8990. Database::query($sql);
  8991. //Cleaning mastery score for exercises
  8992. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8993. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8994. Database::query($sql);
  8995. }
  8996. public function set_previous_step_as_prerequisite_for_all_items()
  8997. {
  8998. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8999. $course_id = $this->get_course_int_id();
  9000. $lp_id = $this->get_id();
  9001. if (!empty($this->items)) {
  9002. $previous_item_id = null;
  9003. $previous_item_max = 0;
  9004. $previous_item_type = null;
  9005. $last_item_not_chapter = null;
  9006. $last_item_not_chapter_type = null;
  9007. $last_item_not_chapter_max = null;
  9008. foreach ($this->items as $item) {
  9009. // if there was a previous item... (otherwise jump to set it)
  9010. if (!empty($previous_item_id)) {
  9011. $current_item_id = $item->get_id(); //save current id
  9012. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9013. // Current item is not a folder, so it qualifies to get a prerequisites
  9014. if ($last_item_not_chapter_type == 'quiz') {
  9015. // if previous is quiz, mark its max score as default score to be achieved
  9016. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9017. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9018. Database::query($sql);
  9019. }
  9020. // now simply update the prerequisite to set it to the last non-chapter item
  9021. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9022. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9023. Database::query($sql);
  9024. // record item as 'non-chapter' reference
  9025. $last_item_not_chapter = $item->get_id();
  9026. $last_item_not_chapter_type = $item->get_type();
  9027. $last_item_not_chapter_max = $item->get_max();
  9028. }
  9029. } else {
  9030. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9031. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9032. $last_item_not_chapter = $item->get_id();
  9033. $last_item_not_chapter_type = $item->get_type();
  9034. $last_item_not_chapter_max = $item->get_max();
  9035. }
  9036. }
  9037. // Saving the item as "previous item" for the next loop
  9038. $previous_item_id = $item->get_id();
  9039. $previous_item_max = $item->get_max();
  9040. $previous_item_type = $item->get_type();
  9041. }
  9042. }
  9043. }
  9044. /**
  9045. * @param array $params
  9046. */
  9047. public static function createCategory($params)
  9048. {
  9049. $em = Database::getManager();
  9050. $item = new CLpCategory();
  9051. $item->setName($params['name']);
  9052. $item->setCId($params['c_id']);
  9053. $em->persist($item);
  9054. $em->flush();
  9055. }
  9056. /**
  9057. * @param array $params
  9058. */
  9059. public static function updateCategory($params)
  9060. {
  9061. $em = Database::getManager();
  9062. /** @var CLpCategory $item */
  9063. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9064. if ($item) {
  9065. $item->setName($params['name']);
  9066. $em->merge($item);
  9067. $em->flush();
  9068. }
  9069. }
  9070. /**
  9071. * @param int $id
  9072. */
  9073. public static function moveUpCategory($id)
  9074. {
  9075. $em = Database::getManager();
  9076. /** @var CLpCategory $item */
  9077. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9078. if ($item) {
  9079. $position = $item->getPosition() - 1;
  9080. $item->setPosition($position);
  9081. $em->persist($item);
  9082. $em->flush();
  9083. }
  9084. }
  9085. /**
  9086. * @param int $id
  9087. */
  9088. public static function moveDownCategory($id)
  9089. {
  9090. $em = Database::getManager();
  9091. /** @var CLpCategory $item */
  9092. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9093. if ($item) {
  9094. $position = $item->getPosition() + 1;
  9095. $item->setPosition($position);
  9096. $em->persist($item);
  9097. $em->flush();
  9098. }
  9099. }
  9100. /**
  9101. * @param int $courseId
  9102. * @return int|mixed
  9103. */
  9104. public static function getCountCategories($courseId)
  9105. {
  9106. if (empty($course_id)) {
  9107. return 0;
  9108. }
  9109. $em = Database::getManager();
  9110. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9111. $query->setParameter('id', $courseId);
  9112. return $query->getSingleScalarResult();
  9113. }
  9114. /**
  9115. * @param int $courseId
  9116. *
  9117. * @return mixed
  9118. */
  9119. public static function getCategories($courseId)
  9120. {
  9121. $em = Database::getManager();
  9122. //Default behaviour
  9123. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9124. array('cId' => $course_id),
  9125. array('name' => 'ASC')
  9126. );*/
  9127. // Using doctrine extensions
  9128. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9129. array('cId' => $courseId)
  9130. )->getResult();
  9131. return $items;
  9132. }
  9133. /**
  9134. * @param int $id
  9135. *
  9136. * @return mixed
  9137. */
  9138. public static function getCategory($id)
  9139. {
  9140. $em = Database::getManager();
  9141. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9142. return $item;
  9143. }
  9144. /**
  9145. * @param int $courseId
  9146. * @return array
  9147. */
  9148. public static function getCategoryByCourse($courseId)
  9149. {
  9150. $em = Database::getManager();
  9151. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9152. return $items;
  9153. }
  9154. /**
  9155. * @param int $id
  9156. *
  9157. * @return mixed
  9158. */
  9159. public static function deleteCategory($id)
  9160. {
  9161. $em = Database::getManager();
  9162. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9163. if ($item) {
  9164. $courseId = $item->getCId();
  9165. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9166. $query->setParameter('id', $courseId);
  9167. $query->setParameter('catId', $item->getId());
  9168. $lps = $query->getResult();
  9169. // Setting category = 0.
  9170. if ($lps) {
  9171. foreach ($lps as $lpItem) {
  9172. $lpItem->setCategoryId(0);
  9173. }
  9174. }
  9175. // Removing category.
  9176. $em->remove($item);
  9177. $em->flush();
  9178. }
  9179. }
  9180. /**
  9181. * @param int $courseId
  9182. * @param bool $addSelectOption
  9183. *
  9184. * @return mixed
  9185. */
  9186. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9187. {
  9188. $items = self::getCategoryByCourse($courseId);
  9189. $cats = array();
  9190. if ($addSelectOption) {
  9191. $cats = array(get_lang('SelectACategory'));
  9192. }
  9193. if (!empty($items)) {
  9194. foreach ($items as $cat) {
  9195. $cats[$cat->getId()] = $cat->getName();
  9196. }
  9197. }
  9198. return $cats;
  9199. }
  9200. /**
  9201. * Return the scorm item type object with spaces replaced with _
  9202. * The return result is use to build a css classname like scorm_type_$return
  9203. * @param $in_type
  9204. * @return mixed
  9205. */
  9206. private static function format_scorm_type_item($in_type)
  9207. {
  9208. return str_replace(' ', '_', $in_type);
  9209. }
  9210. /**
  9211. * @return \learnpath
  9212. */
  9213. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9214. {
  9215. $lpObject = Session::read('lpobject');
  9216. $learnPath = null;
  9217. if (isset($lpObject)) {
  9218. $learnPath = unserialize($lpObject);
  9219. }
  9220. if (!is_object($learnPath)) {
  9221. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9222. }
  9223. return $learnPath;
  9224. }
  9225. /**
  9226. * @param int $itemId
  9227. * @return learnpathItem|false
  9228. */
  9229. public function getItem($itemId)
  9230. {
  9231. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9232. return $this->items[$itemId];
  9233. }
  9234. return false;
  9235. }
  9236. /**
  9237. * @return int
  9238. */
  9239. public function getCategoryId()
  9240. {
  9241. return $this->categoryId;
  9242. }
  9243. /**
  9244. * @param int $categoryId
  9245. * @return bool
  9246. */
  9247. public function setCategoryId($categoryId)
  9248. {
  9249. $this->categoryId = intval($categoryId);
  9250. $courseId = api_get_course_int_id();
  9251. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9252. $lp_id = $this->get_id();
  9253. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9254. WHERE c_id = $courseId AND id = $lp_id";
  9255. Database::query($sql);
  9256. return true;
  9257. }
  9258. /**
  9259. * Get whether this is a learning path with the possibility to subscribe
  9260. * users or not
  9261. * @return int
  9262. */
  9263. public function getSubscribeUsers()
  9264. {
  9265. return $this->subscribeUsers;
  9266. }
  9267. /**
  9268. * Set whether this is a learning path with the possibility to subscribe
  9269. * users or not
  9270. * @param int $subscribeUsers (0 = false, 1 = true)
  9271. */
  9272. public function setSubscribeUsers($value)
  9273. {
  9274. if ($this->debug > 0) {
  9275. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9276. }
  9277. $this->subscribeUsers = intval($value);;
  9278. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9279. $lp_id = $this->get_id();
  9280. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9281. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9282. Database::query($sql);
  9283. return true;
  9284. }
  9285. /**
  9286. * Calculate the count of stars for a user in this LP
  9287. * This calculation is based on the following rules:
  9288. * - the student gets one star when he gets to 50% of the learning path
  9289. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9290. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9291. * - the student gets the final star when the score for the *last* test is >= 80%
  9292. * @param int $sessionId Optional. The session ID
  9293. * @return int The count of stars
  9294. */
  9295. public function getCalculateStars($sessionId = 0)
  9296. {
  9297. $stars = 0;
  9298. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9299. if ($progress >= 50) {
  9300. $stars++;
  9301. }
  9302. // Calculate stars chapters evaluation
  9303. $exercisesItems = $this->getExercisesItems();
  9304. if (!empty($exercisesItems)) {
  9305. $totalResult = 0;
  9306. foreach ($exercisesItems as $exerciseItem) {
  9307. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9308. $this->user_id,
  9309. $exerciseItem->path,
  9310. $this->course_int_id,
  9311. $sessionId,
  9312. $this->lp_id,
  9313. $exerciseItem->db_id
  9314. );
  9315. $exerciseResult = 0;
  9316. foreach ($exerciseResultInfo as $result) {
  9317. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9318. }
  9319. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9320. $totalResult += $exerciseAverage;
  9321. }
  9322. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9323. if ($totalExerciseAverage >= 50) {
  9324. $stars++;
  9325. }
  9326. if ($totalExerciseAverage >= 80) {
  9327. $stars++;
  9328. }
  9329. }
  9330. // Calculate star for final evaluation
  9331. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9332. if (!empty($finalEvaluationItem)) {
  9333. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9334. $this->user_id,
  9335. $finalEvaluationItem->path,
  9336. $this->course_int_id,
  9337. $sessionId,
  9338. $this->lp_id,
  9339. $finalEvaluationItem->db_id
  9340. );
  9341. $evaluationResult = 0;
  9342. foreach ($evaluationResultInfo as $result) {
  9343. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9344. }
  9345. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9346. $evaluationAverage = $evaluationResult / $averageDivisor;
  9347. if ($evaluationAverage >= 80) {
  9348. $stars++;
  9349. }
  9350. }
  9351. return $stars;
  9352. }
  9353. /**
  9354. * Get the items of exercise type
  9355. * @return array The items. Otherwise return false
  9356. */
  9357. public function getExercisesItems()
  9358. {
  9359. $exercises = [];
  9360. foreach ($this->items as $item) {
  9361. if ($item->type != 'quiz') {
  9362. continue;
  9363. }
  9364. $exercises[] = $item;
  9365. }
  9366. array_pop($exercises);
  9367. return $exercises;
  9368. }
  9369. /**
  9370. * Get the item of exercise type (evaluation type)
  9371. * @return array The final evaluation. Otherwise return false
  9372. */
  9373. public function getFinalEvaluationItem()
  9374. {
  9375. $exercises = [];
  9376. foreach ($this->items as $item) {
  9377. if ($item->type != 'quiz') {
  9378. continue;
  9379. }
  9380. $exercises[] = $item;
  9381. }
  9382. return array_pop($exercises);
  9383. }
  9384. /**
  9385. * Calculate the total points achieved for the current user in this learning path
  9386. * @param int $sessionId Optional. The session Id
  9387. * @return int
  9388. */
  9389. public function getCalculateScore($sessionId = 0)
  9390. {
  9391. // Calculate stars chapters evaluation
  9392. $exercisesItems = $this->getExercisesItems();
  9393. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9394. $totalExercisesResult = 0;
  9395. $totalEvaluationResult = 0;
  9396. if ($exercisesItems !== false) {
  9397. foreach ($exercisesItems as $exerciseItem) {
  9398. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9399. $this->user_id,
  9400. $exerciseItem->path,
  9401. $this->course_int_id,
  9402. $sessionId,
  9403. $this->lp_id,
  9404. $exerciseItem->db_id
  9405. );
  9406. $exerciseResult = 0;
  9407. foreach ($exerciseResultInfo as $result) {
  9408. $exerciseResult += $result['exe_result'];
  9409. }
  9410. $totalExercisesResult += $exerciseResult;
  9411. }
  9412. }
  9413. if (!empty($finalEvaluationItem)) {
  9414. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9415. $this->user_id,
  9416. $finalEvaluationItem->path,
  9417. $this->course_int_id,
  9418. $sessionId,
  9419. $this->lp_id,
  9420. $finalEvaluationItem->db_id
  9421. );
  9422. foreach ($evaluationResultInfo as $result) {
  9423. $totalEvaluationResult += $result['exe_result'];
  9424. }
  9425. }
  9426. return $totalExercisesResult + $totalEvaluationResult;
  9427. }
  9428. /**
  9429. * Check if URL is not allowed to be show in a iframe
  9430. * @param string $src
  9431. *
  9432. * @return string
  9433. */
  9434. public function fixBlockedLinks($src)
  9435. {
  9436. $urlInfo = parse_url($src);
  9437. //$platformProtocol = api_get_protocol();
  9438. $platformProtocol = 'https';
  9439. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9440. $platformProtocol = 'http';
  9441. }
  9442. $protocolFixApplied = false;
  9443. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9444. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9445. if ($platformProtocol != $scheme) {
  9446. $_SESSION['x_frame_source'] = $src;
  9447. $src = 'blank.php?error=x_frames_options';
  9448. $protocolFixApplied = true;
  9449. }
  9450. if ($protocolFixApplied == false) {
  9451. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9452. // Check X-Frame-Options
  9453. $ch = curl_init();
  9454. $options = array(
  9455. CURLOPT_URL => $src,
  9456. CURLOPT_RETURNTRANSFER => true,
  9457. CURLOPT_HEADER => true,
  9458. CURLOPT_FOLLOWLOCATION => true,
  9459. CURLOPT_ENCODING => "",
  9460. CURLOPT_AUTOREFERER => true,
  9461. CURLOPT_CONNECTTIMEOUT => 120,
  9462. CURLOPT_TIMEOUT => 120,
  9463. CURLOPT_MAXREDIRS => 10,
  9464. );
  9465. curl_setopt_array($ch, $options);
  9466. $response = curl_exec($ch);
  9467. $httpCode = curl_getinfo($ch);
  9468. $headers = substr($response, 0, $httpCode['header_size']);
  9469. $error = false;
  9470. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9471. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9472. ) {
  9473. $error = true;
  9474. }
  9475. if ($error) {
  9476. $_SESSION['x_frame_source'] = $src;
  9477. $src = 'blank.php?error=x_frames_options';
  9478. }
  9479. }
  9480. }
  9481. return $src;
  9482. }
  9483. /**
  9484. * Check if this LP has a created forum in the basis course
  9485. * @return boolean
  9486. */
  9487. public function lpHasForum()
  9488. {
  9489. $forumTable = Database::get_course_table(TABLE_FORUM);
  9490. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9491. $fakeFrom = "
  9492. $forumTable f
  9493. INNER JOIN $itemProperty ip
  9494. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9495. ";
  9496. $resultData = Database::select(
  9497. 'COUNT(f.iid) AS qty',
  9498. $fakeFrom,
  9499. [
  9500. 'where' => [
  9501. 'ip.visibility != ? AND ' => 2,
  9502. 'ip.tool = ? AND ' => TOOL_FORUM,
  9503. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9504. 'f.lp_id = ?' => intval($this->lp_id)
  9505. ]
  9506. ],
  9507. 'first'
  9508. );
  9509. if ($resultData['qty'] > 0) {
  9510. return true;
  9511. }
  9512. return false;
  9513. }
  9514. /**
  9515. * Get the forum for this learning path
  9516. * @return boolean
  9517. */
  9518. public function getForum($sessionId = 0)
  9519. {
  9520. $forumTable = Database::get_course_table(TABLE_FORUM);
  9521. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9522. $fakeFrom = "$forumTable f
  9523. INNER JOIN $itemProperty ip ";
  9524. if ($this->lp_session_id == 0) {
  9525. $fakeFrom .= "
  9526. ON (
  9527. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9528. f.session_id = ip.session_id OR ip.session_id IS NULL
  9529. )
  9530. )
  9531. ";
  9532. } else {
  9533. $fakeFrom .= "
  9534. ON (
  9535. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9536. )
  9537. ";
  9538. }
  9539. $resultData = Database::select(
  9540. 'f.*',
  9541. $fakeFrom,
  9542. [
  9543. 'where' => [
  9544. 'ip.visibility != ? AND ' => 2,
  9545. 'ip.tool = ? AND ' => TOOL_FORUM,
  9546. 'f.session_id = ? AND ' => $sessionId,
  9547. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9548. 'f.lp_id = ?' => intval($this->lp_id)
  9549. ]
  9550. ],
  9551. 'first'
  9552. );
  9553. if (empty($resultData)) {
  9554. return false;
  9555. }
  9556. return $resultData;
  9557. }
  9558. /**
  9559. * Create a forum for this learning path
  9560. * @param type $forumCategoryId
  9561. * @return int The forum ID if was created. Otherwise return false
  9562. */
  9563. public function createForum($forumCategoryId)
  9564. {
  9565. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  9566. $forumId = store_forum(
  9567. [
  9568. 'lp_id' => $this->lp_id,
  9569. 'forum_title' => $this->name,
  9570. 'forum_comment' => null,
  9571. 'forum_category' => intval($forumCategoryId),
  9572. 'students_can_edit_group' => ['students_can_edit' => 0],
  9573. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9574. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9575. 'group_forum' => 0,
  9576. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9577. ],
  9578. [],
  9579. true
  9580. );
  9581. return $forumId;
  9582. }
  9583. }
  9584. if (!function_exists('trim_value')) {
  9585. function trim_value(& $value) {
  9586. $value = trim($value);
  9587. }
  9588. }