learnpath.class.php 453 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
  5. * learnpaths. It is used by the scorm class.
  6. *
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  10. * @todo this file is too big, need a refactor more than 10000 lines!
  11. */
  12. /**
  13. * Defines the learnpath parent class
  14. * @package chamilo.learnpath
  15. */
  16. class learnpath
  17. {
  18. public $attempt = 0; // The number for the current ID view.
  19. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  20. public $current; // Id of the current item the user is viewing.
  21. public $current_score; // The score of the current item.
  22. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  23. public $current_time_stop; // The time the user closed this resource.
  24. public $default_status = 'not attempted';
  25. public $encoding = 'UTF-8';
  26. public $error = '';
  27. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  28. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  29. public $index; // The index of the active learnpath_item in $ordered_items array.
  30. public $items = array();
  31. public $last; // item_id of last item viewed in the learning path.
  32. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  33. public $license; // Which license this course has been given - not used yet on 20060522.
  34. public $lp_id; // DB ID for this learnpath.
  35. public $lp_view_id; // DB ID for lp_view
  36. public $log_file; // File where to log learnpath API msg.
  37. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  38. public $message = '';
  39. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  40. public $name; // Learnpath name (they generally have one).
  41. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  42. public $path = ''; // Path inside the scorm directory (if scorm).
  43. public $theme; // The current theme of the learning path.
  44. public $preview_image; // The current image of the learning path.
  45. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  46. public $prevent_reinit = 1;
  47. // Describes the mode of progress bar display.
  48. public $seriousgame_mode = 0;
  49. public $progress_bar_mode = '%';
  50. // Percentage progress as saved in the db.
  51. public $progress_db = '0';
  52. public $proximity; // Wether the content is distant or local or unknown.
  53. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  54. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  55. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  56. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  57. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  58. public $user_id; //ID of the user that is viewing/using the course
  59. public $update_queue = array();
  60. public $scorm_debug = 0;
  61. public $arrMenu = array(); // Array for the menu items.
  62. public $debug = 0; // Logging level.
  63. public $lp_session_id = 0;
  64. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  65. public $prerequisite = 0;
  66. public $use_max_score = 1; // 1 or 0
  67. public $subscribe_users = 0;
  68. public $created_on = '';
  69. public $modified_on = '';
  70. public $publicated_on = '';
  71. public $expired_on = '';
  72. public $ref = null;
  73. public $course_int_id;
  74. public $course_info = array();
  75. public function get_course_int_id()
  76. {
  77. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  78. }
  79. public function set_course_int_id($course_id)
  80. {
  81. return $this->course_int_id = intval($course_id);
  82. }
  83. /**
  84. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  85. * Also builds the list of items into $this->items.
  86. * @param string Course code
  87. * @param integer Learnpath ID
  88. * @param integer User ID
  89. * @return boolean True on success, false on error
  90. */
  91. public function __construct($course, $lp_id, $user_id)
  92. {
  93. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  94. // Check course code.
  95. $course_id = api_get_course_int_id();
  96. if ($this->debug > 0) {
  97. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  98. }
  99. if (empty($course)) {
  100. $this->error = 'Course code is empty';
  101. return false;
  102. } else {
  103. $course_info = api_get_course_info($course);
  104. if (!empty($course_info)) {
  105. $this->cc = $course_info['code'];
  106. $this->course_info = $course_info;
  107. $course_id = $course_info['real_id'];
  108. } else {
  109. $this->error = 'Course code does not exist in database';
  110. return false;
  111. }
  112. }
  113. $this->set_course_int_id($course_id);
  114. // Check learnpath ID.
  115. if (empty($lp_id)) {
  116. $this->error = 'Learnpath ID is empty';
  117. return false;
  118. } else {
  119. // TODO: Make it flexible to use any course_code (still using env course code here).
  120. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  121. $lp_id = intval($lp_id);
  122. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  123. if ($this->debug > 2) {
  124. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  125. }
  126. $res = Database::query($sql);
  127. if (Database::num_rows($res) > 0) {
  128. $this->lp_id = $lp_id;
  129. $row = Database::fetch_array($res);
  130. $this->type = $row['lp_type'];
  131. $this->name = stripslashes($row['name']);
  132. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  133. $this->proximity = $row['content_local'];
  134. $this->theme = $row['theme'];
  135. $this->maker = $row['content_maker'];
  136. $this->prevent_reinit = $row['prevent_reinit'];
  137. $this->seriousgame_mode = $row['seriousgame_mode'];
  138. $this->license = $row['content_license'];
  139. $this->scorm_debug = $row['debug'];
  140. $this->js_lib = $row['js_lib'];
  141. $this->path = $row['path'];
  142. $this->preview_image = $row['preview_image'];
  143. $this->author = $row['author'];
  144. $this->hide_toc_frame = $row['hide_toc_frame'];
  145. $this->lp_session_id = $row['session_id'];
  146. $this->use_max_score = $row['use_max_score'];
  147. $this->subscribe_users = $row['subscribe_users'];
  148. $this->created_on = $row['created_on'];
  149. $this->modified_on = $row['modified_on'];
  150. $this->ref = $row['ref'];
  151. $this->category_id = $row['category_id'];
  152. $this->max_attempts = $row['max_attempts'];
  153. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  154. $this->publicated_on = $row['publicated_on'];
  155. }
  156. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  157. $this->expired_on = $row['expired_on'];
  158. }
  159. if ($this->type == 2) {
  160. if ($row['force_commit'] == 1) {
  161. $this->force_commit = true;
  162. }
  163. }
  164. $this->mode = $row['default_view_mod'];
  165. } else {
  166. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  167. return false;
  168. }
  169. }
  170. // Check user ID.
  171. if (empty($user_id)) {
  172. $this->error = 'User ID is empty';
  173. return false;
  174. } else {
  175. $user_info = api_get_user_info($user_id);
  176. if (!empty($user_info)) {
  177. $this->user_id = $user_info['user_id'];
  178. } else {
  179. $this->error = 'User ID does not exist in database ('.$sql.')';
  180. return false;
  181. }
  182. }
  183. // End of variables checking.
  184. $session_id = api_get_session_id();
  185. // Get the session condition for learning paths of the base + session.
  186. $session = api_get_session_condition($session_id);
  187. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  188. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  189. // Selecting by view_count descending allows to get the highest view_count first.
  190. $sql = "SELECT * FROM $lp_table WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session ORDER BY view_count DESC";
  191. $res = Database::query($sql);
  192. if ($this->debug > 2) {
  193. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  194. }
  195. if (Database :: num_rows($res) > 0) {
  196. if ($this->debug > 2) {
  197. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  198. }
  199. $row = Database :: fetch_array($res);
  200. $this->attempt = $row['view_count'];
  201. $this->lp_view_id = $row['id'];
  202. $this->last_item_seen = $row['last_item'];
  203. $this->progress_db = $row['progress'];
  204. $this->lp_view_session_id = $row['session_id'];
  205. } else {
  206. if ($this->debug > 2) {
  207. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  208. }
  209. $this->attempt = 1;
  210. $sql_ins = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id) VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  211. $res_ins = Database::query($sql_ins);
  212. $this->lp_view_id = Database :: insert_id();
  213. if ($this->debug > 2) {
  214. error_log('New LP - learnpath::__construct() '.__LINE__.' - inserting new lp_view: '.$sql_ins, 0);
  215. }
  216. }
  217. // Initialise items.
  218. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  219. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  220. $res = Database::query($sql);
  221. if ($this->debug > 2) {
  222. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  223. error_log('-- Start while--', 0);
  224. }
  225. $lp_item_id_list = array();
  226. while ($row = Database::fetch_array($res)) {
  227. $oItem = '';
  228. $lp_item_id_list[] = $row['id'];
  229. switch ($this->type) {
  230. case 3: //aicc
  231. $oItem = new aiccItem('db', $row['id'], $course_id);
  232. if (is_object($oItem)) {
  233. $my_item_id = $oItem->get_id();
  234. $oItem->set_lp_view($this->lp_view_id, $course_id);
  235. $oItem->set_prevent_reinit($this->prevent_reinit);
  236. // Don't use reference here as the next loop will make the pointed object change.
  237. $this->items[$my_item_id] = $oItem;
  238. $this->refs_list[$oItem->ref] = $my_item_id;
  239. if ($this->debug > 2) {
  240. error_log(
  241. 'New LP - learnpath::__construct() - aicc object with id '.$my_item_id.' set in items[]',
  242. 0
  243. );
  244. }
  245. }
  246. break;
  247. case 2:
  248. require_once 'scorm.class.php';
  249. require_once 'scormItem.class.php';
  250. $oItem = new scormItem('db', $row['id'], $course_id);
  251. if (is_object($oItem)) {
  252. $my_item_id = $oItem->get_id();
  253. $oItem->set_lp_view($this->lp_view_id, $course_id);
  254. $oItem->set_prevent_reinit($this->prevent_reinit);
  255. // Don't use reference here as the next loop will make the pointed object change.
  256. $this->items[$my_item_id] = $oItem;
  257. $this->refs_list[$oItem->ref] = $my_item_id;
  258. if ($this->debug > 2) {
  259. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  260. }
  261. }
  262. break;
  263. case 1:
  264. default:
  265. if ($this->debug > 2) {
  266. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  267. }
  268. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  269. if ($this->debug > 2) {
  270. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  271. }
  272. if (is_object($oItem)) {
  273. $my_item_id = $oItem->get_id();
  274. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  275. $oItem->set_prevent_reinit($this->prevent_reinit);
  276. // Don't use reference here as the next loop will make the pointed object change.
  277. $this->items[$my_item_id] = $oItem;
  278. $this->refs_list[$my_item_id] = $my_item_id;
  279. if ($this->debug > 2) {
  280. error_log(
  281. 'New LP - learnpath::__construct() '.__LINE__.' - object with id '.$my_item_id.' set in items[]',
  282. 0
  283. );
  284. }
  285. }
  286. break;
  287. }
  288. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  289. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  290. if (is_object($this->items[$row['id']])) {
  291. $this->items[$row['id']]->set_level(0);
  292. }
  293. } else {
  294. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  295. $this->items[$row['id']]->set_level($level);
  296. if (is_object($this->items[$row['parent_item_id']])) {
  297. // Items is a list of pointers from item DB ids to item objects.
  298. $this->items[$row['parent_item_id']]->add_child($row['id']);
  299. } else {
  300. if ($this->debug > 2) {
  301. error_log(
  302. 'New LP - learnpath::__construct() '.__LINE__.' - The parent item ('.$row['parent_item_id'].') of item '.$row['id'].' could not be found',
  303. 0
  304. );
  305. }
  306. }
  307. }
  308. // Setting the view in the item object.
  309. if (is_object($this->items[$row['id']])) {
  310. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  311. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  312. $this->items[$row['id']]->current_start_time = 0;
  313. $this->items[$row['id']]->current_stop_time = 0;
  314. }
  315. }
  316. }
  317. if ($this->debug > 2) {
  318. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  319. }
  320. if (!empty($lp_item_id_list)) {
  321. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  322. // Get last viewing vars.
  323. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  324. // This query should only return one or zero result.
  325. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  326. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  327. ORDER BY view_count DESC ";
  328. if ($this->debug > 2) {
  329. error_log('New LP - learnpath::__construct() - Selecting item_views: '.$sql, 0);
  330. }
  331. $status_list = array();
  332. $res = Database::query($sql);
  333. while ($row = Database :: fetch_array($res)) {
  334. $status_list[$row['lp_item_id']] = $row['status'];
  335. }
  336. foreach ($lp_item_id_list as $item_id) {
  337. if (isset($status_list[$item_id])) {
  338. $status = $status_list[$item_id];
  339. if (is_object($this->items[$item_id])) {
  340. $this->items[$item_id]->set_status($status);
  341. if (empty($status)) {
  342. $this->items[$item_id]->set_status($this->default_status);
  343. }
  344. }
  345. } else {
  346. if (is_object($this->items[$item_id])) {
  347. $this->items[$item_id]->set_status($this->default_status);
  348. }
  349. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  350. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  351. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  352. if ($this->debug > 2) {
  353. error_log(
  354. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  355. 0
  356. );
  357. }
  358. Database::query($sql_ins);
  359. }
  360. }
  361. }
  362. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  363. $this->max_ordered_items = 0;
  364. foreach ($this->ordered_items as $index => $dummy) {
  365. if ($index > $this->max_ordered_items && !empty($dummy)) {
  366. $this->max_ordered_items = $index;
  367. }
  368. }
  369. // TODO: Define the current item better.
  370. $this->first();
  371. if ($this->debug > 2) {
  372. error_log(
  373. 'New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(
  374. ),
  375. 0
  376. );
  377. }
  378. }
  379. /**
  380. * Function rewritten based on old_add_item() from Yannick Warnier. Due the fact that users can decide where the item should come, I had to overlook this function and
  381. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  382. *
  383. * @param int $parent
  384. * @param int $previous
  385. * @param string $type
  386. * @param int resource ID (ref)
  387. * @param string $title
  388. * @param string $description
  389. * @return int
  390. */
  391. public function add_item(
  392. $parent,
  393. $previous,
  394. $type = 'dokeos_chapter',
  395. $id,
  396. $title,
  397. $description,
  398. $prerequisites = 0,
  399. $max_time_allowed = 0
  400. ) {
  401. $course_id = api_get_course_int_id();
  402. if ($this->debug > 0) {
  403. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  404. }
  405. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  406. $parent = intval($parent);
  407. $previous = intval($previous);
  408. $type = Database::escape_string($type);
  409. $id = intval($id);
  410. $max_time_allowed = Database::escape_string($max_time_allowed);
  411. if (empty ($max_time_allowed)) {
  412. $max_time_allowed = 0;
  413. }
  414. $title = Database::escape_string($title);
  415. $description = Database::escape_string($description);
  416. $sql_count = " SELECT COUNT(id) AS num
  417. FROM $tbl_lp_item
  418. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND parent_item_id = ".$parent;
  419. $res_count = Database::query($sql_count);
  420. $row = Database :: fetch_array($res_count);
  421. $num = $row['num'];
  422. if ($num > 0) {
  423. if ($previous == 0) {
  424. $sql = "SELECT id, next_item_id, display_order
  425. FROM ".$tbl_lp_item."
  426. WHERE c_id = $course_id AND
  427. lp_id = ".$this->get_id()." AND
  428. parent_item_id = ".$parent." AND
  429. previous_item_id = 0 OR previous_item_id=".$parent;
  430. $result = Database::query($sql);
  431. $row = Database :: fetch_array($result);
  432. $tmp_previous = 0;
  433. $next = $row['id'];
  434. $display_order = 0;
  435. } else {
  436. $previous = (int)$previous;
  437. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  438. FROM $tbl_lp_item
  439. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND id = ".$previous;
  440. $result = Database::query($sql);
  441. $row = Database :: fetch_array($result);
  442. $tmp_previous = $row['id'];
  443. $next = $row['next_item_id'];
  444. $display_order = $row['display_order'];
  445. }
  446. } else {
  447. $tmp_previous = 0;
  448. $next = 0;
  449. $display_order = 0;
  450. }
  451. $new_item_id = -1;
  452. $id = Database::escape_string($id);
  453. if ($type == 'quiz') {
  454. $sql = 'SELECT SUM(ponderation)
  455. FROM '.Database :: get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  456. INNER JOIN '.Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  457. ON quiz_question.iid = quiz_rel_question.question_id
  458. WHERE quiz_rel_question.exercice_id = '.$id." AND
  459. quiz_question.c_id = $course_id AND
  460. quiz_rel_question.c_id = $course_id ";
  461. $rsQuiz = Database::query($sql);
  462. $max_score = Database :: result($rsQuiz, 0, 0);
  463. //Disabling the exercise if we add it inside a LP
  464. $exercise = new Exercise();
  465. $exercise->read($id);
  466. $exercise->disable();
  467. $exercise->save();
  468. } else {
  469. $max_score = 100;
  470. }
  471. if ($prerequisites != 0) {
  472. $sql_ins = "INSERT INTO ".$tbl_lp_item." (
  473. c_id,
  474. lp_id, ".
  475. "item_type, ".
  476. "ref, ".
  477. "title, ".
  478. "description, ".
  479. "path, ".
  480. "max_score, ".
  481. "parent_item_id, ".
  482. "previous_item_id, ".
  483. "next_item_id, ".
  484. "display_order, ".
  485. "prerequisite, ".
  486. "max_time_allowed ".
  487. ") VALUES (
  488. $course_id ,
  489. ".$this->get_id().", ".
  490. "'".$type."', ".
  491. "'', ".
  492. "'".$title."', ".
  493. "'".$description."', ".
  494. "'".$id."', ".
  495. "'".$max_score."', ".
  496. $parent.", ".
  497. $previous.", ".
  498. $next.", ".
  499. ($display_order + 1).", ".
  500. $prerequisites.", ".
  501. $max_time_allowed.
  502. ")";
  503. } else {
  504. // Insert new item.
  505. $sql_ins = "
  506. INSERT INTO ".$tbl_lp_item." ( ".
  507. "c_id, ".
  508. "lp_id, ".
  509. "item_type, ".
  510. "ref, ".
  511. "title, ".
  512. "description, ".
  513. "path, ".
  514. "max_score, ".
  515. "parent_item_id, ".
  516. "previous_item_id, ".
  517. "next_item_id, ".
  518. "display_order, ".
  519. "max_time_allowed ".
  520. ") VALUES (".
  521. $course_id.",".
  522. $this->get_id().",".
  523. "'".$type."',".
  524. "'',".
  525. "'".$title."',".
  526. "'".$description."',".
  527. "'".$id."',".
  528. "'".$max_score."',".
  529. $parent.",".
  530. $previous.",".
  531. $next.",".
  532. ($display_order + 1).",".
  533. $max_time_allowed.
  534. ")";
  535. }
  536. if ($this->debug > 2) {
  537. error_log('New LP - Inserting dokeos_chapter: '.$sql_ins, 0);
  538. }
  539. $res_ins = Database::query($sql_ins);
  540. if ($res_ins) {
  541. $new_item_id = Database::insert_id();
  542. // Update the item that should come after the new item.
  543. $sql_update_next = "
  544. UPDATE ".$tbl_lp_item."
  545. SET previous_item_id = ".$new_item_id."
  546. WHERE c_id = $course_id AND id = ".$next;
  547. Database::query($sql_update_next);
  548. // Update the item that should be before the new item.
  549. $sql_update_previous = "
  550. UPDATE ".$tbl_lp_item."
  551. SET next_item_id = ".$new_item_id."
  552. WHERE c_id = $course_id AND id = ".$tmp_previous;
  553. Database::query($sql_update_previous);
  554. // Update all the items after the new item.
  555. $sql_update_order = "
  556. UPDATE ".$tbl_lp_item."
  557. SET display_order = display_order + 1
  558. WHERE
  559. c_id = $course_id AND
  560. lp_id = ".$this->get_id()." AND
  561. id <> ".$new_item_id." AND
  562. parent_item_id = ".$parent." AND
  563. display_order > ".$display_order;
  564. Database::query($sql_update_order);
  565. // Update the item that should come after the new item.
  566. $sql_update_ref = "UPDATE ".$tbl_lp_item."
  567. SET ref = ".$new_item_id."
  568. WHERE c_id = $course_id AND id = ".$new_item_id;
  569. Database::query($sql_update_ref);
  570. }
  571. // Upload audio.
  572. if (!empty ($_FILES['mp3']['name'])) {
  573. // Create the audio folder if it does not exist yet.
  574. $_course = api_get_course_info();
  575. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  576. if (!is_dir($filepath.'audio')) {
  577. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  578. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  579. api_item_property_update(
  580. $_course,
  581. TOOL_DOCUMENT,
  582. $audio_id,
  583. 'FolderCreated',
  584. api_get_user_id(),
  585. null,
  586. null,
  587. null,
  588. null,
  589. api_get_session_id()
  590. );
  591. api_item_property_update(
  592. $_course,
  593. TOOL_DOCUMENT,
  594. $audio_id,
  595. 'invisible',
  596. api_get_user_id(),
  597. null,
  598. null,
  599. null,
  600. null,
  601. api_get_session_id()
  602. );
  603. }
  604. // Upload the file in the documents tool.
  605. $file_path = FileManager::handle_uploaded_document(
  606. $_course,
  607. $_FILES['mp3'],
  608. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  609. '/audio',
  610. api_get_user_id(),
  611. '',
  612. '',
  613. '',
  614. '',
  615. false
  616. );
  617. // Getting the filename only.
  618. $file_components = explode('/', $file_path);
  619. $file = $file_components[count($file_components) - 1];
  620. // Store the mp3 file in the lp_item table.
  621. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string(
  622. $file
  623. )."' WHERE id = '".Database::escape_string($new_item_id)."'";
  624. Database::query($sql_insert_audio);
  625. }
  626. return $new_item_id;
  627. }
  628. /**
  629. * Static admin function allowing addition of a learnpath to a course.
  630. * @param string Course code
  631. * @param string Learnpath name
  632. * @param string Learnpath description string, if provided
  633. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  634. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  635. * @param string Zip file containing the learnpath or directory containing the learnpath
  636. * @return integer The new learnpath ID on success, 0 on failure
  637. */
  638. public static function add_lp(
  639. $course,
  640. $name,
  641. $description = '',
  642. $learnpath = 'guess',
  643. $origin = 'zip',
  644. $zipname = '',
  645. $publicated_on = '',
  646. $expired_on = '',
  647. $category_id = 0
  648. ) {
  649. global $charset;
  650. $course_id = api_get_course_int_id();
  651. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  652. // Check course code exists.
  653. // Check lp_name doesn't exist, otherwise append something.
  654. $i = 0;
  655. $name = Database::escape_string($name);
  656. $category_id = intval($category_id);
  657. // Session id.
  658. $session_id = api_get_session_id();
  659. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  660. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  661. $res_name = Database::query($check_name);
  662. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  663. //by default the publication date is the same that the creation date
  664. //The behaviour above was changed due BT#2800
  665. global $_custom;
  666. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  667. $publicated_on = '';
  668. } else {
  669. $publicated_on = api_get_utc_datetime();
  670. }
  671. } else {
  672. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  673. }
  674. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  675. $expired_on = '';
  676. } else {
  677. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  678. }
  679. while (Database :: num_rows($res_name)) {
  680. // There is already one such name, update the current one a bit.
  681. $i++;
  682. $name = $name.' - '.$i;
  683. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  684. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  685. $res_name = Database::query($check_name);
  686. }
  687. // New name does not exist yet; keep it.
  688. // Escape description.
  689. $description = Database::escape_string(
  690. api_htmlentities($description, ENT_QUOTES, $charset)
  691. ); // Kevin: added htmlentities().
  692. $type = 1;
  693. switch ($learnpath) {
  694. case 'guess':
  695. break;
  696. case 'dokeos':
  697. case 'chamilo':
  698. $type = 1;
  699. break;
  700. case 'aicc':
  701. break;
  702. }
  703. switch ($origin) {
  704. case 'zip':
  705. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  706. break;
  707. case 'manual':
  708. default:
  709. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  710. $res_max = Database::query($get_max);
  711. if (Database :: num_rows($res_max) < 1) {
  712. $dsp = 1;
  713. } else {
  714. $row = Database :: fetch_array($res_max);
  715. $dsp = $row[0] + 1;
  716. }
  717. $sql_insert = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id) ".
  718. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime(
  719. )."' , '".$publicated_on."' , '".$expired_on."', $category_id)";
  720. Database::query($sql_insert);
  721. $id = Database :: insert_id();
  722. if ($id > 0) {
  723. $course_info = api_get_course_info();
  724. // Insert into item_property.
  725. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  726. api_set_default_visibility($course_info, $id, TOOL_LEARNPATH);
  727. return $id;
  728. }
  729. break;
  730. }
  731. }
  732. /**
  733. * Appends a message to the message attribute
  734. * @param string Message to append.
  735. */
  736. public function append_message($string)
  737. {
  738. if ($this->debug > 0) {
  739. error_log('New LP - In learnpath::append_message()', 0);
  740. }
  741. $this->message .= $string;
  742. }
  743. /**
  744. * Autocompletes the parents of an item in case it's been completed or passed
  745. * @param integer Optional ID of the item from which to look for parents
  746. */
  747. public function autocomplete_parents($item)
  748. {
  749. $debug = $this->debug;
  750. if ($debug) {
  751. error_log('Learnpath::autocomplete_parents()', 0);
  752. }
  753. if (empty($item)) {
  754. $item = $this->current;
  755. }
  756. $parent_id = $this->items[$item]->get_parent();
  757. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  758. // if $item points to an object and there is a parent.
  759. if ($debug) {
  760. error_log('Autocompleting parent of item '.$item.' (item '.$parent_id.')', 0);
  761. }
  762. $current_item = $this->items[$item];
  763. $parent = $this->items[$parent_id]; // Get the parent.
  764. // New experiment including failed and browsed in completed status.
  765. $current_status = $current_item->get_status();
  766. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  767. // If the current item is completed or passes or succeeded.
  768. $completed = true;
  769. if ($debug) {
  770. error_log('Status of current item is alright', 0);
  771. }
  772. foreach ($parent->get_children() as $child) {
  773. // Check all his brothers (parent's children) for completion status.
  774. if ($child != $item) {
  775. if ($debug) {
  776. error_log(
  777. 'Looking at brother with ID '.$child.', status is '.$this->items[$child]->get_status(),
  778. 0
  779. );
  780. }
  781. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  782. // Trying completing parents of failed and browsed items as well.
  783. if ($this->items[$child]->status_is(
  784. array(
  785. 'completed',
  786. 'passed',
  787. 'succeeded',
  788. 'browsed',
  789. 'failed'
  790. )
  791. )
  792. ) {
  793. // Keep completion status to true.
  794. } else {
  795. if ($this->debug > 2) {
  796. error_log(
  797. 'Found one incomplete child of '.$parent_id.': '.$child.' is '.$this->items[$child]->get_status(
  798. ),
  799. 0
  800. );
  801. }
  802. $completed = false;
  803. }
  804. }
  805. }
  806. if ($completed) { // If all the children were completed:
  807. $parent->set_status('completed');
  808. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  809. $this->update_queue[$parent->get_id()] = $parent->get_status();
  810. if ($debug) {
  811. error_log('Added parent to update queue '.print_r($this->update_queue, true), 0);
  812. }
  813. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  814. }
  815. } else {
  816. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  817. }
  818. } else {
  819. if ($debug) {
  820. error_log("#$item is an item doesn't have parents");
  821. }
  822. }
  823. }
  824. /**
  825. * Autosaves the current results into the database for the whole learnpath
  826. */
  827. public function autosave()
  828. {
  829. if ($this->debug > 0) {
  830. error_log('New LP - In learnpath::autosave()', 0);
  831. }
  832. // TODO: Add save operations for the learnpath itself.
  833. }
  834. /**
  835. * Clears the message attribute
  836. */
  837. public function clear_message()
  838. {
  839. if ($this->debug > 0) {
  840. error_log('New LP - In learnpath::clear_message()', 0);
  841. }
  842. $this->message = '';
  843. }
  844. /**
  845. * Closes the current resource
  846. *
  847. * Stops the timer
  848. * Saves into the database if required
  849. * Clears the current resource data from this object
  850. * @return boolean True on success, false on failure
  851. */
  852. public function close()
  853. {
  854. $course_id = api_get_course_int_id();
  855. if ($this->debug > 0) {
  856. error_log('New LP - In learnpath::close()', 0);
  857. }
  858. if (empty ($this->lp_id)) {
  859. $this->error = 'Trying to close this learnpath but no ID is set';
  860. return false;
  861. }
  862. $this->current_time_stop = time();
  863. if ($this->save) {
  864. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  865. /*
  866. $sql = "UPDATE $learnpath_view_table " .
  867. "SET " .
  868. "stop_time = ".$this->current_time_stop.", " .
  869. "score = ".$this->current_score.", ".
  870. "WHERE learnpath_id = '".$this->lp_id."'";
  871. //$res = Database::query($sql);
  872. $res = Database::query($res);
  873. if (Database::affected_rows($res) < 1) {
  874. $this->error = 'Could not update learnpath_view table while closing learnpath';
  875. return false;
  876. }
  877. */
  878. }
  879. $this->ordered_items = array();
  880. $this->index = 0;
  881. unset ($this->lp_id);
  882. //unset other stuff
  883. return true;
  884. }
  885. /**
  886. * Static admin function allowing removal of a learnpath
  887. * @param string Course code
  888. * @param integer Learnpath ID
  889. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  890. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  891. */
  892. public function delete($course = null, $id = null, $delete = 'keep')
  893. {
  894. $course_id = api_get_course_int_id();
  895. // TODO: Implement a way of getting this to work when the current object is not set.
  896. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  897. //if (empty($course)) { $course = api_get_course_id(); }
  898. //if (empty($id)) { $id = $this->get_id(); }
  899. // If an ID is specifically given and the current LP is not the same, prevent delete.
  900. if (!empty ($id) && ($id != $this->lp_id)) {
  901. return false;
  902. }
  903. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  904. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  905. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  906. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  907. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  908. // Delete lp item id.
  909. foreach ($this->items as $id => $dummy) {
  910. //$this->items[$id]->delete();
  911. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  912. $res_del_item_view = Database::query($sql_del_view);
  913. }
  914. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  915. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  916. $res_del_item = Database::query($sql_del_item);
  917. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  918. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  919. $res_del_view = Database::query($sql_del_view);
  920. self::toggle_publish($this->lp_id, 'i');
  921. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  922. if ($this->type == 2 || $this->type == 3) {
  923. // This is a scorm learning path, delete the files as well.
  924. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  925. $res = Database::query($sql);
  926. if (Database :: num_rows($res) > 0) {
  927. $row = Database :: fetch_array($res);
  928. $path = $row['path'];
  929. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  930. $res = Database::query($sql);
  931. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  932. if ($this->debug > 2) {
  933. error_log(
  934. 'New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory',
  935. 0
  936. );
  937. }
  938. } else {
  939. // No other LP uses that directory, delete it.
  940. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  941. $course_scorm_dir = api_get_path(
  942. SYS_COURSE_PATH
  943. ).$course_rel_dir; // The absolute system path for this course.
  944. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) and !empty ($course_scorm_dir)) {
  945. if ($this->debug > 2) {
  946. error_log(
  947. 'New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path,
  948. 0
  949. );
  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. api_rmdirr($course_scorm_dir.$path);
  957. }
  958. }
  959. }
  960. }
  961. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  962. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  963. $res_del_lp = Database::query($sql_del_lp);
  964. $this->update_display_order(); // Updates the display order of all lps.
  965. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  966. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  967. // Delete link of gradebook tool
  968. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  969. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  970. $result = Database::query($sql);
  971. $row = Database :: fetch_array($result, 'ASSOC');*/
  972. // Fixing gradebook link deleted see #5229.
  973. /*
  974. if (!empty($row['id'])) {
  975. $link = LinkFactory :: load($row['id']);
  976. if ($link[0] != null) {
  977. $link[0]->delete();
  978. }
  979. }*/
  980. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  981. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4, $id, api_get_session_id());
  982. if ($link_info !== false) {
  983. remove_resource_from_course_gradebook($link_info['id']);
  984. }
  985. if (api_get_setting('search_enabled') == 'true') {
  986. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  987. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  988. }
  989. }
  990. /**
  991. * Removes all the children of one item - dangerous!
  992. * @param integer Element ID of which children have to be removed
  993. * @return integer Total number of children removed
  994. */
  995. public function delete_children_items($id)
  996. {
  997. $course_id = api_get_course_int_id();
  998. if ($this->debug > 0) {
  999. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  1000. }
  1001. $num = 0;
  1002. if (empty ($id) || $id != strval(intval($id))) {
  1003. return false;
  1004. }
  1005. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1006. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1007. $res = Database::query($sql);
  1008. while ($row = Database :: fetch_array($res)) {
  1009. $num += $this->delete_children_items($row['id']);
  1010. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1011. $res_del = Database::query($sql_del);
  1012. $num++;
  1013. }
  1014. return $num;
  1015. }
  1016. /**
  1017. * Removes an item from the current learnpath
  1018. * @param integer Elem ID (0 if first)
  1019. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  1020. * @return integer Number of elements moved
  1021. * @todo implement resource removal
  1022. */
  1023. public function delete_item($id, $remove = 'keep')
  1024. {
  1025. $course_id = api_get_course_int_id();
  1026. if ($this->debug > 0) {
  1027. error_log('New LP - In learnpath::delete_item()', 0);
  1028. }
  1029. // TODO: Implement the resource removal.
  1030. if (empty ($id) || $id != strval(intval($id))) {
  1031. return false;
  1032. }
  1033. // First select item to get previous, next, and display order.
  1034. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1035. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1036. $res_sel = Database::query($sql_sel);
  1037. if (Database :: num_rows($res_sel) < 1) {
  1038. return false;
  1039. }
  1040. $row = Database :: fetch_array($res_sel);
  1041. $previous = $row['previous_item_id'];
  1042. $next = $row['next_item_id'];
  1043. $display = $row['display_order'];
  1044. $parent = $row['parent_item_id'];
  1045. $lp = $row['lp_id'];
  1046. // Delete children items.
  1047. $num = $this->delete_children_items($id);
  1048. if ($this->debug > 2) {
  1049. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1050. }
  1051. // Now delete the item.
  1052. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1053. if ($this->debug > 2) {
  1054. error_log('New LP - Deleting item: '.$sql_del, 0);
  1055. }
  1056. $res_del = Database::query($sql_del);
  1057. // Now update surrounding items.
  1058. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  1059. $res_upd = Database::query($sql_upd);
  1060. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  1061. $res_upd = 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 WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1064. $res_all = Database::query($sql_all);
  1065. //Removing prerequisites since the item will not longer exist
  1066. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1067. $res_all = Database::query($sql_all);
  1068. // Remove from search engine if enabled.
  1069. if (api_get_setting('search_enabled') == 'true') {
  1070. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1071. $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';
  1072. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1073. $res = Database::query($sql);
  1074. if (Database :: num_rows($res) > 0) {
  1075. $row2 = Database :: fetch_array($res);
  1076. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1077. $di = new ChamiloIndexer();
  1078. $di->remove_document((int)$row2['search_did']);
  1079. }
  1080. $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';
  1081. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1082. Database::query($sql);
  1083. }
  1084. }
  1085. /**
  1086. * Updates an item's content in place
  1087. * @param integer Element ID
  1088. * @param integer Parent item ID
  1089. * @param integer Previous item ID
  1090. * @param string Item title
  1091. * @param string Item description
  1092. * @param string Prerequisites (optional)
  1093. * @param string Indexing terms (optional)
  1094. * @param array The array resulting of the $_FILES[mp3] element
  1095. * @return boolean True on success, false on error
  1096. */
  1097. public function edit_item(
  1098. $id,
  1099. $parent,
  1100. $previous,
  1101. $title,
  1102. $description,
  1103. $prerequisites = 0,
  1104. $audio = null,
  1105. $max_time_allowed = 0
  1106. ) {
  1107. $course_id = api_get_course_int_id();
  1108. if ($this->debug > 0) {
  1109. error_log('New LP - In learnpath::edit_item()', 0);
  1110. }
  1111. if (empty ($max_time_allowed)) {
  1112. $max_time_allowed = 0;
  1113. }
  1114. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1115. return false;
  1116. }
  1117. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1118. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1119. $res_select = Database::query($sql_select);
  1120. $row_select = Database :: fetch_array($res_select);
  1121. $audio_update_sql = '';
  1122. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1123. // Create the audio folder if it does not exist yet.
  1124. $_course = api_get_course_info();
  1125. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1126. if (!is_dir($filepath.'audio')) {
  1127. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1128. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  1129. api_item_property_update(
  1130. $_course,
  1131. TOOL_DOCUMENT,
  1132. $audio_id,
  1133. 'FolderCreated',
  1134. api_get_user_id(),
  1135. null,
  1136. null,
  1137. null,
  1138. null,
  1139. api_get_session_id()
  1140. );
  1141. api_item_property_update(
  1142. $_course,
  1143. TOOL_DOCUMENT,
  1144. $audio_id,
  1145. 'invisible',
  1146. api_get_user_id(),
  1147. null,
  1148. null,
  1149. null,
  1150. null,
  1151. api_get_session_id()
  1152. );
  1153. }
  1154. // Upload file in documents.
  1155. $pi = pathinfo($audio['name']);
  1156. if ($pi['extension'] == 'mp3') {
  1157. $c_det = api_get_course_info($this->cc);
  1158. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1159. $path = FileManager::handle_uploaded_document(
  1160. $c_det,
  1161. $audio,
  1162. $bp,
  1163. '/audio',
  1164. api_get_user_id(),
  1165. 0,
  1166. null,
  1167. 0,
  1168. 'rename',
  1169. false,
  1170. 0
  1171. );
  1172. $path = substr($path, 7);
  1173. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1174. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1175. }
  1176. }
  1177. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1178. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1179. // TODO: htmlspecialchars to be checked for encoding related problems.
  1180. if ($same_parent && $same_previous) {
  1181. // Only update title and description.
  1182. $sql_update = " UPDATE ".$tbl_lp_item."
  1183. SET title = '".Database::escape_string($title)."',
  1184. prerequisite = '".$prerequisites."',
  1185. description = '".Database::escape_string($description)."'
  1186. ".$audio_update_sql.",
  1187. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1188. WHERE c_id = ".$course_id." AND id = ".$id;
  1189. $res_update = Database::query($sql_update);
  1190. } else {
  1191. $old_parent = $row_select['parent_item_id'];
  1192. $old_previous = $row_select['previous_item_id'];
  1193. $old_next = $row_select['next_item_id'];
  1194. $old_order = $row_select['display_order'];
  1195. $old_prerequisite = $row_select['prerequisite'];
  1196. $old_max_time_allowed = $row_select['max_time_allowed'];
  1197. /* BEGIN -- virtually remove the current item id */
  1198. /* for the next and previous item it is like the current item doesn't exist anymore */
  1199. if ($old_previous != 0) {
  1200. $sql_update_next = "
  1201. UPDATE ".$tbl_lp_item."
  1202. SET next_item_id = ".$old_next."
  1203. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1204. $res_update_next = Database::query($sql_update_next);
  1205. //echo '<p>' . $sql_update_next . '</p>';
  1206. }
  1207. if ($old_next != 0) {
  1208. $sql_update_previous = "
  1209. UPDATE ".$tbl_lp_item."
  1210. SET previous_item_id = ".$old_previous."
  1211. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1212. $res_update_previous = Database::query($sql_update_previous);
  1213. //echo '<p>' . $sql_update_previous . '</p>';
  1214. }
  1215. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1216. $sql_update_order = "
  1217. UPDATE ".$tbl_lp_item."
  1218. SET display_order = display_order - 1
  1219. WHERE
  1220. c_id = ".$course_id." AND
  1221. display_order > ".$old_order." AND lp_id = ".$this->lp_id." AND
  1222. parent_item_id = ".$old_parent;
  1223. $res_update_order = Database::query($sql_update_order);
  1224. //echo '<p>' . $sql_update_order . '</p>';
  1225. /* END -- virtually remove the current item id */
  1226. /* BEGIN -- update the current item id to his new location */
  1227. if ($previous == 0) {
  1228. // Select the data of the item that should come after the current item.
  1229. $sql_select_old = "SELECT id, display_order
  1230. FROM ".$tbl_lp_item."
  1231. WHERE
  1232. c_id = ".$course_id." AND
  1233. lp_id = ".$this->lp_id." AND
  1234. parent_item_id = ".$parent." AND
  1235. previous_item_id = ".$previous;
  1236. $res_select_old = Database::query($sql_select_old);
  1237. $row_select_old = Database :: fetch_array($res_select_old);
  1238. //echo '<p>' . $sql_select_old . '</p>';
  1239. // If the new parent didn't have children before.
  1240. if (Database :: num_rows($res_select_old) == 0) {
  1241. $new_next = 0;
  1242. $new_order = 1;
  1243. } else {
  1244. $new_next = $row_select_old['id'];
  1245. $new_order = $row_select_old['display_order'];
  1246. }
  1247. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1248. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1249. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1250. } else {
  1251. // Select the data of the item that should come before the current item.
  1252. $sql_select_old = " SELECT next_item_id, display_order
  1253. FROM ".$tbl_lp_item."
  1254. WHERE c_id = ".$course_id." AND id = ".$previous;
  1255. $res_select_old = Database::query($sql_select_old);
  1256. $row_select_old = Database :: fetch_array($res_select_old);
  1257. //echo '<p>' . $sql_select_old . '</p>';
  1258. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1259. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1260. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1261. $new_next = $row_select_old['next_item_id'];
  1262. $new_order = $row_select_old['display_order'] + 1;
  1263. }
  1264. // TODO: htmlspecialchars to be checked for encoding related problems.
  1265. // Update the current item with the new data.
  1266. $sql_update = "UPDATE ".$tbl_lp_item."
  1267. SET
  1268. title = '".Database::escape_string($title)."',
  1269. description = '".Database::escape_string($description)."',
  1270. parent_item_id = ".$parent.",
  1271. previous_item_id = ".$previous.",
  1272. next_item_id = ".$new_next.",
  1273. display_order = ".$new_order."
  1274. ".$audio_update_sql."
  1275. WHERE c_id = ".$course_id." AND id = ".$id;
  1276. $res_update_next = Database::query($sql_update);
  1277. //echo '<p>' . $sql_update . '</p>';
  1278. if ($previous != 0) {
  1279. // Update the previous item's next_item_id.
  1280. $sql_update_previous = "
  1281. UPDATE ".$tbl_lp_item."
  1282. SET next_item_id = ".$id."
  1283. WHERE c_id = ".$course_id." AND id = ".$previous;
  1284. $res_update_next = Database::query($sql_update_previous);
  1285. //echo '<p>' . $sql_update_previous . '</p>';
  1286. }
  1287. if ($new_next != 0) {
  1288. // Update the next item's previous_item_id.
  1289. $sql_update_next = "
  1290. UPDATE ".$tbl_lp_item."
  1291. SET previous_item_id = ".$id."
  1292. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1293. $res_update_next = Database::query($sql_update_next);
  1294. //echo '<p>' . $sql_update_next . '</p>';
  1295. }
  1296. if ($old_prerequisite != $prerequisites) {
  1297. $sql_update_next = "
  1298. UPDATE ".$tbl_lp_item."
  1299. SET prerequisite = ".$prerequisites."
  1300. WHERE c_id = ".$course_id." AND id = ".$id;
  1301. $res_update_next = Database::query($sql_update_next);
  1302. }
  1303. if ($old_max_time_allowed != $max_time_allowed) {
  1304. $sql_update_max_time_allowed = "
  1305. UPDATE ".$tbl_lp_item."
  1306. SET max_time_allowed = ".$max_time_allowed."
  1307. WHERE c_id = ".$course_id." AND id = ".$id;
  1308. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1309. }
  1310. // Update all the items with the same or a bigger display_order than the current item.
  1311. $sql_update_order = "
  1312. UPDATE ".$tbl_lp_item."
  1313. SET display_order = display_order + 1
  1314. WHERE
  1315. c_id = ".$course_id." AND
  1316. lp_id = ".$this->get_id()." AND
  1317. id <> ".$id." AND
  1318. parent_item_id = ".$parent." AND
  1319. display_order >= ".$new_order;
  1320. $res_update_next = Database::query($sql_update_order);
  1321. }
  1322. }
  1323. /**
  1324. * Updates an item's prereq in place
  1325. * @param integer Element ID
  1326. * @param string Prerequisite Element ID
  1327. * @param string Prerequisite item type
  1328. * @param string Prerequisite min score
  1329. * @param string Prerequisite max score
  1330. * @return boolean True on success, false on error
  1331. */
  1332. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1333. {
  1334. $course_id = api_get_course_int_id();
  1335. if ($this->debug > 0) {
  1336. error_log(
  1337. 'New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')',
  1338. 0
  1339. );
  1340. }
  1341. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1342. return false;
  1343. }
  1344. $prerequisite_id = Database::escape_string($prerequisite_id);
  1345. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1346. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1347. $mastery_score = 0;
  1348. }
  1349. if (!is_numeric($max_score) || $max_score < 0) {
  1350. $max_score = 100;
  1351. }
  1352. if ($mastery_score > $max_score) {
  1353. $max_score = $mastery_score;
  1354. }
  1355. if (!is_numeric($prerequisite_id)) {
  1356. $prerequisite_id = 'NULL';
  1357. }
  1358. $sql_upd = " UPDATE ".$tbl_lp_item."
  1359. SET prerequisite = ".$prerequisite_id." WHERE c_id = ".$course_id." AND id = ".$id;
  1360. $res_upd = Database::query($sql_upd);
  1361. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1362. $sql_upd = " UPDATE ".$tbl_lp_item." SET
  1363. mastery_score = ".$mastery_score.
  1364. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1365. " WHERE c_id = ".$course_id." AND ref = '".$prerequisite_id."'"; // Will this be enough to ensure unicity?
  1366. Database::query($sql_upd);
  1367. }
  1368. // TODO: Update the item object (can be ignored for now because refreshed).
  1369. return true;
  1370. }
  1371. /**
  1372. * Escapes a string with the available database escape function
  1373. * @param string String to escape
  1374. * @return string String escaped
  1375. * @deprecated use Database::escape_string
  1376. */
  1377. public function escape_string($string)
  1378. {
  1379. return Database::escape_string($string);
  1380. }
  1381. /**
  1382. * Static admin function exporting a learnpath into a zip file
  1383. * @param string Export type (scorm, zip, cd)
  1384. * @param string Course code
  1385. * @param integer Learnpath ID
  1386. * @param string Zip file name
  1387. * @return string Zip file path (or false on error)
  1388. */
  1389. public function export_lp($type, $course, $id, $zipname)
  1390. {
  1391. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1392. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1393. return false;
  1394. }
  1395. $url = '';
  1396. switch ($type) {
  1397. case 'scorm':
  1398. break;
  1399. case 'zip':
  1400. break;
  1401. case 'cdrom':
  1402. break;
  1403. }
  1404. return $url;
  1405. }
  1406. /**
  1407. * Gets all the chapters belonging to the same parent as the item/chapter given
  1408. * Can also be called as abstract method
  1409. * @param integer Item ID
  1410. * @return array A list of all the "brother items" (or an empty array on failure)
  1411. */
  1412. public function get_brother_chapters($id)
  1413. {
  1414. $course_id = api_get_course_int_id();
  1415. if ($this->debug > 0) {
  1416. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1417. }
  1418. if (empty ($id) OR $id != strval(intval($id))) {
  1419. return array();
  1420. }
  1421. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1422. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1423. $res_parent = Database::query($sql_parent);
  1424. if (Database :: num_rows($res_parent) > 0) {
  1425. $row_parent = Database :: fetch_array($res_parent);
  1426. $parent = $row_parent['parent_item_id'];
  1427. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent AND id = $id AND item_type='dokeos_chapter' ORDER BY display_order";
  1428. $res_bros = Database::query($sql_bros);
  1429. $list = array();
  1430. while ($row_bro = Database :: fetch_array($res_bros)) {
  1431. $list[] = $row_bro;
  1432. }
  1433. return $list;
  1434. }
  1435. return array();
  1436. }
  1437. /**
  1438. * Gets all the items belonging to the same parent as the item given
  1439. * Can also be called as abstract method
  1440. * @param integer Item ID
  1441. * @return array A list of all the "brother items" (or an empty array on failure)
  1442. */
  1443. public function get_brother_items($id)
  1444. {
  1445. $course_id = api_get_course_int_id();
  1446. if ($this->debug > 0) {
  1447. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1448. }
  1449. if (empty ($id) OR $id != strval(intval($id))) {
  1450. return array();
  1451. }
  1452. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1453. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1454. $res_parent = Database::query($sql_parent);
  1455. if (Database :: num_rows($res_parent) > 0) {
  1456. $row_parent = Database :: fetch_array($res_parent);
  1457. $parent = $row_parent['parent_item_id'];
  1458. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1459. $res_bros = Database::query($sql_bros);
  1460. $list = array();
  1461. while ($row_bro = Database :: fetch_array($res_bros)) {
  1462. $list[] = $row_bro;
  1463. }
  1464. return $list;
  1465. }
  1466. return array();
  1467. }
  1468. /**
  1469. * Get the specific prefix index terms of this learning path
  1470. * @return array Array of terms
  1471. */
  1472. public function get_common_index_terms_by_prefix($prefix)
  1473. {
  1474. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1475. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  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. foreach ($this->items as $id => $dummy) {
  1495. // Trying failed and browsed considered "progressed" as well.
  1496. if ($this->items[$id]->status_is(
  1497. array(
  1498. 'completed',
  1499. 'passed',
  1500. 'succeeded',
  1501. 'browsed',
  1502. 'failed'
  1503. )
  1504. ) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir'
  1505. ) {
  1506. $i++;
  1507. }
  1508. }
  1509. return $i;
  1510. }
  1511. /**
  1512. * Gets the current item ID
  1513. * @return integer The current learnpath item id
  1514. */
  1515. public function get_current_item_id()
  1516. {
  1517. $current = 0;
  1518. if ($this->debug > 0) {
  1519. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1520. }
  1521. if (!empty ($this->current)) {
  1522. $current = $this->current;
  1523. }
  1524. if ($this->debug > 2) {
  1525. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1526. }
  1527. return $current;
  1528. }
  1529. /**
  1530. * Force to get the first learnpath item id
  1531. * @return integer The current learnpath item id
  1532. */
  1533. public function get_first_item_id()
  1534. {
  1535. $current = 0;
  1536. if (is_array($this->ordered_items)) {
  1537. $current = $this->ordered_items[0];
  1538. }
  1539. return $current;
  1540. }
  1541. /**
  1542. * Gets the total number of items available for viewing in this SCORM
  1543. * @return integer The total number of items
  1544. */
  1545. public function get_total_items_count()
  1546. {
  1547. if ($this->debug > 0) {
  1548. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1549. }
  1550. return count($this->items);
  1551. }
  1552. /**
  1553. * Gets the total number of items available for viewing in this SCORM but without chapters
  1554. * @return integer The total no-chapters number of items
  1555. */
  1556. public function get_total_items_count_without_chapters()
  1557. {
  1558. if ($this->debug > 0) {
  1559. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1560. }
  1561. $total = 0;
  1562. foreach ($this->items as $temp2) {
  1563. if (!in_array(
  1564. $temp2->get_type(),
  1565. array(
  1566. 'dokeos_chapter',
  1567. 'chapter',
  1568. 'dir'
  1569. )
  1570. )
  1571. ) {
  1572. $total++;
  1573. }
  1574. }
  1575. return $total;
  1576. }
  1577. /**
  1578. * Gets the first element URL.
  1579. * @return string URL to load into the viewer
  1580. */
  1581. public function first()
  1582. {
  1583. if ($this->debug > 0) {
  1584. error_log('New LP - In learnpath::first()', 0);
  1585. error_log('$this->last_item_seen '.$this->last_item_seen);
  1586. //error_log('$this->items '.print_r($this->items, 1));
  1587. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1588. }
  1589. // Test if the last_item_seen exists and is not a dir.
  1590. if (count($this->ordered_items) == 0) {
  1591. $this->index = 0;
  1592. }
  1593. if ($this->debug > 0) {
  1594. if (isset($this->items[$this->last_item_seen])) {
  1595. $status = $this->items[$this->last_item_seen]->get_status();
  1596. }
  1597. error_log('status '.$status);
  1598. }
  1599. if (!empty($this->last_item_seen) &&
  1600. !empty($this->items[$this->last_item_seen]) &&
  1601. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1602. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1603. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1604. //&& !$this->items[$this->last_item_seen]->is_done()
  1605. ) {
  1606. if ($this->debug > 2) {
  1607. error_log(
  1608. 'New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(
  1609. ),
  1610. 0
  1611. );
  1612. }
  1613. $index = -1;
  1614. foreach ($this->ordered_items as $myindex => $item_id) {
  1615. if ($item_id == $this->last_item_seen) {
  1616. $index = $myindex;
  1617. break;
  1618. }
  1619. }
  1620. if ($index == -1) {
  1621. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1622. if ($this->debug > 2) {
  1623. error_log(
  1624. 'New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!',
  1625. 0
  1626. );
  1627. }
  1628. return false;
  1629. } else {
  1630. $this->last = $this->last_item_seen;
  1631. $this->current = $this->last_item_seen;
  1632. $this->index = $index;
  1633. }
  1634. } else {
  1635. if ($this->debug > 2) {
  1636. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1637. }
  1638. $index = 0;
  1639. // Loop through all ordered items and stop at the first item that is
  1640. // not a directory *and* that has not been completed yet.
  1641. while (!empty($this->ordered_items[$index]) AND
  1642. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1643. (
  1644. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1645. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1646. $this->items[$this->ordered_items[$index]]->is_done() === true
  1647. ) AND $index < $this->max_ordered_items) {
  1648. $index++;
  1649. }
  1650. $this->last = $this->current;
  1651. // current is
  1652. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1653. $this->index = $index;
  1654. if ($this->debug > 2) {
  1655. error_log('$index '.$index);
  1656. }
  1657. if ($this->debug > 2) {
  1658. error_log(
  1659. 'New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')',
  1660. 0
  1661. );
  1662. }
  1663. }
  1664. if ($this->debug > 2) {
  1665. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1666. }
  1667. }
  1668. /**
  1669. * Gets the information about an item in a format usable as JavaScript to update
  1670. * the JS API by just printing this content into the <head> section of the message frame
  1671. * @param integer Item ID
  1672. * @return string
  1673. */
  1674. public function get_js_info($item_id = '')
  1675. {
  1676. if ($this->debug > 0) {
  1677. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1678. }
  1679. $info = '';
  1680. $item_id = Database::escape_string($item_id);
  1681. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1682. //if item is defined, return values from DB
  1683. $oItem = $this->items[$item_id];
  1684. $info .= '<script language="javascript">';
  1685. $info .= "top.set_score(".$oItem->get_score().");\n";
  1686. $info .= "top.set_max(".$oItem->get_max().");\n";
  1687. $info .= "top.set_min(".$oItem->get_min().");\n";
  1688. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1689. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1690. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1691. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1692. $info .= "top.set_flag_synchronized();";
  1693. $info .= '</script>';
  1694. if ($this->debug > 2) {
  1695. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1696. }
  1697. return $info;
  1698. } else {
  1699. // If item_id is empty, just update to default SCORM data.
  1700. $info .= '<script language="javascript">';
  1701. $info .= "top.set_score(".learnpathItem :: get_score().");\n";
  1702. $info .= "top.set_max(".learnpathItem :: get_max().");\n";
  1703. $info .= "top.set_min(".learnpathItem :: get_min().");\n";
  1704. $info .= "top.set_lesson_status('".learnpathItem :: get_status()."');";
  1705. $info .= "top.set_session_time('".learnpathItem :: get_scorm_time('js')."');";
  1706. $info .= "top.set_suspend_data('".learnpathItem :: get_suspend_data()."');";
  1707. $info .= "top.set_saved_lesson_status('".learnpathItem :: get_status()."');";
  1708. $info .= "top.set_flag_synchronized();";
  1709. $info .= '</script>';
  1710. if ($this->debug > 2) {
  1711. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1712. }
  1713. return $info;
  1714. }
  1715. }
  1716. /**
  1717. * Gets the js library from the database
  1718. * @return string The name of the javascript library to be used
  1719. */
  1720. public function get_js_lib()
  1721. {
  1722. $lib = '';
  1723. if (!empty ($this->js_lib)) {
  1724. $lib = $this->js_lib;
  1725. }
  1726. return $lib;
  1727. }
  1728. /**
  1729. * Gets the learnpath database ID
  1730. * @return integer Learnpath ID in the lp table
  1731. */
  1732. public function get_id()
  1733. {
  1734. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1735. if (!empty ($this->lp_id)) {
  1736. return $this->lp_id;
  1737. } else {
  1738. return 0;
  1739. }
  1740. }
  1741. /**
  1742. * Gets the last element URL.
  1743. * @return string URL to load into the viewer
  1744. */
  1745. public function get_last()
  1746. {
  1747. if ($this->debug > 0) {
  1748. error_log('New LP - In learnpath::get_last()', 0);
  1749. }
  1750. $this->index = count($this->ordered_items) - 1;
  1751. return $this->ordered_items[$this->index];
  1752. }
  1753. /**
  1754. * Gets the navigation bar for the learnpath display screen
  1755. * @return string The HTML string to use as a navigation bar
  1756. */
  1757. public function get_navigation_bar()
  1758. {
  1759. if ($this->debug > 0) {
  1760. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1761. }
  1762. $navbar = null;
  1763. $lp_id = $this->lp_id;
  1764. $mycurrentitemid = $this->get_current_item_id();
  1765. if ($this->mode == 'fullscreen') {
  1766. $navbar = '
  1767. <div class="buttons">
  1768. <a href="lp_controller.php?action=stats&'.api_get_cidreq(
  1769. true
  1770. ).'&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/lp_stats.gif" title="'.get_lang(
  1771. 'Reporting'
  1772. ).'"></a>
  1773. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="'.get_lang(
  1774. 'ScormPrevious'
  1775. ).'"></a>
  1776. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="'.get_lang(
  1777. 'ScormNext'
  1778. ).'"></a>.
  1779. <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(
  1780. 'ScormExitFullScreen'
  1781. ).'"></a>
  1782. </div>';
  1783. } else {
  1784. $navbar = '
  1785. <div class="buttons">
  1786. <a href="lp_controller.php?action=stats&'.api_get_cidreq(
  1787. true
  1788. ).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="'.get_lang(
  1789. 'Reporting'
  1790. ).'"></a>
  1791. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="'.get_lang(
  1792. 'ScormPrevious'
  1793. ).'"></a>
  1794. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="'.get_lang(
  1795. 'ScormNext'
  1796. ).'"></a>
  1797. </div>';
  1798. }
  1799. return $navbar;
  1800. }
  1801. /**
  1802. * Gets the next resource in queue (url).
  1803. * @return string URL to load into the viewer
  1804. */
  1805. public function get_next_index()
  1806. {
  1807. if ($this->debug > 0) {
  1808. error_log('New LP - In learnpath::get_next_index()', 0);
  1809. }
  1810. // TODO
  1811. $index = $this->index;
  1812. $index++;
  1813. if ($this->debug > 2) {
  1814. error_log(
  1815. 'New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type,
  1816. 0
  1817. );
  1818. }
  1819. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1820. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type(
  1821. ) == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1822. $index++;
  1823. if ($index == $this->max_ordered_items) {
  1824. if ($this->items[$this->ordered_items[$index]]->get_type(
  1825. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter'
  1826. ) {
  1827. return $this->index;
  1828. } else {
  1829. return $index;
  1830. }
  1831. }
  1832. }
  1833. if (empty ($this->ordered_items[$index])) {
  1834. return $this->index;
  1835. }
  1836. if ($this->debug > 2) {
  1837. error_log('New LP - index is now '.$index, 0);
  1838. }
  1839. return $index;
  1840. }
  1841. /**
  1842. * Gets item_id for the next element
  1843. * @return integer Next item (DB) ID
  1844. */
  1845. public function get_next_item_id()
  1846. {
  1847. if ($this->debug > 0) {
  1848. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1849. }
  1850. $new_index = $this->get_next_index();
  1851. if (!empty ($new_index)) {
  1852. if (isset ($this->ordered_items[$new_index])) {
  1853. if ($this->debug > 2) {
  1854. error_log(
  1855. 'New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index],
  1856. 0
  1857. );
  1858. }
  1859. return $this->ordered_items[$new_index];
  1860. }
  1861. }
  1862. if ($this->debug > 2) {
  1863. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1864. }
  1865. return 0;
  1866. }
  1867. /**
  1868. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1869. *
  1870. * Generally, the package provided is in the form of a zip file, so the function
  1871. * has been written to test a zip file. If not a zip, the function will return the
  1872. * default return value: ''
  1873. * @param string the path to the file
  1874. * @param string the original name of the file
  1875. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1876. */
  1877. public static function get_package_type($file_path, $file_name)
  1878. {
  1879. // Get name of the zip file without the extension.
  1880. $file_info = pathinfo($file_name);
  1881. $filename = $file_info['basename']; // Name including extension.
  1882. $extension = $file_info['extension']; // Extension only.
  1883. if (!empty($_POST['ppt2lp']) && !in_array(
  1884. strtolower($extension),
  1885. array(
  1886. 'dll',
  1887. 'exe'
  1888. )
  1889. )
  1890. ) {
  1891. return 'oogie';
  1892. }
  1893. if (!empty($_POST['woogie']) && !in_array(
  1894. strtolower($extension),
  1895. array(
  1896. 'dll',
  1897. 'exe'
  1898. )
  1899. )
  1900. ) {
  1901. return 'woogie';
  1902. }
  1903. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  1904. $zipFile = new PclZip($file_path);
  1905. // Check the zip content (real size and file extension).
  1906. $zipContentArray = $zipFile->listContent();
  1907. $package_type = '';
  1908. $at_root = false;
  1909. $manifest = '';
  1910. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1911. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1912. foreach ($zipContentArray as $thisContent) {
  1913. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1914. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1915. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1916. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1917. $package_type = 'scorm';
  1918. break; // Exit the foreach loop.
  1919. } elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1920. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1921. $package_type = 'aicc';
  1922. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1923. } else {
  1924. $package_type = '';
  1925. }
  1926. }
  1927. }
  1928. return $package_type;
  1929. }
  1930. /**
  1931. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1932. * @return string URL to load into the viewer
  1933. */
  1934. public function get_previous_index()
  1935. {
  1936. if ($this->debug > 0) {
  1937. error_log('New LP - In learnpath::get_previous_index()', 0);
  1938. }
  1939. $index = $this->index;
  1940. if (isset ($this->ordered_items[$index - 1])) {
  1941. $index--;
  1942. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1943. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1944. $index--;
  1945. if ($index < 0) {
  1946. return $this->index;
  1947. }
  1948. }
  1949. } else {
  1950. if ($this->debug > 2) {
  1951. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1952. }
  1953. // There is no previous item.
  1954. }
  1955. return $index;
  1956. }
  1957. /**
  1958. * Gets item_id for the next element
  1959. * @return integer Previous item (DB) ID
  1960. */
  1961. public function get_previous_item_id()
  1962. {
  1963. if ($this->debug > 0) {
  1964. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1965. }
  1966. $new_index = $this->get_previous_index();
  1967. return $this->ordered_items[$new_index];
  1968. }
  1969. /**
  1970. * Gets the progress value from the progress_db attribute
  1971. * @return integer Current progress value
  1972. */
  1973. public function get_progress()
  1974. {
  1975. if ($this->debug > 0) {
  1976. error_log('New LP - In learnpath::get_progress()', 0);
  1977. }
  1978. if (!empty ($this->progress_db)) {
  1979. return $this->progress_db;
  1980. }
  1981. return 0;
  1982. }
  1983. /**
  1984. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1985. * @param integer Learnpath ID
  1986. * @param integer User ID
  1987. * @param string Mode of display ('%','abs' or 'both')
  1988. * @param string Course database name (optional, defaults to '')
  1989. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1990. * @return integer Current progress value as found in the database
  1991. */
  1992. public static function get_db_progress(
  1993. $lp_id,
  1994. $user_id,
  1995. $mode = '%',
  1996. $course_code = '',
  1997. $sincere = false,
  1998. $session_id = 0
  1999. ) {
  2000. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  2001. $session_id = intval($session_id);
  2002. $course_info = api_get_course_info($course_code);
  2003. $session_condition = api_get_session_condition($session_id);
  2004. $course_id = $course_info['real_id'];
  2005. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2006. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  2007. $res = Database::query($sql);
  2008. $view_id = 0;
  2009. if (Database :: num_rows($res) > 0) {
  2010. $row = Database :: fetch_array($res);
  2011. $progress = $row['progress'];
  2012. $view_id = $row['id'];
  2013. } else {
  2014. if ($sincere) {
  2015. return null;
  2016. }
  2017. }
  2018. if (empty ($progress)) {
  2019. $progress = '0';
  2020. }
  2021. if ($mode == '%') {
  2022. return $progress.'%';
  2023. } else {
  2024. // Get the number of items completed and the number of items total.
  2025. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2026. $sql = "SELECT count(*) FROM $tbl
  2027. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = ".$lp_id." AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  2028. $res = Database::query($sql);
  2029. $row = Database :: fetch_array($res);
  2030. $total = $row[0];
  2031. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2032. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2033. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  2034. // Trying as also counting browsed and failed items.
  2035. $sql = "SELECT count(distinct(lp_item_id))
  2036. FROM $tbl_item_view as item_view
  2037. INNER JOIN $tbl_item as item
  2038. ON item.id = item_view.lp_item_id
  2039. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  2040. WHERE
  2041. item_view.c_id = $course_id AND
  2042. item.c_id = $course_id AND
  2043. lp_view_id = ".$view_id." AND
  2044. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  2045. $res = Database::query($sql);
  2046. $row = Database :: fetch_array($res);
  2047. $completed = $row[0];
  2048. if ($mode == 'abs') {
  2049. return $completed.'/'.$total;
  2050. } elseif ($mode == 'both') {
  2051. if ($progress < ($completed / ($total ? $total : 1))) {
  2052. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2053. }
  2054. return $progress.'% ('.$completed.'/'.$total.')';
  2055. }
  2056. }
  2057. return $progress;
  2058. }
  2059. /**
  2060. * Returns the HTML necessary to print a mediaplayer block inside a page
  2061. * @return string The mediaplayer HTML
  2062. */
  2063. public function get_mediaplayer($autostart = 'true')
  2064. {
  2065. $course_id = api_get_course_int_id();
  2066. $_course = api_get_course_info();
  2067. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2068. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2069. // Getting all the information about the item.
  2070. $sql = "SELECT * FROM ".$tbl_lp_item." as lp INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id ".
  2071. "WHERE lp.id = '".$_SESSION['oLP']->current."' AND
  2072. lp.c_id = $course_id AND
  2073. lp_view.c_id = $course_id";
  2074. $result = Database::query($sql);
  2075. $row = Database::fetch_assoc($result);
  2076. $output = '';
  2077. if (!empty ($row['audio'])) {
  2078. $list = $_SESSION['oLP']->get_toc();
  2079. $type_quiz = false;
  2080. foreach ($list as $toc) {
  2081. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2082. $type_quiz = true;
  2083. }
  2084. }
  2085. if ($type_quiz) {
  2086. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2087. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2088. } else {
  2089. $autostart_audio = $autostart;
  2090. }
  2091. } else {
  2092. $autostart_audio = 'true';
  2093. }
  2094. // The mp3 player.
  2095. $output = '<div id="container">';
  2096. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  2097. $output .= '<script type="text/javascript">
  2098. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  2099. s1.addParam("allowscriptaccess","always");
  2100. s1.addParam("flashvars","file='.api_get_path(
  2101. WEB_COURSE_PATH
  2102. ).$_course['path'].'/document/audio/'.$row['audio'].'&autostart='.$autostart_audio.'");
  2103. s1.write("container");
  2104. </script>
  2105. </div>';
  2106. }
  2107. return $output;
  2108. }
  2109. /**
  2110. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  2111. * @param int Learnpath id
  2112. * @param int Student id
  2113. * @param string Course code (optional)
  2114. * @return bool True if
  2115. */
  2116. public static function is_lp_visible_for_student($lp_id, $student_id, $course_code = null)
  2117. {
  2118. $lp_id = (int)$lp_id;
  2119. $course_info = api_get_course_info($course_code);
  2120. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  2121. // Get current prerequisite
  2122. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2123. FROM $tbl_learnpath WHERE c_id = ".$course_info['real_id']." AND id = $lp_id";
  2124. $rs = Database::query($sql);
  2125. $now = time();
  2126. $session_id = api_get_session_id();
  2127. if (Database::num_rows($rs) > 0) {
  2128. $row = Database::fetch_array($rs, 'ASSOC');
  2129. $prerequisite = $row['prerequisite'];
  2130. $is_visible = true;
  2131. $progress = 0;
  2132. //Checking LP prerequisites
  2133. if (!empty($prerequisite)) {
  2134. $progress = intval(self::get_db_progress($prerequisite, $student_id, '%', '', false, $session_id));
  2135. if ($progress < 100) {
  2136. $is_visible = false;
  2137. }
  2138. }
  2139. // Also check the time availability of the LP
  2140. if ($is_visible) {
  2141. //Adding visibility restrictions
  2142. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  2143. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2144. //api_not_allowed();
  2145. $is_visible = false;
  2146. }
  2147. }
  2148. //Blocking empty start times see BT#2800
  2149. global $_custom;
  2150. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  2151. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2152. //api_not_allowed();
  2153. $is_visible = false;
  2154. }
  2155. }
  2156. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2157. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2158. //api_not_allowed();
  2159. $is_visible = false;
  2160. }
  2161. }
  2162. }
  2163. //Check if the subscription users/group to a LP is ON
  2164. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2165. //Checking only the user visibility
  2166. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2167. if ($userVisibility == 1) {
  2168. $is_visible = true;
  2169. } else {
  2170. $is_visible = false;
  2171. }
  2172. //the group visibility is not going to be checked because the user *must* be related to the LP
  2173. /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2174. if (!empty($groupList)) {
  2175. foreach($groupList as $groupId) {
  2176. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2177. if ($groupVisibility == 1) {
  2178. $is_visible = true;
  2179. break;
  2180. }
  2181. }
  2182. }*/
  2183. }
  2184. return $is_visible;
  2185. }
  2186. return false;
  2187. }
  2188. /**
  2189. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2190. * completed so far.
  2191. * @param string Mode in which we want the values
  2192. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2193. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2194. * @param boolean true if it comes from a Diplay LP view
  2195. * @return string HTML string containing the progress bar
  2196. */
  2197. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2198. {
  2199. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2200. global $lp_theme_css;
  2201. // Setting up the CSS path of the current style if exists.
  2202. if (!empty ($lp_theme_css)) {
  2203. $css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2204. } else {
  2205. $css_path = '../img/';
  2206. }
  2207. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2208. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2209. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2210. }
  2211. $text = $percentage.$text_add;
  2212. //@todo use Display::display_progress();
  2213. $output = '<div class="progress progress-striped">
  2214. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  2215. </div>
  2216. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2217. return $output;
  2218. }
  2219. /**
  2220. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2221. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2222. * @param integer Additional steps to fake as completed
  2223. * @return list Percentage or number and symbol (% or /xx)
  2224. */
  2225. public function get_progress_bar_text($mode = '', $add = 0)
  2226. {
  2227. if ($this->debug > 0) {
  2228. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2229. }
  2230. if (empty ($mode)) {
  2231. $mode = $this->progress_bar_mode;
  2232. }
  2233. $total_items = $this->get_total_items_count_without_chapters();
  2234. if ($this->debug > 2) {
  2235. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2236. }
  2237. $i = $this->get_complete_items_count();
  2238. if ($this->debug > 2) {
  2239. error_log('New LP - Items completed so far: '.$i, 0);
  2240. }
  2241. if ($add != 0) {
  2242. $i += $add;
  2243. if ($this->debug > 2) {
  2244. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2245. }
  2246. }
  2247. $text = '';
  2248. if ($i > $total_items) {
  2249. $i = $total_items;
  2250. }
  2251. if ($mode == '%') {
  2252. if ($total_items > 0) {
  2253. $percentage = ((float)$i / (float)$total_items) * 100;
  2254. } else {
  2255. $percentage = 0;
  2256. }
  2257. $percentage = number_format($percentage, 0);
  2258. $text = '%';
  2259. } elseif ($mode == 'abs') {
  2260. $percentage = $i;
  2261. $text = '/'.$total_items;
  2262. }
  2263. return array(
  2264. $percentage,
  2265. $text
  2266. );
  2267. }
  2268. /**
  2269. * Gets the progress bar mode
  2270. * @return string The progress bar mode attribute
  2271. */
  2272. public function get_progress_bar_mode()
  2273. {
  2274. if ($this->debug > 0) {
  2275. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2276. }
  2277. if (!empty ($this->progress_bar_mode)) {
  2278. return $this->progress_bar_mode;
  2279. } else {
  2280. return '%';
  2281. }
  2282. }
  2283. /**
  2284. * Gets the learnpath proximity (remote or local)
  2285. * @return string Learnpath proximity
  2286. */
  2287. public function get_proximity()
  2288. {
  2289. if ($this->debug > 0) {
  2290. error_log('New LP - In learnpath::get_proximity()', 0);
  2291. }
  2292. if (!empty ($this->proximity)) {
  2293. return $this->proximity;
  2294. } else {
  2295. return '';
  2296. }
  2297. }
  2298. /**
  2299. * Gets the learnpath theme (remote or local)
  2300. * @return string Learnpath theme
  2301. */
  2302. public function get_theme()
  2303. {
  2304. if ($this->debug > 0) {
  2305. error_log('New LP - In learnpath::get_theme()', 0);
  2306. }
  2307. if (!empty ($this->theme)) {
  2308. return $this->theme;
  2309. } else {
  2310. return '';
  2311. }
  2312. }
  2313. /**
  2314. * Gets the learnpath session id
  2315. * @return string Learnpath theme
  2316. */
  2317. public function get_lp_session_id()
  2318. {
  2319. if ($this->debug > 0) {
  2320. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2321. }
  2322. if (!empty ($this->lp_session_id)) {
  2323. return $this->lp_session_id;
  2324. } else {
  2325. return 0;
  2326. }
  2327. }
  2328. /**
  2329. * Gets the learnpath image
  2330. * @return string Web URL of the LP image
  2331. */
  2332. public function get_preview_image()
  2333. {
  2334. if ($this->debug > 0) {
  2335. error_log('New LP - In learnpath::get_preview_image()', 0);
  2336. }
  2337. if (!empty($this->preview_image)) {
  2338. return $this->preview_image;
  2339. } else {
  2340. return '';
  2341. }
  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. public function get_category_id()
  2370. {
  2371. return $this->category_id;
  2372. }
  2373. /**
  2374. * Gets the learnpath author
  2375. * @return string LP's author
  2376. */
  2377. public function get_author()
  2378. {
  2379. if ($this->debug > 0) {
  2380. error_log('New LP - In learnpath::get_author()', 0);
  2381. }
  2382. if (!empty ($this->author)) {
  2383. return $this->author;
  2384. } else {
  2385. return '';
  2386. }
  2387. }
  2388. /**
  2389. * Gets the learnpath author
  2390. * @return string LP's author
  2391. */
  2392. public function get_hide_toc_frame()
  2393. {
  2394. if ($this->debug > 0) {
  2395. error_log('New LP - In learnpath::get_author()', 0);
  2396. }
  2397. if (!empty ($this->hide_toc_frame)) {
  2398. return $this->hide_toc_frame;
  2399. } else {
  2400. return '';
  2401. }
  2402. }
  2403. /**
  2404. * Generate a new prerequisites string for a given item. If this item was a sco and
  2405. * its prerequisites were strings (instead of IDs), then transform those strings into
  2406. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2407. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2408. * same rule as the scorm_export() method
  2409. * @param integer Item ID
  2410. * @return string Prerequisites string ready for the export as SCORM
  2411. */
  2412. public function get_scorm_prereq_string($item_id)
  2413. {
  2414. if ($this->debug > 0) {
  2415. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2416. }
  2417. if (!is_object($this->items[$item_id])) {
  2418. return false;
  2419. }
  2420. $oItem = $this->items[$item_id];
  2421. $prereq = $oItem->get_prereq_string();
  2422. if (empty($prereq)) {
  2423. return '';
  2424. }
  2425. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2426. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2427. // then simply return it (with the ITEM_ prefix).
  2428. //return 'ITEM_' . $prereq;
  2429. return $this->items[$prereq]->ref;
  2430. } else {
  2431. if (isset($this->refs_list[$prereq])) {
  2432. // It's a simple string item from which the ID can be found in the refs list,
  2433. // so we can transform it directly to an ID for export.
  2434. return $this->items[$this->refs_list[$prereq]]->ref;
  2435. } else {
  2436. if (isset($this->refs_list['ITEM_'.$prereq])) {
  2437. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2438. } else {
  2439. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2440. // and replace them, one by one, by the internal IDs (chamilo db)
  2441. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2442. // by a space as well.
  2443. $find = array(
  2444. '&',
  2445. '|',
  2446. '~',
  2447. '=',
  2448. '<>',
  2449. '{',
  2450. '}',
  2451. '*',
  2452. '(',
  2453. ')'
  2454. );
  2455. $replace = array(
  2456. ' ',
  2457. ' ',
  2458. ' ',
  2459. ' ',
  2460. ' ',
  2461. ' ',
  2462. ' ',
  2463. ' ',
  2464. ' ',
  2465. ' '
  2466. );
  2467. $prereq_mod = str_replace($find, $replace, $prereq);
  2468. $ids = split(' ', $prereq_mod);
  2469. foreach ($ids as $id) {
  2470. $id = trim($id);
  2471. if (isset ($this->refs_list[$id])) {
  2472. $prereq = preg_replace(
  2473. '/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/',
  2474. 'ITEM_'.$this->refs_list[$id],
  2475. $prereq
  2476. );
  2477. }
  2478. }
  2479. error_log(
  2480. 'New LP - In learnpath::get_scorm_prereq_string(): returning modified string: '.$prereq,
  2481. 0
  2482. );
  2483. return $prereq;
  2484. }
  2485. }
  2486. }
  2487. }
  2488. /**
  2489. * Returns the XML DOM document's node
  2490. * @param resource Reference to a list of objects to search for the given ITEM_*
  2491. * @param string The identifier to look for
  2492. * @return mixed The reference to the element found with that identifier. False if not found
  2493. */
  2494. public function get_scorm_xml_node(& $children, $id)
  2495. {
  2496. for ($i = 0; $i < $children->length; $i++) {
  2497. $item_temp = $children->item($i);
  2498. if ($item_temp->nodeName == 'item') {
  2499. if ($item_temp->getAttribute('identifier') == $id) {
  2500. return $item_temp;
  2501. }
  2502. }
  2503. $subchildren = $item_temp->childNodes;
  2504. if ($subchildren->length > 0) {
  2505. $val = $this->get_scorm_xml_node($subchildren, $id);
  2506. if (is_object($val)) {
  2507. return $val;
  2508. }
  2509. }
  2510. }
  2511. return false;
  2512. }
  2513. /**
  2514. * Returns a usable array of stats related to the current learnpath and user
  2515. * @return array Well-formatted array containing status for the current learnpath
  2516. */
  2517. public function get_stats()
  2518. {
  2519. if ($this->debug > 0) {
  2520. error_log('New LP - In learnpath::get_stats()', 0);
  2521. }
  2522. // TODO
  2523. }
  2524. /**
  2525. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2526. * the given course (for all learnpaths and all users)
  2527. * @param string Course code
  2528. * @return array Well-formatted array containing status for the course's learnpaths
  2529. */
  2530. public function get_stats_course($course)
  2531. {
  2532. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2533. // TODO
  2534. }
  2535. /**
  2536. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2537. * the given course and learnpath (for all users)
  2538. * @param string Course code
  2539. * @param integer Learnpath ID
  2540. * @return array Well-formatted array containing status for the specified learnpath
  2541. */
  2542. public function get_stats_lp($course, $lp)
  2543. {
  2544. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2545. // TODO
  2546. }
  2547. /**
  2548. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2549. * the given course, learnpath and user.
  2550. * @param string Course code
  2551. * @param integer Learnpath ID
  2552. * @param integer User ID
  2553. * @return array Well-formatted array containing status for the specified learnpath and user
  2554. */
  2555. public function get_stats_lp_user($course, $lp, $user)
  2556. {
  2557. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2558. // TODO
  2559. }
  2560. /**
  2561. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2562. * the given course and learnpath (for all users)
  2563. * @param string Course code
  2564. * @param integer User ID
  2565. * @return array Well-formatted array containing status for the user's learnpaths
  2566. */
  2567. public function get_stats_user($course, $user)
  2568. {
  2569. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2570. // TODO
  2571. }
  2572. /**
  2573. * Gets the status list for all LP's items
  2574. * @return array Array of [index] => [item ID => current status]
  2575. */
  2576. public function get_items_status_list()
  2577. {
  2578. if ($this->debug > 0) {
  2579. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2580. }
  2581. $list = array();
  2582. foreach ($this->ordered_items as $item_id) {
  2583. $list[] = array(
  2584. $item_id => $this->items[$item_id]->get_status()
  2585. );
  2586. }
  2587. return $list;
  2588. }
  2589. /**
  2590. * Return the number of interactions for the given learnpath Item View ID.
  2591. * This method can be used as static.
  2592. * @param integer Item View ID
  2593. * @param integer course id
  2594. * @return integer Number of interactions
  2595. */
  2596. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2597. {
  2598. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2599. $lp_iv_id = intval($lp_iv_id);
  2600. $course_id = intval($course_id);
  2601. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2602. $res = Database::query($sql);
  2603. $res = 0;
  2604. $num = 0;
  2605. if (Database::num_rows($res)) {
  2606. $row = Database::fetch_array($res);
  2607. $num = $row[0];
  2608. }
  2609. return $num;
  2610. }
  2611. /**
  2612. * Return the interactions as an array for the given lp_iv_id.
  2613. * This method can be used as static.
  2614. * @param integer Learnpath Item View ID
  2615. * @return array
  2616. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2617. */
  2618. public static function get_iv_interactions_array($lp_iv_id = 0)
  2619. {
  2620. $course_id = api_get_course_int_id();
  2621. $list = array();
  2622. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2623. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2624. $res = Database::query($sql);
  2625. $num = Database :: num_rows($res);
  2626. if ($num > 0) {
  2627. $list[] = array(
  2628. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2629. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2630. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2631. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2632. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2633. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2634. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2635. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2636. );
  2637. while ($row = Database :: fetch_array($res)) {
  2638. $list[] = array(
  2639. 'order_id' => ($row['order_id'] + 1),
  2640. 'id' => urldecode($row['interaction_id']),
  2641. //urldecode because they often have %2F or stuff like that
  2642. 'type' => $row['interaction_type'],
  2643. 'time' => $row['completion_time'],
  2644. //'correct_responses' => $row['correct_responses'],
  2645. 'correct_responses' => '',
  2646. // Hide correct responses from students.
  2647. 'student_response' => $row['student_response'],
  2648. 'result' => $row['result'],
  2649. 'latency' => $row['latency']
  2650. );
  2651. }
  2652. }
  2653. return $list;
  2654. }
  2655. /**
  2656. * Return the number of objectives for the given learnpath Item View ID.
  2657. * This method can be used as static.
  2658. * @param integer Item View ID
  2659. * @return integer Number of objectives
  2660. */
  2661. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2662. {
  2663. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2664. $course_id = intval($course_id);
  2665. $lp_iv_id = intval($lp_iv_id);
  2666. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2667. //@todo seems that this always returns 0
  2668. $res = Database::query($sql);
  2669. $res = 0;
  2670. $num = 0;
  2671. if (Database::num_rows($res)) {
  2672. $row = Database :: fetch_array($res);
  2673. $num = $row[0];
  2674. }
  2675. return $num;
  2676. }
  2677. /**
  2678. * Return the objectives as an array for the given lp_iv_id.
  2679. * This method can be used as static.
  2680. * @param integer Learnpath Item View ID
  2681. * @return array
  2682. * @todo Translate labels
  2683. */
  2684. public function get_iv_objectives_array($lp_iv_id = 0)
  2685. {
  2686. $course_id = api_get_course_int_id();
  2687. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2688. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2689. $res = Database::query($sql);
  2690. $num = Database :: num_rows($res);
  2691. $list = array();
  2692. if ($num > 0) {
  2693. $list[] = array(
  2694. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2695. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2696. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2697. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2698. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2699. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2700. );
  2701. while ($row = Database :: fetch_array($res)) {
  2702. $list[] = array(
  2703. 'order_id' => ($row['order_id'] + 1),
  2704. 'objective_id' => urldecode($row['objective_id']),
  2705. // urldecode() because they often have %2F or stuff like that.
  2706. 'score_raw' => $row['score_raw'],
  2707. 'score_max' => $row['score_max'],
  2708. 'score_min' => $row['score_min'],
  2709. 'status' => $row['status']
  2710. );
  2711. }
  2712. }
  2713. return $list;
  2714. }
  2715. /**
  2716. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2717. * used by get_html_toc() to be ready to display
  2718. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2719. */
  2720. public function get_toc()
  2721. {
  2722. if ($this->debug > 0) {
  2723. error_log('learnpath::get_toc()', 0);
  2724. }
  2725. $toc = array();
  2726. //echo "<pre>".print_r($this->items,true)."</pre>";
  2727. foreach ($this->ordered_items as $item_id) {
  2728. if ($this->debug > 2) {
  2729. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2730. }
  2731. // TODO: Change this link generation and use new function instead.
  2732. $toc[] = array(
  2733. 'id' => $item_id,
  2734. 'title' => $this->items[$item_id]->get_title(),
  2735. 'status' => $this->items[$item_id]->get_status(),
  2736. 'level' => $this->items[$item_id]->get_level(),
  2737. 'type' => $this->items[$item_id]->get_type(),
  2738. 'description' => $this->items[$item_id]->get_description(),
  2739. 'path' => $this->items[$item_id]->get_path(),
  2740. );
  2741. }
  2742. if ($this->debug > 2) {
  2743. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2744. }
  2745. return $toc;
  2746. }
  2747. /**
  2748. * Generate and return the table of contents for this learnpath. The JS
  2749. * table returned is used inside of scorm_api.php
  2750. * @return string A JS array vairiable construction
  2751. */
  2752. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2753. {
  2754. if ($this->debug > 0) {
  2755. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2756. }
  2757. $toc = $varname.' = new Array();';
  2758. //echo "<pre>".print_r($this->items,true)."</pre>";
  2759. foreach ($this->ordered_items as $item_id) {
  2760. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2761. }
  2762. if ($this->debug > 2) {
  2763. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2764. }
  2765. return $toc;
  2766. }
  2767. /**
  2768. * Gets the learning path type
  2769. * @param boolean Return the name? If false, return the ID. Default is false.
  2770. * @return mixed Type ID or name, depending on the parameter
  2771. */
  2772. public function get_type($get_name = false)
  2773. {
  2774. $res = false;
  2775. if ($this->debug > 0) {
  2776. error_log('New LP - In learnpath::get_type()', 0);
  2777. }
  2778. if (!empty ($this->type)) {
  2779. if ($get_name) {
  2780. // Get it from the lp_type table in main db.
  2781. } else {
  2782. $res = $this->type;
  2783. }
  2784. }
  2785. if ($this->debug > 2) {
  2786. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2787. }
  2788. return $res;
  2789. }
  2790. /**
  2791. * Gets the learning path type as static method
  2792. * @param boolean Return the name? If false, return the ID. Default is false.
  2793. * @return mixed Type ID or name, depending on the parameter
  2794. */
  2795. public static function get_type_static($lp_id = 0)
  2796. {
  2797. $course_id = api_get_course_int_id();
  2798. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2799. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2800. $res = Database::query($sql);
  2801. if ($res === false) {
  2802. return null;
  2803. }
  2804. if (Database :: num_rows($res) <= 0) {
  2805. return null;
  2806. }
  2807. $row = Database :: fetch_array($res);
  2808. return $row['lp_type'];
  2809. }
  2810. /**
  2811. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2812. * This method can be used as abstract and is recursive
  2813. * @param integer Learnpath ID
  2814. * @param integer Parent ID of the items to look for
  2815. * @return mixed Ordered list of item IDs or false on error
  2816. */
  2817. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2818. {
  2819. if (empty($course_id)) {
  2820. $course_id = api_get_course_int_id();
  2821. } else {
  2822. $course_id = intval($course_id);
  2823. }
  2824. $list = array();
  2825. if (empty ($lp)) {
  2826. return false;
  2827. }
  2828. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2829. $sql = "SELECT id FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent ORDER BY display_order";
  2830. $res = Database::query($sql);
  2831. while ($row = Database :: fetch_array($res)) {
  2832. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2833. $list[] = $row['id'];
  2834. foreach ($sublist as $item) {
  2835. $list[] = $item;
  2836. }
  2837. }
  2838. return $list;
  2839. }
  2840. /**
  2841. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2842. * @return string HTML TOC ready to display
  2843. */
  2844. public function get_html_toc($toc_list = null)
  2845. {
  2846. global $_configuration;
  2847. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2848. if ($this->debug > 0) {
  2849. error_log('In learnpath::get_html_toc()', 0);
  2850. }
  2851. if (empty($toc_list)) {
  2852. $toc_list = $this->get_toc();
  2853. }
  2854. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()).'</div>';
  2855. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2856. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2857. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2858. if ($this->get_lp_session_id() == api_get_session_id()) {
  2859. $html .= '<div id="actions_lp" class="actions_lp">';
  2860. $html .= '<div class="btn-group">';
  2861. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2862. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2863. 'Overview'
  2864. )."</a>";
  2865. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2866. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2867. 'Edit'
  2868. )."</a>";
  2869. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2870. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2871. $html .= '</div>';
  2872. $html .= '</div>';
  2873. }
  2874. }
  2875. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2876. require_once 'resourcelinker.inc.php';
  2877. // Temporary variables.
  2878. $mycurrentitemid = $this->get_current_item_id();
  2879. $color_counter = 0;
  2880. $i = 0;
  2881. foreach ($toc_list as $item) {
  2882. // TODO: Complete this
  2883. $icon_name = array(
  2884. 'not attempted' => '../img/notattempted.gif',
  2885. 'incomplete' => '../img/incomplete.png',
  2886. 'failed' => '../img/delete.png',
  2887. 'completed' => '../img/completed.png',
  2888. 'passed' => '../img/passed.png',
  2889. 'succeeded' => '../img/succeeded.png',
  2890. 'browsed' => '../img/completed.png',
  2891. );
  2892. $style = 'scorm_item';
  2893. $scorm_color_background = 'scorm_item';
  2894. $style_item = 'scorm_item';
  2895. $current = false;
  2896. if ($item['id'] == $this->current) {
  2897. $style = 'scorm_item_highlight';
  2898. $scorm_color_background = 'scorm_item_highlight';
  2899. } else {
  2900. if ($color_counter % 2 == 0) {
  2901. $scorm_color_background = 'scorm_item_1';
  2902. } else {
  2903. $scorm_color_background = 'scorm_item_2';
  2904. }
  2905. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2906. $scorm_color_background = ' scorm_item_section ';
  2907. }
  2908. }
  2909. if ($scorm_color_background != '') {
  2910. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2911. }
  2912. // The anchor will let us center the TOC on the currently viewed item &^D
  2913. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2914. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2915. $html .= '<a name="atoc_'.$item['id'].'" />';
  2916. } else {
  2917. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2918. }
  2919. $title = $item['title'];
  2920. if (empty ($title)) {
  2921. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2922. }
  2923. $title = Security::remove_XSS($title);
  2924. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2925. //$html .= "<a href='lp_controller.php?".api_get_cidreq()."&action=content&lp_id=".$this->get_id()."&item_id=".$item['id']."' target='lp_content_frame_name'>".$title."</a>" ;
  2926. $url = $this->get_link('http', $item['id'], $toc_list);
  2927. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2928. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2929. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2930. $html .= '<a href="" onClick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;" >'.stripslashes(
  2931. $title
  2932. ).'</a>';
  2933. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2934. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;".stripslashes(
  2935. $title
  2936. );
  2937. } elseif ($item['type'] == 'dir') {
  2938. $html .= stripslashes($title);
  2939. }
  2940. if ($item['type'] == 'quiz') {
  2941. // error_log("1-->>>>>>>>>>>>>>>>");
  2942. // error_log($item['status']);
  2943. if ($item['status'] == 'completed') {
  2944. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2945. $item['status'],
  2946. 0,
  2947. 1
  2948. )."' width='14' />";
  2949. } else {
  2950. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name['not attempted']."' alt='".substr(
  2951. 'not attempted',
  2952. 0,
  2953. 1
  2954. )."' width='14' />";
  2955. }
  2956. } else {
  2957. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2958. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2959. $item['status'],
  2960. 0,
  2961. 1
  2962. )."' width='14' />";
  2963. }
  2964. }
  2965. $html .= "</div>";
  2966. if ($scorm_color_background != '') {
  2967. $html .= '</div>';
  2968. }
  2969. $color_counter++;
  2970. }
  2971. $html .= "</div>";
  2972. return $html;
  2973. }
  2974. /**
  2975. * Gets the learnpath maker name - generally the editor's name
  2976. * @return string Learnpath maker name
  2977. */
  2978. public function get_maker()
  2979. {
  2980. if ($this->debug > 0) {
  2981. error_log('New LP - In learnpath::get_maker()', 0);
  2982. }
  2983. if (!empty ($this->maker)) {
  2984. return $this->maker;
  2985. } else {
  2986. return '';
  2987. }
  2988. }
  2989. /**
  2990. * Gets the user-friendly message stored in $this->message
  2991. * @return string Message
  2992. */
  2993. public function get_message()
  2994. {
  2995. if ($this->debug > 0) {
  2996. error_log('New LP - In learnpath::get_message()', 0);
  2997. }
  2998. return $this->message;
  2999. }
  3000. /**
  3001. * Gets the learnpath name/title
  3002. * @return string Learnpath name/title
  3003. */
  3004. public function get_name()
  3005. {
  3006. if ($this->debug > 0) {
  3007. error_log('New LP - In learnpath::get_name()', 0);
  3008. }
  3009. if (!empty ($this->name)) {
  3010. return $this->name;
  3011. } else {
  3012. return 'N/A';
  3013. }
  3014. }
  3015. /**
  3016. * Gets a link to the resource from the present location, depending on item ID.
  3017. * @param string Type of link expected
  3018. * @param integer Learnpath item ID
  3019. * @return string Link to the lp_item resource
  3020. */
  3021. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3022. {
  3023. $course_id = $this->get_course_int_id();
  3024. if ($this->debug > 0) {
  3025. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3026. }
  3027. if (empty($item_id)) {
  3028. if ($this->debug > 2) {
  3029. error_log(
  3030. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  3031. ),
  3032. 0
  3033. );
  3034. }
  3035. $item_id = $this->get_current_item_id();
  3036. }
  3037. if (empty($item_id)) {
  3038. if ($this->debug > 2) {
  3039. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3040. }
  3041. //still empty, this means there was no item_id given and we are not in an object context or
  3042. //the object property is empty, return empty link
  3043. $item_id = $this->first();
  3044. return '';
  3045. }
  3046. $file = '';
  3047. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3048. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3049. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3050. $item_id = Database::escape_string($item_id);
  3051. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3052. FROM $lp_table l
  3053. INNER JOIN $lp_item_table li
  3054. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3055. WHERE li.id = $item_id ";
  3056. if ($this->debug > 2) {
  3057. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3058. }
  3059. $res = Database::query($sql);
  3060. if (Database :: num_rows($res) > 0) {
  3061. $row = Database :: fetch_array($res);
  3062. $lp_type = $row['ltype'];
  3063. $lp_path = $row['lpath'];
  3064. $lp_item_type = $row['litype'];
  3065. $lp_item_path = $row['lipath'];
  3066. $lp_item_params = $row['liparams'];
  3067. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3068. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3069. }
  3070. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3071. if ($type == 'http') {
  3072. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3073. } else {
  3074. $course_path = $sys_course_path; //system path
  3075. }
  3076. // 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.
  3077. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3078. $lp_type = 1;
  3079. }
  3080. if ($this->debug > 2) {
  3081. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3082. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3083. }
  3084. // Now go through the specific cases to get the end of the path
  3085. // @todo Use constants instead of int values.
  3086. switch ($lp_type) {
  3087. case 1 :
  3088. if ($lp_item_type == 'dokeos_chapter') {
  3089. $file = 'lp_content.php?type=dir';
  3090. } else {
  3091. require_once 'resourcelinker.inc.php';
  3092. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3093. if ($this->debug > 0) {
  3094. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3095. }
  3096. if ($lp_item_type == 'link') {
  3097. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3098. if (is_youtube_link($file)) {
  3099. $src = get_youtube_video_id($file);
  3100. $file = 'embed.php?type=youtube&src='.$src;
  3101. }
  3102. } else {
  3103. // check how much attempts of a exercise exits in lp
  3104. $lp_item_id = $this->get_current_item_id();
  3105. $lp_view_id = $this->get_view_id();
  3106. $prevent_reinit = null;
  3107. if (isset($this->items[$this->current])) {
  3108. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3109. }
  3110. if (empty($provided_toc)) {
  3111. if ($this->debug > 0) {
  3112. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3113. }
  3114. $list = $this->get_toc();
  3115. } else {
  3116. if ($this->debug > 0) {
  3117. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3118. }
  3119. $list = $provided_toc;
  3120. }
  3121. $type_quiz = false;
  3122. foreach ($list as $toc) {
  3123. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3124. $type_quiz = true;
  3125. }
  3126. }
  3127. if ($type_quiz) {
  3128. $lp_item_id = Database::escape_string($lp_item_id);
  3129. $lp_view_id = Database::escape_string($lp_view_id);
  3130. $sql = "SELECT count(*) FROM $lp_item_view_table
  3131. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3132. $result = Database::query($sql);
  3133. $row_count = Database :: fetch_row($result);
  3134. $count_item_view = (int)$row_count[0];
  3135. $not_multiple_attempt = 0;
  3136. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3137. $not_multiple_attempt = 1;
  3138. }
  3139. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3140. }
  3141. $tmp_array = explode('/', $file);
  3142. $document_name = $tmp_array[count($tmp_array) - 1];
  3143. if (strpos($document_name, '_DELETED_')) {
  3144. $file = 'blank.php?error=document_deleted';
  3145. }
  3146. }
  3147. }
  3148. break;
  3149. case 2 :
  3150. if ($this->debug > 2) {
  3151. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3152. }
  3153. if ($lp_item_type != 'dir') {
  3154. // Quite complex here:
  3155. // We want to make sure 'http://' (and similar) links can
  3156. // be loaded as is (withouth the Chamilo path in front) but
  3157. // some contents use this form: resource.htm?resource=http://blablabla
  3158. // which means we have to find a protocol at the path's start, otherwise
  3159. // it should not be considered as an external URL.
  3160. //if ($this->prerequisites_match($item_id)) {
  3161. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3162. if ($this->debug > 2) {
  3163. error_log(
  3164. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3165. 0
  3166. );
  3167. }
  3168. // Distant url, return as is.
  3169. $file = $lp_item_path;
  3170. } else {
  3171. if ($this->debug > 2) {
  3172. error_log(
  3173. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3174. 0
  3175. );
  3176. }
  3177. // Prevent getting untranslatable urls.
  3178. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3179. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3180. // Prepare the path.
  3181. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3182. // TODO: Fix this for urls with protocol header.
  3183. $file = str_replace('//', '/', $file);
  3184. $file = str_replace(':/', '://', $file);
  3185. if (substr($lp_path, -1) == '/') {
  3186. $lp_path = substr($lp_path, 0, -1);
  3187. }
  3188. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3189. // if file not found.
  3190. $decoded = html_entity_decode($lp_item_path);
  3191. list ($decoded) = explode('?', $decoded);
  3192. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3193. require_once 'resourcelinker.inc.php';
  3194. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3195. if (empty($file)) {
  3196. $file = 'blank.php?error=document_not_found';
  3197. } else {
  3198. $tmp_array = explode('/', $file);
  3199. $document_name = $tmp_array[count($tmp_array) - 1];
  3200. if (strpos($document_name, '_DELETED_')) {
  3201. $file = 'blank.php?error=document_deleted';
  3202. } else {
  3203. $file = 'blank.php?error=document_not_found';
  3204. }
  3205. }
  3206. } else {
  3207. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3208. }
  3209. }
  3210. }
  3211. //}else{
  3212. //prerequisites did not match
  3213. //$file = 'blank.php';
  3214. //}
  3215. // We want to use parameters if they were defined in the imsmanifest
  3216. if (strpos($file, 'blank.php') === false) {
  3217. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3218. }
  3219. } else {
  3220. $file = 'lp_content.php?type=dir';
  3221. }
  3222. break;
  3223. case 3 :
  3224. if ($this->debug > 2) {
  3225. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3226. }
  3227. // Formatting AICC HACP append URL.
  3228. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3229. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3230. ).'&';
  3231. if ($lp_item_type != 'dir') {
  3232. // Quite complex here:
  3233. // We want to make sure 'http://' (and similar) links can
  3234. // be loaded as is (withouth the Chamilo path in front) but
  3235. // some contents use this form: resource.htm?resource=http://blablabla
  3236. // which means we have to find a protocol at the path's start, otherwise
  3237. // it should not be considered as an external URL.
  3238. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3239. if ($this->debug > 2) {
  3240. error_log(
  3241. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3242. 0
  3243. );
  3244. }
  3245. // Distant url, return as is.
  3246. $file = $lp_item_path;
  3247. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3248. /*
  3249. if (stristr($file,'<servername>') !== false) {
  3250. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3251. }
  3252. */
  3253. if (stripos($file, '<servername>') !== false) {
  3254. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3255. $web_course_path = str_replace(
  3256. 'https://',
  3257. '',
  3258. str_replace('http://', '', $course_path)
  3259. );
  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(
  3267. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3268. 0
  3269. );
  3270. }
  3271. // Prevent getting untranslatable urls.
  3272. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3273. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3274. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3275. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3276. // TODO: Fix this for urls with protocol header.
  3277. $file = str_replace('//', '/', $file);
  3278. $file = str_replace(':/', '://', $file);
  3279. $file .= $aicc_append;
  3280. }
  3281. } else {
  3282. $file = 'lp_content.php?type=dir';
  3283. }
  3284. break;
  3285. case 4 :
  3286. break;
  3287. default :
  3288. break;
  3289. }
  3290. }
  3291. if ($this->debug > 2) {
  3292. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3293. }
  3294. return $file;
  3295. }
  3296. /**
  3297. * Gets the latest usable view or generate a new one
  3298. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3299. * @return integer DB lp_view id
  3300. */
  3301. public function get_view($attempt_num = 0)
  3302. {
  3303. if ($this->debug > 0) {
  3304. error_log('New LP - In learnpath::get_view()', 0);
  3305. }
  3306. $search = '';
  3307. // Use $attempt_num to enable multi-views management (disabled so far).
  3308. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3309. $search = 'AND view_count = '.$attempt_num;
  3310. }
  3311. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3312. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3313. $course_id = api_get_course_int_id();
  3314. $sql = "SELECT id, view_count FROM $lp_view_table
  3315. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3316. )." ".$search.
  3317. " ORDER BY view_count DESC";
  3318. $res = Database::query($sql);
  3319. if (Database :: num_rows($res) > 0) {
  3320. $row = Database :: fetch_array($res);
  3321. $this->lp_view_id = $row['id'];
  3322. } else {
  3323. // There is no database record, create one.
  3324. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3325. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3326. $res = Database::query($sql);
  3327. $id = Database :: insert_id();
  3328. $this->lp_view_id = $id;
  3329. }
  3330. return $this->lp_view_id;
  3331. }
  3332. /**
  3333. * Gets the current view id
  3334. * @return integer View ID (from lp_view)
  3335. */
  3336. public function get_view_id()
  3337. {
  3338. if ($this->debug > 0) {
  3339. error_log('New LP - In learnpath::get_view_id()', 0);
  3340. }
  3341. if (!empty ($this->lp_view_id)) {
  3342. return $this->lp_view_id;
  3343. } else {
  3344. return 0;
  3345. }
  3346. }
  3347. /**
  3348. * Gets the update queue
  3349. * @return array Array containing IDs of items to be updated by JavaScript
  3350. */
  3351. public function get_update_queue()
  3352. {
  3353. if ($this->debug > 0) {
  3354. error_log('New LP - In learnpath::get_update_queue()', 0);
  3355. }
  3356. return $this->update_queue;
  3357. }
  3358. /**
  3359. * Gets the user ID
  3360. * @return integer User ID
  3361. */
  3362. public function get_user_id()
  3363. {
  3364. if ($this->debug > 0) {
  3365. error_log('New LP - In learnpath::get_user_id()', 0);
  3366. }
  3367. if (!empty ($this->user_id)) {
  3368. return $this->user_id;
  3369. } else {
  3370. return false;
  3371. }
  3372. }
  3373. /**
  3374. * Checks if any of the items has an audio element attached
  3375. * @return bool True or false
  3376. */
  3377. public function has_audio()
  3378. {
  3379. if ($this->debug > 1) {
  3380. error_log('New LP - In learnpath::has_audio()', 0);
  3381. }
  3382. $has = false;
  3383. foreach ($this->items as $i => $item) {
  3384. if (!empty ($this->items[$i]->audio)) {
  3385. $has = true;
  3386. break;
  3387. }
  3388. }
  3389. return $has;
  3390. }
  3391. /**
  3392. * Logs a message into a file
  3393. * @param string Message to log
  3394. * @return boolean True on success, false on error or if msg empty
  3395. */
  3396. public function log($msg)
  3397. {
  3398. if ($this->debug > 0) {
  3399. error_log('New LP - In learnpath::log()', 0);
  3400. }
  3401. // TODO
  3402. $this->error .= $msg;
  3403. return true;
  3404. }
  3405. /**
  3406. * Moves an item up and down at its level
  3407. * @param integer Item to move up and down
  3408. * @param string Direction 'up' or 'down'
  3409. * @return integer New display order, or false on error
  3410. */
  3411. public function move_item($id, $direction)
  3412. {
  3413. $course_id = api_get_course_int_id();
  3414. if ($this->debug > 0) {
  3415. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3416. }
  3417. if (empty ($id) or empty ($direction)) {
  3418. return false;
  3419. }
  3420. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3421. $sql_sel = "SELECT *
  3422. FROM ".$tbl_lp_item."
  3423. WHERE c_id = ".$course_id." AND id = ".$id;
  3424. $res_sel = Database::query($sql_sel);
  3425. // Check if elem exists.
  3426. if (Database :: num_rows($res_sel) < 1) {
  3427. return false;
  3428. }
  3429. // Gather data.
  3430. $row = Database :: fetch_array($res_sel);
  3431. $previous = $row['previous_item_id'];
  3432. $next = $row['next_item_id'];
  3433. $display = $row['display_order'];
  3434. $parent = $row['parent_item_id'];
  3435. $lp = $row['lp_id'];
  3436. // Update the item (switch with previous/next one).
  3437. switch ($direction) {
  3438. case 'up' :
  3439. if ($this->debug > 2) {
  3440. error_log('Movement up detected', 0);
  3441. }
  3442. if ($display <= 1) { /*do nothing*/
  3443. } else {
  3444. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3445. WHERE c_id = ".$course_id." AND id = $previous";
  3446. if ($this->debug > 2) {
  3447. error_log('Selecting previous: '.$sql_sel2, 0);
  3448. }
  3449. $res_sel2 = Database::query($sql_sel2);
  3450. if (Database :: num_rows($res_sel2) < 1) {
  3451. $previous_previous = 0;
  3452. }
  3453. // Gather data.
  3454. $row2 = Database :: fetch_array($res_sel2);
  3455. $previous_previous = $row2['previous_item_id'];
  3456. // Update previous_previous item (switch "next" with current).
  3457. if ($previous_previous != 0) {
  3458. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3459. if ($this->debug > 2) {
  3460. error_log($sql_upd2, 0);
  3461. }
  3462. $res_upd2 = Database::query($sql_upd2);
  3463. }
  3464. // Update previous item (switch with current).
  3465. if ($previous != 0) {
  3466. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3467. WHERE c_id = ".$course_id." AND id = $previous";
  3468. if ($this->debug > 2) {
  3469. error_log($sql_upd2, 0);
  3470. }
  3471. $res_upd2 = Database::query($sql_upd2);
  3472. }
  3473. // Update current item (switch with previous).
  3474. if ($id != 0) {
  3475. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3476. WHERE c_id = ".$course_id." AND id = $id";
  3477. if ($this->debug > 2) {
  3478. error_log($sql_upd2, 0);
  3479. }
  3480. $res_upd2 = Database::query($sql_upd2);
  3481. }
  3482. // Update next item (new previous item).
  3483. if ($next != 0) {
  3484. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3485. WHERE c_id = ".$course_id." AND id = $next";
  3486. if ($this->debug > 2) {
  3487. error_log($sql_upd2, 0);
  3488. }
  3489. $res_upd2 = Database::query($sql_upd2);
  3490. }
  3491. $display = $display - 1;
  3492. }
  3493. break;
  3494. case 'down' :
  3495. if ($this->debug > 2) {
  3496. error_log('Movement down detected', 0);
  3497. }
  3498. if ($next == 0) { /* Do nothing. */
  3499. } else {
  3500. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3501. if ($this->debug > 2) {
  3502. error_log('Selecting next: '.$sql_sel2, 0);
  3503. }
  3504. $res_sel2 = Database::query($sql_sel2);
  3505. if (Database :: num_rows($res_sel2) < 1) {
  3506. $next_next = 0;
  3507. }
  3508. // Gather data.
  3509. $row2 = Database :: fetch_array($res_sel2);
  3510. $next_next = $row2['next_item_id'];
  3511. // Update previous item (switch with current).
  3512. if ($previous != 0) {
  3513. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3514. WHERE c_id = ".$course_id." AND id = $previous";
  3515. $res_upd2 = Database::query($sql_upd2);
  3516. }
  3517. // Update current item (switch with previous).
  3518. if ($id != 0) {
  3519. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3520. WHERE c_id = ".$course_id." AND id = $id";
  3521. $res_upd2 = Database::query($sql_upd2);
  3522. }
  3523. // Update next item (new previous item).
  3524. if ($next != 0) {
  3525. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3526. WHERE c_id = ".$course_id." AND id = $next";
  3527. $res_upd2 = Database::query($sql_upd2);
  3528. }
  3529. // Update next_next item (switch "previous" with current).
  3530. if ($next_next != 0) {
  3531. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3532. WHERE c_id = ".$course_id." AND id = $next_next";
  3533. $res_upd2 = Database::query($sql_upd2);
  3534. }
  3535. $display = $display + 1;
  3536. }
  3537. break;
  3538. default :
  3539. return false;
  3540. }
  3541. return $display;
  3542. }
  3543. /**
  3544. * Move a learnpath up (display_order)
  3545. * @param integer Learnpath ID
  3546. */
  3547. public function move_up($lp_id)
  3548. {
  3549. $course_id = api_get_course_int_id();
  3550. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3551. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3552. $res = Database::query($sql);
  3553. if ($res === false) {
  3554. return false;
  3555. }
  3556. $lps = array();
  3557. $lp_order = array();
  3558. $num = Database :: num_rows($res);
  3559. // First check the order is correct, globally (might be wrong because
  3560. // of versions < 1.8.4)
  3561. if ($num > 0) {
  3562. $i = 1;
  3563. while ($row = Database :: fetch_array($res)) {
  3564. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3565. $need_fix = true;
  3566. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3567. $res_u = Database::query($sql_u);
  3568. }
  3569. $row['display_order'] = $i;
  3570. $lps[$row['id']] = $row;
  3571. $lp_order[$i] = $row['id'];
  3572. $i++;
  3573. }
  3574. }
  3575. if ($num > 1) { // If there's only one element, no need to sort.
  3576. $order = $lps[$lp_id]['display_order'];
  3577. if ($order > 1) { // If it's the first element, no need to move up.
  3578. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3579. $res_u1 = Database::query($sql_u1);
  3580. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3581. $res_u2 = Database::query($sql_u2);
  3582. }
  3583. }
  3584. }
  3585. /**
  3586. * Move a learnpath down (display_order)
  3587. * @param integer Learnpath ID
  3588. */
  3589. public function move_down($lp_id)
  3590. {
  3591. $course_id = api_get_course_int_id();
  3592. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3593. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3594. $res = Database::query($sql);
  3595. if ($res === false) {
  3596. return false;
  3597. }
  3598. $lps = array();
  3599. $lp_order = array();
  3600. $num = Database :: num_rows($res);
  3601. $max = 0;
  3602. // First check the order is correct, globally (might be wrong because
  3603. // of versions < 1.8.4).
  3604. if ($num > 0) {
  3605. $i = 1;
  3606. while ($row = Database :: fetch_array($res)) {
  3607. $max = $i;
  3608. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3609. $need_fix = true;
  3610. $sql_u = "UPDATE $lp_table SET display_order = $i
  3611. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3612. $res_u = Database::query($sql_u);
  3613. }
  3614. $row['display_order'] = $i;
  3615. $lps[$row['id']] = $row;
  3616. $lp_order[$i] = $row['id'];
  3617. $i++;
  3618. }
  3619. }
  3620. if ($num > 1) { // If there's only one element, no need to sort.
  3621. $order = $lps[$lp_id]['display_order'];
  3622. if ($order < $max) { // If it's the first element, no need to move up.
  3623. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3624. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3625. $res_u1 = Database::query($sql_u1);
  3626. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3627. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3628. $res_u2 = Database::query($sql_u2);
  3629. }
  3630. }
  3631. }
  3632. /**
  3633. * Updates learnpath attributes to point to the next element
  3634. * The last part is similar to set_current_item but processing the other way around
  3635. */
  3636. public function next()
  3637. {
  3638. if ($this->debug > 0) {
  3639. error_log('New LP - In learnpath::next()', 0);
  3640. }
  3641. $this->last = $this->get_current_item_id();
  3642. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3643. $this->autocomplete_parents($this->last);
  3644. $new_index = $this->get_next_index();
  3645. if ($this->debug > 2) {
  3646. error_log('New LP - New index: '.$new_index, 0);
  3647. }
  3648. $this->index = $new_index;
  3649. if ($this->debug > 2) {
  3650. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3651. }
  3652. $this->current = $this->ordered_items[$new_index];
  3653. if ($this->debug > 2) {
  3654. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3655. }
  3656. }
  3657. /**
  3658. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3659. * class, this might be redefined to allow several behaviours depending on the document type.
  3660. * @param integer Resource ID
  3661. * @return boolean True on success, false otherwise
  3662. */
  3663. public function open($id)
  3664. {
  3665. if ($this->debug > 0) {
  3666. error_log('New LP - In learnpath::open()', 0);
  3667. }
  3668. // TODO:
  3669. // set the current resource attribute to this resource
  3670. // switch on element type (redefine in child class?)
  3671. // set status for this item to "opened"
  3672. // start timer
  3673. // initialise score
  3674. $this->index = 0; //or = the last item seen (see $this->last)
  3675. }
  3676. /**
  3677. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3678. * and the prerequisite string on error.
  3679. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3680. * @param integer Optional item ID. If none given, uses the current open item.
  3681. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3682. */
  3683. public function prerequisites_match($item = null)
  3684. {
  3685. if ($this->debug > 0) {
  3686. error_log('In learnpath::prerequisites_match()', 0);
  3687. }
  3688. if (empty($item)) {
  3689. $item = $this->current;
  3690. }
  3691. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3692. if ($this->type == 2) {
  3693. //Getting prereq from scorm
  3694. $prereq_string = $this->get_scorm_prereq_string($item);
  3695. } else {
  3696. $prereq_string = $this->items[$item]->get_prereq_string();
  3697. }
  3698. if (empty($prereq_string)) {
  3699. return true;
  3700. }
  3701. // Clean spaces.
  3702. $prereq_string = str_replace(' ', '', $prereq_string);
  3703. if ($this->debug > 0) {
  3704. error_log('Found prereq_string: '.$prereq_string, 0);
  3705. }
  3706. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3707. $result = $this->items[$item]->parse_prereq(
  3708. $prereq_string,
  3709. $this->items,
  3710. $this->refs_list,
  3711. $this->get_user_id()
  3712. );
  3713. if ($result === false) {
  3714. $this->set_error_msg($this->items[$item]->prereq_alert);
  3715. }
  3716. } else {
  3717. $result = true;
  3718. if ($this->debug > 1) {
  3719. error_log('$this->items['.$item.'] was not an object', 0);
  3720. }
  3721. }
  3722. if ($this->debug > 1) {
  3723. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3724. }
  3725. return $result;
  3726. }
  3727. /**
  3728. * Updates learnpath attributes to point to the previous element
  3729. * The last part is similar to set_current_item but processing the other way around
  3730. */
  3731. public function previous()
  3732. {
  3733. if ($this->debug > 0) {
  3734. error_log('New LP - In learnpath::previous()', 0);
  3735. }
  3736. $this->last = $this->get_current_item_id();
  3737. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3738. $this->autocomplete_parents($this->last);
  3739. $new_index = $this->get_previous_index();
  3740. $this->index = $new_index;
  3741. $this->current = $this->ordered_items[$new_index];
  3742. }
  3743. /**
  3744. * Publishes a learnpath. This basically means show or hide the learnpath
  3745. * to normal users.
  3746. * Can be used as abstract
  3747. * @param integer Learnpath ID
  3748. * @param string New visibility
  3749. */
  3750. public function toggle_visibility($lp_id, $set_visibility = 1)
  3751. {
  3752. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3753. $action = 'visible';
  3754. if ($set_visibility != 1) {
  3755. $action = 'invisible';
  3756. }
  3757. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3758. }
  3759. /**
  3760. * Publishes a learnpath. This basically means show or hide the learnpath
  3761. * on the course homepage
  3762. * Can be used as abstract
  3763. * @param integer Learnpath id
  3764. * @param string New visibility (v/s - visible/invisible)
  3765. */
  3766. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3767. {
  3768. $course_id = api_get_course_int_id();
  3769. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3770. $lp_id = Database::escape_string($lp_id);
  3771. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3772. $result = Database::query($sql);
  3773. if (Database::num_rows($result)) {
  3774. $row = Database :: fetch_array($result);
  3775. $name = Text::domesticate($row['name']);
  3776. if ($set_visibility == 'i') {
  3777. $s = $name." ".get_lang('LearnpathNotPublished');
  3778. $dialogBox = $s;
  3779. $v = 0;
  3780. }
  3781. if ($set_visibility == 'v') {
  3782. $s = $name." ".get_lang('LearnpathPublished');
  3783. $dialogBox = $s;
  3784. $v = 1;
  3785. }
  3786. } else {
  3787. return false;
  3788. }
  3789. $session_id = api_get_session_id();
  3790. $session_condition = api_get_session_condition($session_id);
  3791. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3792. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3793. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3794. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3795. $result = Database::query($sql);
  3796. $num = Database :: num_rows($result);
  3797. if (($set_visibility == 'i') && ($num > 0)) {
  3798. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3799. Database::query($sql);
  3800. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3801. Database::query($sql);
  3802. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3803. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3804. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3805. Database::query($sql);
  3806. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3807. $sql = "UPDATE $tbl_tool SET c_id = $course_id, name = '$name', link = '$link', image = 'scormbuilder.gif', visibility = '$v', admin = '0', address = 'pastillegris.gif', added_tool = 0, session_id = $session_id
  3808. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3809. Database::query($sql);
  3810. } else {
  3811. // Parameter and database incompatible, do nothing, exit.
  3812. return false;
  3813. }
  3814. }
  3815. /**
  3816. * Restart the whole learnpath. Return the URL of the first element.
  3817. * Make sure the results are saved with anoter method. This method should probably be
  3818. * redefined in children classes.
  3819. * To use a similar method statically, use the create_new_attempt() method
  3820. * @return string URL to load in the viewer
  3821. */
  3822. public function restart()
  3823. {
  3824. if ($this->debug > 0) {
  3825. error_log('New LP - In learnpath::restart()', 0);
  3826. }
  3827. // TODO
  3828. // Call autosave method to save the current progress.
  3829. //$this->index = 0;
  3830. $session_id = api_get_session_id();
  3831. $course_id = api_get_course_int_id();
  3832. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3833. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3834. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3835. if ($this->debug > 2) {
  3836. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3837. }
  3838. $res = Database::query($sql);
  3839. if ($view_id = Database :: insert_id($res)) {
  3840. $this->lp_view_id = $view_id;
  3841. $this->attempt = $this->attempt + 1;
  3842. } else {
  3843. $this->error = 'Could not insert into item_view table...';
  3844. return false;
  3845. }
  3846. $this->autocomplete_parents($this->current);
  3847. foreach ($this->items as $index => $dummy) {
  3848. $this->items[$index]->restart();
  3849. $this->items[$index]->set_lp_view($this->lp_view_id);
  3850. }
  3851. $this->first();
  3852. return true;
  3853. }
  3854. /**
  3855. * Saves the current item
  3856. * @return boolean
  3857. */
  3858. public function save_current()
  3859. {
  3860. if ($this->debug > 0) {
  3861. error_log('learnpath::save_current()', 0);
  3862. }
  3863. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3864. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3865. if ($this->debug > 2) {
  3866. error_log('New LP - save_current() saving item '.$this->current, 0);
  3867. }
  3868. if ($this->debug > 2) {
  3869. error_log(''.print_r($this->items, true), 0);
  3870. }
  3871. if (is_object($this->items[$this->current])) {
  3872. //$res = $this->items[$this->current]->save(false);
  3873. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3874. $this->autocomplete_parents($this->current);
  3875. $status = $this->items[$this->current]->get_status();
  3876. $this->append_message('new_item_status: '.$status);
  3877. $this->update_queue[$this->current] = $status;
  3878. return $res;
  3879. }
  3880. return false;
  3881. }
  3882. /**
  3883. * Saves the given item
  3884. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3885. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3886. * @return boolean
  3887. */
  3888. public function save_item($item_id = null, $from_outside = true)
  3889. {
  3890. $debug = $this->debug;
  3891. if ($debug) {
  3892. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3893. }
  3894. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3895. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3896. if (empty($item_id)) {
  3897. $item_id = intval($_REQUEST['id']);
  3898. }
  3899. if (empty($item_id)) {
  3900. $item_id = $this->get_current_item_id();
  3901. }
  3902. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3903. if ($debug) {
  3904. error_log('Object exists');
  3905. }
  3906. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3907. if ($debug) {
  3908. error_log('update_queue before:');
  3909. error_log(print_r($this->update_queue, 1));
  3910. }
  3911. $this->autocomplete_parents($item_id);
  3912. $status = $this->items[$item_id]->get_status();
  3913. $this->update_queue[$item_id] = $status;
  3914. if ($debug) {
  3915. error_log('get_status(): '.$status);
  3916. error_log('update_queue after:');
  3917. error_log(print_r($this->update_queue, 1));
  3918. }
  3919. return $res;
  3920. }
  3921. return false;
  3922. }
  3923. /**
  3924. * Saves the last item seen's ID only in case
  3925. */
  3926. public function save_last()
  3927. {
  3928. $course_id = api_get_course_int_id();
  3929. if ($this->debug > 0) {
  3930. error_log('New LP - In learnpath::save_last()', 0);
  3931. }
  3932. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3933. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3934. if (isset($this->current)) {
  3935. if ($this->debug > 2) {
  3936. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3937. }
  3938. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3939. WHERE c_id = $course_id AND
  3940. lp_id = ".$this->get_id()." AND
  3941. user_id = ".$this->get_user_id()." ".$session_condition;
  3942. if ($this->debug > 2) {
  3943. error_log('New LP - Saving last item seen : '.$sql, 0);
  3944. }
  3945. $res = Database::query($sql);
  3946. }
  3947. // Save progress.
  3948. list($progress, $text) = $this->get_progress_bar_text('%');
  3949. if ($progress >= 0 && $progress <= 100) {
  3950. $progress = (int)$progress;
  3951. $sql = "UPDATE $table SET progress = $progress
  3952. WHERE c_id = ".$course_id." AND
  3953. lp_id = ".$this->get_id()." AND
  3954. user_id = ".$this->get_user_id()." ".$session_condition;
  3955. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3956. $this->progress_db = $progress;
  3957. }
  3958. }
  3959. /**
  3960. * Sets the current item ID (checks if valid and authorized first)
  3961. * @param integer New item ID. If not given or not authorized, defaults to current
  3962. */
  3963. public function set_current_item($item_id = null)
  3964. {
  3965. if ($this->debug > 0) {
  3966. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3967. }
  3968. if (empty ($item_id)) {
  3969. if ($this->debug > 2) {
  3970. error_log('New LP - No new current item given, ignore...', 0);
  3971. }
  3972. // Do nothing.
  3973. } else {
  3974. if ($this->debug > 2) {
  3975. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3976. }
  3977. if (is_numeric($item_id)) {
  3978. $item_id = Database::escape_string($item_id);
  3979. // TODO: Check in database here.
  3980. $this->last = $this->current;
  3981. $this->current = $item_id;
  3982. // TODO: Update $this->index as well.
  3983. foreach ($this->ordered_items as $index => $item) {
  3984. if ($item == $this->current) {
  3985. $this->index = $index;
  3986. break;
  3987. }
  3988. }
  3989. if ($this->debug > 2) {
  3990. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  3991. }
  3992. } else {
  3993. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  3994. }
  3995. }
  3996. }
  3997. /**
  3998. * Sets the encoding
  3999. * @param string New encoding
  4000. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4001. */
  4002. public function set_encoding($enc = 'UTF-8')
  4003. {
  4004. if ($this->debug > 0) {
  4005. error_log('New LP - In learnpath::set_encoding()', 0);
  4006. }
  4007. $course_id = api_get_course_int_id();
  4008. /* // Deprecated code (Chamilo 1.8.8).
  4009. $enc = strtoupper($enc);
  4010. $encodings = array (
  4011. 'UTF-8',
  4012. 'ISO-8859-1',
  4013. 'ISO-8859-15',
  4014. 'cp1251',
  4015. 'cp1252',
  4016. 'KOI8-R',
  4017. 'BIG5',
  4018. 'GB2312',
  4019. 'Shift_JIS',
  4020. 'EUC-JP',
  4021. ''
  4022. );
  4023. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  4024. $lp = $this->get_id();
  4025. if ($lp != 0) {
  4026. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4027. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4028. $res = Database::query($sql);
  4029. return $res;
  4030. }
  4031. }
  4032. return false;
  4033. */
  4034. $enc = api_refine_encoding_id($enc);
  4035. if (empty($enc)) {
  4036. $enc = api_get_system_encoding();
  4037. }
  4038. if (api_is_encoding_supported($enc)) {
  4039. $lp = $this->get_id();
  4040. if ($lp != 0) {
  4041. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4042. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4043. $res = Database::query($sql);
  4044. return $res;
  4045. }
  4046. }
  4047. return false;
  4048. }
  4049. /**
  4050. * Sets the JS lib setting in the database directly.
  4051. * This is the JavaScript library file this lp needs to load on startup
  4052. * @param string Proximity setting
  4053. * @return boolean True on update success. False otherwise.
  4054. */
  4055. public function set_jslib($lib = '')
  4056. {
  4057. if ($this->debug > 0) {
  4058. error_log('New LP - In learnpath::set_jslib()', 0);
  4059. }
  4060. $lp = $this->get_id();
  4061. $course_id = api_get_course_int_id();
  4062. if ($lp != 0) {
  4063. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4064. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4065. $res = Database::query($sql);
  4066. return $res;
  4067. } else {
  4068. return false;
  4069. }
  4070. }
  4071. /**
  4072. * Sets the name of the LP maker (publisher) (and save)
  4073. * @param string Optional string giving the new content_maker of this learnpath
  4074. * @return boolean True
  4075. */
  4076. public function set_maker($name = '')
  4077. {
  4078. if ($this->debug > 0) {
  4079. error_log('New LP - In learnpath::set_maker()', 0);
  4080. }
  4081. if (empty ($name)) {
  4082. return false;
  4083. }
  4084. $this->maker = Database::escape_string($name);
  4085. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4086. $course_id = api_get_course_int_id();
  4087. $lp_id = $this->get_id();
  4088. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4089. if ($this->debug > 2) {
  4090. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4091. }
  4092. Database::query($sql);
  4093. return true;
  4094. }
  4095. /**
  4096. * Sets the name of the current learnpath (and save)
  4097. * @param string Optional string giving the new name of this learnpath
  4098. * @return boolean True/False
  4099. */
  4100. public function set_name($name = null)
  4101. {
  4102. if ($this->debug > 0) {
  4103. error_log('New LP - In learnpath::set_name()', 0);
  4104. }
  4105. if (empty($name)) {
  4106. return false;
  4107. }
  4108. $this->name = Database::escape_string($name);
  4109. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4110. $lp_id = $this->get_id();
  4111. $course_id = api_get_course_int_id();
  4112. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4113. if ($this->debug > 2) {
  4114. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4115. }
  4116. $result = Database::query($sql);
  4117. // If the lp is visible on the homepage, change his name there.
  4118. if (Database::affected_rows($result)) {
  4119. $session_id = api_get_session_id();
  4120. $session_condition = api_get_session_condition($session_id);
  4121. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4122. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4123. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4124. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4125. Database::query($sql);
  4126. return true;
  4127. } else {
  4128. return false;
  4129. }
  4130. }
  4131. /**
  4132. * Set index specified prefix terms for all items in this path
  4133. * @param string Comma-separated list of terms
  4134. * @param char Xapian term prefix
  4135. * @return boolean False on error, true otherwise
  4136. */
  4137. public function set_terms_by_prefix($terms_string, $prefix)
  4138. {
  4139. $course_id = api_get_course_int_id();
  4140. if (api_get_setting('search_enabled') !== 'true') {
  4141. return false;
  4142. }
  4143. if (!extension_loaded('xapian')) {
  4144. return false;
  4145. }
  4146. $terms_string = trim($terms_string);
  4147. $terms = explode(',', $terms_string);
  4148. array_walk($terms, 'trim_value');
  4149. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4150. // Don't do anything if no change, verify only at DB, not the search engine.
  4151. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4152. return false;
  4153. }
  4154. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4155. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4156. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4157. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4158. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4159. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4160. $lp_id = intval($_POST['lp_id']);
  4161. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4162. $result = Database::query($sql);
  4163. $di = new ChamiloIndexer();
  4164. while ($lp_item = Database :: fetch_array($result)) {
  4165. // Get search_did.
  4166. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4167. $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';
  4168. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4169. //echo $sql; echo '<br>';
  4170. $res = Database::query($sql);
  4171. if (Database::num_rows($res) > 0) {
  4172. $se_ref = Database :: fetch_array($res);
  4173. // Compare terms.
  4174. $doc = $di->get_document($se_ref['search_did']);
  4175. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4176. $xterms = array();
  4177. foreach ($xapian_terms as $xapian_term) {
  4178. $xterms[] = substr($xapian_term['name'], 1);
  4179. }
  4180. $dterms = $terms;
  4181. $missing_terms = array_diff($dterms, $xterms);
  4182. $deprecated_terms = array_diff($xterms, $dterms);
  4183. // Save it to search engine.
  4184. foreach ($missing_terms as $term) {
  4185. $doc->add_term($prefix.$term, 1);
  4186. }
  4187. foreach ($deprecated_terms as $term) {
  4188. $doc->remove_term($prefix.$term);
  4189. }
  4190. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4191. $di->getDb()->flush();
  4192. } else {
  4193. //@todo What we should do here?
  4194. }
  4195. }
  4196. return true;
  4197. }
  4198. /**
  4199. * Sets the theme of the LP (local/remote) (and save)
  4200. * @param string Optional string giving the new theme of this learnpath
  4201. * @return bool Returns true if theme name is not empty
  4202. */
  4203. public function set_theme($name = '')
  4204. {
  4205. $course_id = api_get_course_int_id();
  4206. if ($this->debug > 0) {
  4207. error_log('New LP - In learnpath::set_theme()', 0);
  4208. }
  4209. $this->theme = Database::escape_string($name);
  4210. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4211. $lp_id = $this->get_id();
  4212. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4213. if ($this->debug > 2) {
  4214. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4215. }
  4216. Database::query($sql);
  4217. return true;
  4218. }
  4219. /**
  4220. * Sets the image of an LP (and save)
  4221. * @param string Optional string giving the new image of this learnpath
  4222. * @return bool Returns true if theme name is not empty
  4223. */
  4224. public function set_preview_image($name = '')
  4225. {
  4226. $course_id = api_get_course_int_id();
  4227. if ($this->debug > 0) {
  4228. error_log('New LP - In learnpath::set_preview_image()', 0);
  4229. }
  4230. $this->preview_image = Database::escape_string($name);
  4231. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4232. $lp_id = $this->get_id();
  4233. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4234. if ($this->debug > 2) {
  4235. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4236. }
  4237. Database::query($sql);
  4238. return true;
  4239. }
  4240. /**
  4241. * Sets the author of a LP (and save)
  4242. * @param string Optional string giving the new author of this learnpath
  4243. * @return bool Returns true if author's name is not empty
  4244. */
  4245. public function set_author($name = '')
  4246. {
  4247. $course_id = api_get_course_int_id();
  4248. if ($this->debug > 0) {
  4249. error_log('New LP - In learnpath::set_author()', 0);
  4250. }
  4251. $this->author = Database::escape_string($name);
  4252. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4253. $lp_id = $this->get_id();
  4254. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4255. if ($this->debug > 2) {
  4256. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4257. }
  4258. Database::query($sql);
  4259. return true;
  4260. }
  4261. /**
  4262. * Sets the hide_toc_frame parameter of a LP (and save)
  4263. * @param int 1 if frame is hiddent 0 thenelse
  4264. * @return bool Returns true if author's name is not empty
  4265. */
  4266. public function set_hide_toc_frame($hide)
  4267. {
  4268. $course_id = api_get_course_int_id();
  4269. if ($this->debug > 0) {
  4270. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4271. }
  4272. if (intval($hide) == $hide) {
  4273. $this->hide_toc_frame = $hide;
  4274. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4275. $lp_id = $this->get_id();
  4276. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4277. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4278. if ($this->debug > 2) {
  4279. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4280. }
  4281. Database::query($sql);
  4282. return true;
  4283. } else {
  4284. return false;
  4285. }
  4286. }
  4287. /**
  4288. * Sets the prerequisite of a LP (and save)
  4289. * @param int integer giving the new prerequisite of this learnpath
  4290. * @return bool returns true if prerequisite is not empty
  4291. */
  4292. public function set_prerequisite($prerequisite)
  4293. {
  4294. $course_id = api_get_course_int_id();
  4295. if ($this->debug > 0) {
  4296. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4297. }
  4298. $this->prerequisite = intval($prerequisite);
  4299. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4300. $lp_id = $this->get_id();
  4301. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4302. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4303. if ($this->debug > 2) {
  4304. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4305. }
  4306. Database::query($sql);
  4307. return true;
  4308. }
  4309. /**
  4310. * Sets the location/proximity of the LP (local/remote) (and save)
  4311. * @param string Optional string giving the new location of this learnpath
  4312. * @return boolean True on success / False on error
  4313. */
  4314. public function set_proximity($name = '')
  4315. {
  4316. $course_id = api_get_course_int_id();
  4317. if ($this->debug > 0) {
  4318. error_log('New LP - In learnpath::set_proximity()', 0);
  4319. }
  4320. if (empty ($name)) {
  4321. return false;
  4322. }
  4323. $this->proximity = Database::escape_string($name);
  4324. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4325. $lp_id = $this->get_id();
  4326. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4327. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4328. if ($this->debug > 2) {
  4329. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4330. }
  4331. Database::query($sql);
  4332. return true;
  4333. }
  4334. /**
  4335. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4336. * @param integer DB ID of the item
  4337. */
  4338. public function set_previous_item($id)
  4339. {
  4340. if ($this->debug > 0) {
  4341. error_log('New LP - In learnpath::set_previous_item()', 0);
  4342. }
  4343. $this->last = $id;
  4344. }
  4345. public function get_max_attempts()
  4346. {
  4347. return $this->max_attempts;
  4348. }
  4349. public function get_subscribe_users()
  4350. {
  4351. return $this->subscribe_users;
  4352. }
  4353. public function check_attempts()
  4354. {
  4355. $max_attempts = $this->get_max_attempts();
  4356. switch ($max_attempts) {
  4357. case 0: //unlimited
  4358. return true;
  4359. break;
  4360. case $max_attempts >= 1:
  4361. if ($this->attempt <= $max_attempts) {
  4362. return true;
  4363. }
  4364. break;
  4365. }
  4366. return false;
  4367. }
  4368. function check_item_attempts($item_id)
  4369. {
  4370. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4371. $count = $this->items[$item_id]->get_view_count();
  4372. $max_attempts = $this->get_max_attempts();
  4373. switch ($max_attempts) {
  4374. case 0: //unlimited
  4375. return true;
  4376. break;
  4377. case $max_attempts >= 1:
  4378. if ($count <= $max_attempts) {
  4379. return true;
  4380. }
  4381. break;
  4382. }
  4383. return false;
  4384. }
  4385. }
  4386. public function set_max_attempts($attempts)
  4387. {
  4388. $mode = 'multiple';
  4389. if ($attempts == 1) {
  4390. $mode = 'single';
  4391. }
  4392. $this->max_attempts = intval($attempts);
  4393. $lp_id = $this->get_id();
  4394. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4395. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4396. Database::query($sql);
  4397. $this->set_attempt_mode($mode);
  4398. }
  4399. /**
  4400. * Sets subscribe_users
  4401. * @param string $value Optional string giving the new location of this learnpath
  4402. * @return bool True on success / False on error
  4403. */
  4404. public function set_subscribe_users($value = 0)
  4405. {
  4406. if ($this->debug > 0) {
  4407. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4408. }
  4409. $this->subscribe_users = intval($value);;
  4410. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4411. $lp_id = $this->get_id();
  4412. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4413. if ($this->debug > 2) {
  4414. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4415. }
  4416. Database::query($sql);
  4417. return true;
  4418. }
  4419. /**
  4420. * Sets use_max_score
  4421. * @param string Optional string giving the new location of this learnpath
  4422. * @return boolean True on success / False on error
  4423. */
  4424. public function set_use_max_score($use_max_score = 1)
  4425. {
  4426. if ($this->debug > 0) {
  4427. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4428. }
  4429. $this->use_max_score = intval($use_max_score);
  4430. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4431. $lp_id = $this->get_id();
  4432. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4433. if ($this->debug > 2) {
  4434. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4435. }
  4436. Database::query($sql);
  4437. return true;
  4438. }
  4439. /**
  4440. * Sets and saves the expired_on date
  4441. * @param string Optional string giving the new author of this learnpath
  4442. * @return bool Returns true if author's name is not empty
  4443. */
  4444. public function set_expired_on($expired_on)
  4445. {
  4446. $course_id = api_get_course_int_id();
  4447. if ($this->debug > 0) {
  4448. error_log('New LP - In learnpath::set_expired_on()', 0);
  4449. }
  4450. if (!empty($expired_on)) {
  4451. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4452. } else {
  4453. $this->expired_on = '';
  4454. }
  4455. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4456. $lp_id = $this->get_id();
  4457. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4458. if ($this->debug > 2) {
  4459. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4460. }
  4461. Database::query($sql);
  4462. return true;
  4463. }
  4464. /**
  4465. * Sets and saves the publicated_on date
  4466. * @param string Optional string giving the new author of this learnpath
  4467. * @return bool Returns true if author's name is not empty
  4468. */
  4469. public function set_publicated_on($publicated_on)
  4470. {
  4471. $course_id = api_get_course_int_id();
  4472. if ($this->debug > 0) {
  4473. error_log('New LP - In learnpath::set_expired_on()', 0);
  4474. }
  4475. if (!empty($publicated_on)) {
  4476. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4477. } else {
  4478. $this->publicated_on = '';
  4479. }
  4480. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4481. $lp_id = $this->get_id();
  4482. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4483. if ($this->debug > 2) {
  4484. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4485. }
  4486. Database::query($sql);
  4487. return true;
  4488. }
  4489. public function set_category_id($category_id)
  4490. {
  4491. $this->category_id = $category_id;
  4492. $course_id = api_get_course_int_id();
  4493. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4494. $lp_id = $this->get_id();
  4495. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4496. $category_id
  4497. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4498. Database::query($sql);
  4499. return true;
  4500. }
  4501. /**
  4502. * Sets and saves the expired_on date
  4503. * @param string Optional string giving the new author of this learnpath
  4504. * @return bool Returns true if author's name is not empty
  4505. */
  4506. public function set_modified_on()
  4507. {
  4508. $course_id = api_get_course_int_id();
  4509. if ($this->debug > 0) {
  4510. error_log('New LP - In learnpath::set_expired_on()', 0);
  4511. }
  4512. $this->modified_on = api_get_utc_datetime();
  4513. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4514. $lp_id = $this->get_id();
  4515. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4516. if ($this->debug > 2) {
  4517. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4518. }
  4519. Database::query($sql);
  4520. return true;
  4521. }
  4522. /**
  4523. * Sets the object's error message
  4524. * @param string Error message. If empty, reinits the error string
  4525. * @return void
  4526. */
  4527. public function set_error_msg($error = '')
  4528. {
  4529. if ($this->debug > 0) {
  4530. error_log('New LP - In learnpath::set_error_msg()', 0);
  4531. }
  4532. if (empty ($error)) {
  4533. $this->error = '';
  4534. } else {
  4535. $this->error .= $error;
  4536. }
  4537. }
  4538. /**
  4539. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4540. * @param boolean Whether to allow a new attempt or not
  4541. * @return boolean True
  4542. */
  4543. public function start_current_item($allow_new_attempt = false)
  4544. {
  4545. if ($this->debug > 0) {
  4546. error_log('New LP - In learnpath::start_current_item()', 0);
  4547. }
  4548. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4549. $type = $this->get_type();
  4550. $item_type = $this->items[$this->current]->get_type();
  4551. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4552. $this->items[$this->current]->open($allow_new_attempt);
  4553. $this->autocomplete_parents($this->current);
  4554. $prereq_check = $this->prerequisites_match($this->current);
  4555. $this->items[$this->current]->save(false, $prereq_check);
  4556. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4557. } else {
  4558. // If sco, then it is supposed to have been updated by some other call.
  4559. }
  4560. if ($item_type == 'sco') {
  4561. $this->items[$this->current]->restart();
  4562. }
  4563. }
  4564. if ($this->debug > 0) {
  4565. error_log('New LP - End of learnpath::start_current_item()', 0);
  4566. }
  4567. return true;
  4568. }
  4569. /**
  4570. * Stops the processing and counters for the old item (as held in $this->last)
  4571. * @return boolean True/False
  4572. */
  4573. public function stop_previous_item()
  4574. {
  4575. if ($this->debug > 0) {
  4576. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4577. }
  4578. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4579. if ($this->debug > 2) {
  4580. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4581. }
  4582. switch ($this->get_type()) {
  4583. case '3' :
  4584. if ($this->items[$this->last]->get_type() != 'au') {
  4585. if ($this->debug > 2) {
  4586. error_log(
  4587. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4588. 0
  4589. );
  4590. }
  4591. $this->items[$this->last]->close();
  4592. //$this->autocomplete_parents($this->last);
  4593. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4594. } else {
  4595. if ($this->debug > 2) {
  4596. error_log(
  4597. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4598. 0
  4599. );
  4600. }
  4601. }
  4602. case '2' :
  4603. if ($this->items[$this->last]->get_type() != 'sco') {
  4604. if ($this->debug > 2) {
  4605. error_log(
  4606. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4607. 0
  4608. );
  4609. }
  4610. $this->items[$this->last]->close();
  4611. //$this->autocomplete_parents($this->last);
  4612. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4613. } else {
  4614. if ($this->debug > 2) {
  4615. error_log(
  4616. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4617. 0
  4618. );
  4619. }
  4620. }
  4621. break;
  4622. case '1' :
  4623. default :
  4624. if ($this->debug > 2) {
  4625. error_log(
  4626. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4627. 0
  4628. );
  4629. }
  4630. $this->items[$this->last]->close();
  4631. break;
  4632. }
  4633. } else {
  4634. if ($this->debug > 2) {
  4635. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4636. }
  4637. return false;
  4638. }
  4639. return true;
  4640. }
  4641. /**
  4642. * Updates the default view mode from fullscreen to embedded and inversely
  4643. * @return string The current default view mode ('fullscreen' or 'embedded')
  4644. */
  4645. public function update_default_view_mode()
  4646. {
  4647. $course_id = api_get_course_int_id();
  4648. if ($this->debug > 0) {
  4649. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4650. }
  4651. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4652. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4653. $res = Database::query($sql);
  4654. if (Database :: num_rows($res) > 0) {
  4655. $row = Database :: fetch_array($res);
  4656. $default_view_mode = $row['default_view_mod'];
  4657. $view_mode = $default_view_mode;
  4658. switch ($default_view_mode) {
  4659. case 'fullscreen':
  4660. $view_mode = 'embedded';
  4661. break;
  4662. case 'embedded':
  4663. $view_mode = 'embedframe';
  4664. break;
  4665. case 'embedframe':
  4666. $view_mode = 'impress';
  4667. break;
  4668. case 'impress':
  4669. $view_mode = 'fullscreen';
  4670. break;
  4671. }
  4672. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4673. );
  4674. $res = Database::query($sql);
  4675. $this->mode = $view_mode;
  4676. return $view_mode;
  4677. } else {
  4678. if ($this->debug > 2) {
  4679. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4680. }
  4681. }
  4682. return -1;
  4683. }
  4684. /**
  4685. * Updates the default behaviour about auto-commiting SCORM updates
  4686. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4687. */
  4688. public function update_default_scorm_commit()
  4689. {
  4690. $course_id = api_get_course_int_id();
  4691. if ($this->debug > 0) {
  4692. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4693. }
  4694. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4695. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4696. $res = Database::query($sql);
  4697. if (Database :: num_rows($res) > 0) {
  4698. $row = Database :: fetch_array($res);
  4699. $force = $row['force_commit'];
  4700. if ($force == 1) {
  4701. $force = 0;
  4702. $force_return = false;
  4703. } elseif ($force == 0) {
  4704. $force = 1;
  4705. $force_return = true;
  4706. }
  4707. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4708. $res = Database::query($sql);
  4709. $this->force_commit = $force_return;
  4710. return $force_return;
  4711. } else {
  4712. if ($this->debug > 2) {
  4713. error_log(
  4714. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4715. 0
  4716. );
  4717. }
  4718. }
  4719. return -1;
  4720. }
  4721. /**
  4722. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4723. * @return bool True on success, false on failure
  4724. */
  4725. public function update_display_order()
  4726. {
  4727. $course_id = api_get_course_int_id();
  4728. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4729. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4730. $res = Database::query($sql);
  4731. if ($res === false) {
  4732. return false;
  4733. }
  4734. $lps = array();
  4735. $lp_order = array();
  4736. $num = Database :: num_rows($res);
  4737. // First check the order is correct, globally (might be wrong because
  4738. // of versions < 1.8.4).
  4739. if ($num > 0) {
  4740. $i = 1;
  4741. while ($row = Database :: fetch_array($res)) {
  4742. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4743. $need_fix = true;
  4744. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4745. $res_u = Database::query($sql_u);
  4746. }
  4747. $i++;
  4748. }
  4749. }
  4750. return true;
  4751. }
  4752. /**
  4753. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4754. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4755. */
  4756. public function update_reinit()
  4757. {
  4758. $course_id = api_get_course_int_id();
  4759. if ($this->debug > 0) {
  4760. error_log('New LP - In learnpath::update_reinit()', 0);
  4761. }
  4762. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4763. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4764. $res = Database::query($sql);
  4765. if (Database :: num_rows($res) > 0) {
  4766. $row = Database :: fetch_array($res);
  4767. $force = $row['prevent_reinit'];
  4768. if ($force == 1) {
  4769. $force = 0;
  4770. } elseif ($force == 0) {
  4771. $force = 1;
  4772. }
  4773. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4774. $res = Database::query($sql);
  4775. $this->prevent_reinit = $force;
  4776. return $force;
  4777. } else {
  4778. if ($this->debug > 2) {
  4779. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4780. }
  4781. }
  4782. return -1;
  4783. }
  4784. /**
  4785. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4786. *
  4787. * @return string 'single', 'multi' or 'seriousgame'
  4788. * @author ndiechburg <noel@cblue.be>
  4789. **/
  4790. public function get_attempt_mode()
  4791. {
  4792. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4793. $this->seriousgame_mode = 0;
  4794. }
  4795. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4796. $this->prevent_reinit = 1;
  4797. }
  4798. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4799. return 'seriousgame';
  4800. }
  4801. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4802. return 'single';
  4803. }
  4804. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4805. return 'multiple';
  4806. }
  4807. return 'single';
  4808. }
  4809. /**
  4810. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4811. *
  4812. * @param string 'seriousgame', 'single' or 'multiple'
  4813. * @return boolean
  4814. * @author ndiechburg <noel@cblue.be>
  4815. **/
  4816. public function set_attempt_mode($mode)
  4817. {
  4818. $course_id = api_get_course_int_id();
  4819. switch ($mode) {
  4820. case 'seriousgame' :
  4821. $sg_mode = 1;
  4822. $prevent_reinit = 1;
  4823. break;
  4824. case 'single' :
  4825. $sg_mode = 0;
  4826. $prevent_reinit = 1;
  4827. break;
  4828. case 'multiple' :
  4829. $sg_mode = 0;
  4830. $prevent_reinit = 0;
  4831. break;
  4832. default :
  4833. $sg_mode = 0;
  4834. $prevent_reinit = 0;
  4835. break;
  4836. }
  4837. $this->prevent_reinit = $prevent_reinit;
  4838. $this->seriousgame_mode = $sg_mode;
  4839. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4840. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4841. );
  4842. $res = Database::query($sql);
  4843. if ($res) {
  4844. return true;
  4845. } else {
  4846. return false;
  4847. }
  4848. }
  4849. /**
  4850. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4851. *
  4852. * @return boolean
  4853. * @author ndiechburg <noel@cblue.be>
  4854. **/
  4855. public function switch_attempt_mode()
  4856. {
  4857. if ($this->debug > 0) {
  4858. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4859. }
  4860. $mode = $this->get_attempt_mode();
  4861. switch ($mode) {
  4862. case 'single' :
  4863. $next_mode = 'multiple';
  4864. break;
  4865. case 'multiple' :
  4866. $next_mode = 'seriousgame';
  4867. break;
  4868. case 'seriousgame' :
  4869. $next_mode = 'single';
  4870. break;
  4871. default :
  4872. $next_mode = 'single';
  4873. break;
  4874. }
  4875. $this->set_attempt_mode($next_mode);
  4876. }
  4877. /**
  4878. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4879. *
  4880. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4881. * @author ndiechburg <noel@cblue.be>
  4882. **/
  4883. public function set_seriousgame_mode()
  4884. {
  4885. $course_id = api_get_course_int_id();
  4886. if ($this->debug > 0) {
  4887. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4888. }
  4889. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4890. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4891. $res = Database::query($sql);
  4892. if (Database :: num_rows($res) > 0) {
  4893. $row = Database :: fetch_array($res);
  4894. $force = $row['seriousgame_mode'];
  4895. if ($force == 1) {
  4896. $force = 0;
  4897. } elseif ($force == 0) {
  4898. $force = 1;
  4899. }
  4900. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4901. );
  4902. $res = Database::query($sql);
  4903. $this->seriousgame_mode = $force;
  4904. return $force;
  4905. } else {
  4906. if ($this->debug > 2) {
  4907. error_log(
  4908. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4909. 0
  4910. );
  4911. }
  4912. }
  4913. return -1;
  4914. }
  4915. /**
  4916. * Updates the "scorm_debug" value that shows or hide the debug window
  4917. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4918. */
  4919. public function update_scorm_debug()
  4920. {
  4921. $course_id = api_get_course_int_id();
  4922. if ($this->debug > 0) {
  4923. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4924. }
  4925. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4926. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4927. $res = Database::query($sql);
  4928. if (Database :: num_rows($res) > 0) {
  4929. $row = Database :: fetch_array($res);
  4930. $force = $row['debug'];
  4931. if ($force == 1) {
  4932. $force = 0;
  4933. } elseif ($force == 0) {
  4934. $force = 1;
  4935. }
  4936. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4937. $res = Database::query($sql);
  4938. $this->scorm_debug = $force;
  4939. return $force;
  4940. } else {
  4941. if ($this->debug > 2) {
  4942. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4943. }
  4944. }
  4945. return -1;
  4946. }
  4947. /**
  4948. * Function that makes a call to the function sort_tree_array and create_tree_array
  4949. * @author Kevin Van Den Haute
  4950. * @param array
  4951. */
  4952. public function tree_array($array)
  4953. {
  4954. if ($this->debug > 1) {
  4955. error_log('New LP - In learnpath::tree_array()', 0);
  4956. }
  4957. $array = $this->sort_tree_array($array);
  4958. $this->create_tree_array($array);
  4959. }
  4960. /**
  4961. * Creates an array with the elements of the learning path tree in it
  4962. *
  4963. * @author Kevin Van Den Haute
  4964. * @param array $array
  4965. * @param int $parent
  4966. * @param int $depth
  4967. * @param array $tmp
  4968. */
  4969. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4970. {
  4971. if ($this->debug > 1) {
  4972. error_log('New LP - In learnpath::create_tree_array())', 0);
  4973. }
  4974. if (is_array($array)) {
  4975. for ($i = 0; $i < count($array); $i++) {
  4976. if ($array[$i]['parent_item_id'] == $parent) {
  4977. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4978. $tmp[] = $array[$i]['parent_item_id'];
  4979. $depth++;
  4980. }
  4981. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4982. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4983. $this->arrMenu[] = array(
  4984. 'id' => $array[$i]['id'],
  4985. 'item_type' => $array[$i]['item_type'],
  4986. 'title' => $array[$i]['title'],
  4987. 'path' => $array[$i]['path'],
  4988. 'description' => $array[$i]['description'],
  4989. 'parent_item_id' => $array[$i]['parent_item_id'],
  4990. 'previous_item_id' => $array[$i]['previous_item_id'],
  4991. 'next_item_id' => $array[$i]['next_item_id'],
  4992. 'min_score' => $array[$i]['min_score'],
  4993. 'max_score' => $array[$i]['max_score'],
  4994. 'mastery_score' => $array[$i]['mastery_score'],
  4995. 'display_order' => $array[$i]['display_order'],
  4996. 'prerequisite' => $preq,
  4997. 'depth' => $depth,
  4998. 'audio' => $audio
  4999. );
  5000. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5001. }
  5002. }
  5003. }
  5004. }
  5005. /**
  5006. * Sorts a multi dimensional array by parent id and display order
  5007. * @author Kevin Van Den Haute
  5008. *
  5009. * @param array $array (array with al the learning path items in it)
  5010. *
  5011. * @return array
  5012. */
  5013. public function sort_tree_array($array)
  5014. {
  5015. foreach ($array as $key => $row) {
  5016. $parent[$key] = $row['parent_item_id'];
  5017. $position[$key] = $row['display_order'];
  5018. }
  5019. if (count($array) > 0) {
  5020. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5021. }
  5022. return $array;
  5023. }
  5024. /**
  5025. * Function that creates a table structure with a learning path his modules, chapters and documents.
  5026. * Also the actions for the modules, chapters and documents are in this table.
  5027. * @author Kevin Van Den Haute
  5028. * @param int $lp_id
  5029. * @return string
  5030. */
  5031. public function overview()
  5032. {
  5033. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5034. if ($this->debug > 0) {
  5035. error_log('New LP - In learnpath::overview()', 0);
  5036. }
  5037. $_course = api_get_course_info();
  5038. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5039. $return = '';
  5040. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5041. // we need to start a form when we want to update all the mp3 files
  5042. if ($update_audio == 'true') {
  5043. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5044. $_GET['cidReq']
  5045. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5046. $_GET['action']
  5047. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5048. }
  5049. $return .= '<div id="message"></div>';
  5050. $return_audio = '<table class="data_table">';
  5051. $return_audio .= '<tr>';
  5052. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5053. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5054. $return_audio .= '</tr>';
  5055. if ($update_audio != 'true') {
  5056. $return .= '<div class="span12">';
  5057. $return .= self::return_new_tree($update_audio);
  5058. $return .= '</div>';
  5059. $return .= Display::div(
  5060. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5061. array('style' => 'float:left; margin-top:15px;width:100%')
  5062. );
  5063. } else {
  5064. $return_audio .= self::return_new_tree($update_audio);
  5065. $return .= $return_audio.'</table>';
  5066. }
  5067. // We need to close the form when we are updating the mp3 files.
  5068. if ($update_audio == 'true') {
  5069. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5070. 'SaveAudioAndOrganization'
  5071. ).'</button></div>'; // TODO: What kind of language variable is this?
  5072. }
  5073. // We need to close the form when we are updating the mp3 files.
  5074. if ($update_audio == 'true' && count($arrLP) != 0) {
  5075. $return .= '</form>';
  5076. }
  5077. return $return;
  5078. }
  5079. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5080. {
  5081. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5082. echo '<script>
  5083. var newOrderData= "";
  5084. function processChildren(parentId) {
  5085. //Loop through the children of the UL element defined by the parentId
  5086. var ulParentID= "UL_" + parentId;
  5087. $("#" + ulParentID).children().each(function () {
  5088. /*Only process elements with an id attribute (in order to skip the blank,
  5089. unmovable <li> elements.*/
  5090. if ($(this).attr("id")) {
  5091. /*Build a string of data with the childs ID and parent ID,
  5092. using the "|" as a delimiter between the two IDs and the "^"
  5093. as a record delimiter (these delimiters were chosen in case the data
  5094. involved includes more common delimiters like commas within the content)
  5095. */
  5096. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5097. //Determine if this child is a containter
  5098. if ($(this).is(".container")) {
  5099. //Process the child elements of the container
  5100. processChildren($(this).attr("id"));
  5101. }
  5102. }
  5103. }); //end of children loop
  5104. } //end of processChildren function
  5105. $(function() {
  5106. $(".item_data").live("mouseover", function(event) {
  5107. $(".button_actions", this).show();
  5108. });
  5109. $(".item_data").live("mouseout", function() {
  5110. $(".button_actions",this).hide();
  5111. });
  5112. $(".button_actions").hide();
  5113. $( ".lp_resource" ).sortable({
  5114. items: ".lp_resource_element ",
  5115. handle: ".moved", //only the class "moved"
  5116. cursor: "move",
  5117. connectWith: "#lp_item_list",
  5118. placeholder: "ui-state-highlight", //defines the yellow highlight
  5119. start: function(event, ui) {
  5120. $(ui.item).css("width", "160px");
  5121. $(ui.item).find(".item_data").attr("style", "");
  5122. },
  5123. stop: function(event, ui) {
  5124. $(ui.item).css("width", "100%");
  5125. },
  5126. });
  5127. $("#lp_item_list").sortable({
  5128. items: "li",
  5129. handle: ".moved", //only the class "moved"
  5130. cursor: "move",
  5131. placeholder: "ui-state-highlight", //defines the yellow highlight
  5132. update: function(event, ui) {
  5133. //Walk through the direct descendants of the lp_item_list <ul>
  5134. $("#lp_item_list").children().each(function () {
  5135. /*Only process elements with an id attribute (in order to skip the blank,
  5136. unmovable <li> elements.*/
  5137. if ($(this).attr("id")) {
  5138. /*Build a string of data with the child s ID and parent ID,
  5139. using the "|" as a delimiter between the two IDs and the "^"
  5140. as a record delimiter (these delimiters were chosen in case the data
  5141. involved includes more common delimiters like commas within the content)
  5142. */
  5143. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5144. //Determine if this child is a containter
  5145. if ($(this).is(".li_container")) {
  5146. //Process the child elements of the container
  5147. processChildren($(this).attr("id"));
  5148. }
  5149. }
  5150. }); //end of lp_item_list children loop
  5151. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5152. $.post("'.$ajax_url.'", order, function(reponse){
  5153. $("#message").html(reponse);
  5154. });
  5155. },
  5156. receive: function(event, ui) {
  5157. var id = $(ui.item).attr("data_id");
  5158. var type = $(ui.item).attr("data_type");
  5159. var title = $(ui.item).attr("title");
  5160. if (ui.item.parent()[0]) {
  5161. var parent_id = $(ui.item.parent()[0]).attr("id");
  5162. var previous_id = $(ui.item.prev()).attr("id");
  5163. if (parent_id) {
  5164. parent_id = parent_id.split("_")[1];
  5165. var params = {
  5166. "a": "add_lp_item",
  5167. "id": id,
  5168. "parent_id": parent_id,
  5169. "previous_id": previous_id,
  5170. "type": type,
  5171. "title" : title
  5172. };
  5173. $.ajax({
  5174. type: "GET",
  5175. url: "'.$ajax_url.'",
  5176. data: params,
  5177. async: false,
  5178. success: function(data) {
  5179. if (data == -1) {
  5180. } else {
  5181. $(".normal-message").hide();
  5182. $(ui.item).attr("id", data);
  5183. $(ui.item).addClass("lp_resource_element_new");
  5184. $(ui.item).find(".item_data").attr("style", "");
  5185. $(ui.item).addClass("record li_container");
  5186. $(ui.item).removeClass("lp_resource_element");
  5187. $(ui.item).removeClass("doc_resource");
  5188. }
  5189. }
  5190. });
  5191. }
  5192. }//
  5193. }//end receive
  5194. });
  5195. });
  5196. </script>';
  5197. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5198. $course_id = api_get_course_int_id();
  5199. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5200. $sql = "SELECT * FROM $tbl_lp_item
  5201. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5202. $result = Database::query($sql);
  5203. $arrLP = array();
  5204. while ($row = Database :: fetch_array($result)) {
  5205. $row['title'] = Security :: remove_XSS($row['title']);
  5206. $row['description'] = Security :: remove_XSS($row['description']);
  5207. $arrLP[] = array(
  5208. 'id' => $row['id'],
  5209. 'item_type' => $row['item_type'],
  5210. 'title' => $row['title'],
  5211. 'path' => $row['path'],
  5212. 'description' => $row['description'],
  5213. 'parent_item_id' => $row['parent_item_id'],
  5214. 'previous_item_id' => $row['previous_item_id'],
  5215. 'next_item_id' => $row['next_item_id'],
  5216. 'max_score' => $row['max_score'],
  5217. 'min_score' => $row['min_score'],
  5218. 'mastery_score' => $row['mastery_score'],
  5219. 'prerequisite' => $row['prerequisite'],
  5220. 'display_order' => $row['display_order'],
  5221. 'audio' => $row['audio']
  5222. );
  5223. }
  5224. $this->tree_array($arrLP);
  5225. $arrLP = $this->arrMenu;
  5226. unset ($this->arrMenu);
  5227. $default_data = null;
  5228. $default_content = null;
  5229. $elements = array();
  5230. $return_audio = null;
  5231. for ($i = 0; $i < count($arrLP); $i++) {
  5232. $title = $arrLP[$i]['title'];
  5233. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5234. //Link for the documents
  5235. if ($arrLP[$i]['item_type'] == 'document') {
  5236. $url = api_get_self().'?'.api_get_cidreq(
  5237. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5238. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5239. }
  5240. if (($i % 2) == 0) {
  5241. $oddclass = 'row_odd';
  5242. } else {
  5243. $oddclass = 'row_even';
  5244. }
  5245. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5246. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5247. $icon = '';
  5248. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5249. $icon = '<img src="../img/lp_'.$icon_name.'.png" />';
  5250. } else {
  5251. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5252. $icon = '<img src="../img/lp_'.$icon_name.'.gif" />';
  5253. } else {
  5254. $icon = '<img src="../img/folder_document.gif" />';
  5255. }
  5256. }
  5257. // The audio column.
  5258. $return_audio .= '<td align="center">';
  5259. $audio = '';
  5260. if (!$update_audio OR $update_audio <> 'true') {
  5261. if (!empty($arrLP[$i]['audio'])) {
  5262. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5263. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5264. $audio .= '<script type="text/javascript">
  5265. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5266. s1.addParam("allowscriptaccess","always");
  5267. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5268. s1.write("container' . $i . '");
  5269. </script>';*/
  5270. } else {
  5271. $audio .= '';
  5272. }
  5273. } else {
  5274. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5275. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5276. if (!empty ($arrLP[$i]['audio'])) {
  5277. $audio .= '<br />'.Security::remove_XSS(
  5278. $arrLP[$i]['audio']
  5279. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5280. 'RemoveAudio'
  5281. );
  5282. }
  5283. }
  5284. }
  5285. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5286. $return_audio .= '</td>';
  5287. $move_icon = '';
  5288. $move_item_icon = '';
  5289. $edit_icon = '';
  5290. $delete_icon = '';
  5291. $audio_icon = '';
  5292. $prerequisities_icon = '';
  5293. if ($is_allowed_to_edit) {
  5294. if (!$update_audio OR $update_audio <> 'true') {
  5295. $move_icon .= '<a class="moved" href="#">';
  5296. $move_icon .= Display::return_icon(
  5297. 'move_everywhere.png',
  5298. get_lang('Move'),
  5299. array(),
  5300. ICON_SIZE_TINY
  5301. );
  5302. $move_icon .= '</a>';
  5303. }
  5304. // No edit for this item types
  5305. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5306. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5307. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=edit_item&amp;view=build&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5308. $edit_icon .= Display::return_icon(
  5309. 'edit.png',
  5310. get_lang('LearnpathEditModule'),
  5311. array(),
  5312. ICON_SIZE_TINY
  5313. );
  5314. $edit_icon .= '</a>';
  5315. } else {
  5316. $edit_icon .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5317. $_GET['cidReq']
  5318. ).'&amp;action=edit_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5319. $edit_icon .= Display::return_icon(
  5320. 'edit.png',
  5321. get_lang('LearnpathEditModule'),
  5322. array(),
  5323. ICON_SIZE_TINY
  5324. );
  5325. $edit_icon .= '</a>';
  5326. }
  5327. }
  5328. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=delete_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'" onClick="return confirmation(\''.addslashes(
  5329. $title
  5330. ).'\');">';
  5331. $delete_icon .= Display::return_icon(
  5332. 'delete.png',
  5333. get_lang('LearnpathDeleteModule'),
  5334. array(),
  5335. ICON_SIZE_TINY
  5336. );
  5337. $delete_icon .= '</a>';
  5338. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5339. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5340. $prerequisities_icon = Display::url(
  5341. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5342. $url.'&action=edit_item_prereq'
  5343. );
  5344. $move_item_icon = Display::url(
  5345. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5346. $url.'&action=move_item'
  5347. );
  5348. $audio_icon = Display::url(
  5349. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5350. $url.'&action=add_audio'
  5351. );
  5352. }
  5353. }
  5354. if ($update_audio != 'true') {
  5355. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5356. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5357. array('class' => 'button_actions')
  5358. );
  5359. } else {
  5360. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5361. }
  5362. $parent_id = $arrLP[$i]['parent_item_id'];
  5363. $default_data[$arrLP[$i]['id']] = $row;
  5364. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5365. if (empty($parent_id)) {
  5366. $elements[$arrLP[$i]['id']]['data'] = $row;
  5367. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5368. } else {
  5369. $parent_arrays = array();
  5370. if ($arrLP[$i]['depth'] > 1) {
  5371. //Getting list of parents
  5372. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5373. foreach ($arrLP as $item) {
  5374. if ($item['id'] == $parent_id) {
  5375. if ($item['parent_item_id'] == 0) {
  5376. $parent_id = $item['id'];
  5377. break;
  5378. } else {
  5379. $parent_id = $item['parent_item_id'];
  5380. if (empty($parent_arrays)) {
  5381. $parent_arrays[] = intval($item['id']);
  5382. }
  5383. $parent_arrays[] = $parent_id;
  5384. break;
  5385. }
  5386. }
  5387. }
  5388. }
  5389. }
  5390. if (!empty($parent_arrays)) {
  5391. $parent_arrays = array_reverse($parent_arrays);
  5392. $val = '$elements';
  5393. $x = 0;
  5394. foreach ($parent_arrays as $item) {
  5395. if ($x != count($parent_arrays) - 1) {
  5396. $val .= '["'.$item.'"]["children"]';
  5397. } else {
  5398. $val .= '["'.$item.'"]["children"]';
  5399. }
  5400. $x++;
  5401. }
  5402. $val .= "";
  5403. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5404. eval($code_str);
  5405. } else {
  5406. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5407. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5408. }
  5409. }
  5410. }
  5411. $return = '<div class="lp_tree well">';
  5412. $return .= '<ul id="lp_item_list">';
  5413. $return .= '<h4>'.$this->name.'</h4><br>';
  5414. $tree = self::print_recursive($elements, $default_data, $default_content);
  5415. if (!empty($tree)) {
  5416. $return .= $tree;
  5417. } else {
  5418. if ($drop_element_here) {
  5419. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5420. }
  5421. }
  5422. $return .= '</ul>';
  5423. if ($update_audio == 'true') {
  5424. $return = $return_audio;
  5425. } else {
  5426. $return .= '</div>';
  5427. }
  5428. return $return;
  5429. }
  5430. function print_recursive($elements, $default_data, $default_content)
  5431. {
  5432. $return = '';
  5433. foreach ($elements as $key => $item) {
  5434. if (isset($item['load_data']) || empty($item['data'])) {
  5435. $item['data'] = $default_data[$item['load_data']];
  5436. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5437. }
  5438. $sub_list = '';
  5439. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5440. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5441. }
  5442. if (empty($item['children'])) {
  5443. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5444. $active = null;
  5445. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5446. $active = 'active';
  5447. }
  5448. $return .= Display::tag(
  5449. 'li',
  5450. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5451. array('id' => $key, 'class' => 'record li_container')
  5452. );
  5453. } else {
  5454. //sections
  5455. if (isset($item['children'])) {
  5456. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5457. }
  5458. $sub_list = Display::tag(
  5459. 'ul',
  5460. $sub_list.$data,
  5461. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5462. );
  5463. $return .= Display::tag(
  5464. 'li',
  5465. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5466. array('id' => $key, 'class' => 'record li_container')
  5467. );
  5468. }
  5469. }
  5470. return $return;
  5471. }
  5472. /**
  5473. * This function builds the action menu
  5474. * @return void
  5475. */
  5476. public function build_action_menu()
  5477. {
  5478. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5479. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5480. $return = '<div class="actions">';
  5481. //$return .= '<a href="lp_controller.php?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=build&lp_id=' . $this->lp_id . '">' . Display :: return_icon('home.png', get_lang('Build'),'',ICON_SIZE_MEDIUM).'</a>';
  5482. //$return .= '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('BasicOverview') . '">' . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  5483. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5484. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5485. 'preview_view.png',
  5486. get_lang('Display'),
  5487. '',
  5488. ICON_SIZE_MEDIUM
  5489. ).'</a> ';
  5490. //$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=step&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewStep') . '">' . Display :: return_icon('new_learnigpath_object.png', get_lang('NewStep'),'',ICON_SIZE_MEDIUM).'</a>';
  5491. // echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=chapter&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewChapter') . '">' . Display :: return_icon('add_learnpath_section.png', get_lang('NewChapter'),'',ICON_SIZE_MEDIUM).'</a>';
  5492. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5493. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5494. 'upload_audio.png',
  5495. get_lang('UpdateAllAudioFragments'),
  5496. '',
  5497. ICON_SIZE_MEDIUM
  5498. ).'</a>';*/
  5499. $return .= '<a href="lp_controller.php?referer=add_item&'.api_get_cidreq().'&amp;action=edit&amp;lp_id='.$_SESSION['oLP']->lp_id.'">'.Display :: return_icon(
  5500. 'settings.png',
  5501. get_lang('CourseSettings'),
  5502. '',
  5503. ICON_SIZE_MEDIUM
  5504. ).'</a>';
  5505. $buttons = array(
  5506. array(
  5507. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5508. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5509. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5510. ),
  5511. array(
  5512. 'title' => get_lang('ClearAllPrerequisites'),
  5513. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5514. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5515. ),
  5516. );
  5517. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5518. $return .= '</div>';
  5519. echo $return;
  5520. }
  5521. /**
  5522. * This functions builds the LP tree based on data from the database.
  5523. * @return string
  5524. * @deprecated use the return_new_tree() function
  5525. * @uses dtree.js :: necessary javascript for building this tree
  5526. */
  5527. public function build_tree()
  5528. {
  5529. $course_id = api_get_course_int_id();
  5530. $return = "<script type=\"text/javascript\">\n";
  5531. $return .= "\tm = new dTree('m');\n\n";
  5532. $return .= "\tm.config.folderLinks = true;\n";
  5533. $return .= "\tm.config.useCookies = true;\n";
  5534. $return .= "\tm.config.useIcons = true;\n";
  5535. $return .= "\tm.config.useLines = true;\n";
  5536. $return .= "\tm.config.useSelection = true;\n";
  5537. $return .= "\tm.config.useStatustext = false;\n\n";
  5538. $menu = 0;
  5539. $parent = '';
  5540. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5541. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5542. $sql = " SELECT id, title, description, item_type, path, parent_item_id, previous_item_id, next_item_id, max_score, min_score, mastery_score, display_order
  5543. FROM $tbl_lp_item
  5544. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5545. $result = Database::query($sql);
  5546. $arrLP = array();
  5547. while ($row = Database :: fetch_array($result)) {
  5548. $row['title'] = Security :: remove_XSS($row['title']);
  5549. $row['description'] = Security :: remove_XSS($row['description']);
  5550. $arrLP[] = array(
  5551. 'id' => $row['id'],
  5552. 'item_type' => $row['item_type'],
  5553. 'title' => $row['title'],
  5554. 'path' => $row['path'],
  5555. 'description' => $row['description'],
  5556. 'parent_item_id' => $row['parent_item_id'],
  5557. 'previous_item_id' => $row['previous_item_id'],
  5558. 'next_item_id' => $row['next_item_id'],
  5559. 'max_score' => $row['max_score'],
  5560. 'min_score' => $row['min_score'],
  5561. 'mastery_score' => $row['mastery_score'],
  5562. 'display_order' => $row['display_order']
  5563. );
  5564. }
  5565. $this->tree_array($arrLP);
  5566. $arrLP = $this->arrMenu;
  5567. unset ($this->arrMenu);
  5568. $title = '';
  5569. for ($i = 0; $i < count($arrLP); $i++) {
  5570. $title = addslashes($arrLP[$i]['title']);
  5571. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5572. $_GET['cidReq']
  5573. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5574. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5575. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5576. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/lp_".$icon_name.".png', '../img/lp_".$icon_name.".png');\n";
  5577. } else {
  5578. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5579. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/lp_".$icon_name.".gif', '../img/lp_".$icon_name.".gif');\n";
  5580. } else {
  5581. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5582. }
  5583. }
  5584. if ($menu < $arrLP[$i]['id']) {
  5585. $menu = $arrLP[$i]['id'];
  5586. }
  5587. }
  5588. $return .= "\n\tdocument.write(m);\n";
  5589. $return .= "\t if(!m.selectedNode) m.s(1);";
  5590. $return .= "</script>\n";
  5591. return $return;
  5592. }
  5593. /**
  5594. * Creates the default learning path folder
  5595. */
  5596. public static function generate_learning_path_folder($course)
  5597. {
  5598. //Creating learning_path folder
  5599. $dir = '/learning_path';
  5600. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5601. $folder = false;
  5602. if (!is_dir($filepath.'/'.$dir)) {
  5603. $folder = FileManager::create_unexisting_directory(
  5604. $course,
  5605. api_get_user_id(),
  5606. api_get_session_id(),
  5607. 0,
  5608. 0,
  5609. $filepath,
  5610. $dir,
  5611. get_lang('LearningPaths'),
  5612. 0
  5613. );
  5614. } else {
  5615. $folder = true;
  5616. }
  5617. return $folder;
  5618. }
  5619. public function generate_lp_folder($course, $lp_name = null)
  5620. {
  5621. $filepath = '';
  5622. $dir = '/learning_path/';
  5623. if (empty($lp_name)) {
  5624. $lp_name = $this->name;
  5625. }
  5626. $folder = self::generate_learning_path_folder($course);
  5627. //Creating LP folder
  5628. if ($folder) {
  5629. //Limits title size
  5630. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5631. $dir = $dir.$title;
  5632. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5633. if (!is_dir($filepath.'/'.$dir)) {
  5634. $folder = FileManager::create_unexisting_directory(
  5635. $course,
  5636. api_get_user_id(),
  5637. 0,
  5638. 0,
  5639. 0,
  5640. $filepath,
  5641. $dir,
  5642. $lp_name
  5643. );
  5644. } else {
  5645. $folder = true;
  5646. }
  5647. $dir = $dir.'/';
  5648. if ($folder) {
  5649. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5650. }
  5651. }
  5652. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5653. return $array;
  5654. }
  5655. /**
  5656. * Create a new document //still needs some finetuning
  5657. * @param array $_course
  5658. * @return string
  5659. */
  5660. public function create_document($_course)
  5661. {
  5662. $course_id = api_get_course_int_id();
  5663. global $charset;
  5664. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5665. if (strstr($dir, '..')) {
  5666. $dir = '/';
  5667. }
  5668. if ($dir[0] == '.') {
  5669. $dir = substr($dir, 1);
  5670. }
  5671. if ($dir[0] != '/') {
  5672. $dir = '/'.$dir;
  5673. }
  5674. if ($dir[strlen($dir) - 1] != '/') {
  5675. $dir .= '/';
  5676. }
  5677. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5678. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5679. //Generates folder
  5680. $result = $this->generate_lp_folder($_course);
  5681. $dir = $result['dir'];
  5682. $filepath = $result['filepath'];
  5683. }
  5684. if (!is_dir($filepath)) {
  5685. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5686. $dir = '/';
  5687. }
  5688. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5689. // is already escaped twice when it gets here.
  5690. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5691. $title = FileManager::disable_dangerous_file($title);
  5692. $filename = $title;
  5693. $content = $_POST['content_lp'];
  5694. $tmp_filename = $filename;
  5695. $i = 0;
  5696. while (file_exists($filepath.$tmp_filename.'.html')) {
  5697. $tmp_filename = $filename.'_'.++$i;
  5698. }
  5699. $filename = $tmp_filename.'.html';
  5700. $content = stripslashes($content);
  5701. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5702. // Change the path of mp3 to absolute.
  5703. // The first regexp deals with ../../../ urls.
  5704. $content = preg_replace(
  5705. "|(flashvars=\"file=)(\.+/)+|",
  5706. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5707. $content
  5708. );
  5709. // The second regexp deals with audio/ urls.
  5710. $content = preg_replace(
  5711. "|(flashvars=\"file=)([^/]+)/|",
  5712. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5713. $content
  5714. );
  5715. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5716. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5717. if (!file_exists($filepath.$filename)) {
  5718. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5719. fputs($fp, $content);
  5720. fclose($fp);
  5721. $file_size = filesize($filepath.$filename);
  5722. $save_file_path = $dir.$filename;
  5723. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5724. if ($document_id) {
  5725. api_item_property_update(
  5726. $_course,
  5727. TOOL_DOCUMENT,
  5728. $document_id,
  5729. 'DocumentAdded',
  5730. api_get_user_id(),
  5731. null,
  5732. null,
  5733. null,
  5734. null,
  5735. api_get_session_id()
  5736. );
  5737. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5738. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5739. if ($new_comment || $new_title) {
  5740. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5741. $ct = '';
  5742. if ($new_comment) {
  5743. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5744. }
  5745. if ($new_title) {
  5746. $ct .= ", title='".Database::escape_string(
  5747. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5748. )."' ";
  5749. }
  5750. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5751. $ct,
  5752. 1
  5753. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5754. Database::query($sql_update);
  5755. }
  5756. }
  5757. return $document_id;
  5758. }
  5759. }
  5760. }
  5761. /**
  5762. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5763. * @param array $_course array
  5764. * @return void
  5765. */
  5766. public function edit_document($_course)
  5767. {
  5768. $course_id = api_get_course_int_id();
  5769. global $_configuration;
  5770. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5771. if (strstr($dir, '..')) {
  5772. $dir = '/';
  5773. }
  5774. if ($dir[0] == '.') {
  5775. $dir = substr($dir, 1);
  5776. }
  5777. if ($dir[0] != '/') {
  5778. $dir = '/'.$dir;
  5779. }
  5780. if ($dir[strlen($dir) - 1] != '/') {
  5781. $dir .= '/';
  5782. }
  5783. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5784. if (!is_dir($filepath)) {
  5785. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5786. $dir = '/';
  5787. }
  5788. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5789. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5790. $document_id = intval($_POST['path']);
  5791. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5792. $res = Database::query($sql);
  5793. $row = Database :: fetch_array($res);
  5794. $content = stripslashes($_POST['content_lp']);
  5795. $file = $filepath.$row['path'];
  5796. if ($fp = @ fopen($file, 'w')) {
  5797. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/',$content);
  5798. // Change the path of mp3 to absolute.
  5799. // The first regexp deals with ../../../ urls.
  5800. $content = preg_replace(
  5801. "|(flashvars=\"file=)(\.+/)+|",
  5802. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5803. $content
  5804. );
  5805. // The second regexp deals with audio/ urls.
  5806. $content = preg_replace(
  5807. "|(flashvars=\"file=)([^/]+)/|",
  5808. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5809. $content
  5810. );
  5811. fputs($fp, $content);
  5812. fclose($fp);
  5813. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5814. $_POST['title']
  5815. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5816. Database::query($sql_update);
  5817. }
  5818. }
  5819. }
  5820. /**
  5821. * Displays the selected item, with a panel for manipulating the item
  5822. * @param int $item_id
  5823. * @param string $msg
  5824. * @return string
  5825. */
  5826. public function display_item($item_id, $msg = null, $show_actions = true)
  5827. {
  5828. $course_id = api_get_course_int_id();
  5829. $return = '';
  5830. if (is_numeric($item_id)) {
  5831. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5832. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5833. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5834. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5835. $result = Database::query($sql);
  5836. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5837. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5838. // Prevents wrong parent selection for document, see Bug#1251.
  5839. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5840. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5841. }
  5842. if ($show_actions) {
  5843. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5844. }
  5845. $return .= '<div style="padding:10px;">';
  5846. if ($msg != '') {
  5847. $return .= $msg;
  5848. }
  5849. $return .= '<h3>'.$row['title'].'</h3>';
  5850. switch ($row['item_type']) {
  5851. case TOOL_QUIZ:
  5852. if (!empty($row['path'])) {
  5853. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5854. $exercise = new Exercise();
  5855. $exercise->read($row['path']);
  5856. $return .= $exercise->description.'<br />';
  5857. }
  5858. break;
  5859. case TOOL_DOCUMENT:
  5860. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5861. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5862. $row['path']
  5863. );
  5864. $result = Database::query($sql_doc);
  5865. $path_file = Database::result($result, 0, 0);
  5866. $path_parts = pathinfo($path_file);
  5867. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5868. if (in_array(
  5869. $path_parts['extension'],
  5870. array(
  5871. 'html',
  5872. 'txt',
  5873. 'png',
  5874. 'jpg',
  5875. 'JPG',
  5876. 'jpeg',
  5877. 'JPEG',
  5878. 'gif',
  5879. 'swf'
  5880. )
  5881. )
  5882. ) {
  5883. $return .= $this->display_document($row['path'], true, true);
  5884. }
  5885. break;
  5886. }
  5887. $return .= '</div>';
  5888. }
  5889. }
  5890. return $return;
  5891. }
  5892. /**
  5893. * Shows the needed forms for editing a specific item
  5894. * @param int $item_id
  5895. * @return string
  5896. */
  5897. public function display_edit_item($item_id)
  5898. {
  5899. $_course = api_get_course_info(); // It will disappear.
  5900. $course_id = api_get_course_int_id();
  5901. $return = '';
  5902. if (is_numeric($item_id)) {
  5903. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5904. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5905. $res = Database::query($sql);
  5906. $row = Database::fetch_array($res);
  5907. switch ($row['item_type']) {
  5908. case 'dokeos_chapter' :
  5909. case 'dir' :
  5910. case 'asset' :
  5911. case 'sco' :
  5912. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5913. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5914. $return .= $this->display_item_form(
  5915. $row['item_type'],
  5916. get_lang('EditCurrentChapter').' :',
  5917. 'edit',
  5918. $item_id,
  5919. $row
  5920. );
  5921. } else {
  5922. $return .= $this->display_item_small_form(
  5923. $row['item_type'],
  5924. get_lang('EditCurrentChapter').' :',
  5925. $row
  5926. );
  5927. }
  5928. break;
  5929. case TOOL_DOCUMENT :
  5930. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5931. $sql_step = " SELECT lp.*, doc.path as dir
  5932. FROM ".$tbl_lp_item." as lp
  5933. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5934. WHERE lp.c_id = $course_id AND
  5935. doc.c_id = $course_id AND
  5936. lp.id = ".Database::escape_string($item_id);
  5937. $res_step = Database::query($sql_step);
  5938. $row_step = Database :: fetch_array($res_step);
  5939. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5940. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5941. break;
  5942. case TOOL_LINK :
  5943. $link_id = (string)$row['path'];
  5944. if (ctype_digit($link_id)) {
  5945. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5946. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5947. $link_id
  5948. );
  5949. $res_link = Database::query($sql_select);
  5950. $row_link = Database :: fetch_array($res_link);
  5951. if (is_array($row_link)) {
  5952. $row['url'] = $row_link['url'];
  5953. }
  5954. }
  5955. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5956. $return .= $this->display_link_form('edit', $item_id, $row);
  5957. break;
  5958. case 'dokeos_module' :
  5959. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5960. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5961. $return .= $this->display_item_form(
  5962. $row['item_type'],
  5963. get_lang('EditCurrentModule').' :',
  5964. 'edit',
  5965. $item_id,
  5966. $row
  5967. );
  5968. } else {
  5969. $return .= $this->display_item_small_form(
  5970. $row['item_type'],
  5971. get_lang('EditCurrentModule').' :',
  5972. $row
  5973. );
  5974. }
  5975. break;
  5976. case TOOL_QUIZ :
  5977. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5978. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5979. break;
  5980. case TOOL_HOTPOTATOES :
  5981. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5982. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5983. break;
  5984. case TOOL_STUDENTPUBLICATION :
  5985. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5986. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5987. break;
  5988. case TOOL_FORUM :
  5989. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5990. $return .= $this->display_forum_form('edit', $item_id, $row);
  5991. break;
  5992. case TOOL_THREAD :
  5993. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5994. $return .= $this->display_thread_form('edit', $item_id, $row);
  5995. break;
  5996. }
  5997. }
  5998. return $return;
  5999. }
  6000. /**
  6001. * Function that displays a list with al the resources that could be added to the learning path
  6002. * @return string
  6003. */
  6004. public function display_resources()
  6005. {
  6006. $_course = api_get_course_info(); // TODO: Don't use globals.
  6007. $course_code = api_get_course_id();
  6008. //Get all the docs
  6009. $documents = $this->get_documents();
  6010. //Get all the exercises
  6011. $exercises = $this->get_exercises();
  6012. // Get all the links
  6013. $links = $this->get_links();
  6014. //Get al the student publications
  6015. $works = $this->get_student_publications();
  6016. //Get al the forums
  6017. $forums = $this->get_forums(null, $course_code);
  6018. $headers = array(
  6019. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  6020. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  6021. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  6022. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  6023. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  6024. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  6025. );
  6026. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6027. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6028. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6029. return true;
  6030. }
  6031. /**
  6032. * Returns the extension of a document
  6033. * @param string filename
  6034. * @return string Extension (part after the last dot)
  6035. */
  6036. public function get_extension($filename)
  6037. {
  6038. $explode = explode('.', $filename);
  6039. return $explode[count($explode) - 1];
  6040. }
  6041. /**
  6042. * Displays a document by id
  6043. *
  6044. * @param unknown_type $id
  6045. * @return unknown
  6046. */
  6047. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6048. {
  6049. $_course = api_get_course_info(); // It is temporary.
  6050. $course_id = api_get_course_int_id();
  6051. $return = '';
  6052. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6053. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6054. WHERE c_id = ".$course_id." AND id = ".$id;
  6055. $res_doc = Database::query($sql_doc);
  6056. $row_doc = Database :: fetch_array($res_doc);
  6057. // TODO: Add a path filter.
  6058. if ($iframe) {
  6059. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.api_get_path(
  6060. WEB_COURSE_PATH
  6061. ).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq(
  6062. ).'"></iframe>';
  6063. } else {
  6064. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6065. }
  6066. return $return;
  6067. }
  6068. /**
  6069. * Return HTML form to add/edit a quiz
  6070. * @param string Action (add/edit)
  6071. * @param integer Item ID if already exists
  6072. * @param mixed Extra information (quiz ID if integer)
  6073. * @return string HTML form
  6074. */
  6075. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6076. {
  6077. $course_id = api_get_course_int_id();
  6078. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6079. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6080. if ($id != 0 && is_array($extra_info)) {
  6081. $item_title = $extra_info['title'];
  6082. $item_description = $extra_info['description'];
  6083. } elseif (is_numeric($extra_info)) {
  6084. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  6085. $result = Database::query($sql_quiz);
  6086. $row = Database :: fetch_array($result);
  6087. $item_title = $row['title'];
  6088. $item_description = $row['description'];
  6089. } else {
  6090. $item_title = '';
  6091. $item_description = '';
  6092. }
  6093. $item_title = Security::remove_XSS($item_title);
  6094. $item_description = Security::remove_XSS($item_description);
  6095. $legend = '<legend>';
  6096. if ($id != 0 && is_array($extra_info)) {
  6097. $parent = $extra_info['parent_item_id'];
  6098. } else {
  6099. $parent = 0;
  6100. }
  6101. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6102. $result = Database::query($sql);
  6103. $arrLP = array();
  6104. while ($row = Database :: fetch_array($result)) {
  6105. $arrLP[] = array(
  6106. 'id' => $row['id'],
  6107. 'item_type' => $row['item_type'],
  6108. 'title' => $row['title'],
  6109. 'path' => $row['path'],
  6110. 'description' => $row['description'],
  6111. 'parent_item_id' => $row['parent_item_id'],
  6112. 'previous_item_id' => $row['previous_item_id'],
  6113. 'next_item_id' => $row['next_item_id'],
  6114. 'display_order' => $row['display_order'],
  6115. 'max_score' => $row['max_score'],
  6116. 'min_score' => $row['min_score'],
  6117. 'mastery_score' => $row['mastery_score'],
  6118. 'prerequisite' => $row['prerequisite'],
  6119. 'max_time_allowed' => $row['max_time_allowed']
  6120. );
  6121. }
  6122. $this->tree_array($arrLP);
  6123. $arrLP = $this->arrMenu;
  6124. unset ($this->arrMenu);
  6125. if ($action == 'add') {
  6126. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6127. } elseif ($action == 'move') {
  6128. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6129. } else {
  6130. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6131. }
  6132. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6133. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6134. }
  6135. $legend .= '</legend>';
  6136. $return .= '<div class="sectioncomment">';
  6137. $return .= '<form method="POST">';
  6138. $return .= $legend;
  6139. $return .= '<table class="lp_form">';
  6140. if ($action != 'move') {
  6141. $return .= '<tr>';
  6142. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6143. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6144. $return .= '</tr>';
  6145. }
  6146. $return .= '<tr>';
  6147. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6148. $return .= '<td class="input">';
  6149. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6150. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6151. $arrHide = array(
  6152. $id
  6153. );
  6154. //$parent_item_id = $_SESSION['parent_item_id'];
  6155. for ($i = 0; $i < count($arrLP); $i++) {
  6156. if ($action != 'add') {
  6157. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6158. $arrLP[$i]['id'],
  6159. $arrHide
  6160. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6161. ) {
  6162. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6163. } else {
  6164. $arrHide[] = $arrLP[$i]['id'];
  6165. }
  6166. } else {
  6167. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6168. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6169. }
  6170. }
  6171. }
  6172. if (is_array($arrLP)) {
  6173. reset($arrLP);
  6174. }
  6175. $return .= '</select>';
  6176. $return .= '</td>';
  6177. $return .= '</tr>';
  6178. $return .= '<tr>';
  6179. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6180. $return .= '<td class="input">';
  6181. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6182. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6183. for ($i = 0; $i < count($arrLP); $i++) {
  6184. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6185. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6186. $selected = 'selected="selected" ';
  6187. } elseif ($action == 'add') {
  6188. $selected = 'selected="selected" ';
  6189. } else {
  6190. $selected = '';
  6191. }
  6192. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6193. 'After'
  6194. ).' "'.$arrLP[$i]['title'].'"</option>';
  6195. }
  6196. }
  6197. $return .= '</select>';
  6198. $return .= '</td>';
  6199. $return .= '</tr>';
  6200. if ($action != 'move') {
  6201. $id_prerequisite = 0;
  6202. if (is_array($arrLP)) {
  6203. foreach ($arrLP as $key => $value) {
  6204. if ($value['id'] == $id) {
  6205. $id_prerequisite = $value['prerequisite'];
  6206. break;
  6207. }
  6208. }
  6209. }
  6210. $arrHide = array();
  6211. for ($i = 0; $i < count($arrLP); $i++) {
  6212. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6213. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6214. $s_selected_position = $arrLP[$i]['id'];
  6215. } elseif ($action == 'add') {
  6216. $s_selected_position = 0;
  6217. }
  6218. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6219. }
  6220. }
  6221. /*// Commented the prerequisites, only visible in edit (exercise).
  6222. $return .= '<tr>';
  6223. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6224. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6225. foreach($arrHide as $key => $value){
  6226. if($key==$s_selected_position && $action == 'add'){
  6227. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6228. }
  6229. elseif($key==$id_prerequisite && $action == 'edit'){
  6230. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6231. }
  6232. else{
  6233. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6234. }
  6235. }
  6236. $return .= "</select></td>";
  6237. */
  6238. $return .= '</tr>';
  6239. /*$return .= '<tr>';
  6240. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6241. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6242. // Remove temporarily the test description.
  6243. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6244. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6245. $return .= '</tr>'; */
  6246. }
  6247. $return .= '<tr>';
  6248. if ($action == 'add') {
  6249. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6250. 'AddExercise'
  6251. ).'</button></td>';
  6252. } else {
  6253. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6254. 'EditCurrentExecice'
  6255. ).'</button></td>';
  6256. }
  6257. $return .= '</tr>';
  6258. $return .= '</table>';
  6259. if ($action == 'move') {
  6260. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6261. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6262. }
  6263. if (is_numeric($extra_info)) {
  6264. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6265. } elseif (is_array($extra_info)) {
  6266. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6267. }
  6268. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6269. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6270. $return .= '</form>';
  6271. $return .= '</div>';
  6272. return $return;
  6273. }
  6274. /**
  6275. * Addition of Hotpotatoes tests
  6276. * @param string Action
  6277. * @param integer Internal ID of the item
  6278. * @param mixed Extra information - can be an array with title and description indexes
  6279. * @return string HTML structure to display the hotpotatoes addition formular
  6280. */
  6281. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6282. {
  6283. $course_id = api_get_course_int_id();
  6284. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6285. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6286. if ($id != 0 && is_array($extra_info)) {
  6287. $item_title = stripslashes($extra_info['title']);
  6288. $item_description = stripslashes($extra_info['description']);
  6289. } elseif (is_numeric($extra_info)) {
  6290. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6291. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6292. WHERE c_id = ".$course_id." AND
  6293. path LIKE '".$uploadPath."/%/%htm%' AND
  6294. id = ".(int)$extra_info."
  6295. ORDER BY id ASC";
  6296. $res_hot = Database::query($sql_hot);
  6297. $row = Database::fetch_array($res_hot);
  6298. $item_title = $row['title'];
  6299. $item_description = $row['description'];
  6300. if (!empty ($row['comment'])) {
  6301. $item_title = $row['comment'];
  6302. }
  6303. } else {
  6304. $item_title = '';
  6305. $item_description = '';
  6306. }
  6307. if ($id != 0 && is_array($extra_info)) {
  6308. $parent = $extra_info['parent_item_id'];
  6309. } else {
  6310. $parent = 0;
  6311. }
  6312. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6313. $result = Database::query($sql);
  6314. $arrLP = array();
  6315. while ($row = Database :: fetch_array($result)) {
  6316. $arrLP[] = array(
  6317. 'id' => $row['id'],
  6318. 'item_type' => $row['item_type'],
  6319. 'title' => $row['title'],
  6320. 'path' => $row['path'],
  6321. 'description' => $row['description'],
  6322. 'parent_item_id' => $row['parent_item_id'],
  6323. 'previous_item_id' => $row['previous_item_id'],
  6324. 'next_item_id' => $row['next_item_id'],
  6325. 'display_order' => $row['display_order'],
  6326. 'max_score' => $row['max_score'],
  6327. 'min_score' => $row['min_score'],
  6328. 'mastery_score' => $row['mastery_score'],
  6329. 'prerequisite' => $row['prerequisite'],
  6330. 'max_time_allowed' => $row['max_time_allowed']
  6331. );
  6332. }
  6333. $legend = '<legend>';
  6334. if ($action == 'add') {
  6335. $legend .= get_lang('CreateTheExercise');
  6336. } elseif ($action == 'move') {
  6337. $legend .= get_lang('MoveTheCurrentExercise');
  6338. } else {
  6339. $legend .= get_lang('EditCurrentExecice');
  6340. }
  6341. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6342. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6343. }
  6344. $legend .= '</legend>';
  6345. $return .= '<form method="POST">';
  6346. $return .= $legend;
  6347. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6348. $return .= '<tr>';
  6349. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6350. $return .= '<td class="input">';
  6351. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6352. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6353. $arrHide = array(
  6354. $id
  6355. );
  6356. if (count($arrLP) > 0) {
  6357. for ($i = 0; $i < count($arrLP); $i++) {
  6358. if ($action != 'add') {
  6359. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6360. $arrLP[$i]['id'],
  6361. $arrHide
  6362. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6363. ) {
  6364. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6365. } else {
  6366. $arrHide[] = $arrLP[$i]['id'];
  6367. }
  6368. } else {
  6369. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6370. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6371. }
  6372. }
  6373. }
  6374. reset($arrLP);
  6375. }
  6376. $return .= '</select>';
  6377. $return .= '</td>';
  6378. $return .= '</tr>';
  6379. $return .= '<tr>';
  6380. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6381. $return .= '<td class="input">';
  6382. $return .= '<select id="previous" name="previous" size="1">';
  6383. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6384. for ($i = 0; $i < count($arrLP); $i++) {
  6385. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6386. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6387. $selected = 'selected="selected" ';
  6388. } elseif ($action == 'add') {
  6389. $selected = 'selected="selected" ';
  6390. } else {
  6391. $selected = '';
  6392. }
  6393. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6394. 'After'
  6395. ).' "'.$arrLP[$i]['title'].'"</option>';
  6396. }
  6397. }
  6398. $return .= '</select>';
  6399. $return .= '</td>';
  6400. $return .= '</tr>';
  6401. if ($action != 'move') {
  6402. $return .= '<tr>';
  6403. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6404. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6405. $return .= '</tr>';
  6406. $id_prerequisite = 0;
  6407. if (is_array($arrLP) && count($arrLP) > 0) {
  6408. foreach ($arrLP as $key => $value) {
  6409. if ($value['id'] == $id) {
  6410. $id_prerequisite = $value['prerequisite'];
  6411. break;
  6412. }
  6413. }
  6414. $arrHide = array();
  6415. for ($i = 0; $i < count($arrLP); $i++) {
  6416. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6417. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6418. $s_selected_position = $arrLP[$i]['id'];
  6419. } elseif ($action == 'add') {
  6420. $s_selected_position = 0;
  6421. }
  6422. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6423. }
  6424. }
  6425. }
  6426. }
  6427. $return .= '<tr>';
  6428. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6429. 'SaveHotpotatoes'
  6430. ).'</button></td>';
  6431. $return .= '</tr>';
  6432. $return .= '</table>';
  6433. if ($action == 'move') {
  6434. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6435. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6436. }
  6437. if (is_numeric($extra_info)) {
  6438. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6439. } elseif (is_array($extra_info)) {
  6440. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6441. }
  6442. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6443. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6444. $return .= '</form>';
  6445. return $return;
  6446. }
  6447. /**
  6448. * Return the form to display the forum edit/add option
  6449. * @param string Action (add/edit)
  6450. * @param integer ID of the lp_item if already exists
  6451. * @param mixed Forum ID or title
  6452. * @return string HTML form
  6453. */
  6454. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6455. {
  6456. $course_id = api_get_course_int_id();
  6457. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6458. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6459. if ($id != 0 && is_array($extra_info)) {
  6460. $item_title = stripslashes($extra_info['title']);
  6461. } elseif (is_numeric($extra_info)) {
  6462. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6463. FROM ".$tbl_forum."
  6464. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6465. $result = Database::query($sql_forum);
  6466. $row = Database :: fetch_array($result);
  6467. $item_title = $row['title'];
  6468. $item_description = $row['comment'];
  6469. } else {
  6470. $item_title = '';
  6471. $item_description = '';
  6472. }
  6473. $legend = '<legend>';
  6474. if ($id != 0 && is_array($extra_info)) {
  6475. $parent = $extra_info['parent_item_id'];
  6476. } else {
  6477. $parent = 0;
  6478. }
  6479. $sql = "SELECT * FROM ".$tbl_lp_item."
  6480. WHERE c_id = ".$course_id." AND
  6481. lp_id = ".$this->lp_id;
  6482. $result = Database::query($sql);
  6483. $arrLP = array();
  6484. while ($row = Database :: fetch_array($result)) {
  6485. $arrLP[] = array(
  6486. 'id' => $row['id'],
  6487. 'item_type' => $row['item_type'],
  6488. 'title' => $row['title'],
  6489. 'path' => $row['path'],
  6490. 'description' => $row['description'],
  6491. 'parent_item_id' => $row['parent_item_id'],
  6492. 'previous_item_id' => $row['previous_item_id'],
  6493. 'next_item_id' => $row['next_item_id'],
  6494. 'display_order' => $row['display_order'],
  6495. 'max_score' => $row['max_score'],
  6496. 'min_score' => $row['min_score'],
  6497. 'mastery_score' => $row['mastery_score'],
  6498. 'prerequisite' => $row['prerequisite']
  6499. );
  6500. }
  6501. $this->tree_array($arrLP);
  6502. $arrLP = $this->arrMenu;
  6503. unset ($this->arrMenu);
  6504. if ($action == 'add') {
  6505. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6506. } elseif ($action == 'move') {
  6507. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6508. } else {
  6509. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6510. }
  6511. $legend .= '</legend>';
  6512. $return .= '<div class="sectioncomment">';
  6513. $return .= '<form method="POST">';
  6514. $return .= $legend;
  6515. $return .= '<table class="lp_form">';
  6516. if ($action != 'move') {
  6517. $return .= '<tr>';
  6518. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6519. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6520. $return .= '</tr>';
  6521. }
  6522. $return .= '<tr>';
  6523. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6524. $return .= '<td class="input">';
  6525. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6526. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6527. $arrHide = array(
  6528. $id
  6529. );
  6530. //$parent_item_id = $_SESSION['parent_item_id'];
  6531. for ($i = 0; $i < count($arrLP); $i++) {
  6532. if ($action != 'add') {
  6533. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6534. $arrLP[$i]['id'],
  6535. $arrHide
  6536. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6537. ) {
  6538. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6539. } else {
  6540. $arrHide[] = $arrLP[$i]['id'];
  6541. }
  6542. } else {
  6543. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6544. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6545. }
  6546. }
  6547. }
  6548. if (is_array($arrLP)) {
  6549. reset($arrLP);
  6550. }
  6551. $return .= "\t\t\t\t".'</select>';
  6552. $return .= '</td>';
  6553. $return .= '</tr>';
  6554. $return .= '<tr>';
  6555. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6556. $return .= '<td class="input">';
  6557. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6558. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6559. for ($i = 0; $i < count($arrLP); $i++) {
  6560. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6561. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6562. $selected = 'selected="selected" ';
  6563. } elseif ($action == 'add') {
  6564. $selected = 'selected="selected" ';
  6565. } else {
  6566. $selected = '';
  6567. }
  6568. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6569. 'After'
  6570. ).' "'.$arrLP[$i]['title'].'"</option>';
  6571. }
  6572. }
  6573. $return .= "\t\t\t\t".'</select>';
  6574. $return .= '</td>';
  6575. $return .= '</tr>';
  6576. if ($action != 'move') {
  6577. $return .= '<tr>';
  6578. $return .= '</tr>';
  6579. $id_prerequisite = 0;
  6580. if (is_array($arrLP)) {
  6581. foreach ($arrLP as $key => $value) {
  6582. if ($value['id'] == $id) {
  6583. $id_prerequisite = $value['prerequisite'];
  6584. break;
  6585. }
  6586. }
  6587. }
  6588. $arrHide = array();
  6589. for ($i = 0; $i < count($arrLP); $i++) {
  6590. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6591. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6592. $s_selected_position = $arrLP[$i]['id'];
  6593. } elseif ($action == 'add') {
  6594. $s_selected_position = 0;
  6595. }
  6596. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6597. }
  6598. }
  6599. $return .= '</tr>';
  6600. }
  6601. $return .= '<tr>';
  6602. if ($action == 'add') {
  6603. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6604. 'AddForumToCourse'
  6605. ).' </button></td>';
  6606. } else {
  6607. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6608. 'EditCurrentForum'
  6609. ).' </button></td>';
  6610. }
  6611. $return .= '</tr>';
  6612. $return .= '</table>';
  6613. if ($action == 'move') {
  6614. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6615. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6616. }
  6617. if (is_numeric($extra_info)) {
  6618. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6619. } elseif (is_array($extra_info)) {
  6620. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6621. }
  6622. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6623. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6624. $return .= '</form>';
  6625. $return .= '</div>';
  6626. return $return;
  6627. }
  6628. /**
  6629. * Return HTML form to add/edit forum threads
  6630. * @param string Action (add/edit)
  6631. * @param integer Item ID if already exists in learning path
  6632. * @param mixed Extra information (thread ID if integer)
  6633. * @return string HTML form
  6634. */
  6635. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6636. {
  6637. $course_id = api_get_course_int_id();
  6638. if (empty($course_id)) {
  6639. return null;
  6640. }
  6641. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6642. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6643. if ($id != 0 && is_array($extra_info)) {
  6644. $item_title = stripslashes($extra_info['title']);
  6645. } elseif (is_numeric($extra_info)) {
  6646. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6647. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6648. $result = Database::query($sql_forum);
  6649. $row = Database :: fetch_array($result);
  6650. $item_title = $row['title'];
  6651. $item_description = '';
  6652. } else {
  6653. $item_title = '';
  6654. $item_description = '';
  6655. }
  6656. $return = null;
  6657. if ($id != 0 && is_array($extra_info)) {
  6658. $parent = $extra_info['parent_item_id'];
  6659. } else {
  6660. $parent = 0;
  6661. }
  6662. $sql = "SELECT * FROM ".$tbl_lp_item."
  6663. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6664. $result = Database::query($sql);
  6665. $arrLP = array();
  6666. while ($row = Database :: fetch_array($result)) {
  6667. $arrLP[] = array(
  6668. 'id' => $row['id'],
  6669. 'item_type' => $row['item_type'],
  6670. 'title' => $row['title'],
  6671. 'path' => $row['path'],
  6672. 'description' => $row['description'],
  6673. 'parent_item_id' => $row['parent_item_id'],
  6674. 'previous_item_id' => $row['previous_item_id'],
  6675. 'next_item_id' => $row['next_item_id'],
  6676. 'display_order' => $row['display_order'],
  6677. 'max_score' => $row['max_score'],
  6678. 'min_score' => $row['min_score'],
  6679. 'mastery_score' => $row['mastery_score'],
  6680. 'prerequisite' => $row['prerequisite']
  6681. );
  6682. }
  6683. $this->tree_array($arrLP);
  6684. $arrLP = $this->arrMenu;
  6685. unset ($this->arrMenu);
  6686. $return .= '<form method="POST">';
  6687. if ($action == 'add') {
  6688. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6689. } elseif ($action == 'move') {
  6690. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6691. } else {
  6692. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6693. }
  6694. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6695. $return .= '<tr>';
  6696. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6697. $return .= '<td class="input">';
  6698. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6699. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6700. $arrHide = array(
  6701. $id
  6702. );
  6703. for ($i = 0; $i < count($arrLP); $i++) {
  6704. if ($action != 'add') {
  6705. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6706. $arrLP[$i]['id'],
  6707. $arrHide
  6708. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6709. ) {
  6710. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6711. } else {
  6712. $arrHide[] = $arrLP[$i]['id'];
  6713. }
  6714. } else {
  6715. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6716. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6717. }
  6718. }
  6719. }
  6720. if ($arrLP != null) {
  6721. reset($arrLP);
  6722. }
  6723. $return .= '</select>';
  6724. $return .= '</td>';
  6725. $return .= '</tr>';
  6726. $return .= '<tr>';
  6727. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6728. $return .= '<td class="input">';
  6729. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6730. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6731. for ($i = 0; $i < count($arrLP); $i++) {
  6732. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6733. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6734. $selected = 'selected="selected" ';
  6735. } elseif ($action == 'add') {
  6736. $selected = 'selected="selected" ';
  6737. } else {
  6738. $selected = '';
  6739. }
  6740. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6741. 'After'
  6742. ).' "'.$arrLP[$i]['title'].'"</option>';
  6743. }
  6744. }
  6745. $return .= "\t\t\t\t".'</select>';
  6746. $return .= '</td>';
  6747. $return .= '</tr>';
  6748. if ($action != 'move') {
  6749. $return .= '<tr>';
  6750. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6751. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6752. $return .= '</tr>';
  6753. $return .= '<tr>';
  6754. $return .= '</tr>';
  6755. $id_prerequisite = 0;
  6756. if ($arrLP != null) {
  6757. foreach ($arrLP as $key => $value) {
  6758. if ($value['id'] == $id) {
  6759. $id_prerequisite = $value['prerequisite'];
  6760. break;
  6761. }
  6762. }
  6763. }
  6764. $arrHide = array();
  6765. for ($i = 0; $i < count($arrLP); $i++) {
  6766. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6767. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6768. $s_selected_position = $arrLP[$i]['id'];
  6769. } elseif ($action == 'add') {
  6770. $s_selected_position = 0;
  6771. }
  6772. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6773. }
  6774. }
  6775. $return .= '<tr>';
  6776. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6777. 'LearnpathPrerequisites'
  6778. ).'</label></td>';
  6779. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6780. 'NoPrerequisites'
  6781. ).'</option>';
  6782. foreach ($arrHide as $key => $value) {
  6783. if ($key == $s_selected_position && $action == 'add') {
  6784. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6785. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6786. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6787. } else {
  6788. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6789. }
  6790. }
  6791. $return .= "</select></td>";
  6792. $return .= '</tr>';
  6793. }
  6794. $return .= '<tr>';
  6795. $return .= '<td></td><td>
  6796. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6797. 'Ok'
  6798. ).'</button></td>';
  6799. $return .= '</tr>';
  6800. $return .= '</table>';
  6801. if ($action == 'move') {
  6802. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6803. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6804. }
  6805. if (is_numeric($extra_info)) {
  6806. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6807. } elseif (is_array($extra_info)) {
  6808. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6809. }
  6810. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6811. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6812. $return .= '</form>';
  6813. $return .= '</div>';
  6814. return $return;
  6815. }
  6816. /**
  6817. * Return the HTML form to display an item (generally a section/module item)
  6818. * @param string Item type (module/dokeos_module)
  6819. * @param string Title (optional, only when creating)
  6820. * @param string Action ('add'/'edit')
  6821. * @param integer lp_item ID
  6822. * @param mixed Extra info
  6823. * @return string HTML form
  6824. */
  6825. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6826. {
  6827. $course_id = api_get_course_int_id();
  6828. $_course = api_get_course_info();
  6829. global $charset;
  6830. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6831. if ($id != 0 && is_array($extra_info)) {
  6832. $item_title = $extra_info['title'];
  6833. $item_description = $extra_info['description'];
  6834. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6835. $extra_info['path']
  6836. );
  6837. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6838. } else {
  6839. $item_title = '';
  6840. $item_description = '';
  6841. $item_path_fck = '';
  6842. }
  6843. if ($id != 0 && is_array($extra_info)) {
  6844. $parent = $extra_info['parent_item_id'];
  6845. } else {
  6846. $parent = 0;
  6847. }
  6848. $id = intval($id);
  6849. $sql = "SELECT * FROM ".$tbl_lp_item."
  6850. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6851. if ($item_type == 'module') {
  6852. $sql .= " AND parent_item_id = 0";
  6853. }
  6854. $result = Database::query($sql);
  6855. $arrLP = array();
  6856. while ($row = Database :: fetch_array($result)) {
  6857. $arrLP[] = array(
  6858. 'id' => $row['id'],
  6859. 'item_type' => $row['item_type'],
  6860. 'title' => $row['title'],
  6861. 'path' => $row['path'],
  6862. 'description' => $row['description'],
  6863. 'parent_item_id' => $row['parent_item_id'],
  6864. 'previous_item_id' => $row['previous_item_id'],
  6865. 'next_item_id' => $row['next_item_id'],
  6866. 'max_score' => $row['max_score'],
  6867. 'min_score' => $row['min_score'],
  6868. 'mastery_score' => $row['mastery_score'],
  6869. 'prerequisite' => $row['prerequisite'],
  6870. 'display_order' => $row['display_order']
  6871. );
  6872. }
  6873. $this->tree_array($arrLP);
  6874. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6875. unset ($this->arrMenu);
  6876. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6877. $url = api_get_self().'?'.api_get_cidreq(
  6878. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6879. $form = new FormValidator('form', 'POST', $url);
  6880. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6881. $defaults['description'] = $item_description;
  6882. $form->addElement('header', $title);
  6883. //$arrHide = array($id);
  6884. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6885. $arrHide[0]['padding'] = 3;
  6886. $charset = api_get_system_encoding();
  6887. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6888. for ($i = 0; $i < count($arrLP); $i++) {
  6889. if ($action != 'add') {
  6890. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6891. $arrLP[$i]['id'],
  6892. $arrHide
  6893. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6894. ) {
  6895. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6896. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6897. if ($parent == $arrLP[$i]['id']) {
  6898. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6899. }
  6900. }
  6901. } else {
  6902. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6903. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6904. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6905. if ($parent == $arrLP[$i]['id']) {
  6906. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6907. }
  6908. }
  6909. }
  6910. }
  6911. if ($action != 'move') {
  6912. $form->addElement(
  6913. 'text',
  6914. 'title',
  6915. get_lang('Title'),
  6916. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6917. );
  6918. $form->applyFilter('title', 'html_filter');
  6919. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6920. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6921. } else {
  6922. $form->addElement('hidden', 'title');
  6923. }
  6924. $parent_select = $form->addElement(
  6925. 'select',
  6926. 'parent',
  6927. get_lang('Parent'),
  6928. '',
  6929. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6930. );
  6931. foreach ($arrHide as $key => $value) {
  6932. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6933. }
  6934. if (!empty($s_selected_parent)) {
  6935. $parent_select->setSelected($s_selected_parent);
  6936. }
  6937. }
  6938. if (is_array($arrLP)) {
  6939. reset($arrLP);
  6940. }
  6941. $arrHide = array();
  6942. // POSITION
  6943. for ($i = 0; $i < count($arrLP); $i++) {
  6944. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6945. //this is the same!
  6946. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6947. $s_selected_position = $arrLP[$i]['id'];
  6948. } elseif ($action == 'add') {
  6949. $s_selected_position = $arrLP[$i]['id'];
  6950. }
  6951. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6952. }
  6953. }
  6954. $position = $form->addElement(
  6955. 'select',
  6956. 'previous',
  6957. get_lang('Position'),
  6958. '',
  6959. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6960. );
  6961. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6962. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6963. foreach ($arrHide as $key => $value) {
  6964. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6965. }
  6966. if (!empty ($s_selected_position)) {
  6967. $position->setSelected($s_selected_position);
  6968. }
  6969. if (is_array($arrLP)) {
  6970. reset($arrLP);
  6971. }
  6972. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6973. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6974. $form->addElement('hidden', 'parent', '0');
  6975. }
  6976. //fix in order to use the tab
  6977. if ($item_type == 'chapter') {
  6978. $form->addElement('hidden', 'type', 'chapter');
  6979. }
  6980. $extension = null;
  6981. if (!empty($item_path)) {
  6982. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6983. }
  6984. //assets can't be modified
  6985. //$item_type == 'asset' ||
  6986. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6987. if ($item_type == 'sco') {
  6988. $form->addElement(
  6989. 'html',
  6990. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  6991. );
  6992. }
  6993. $renderer = $form->defaultRenderer();
  6994. $renderer->setElementTemplate(
  6995. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  6996. 'content_lp'
  6997. );
  6998. $relative_prefix = '';
  6999. $editor_config = array(
  7000. 'ToolbarSet' => 'LearningPathDocuments',
  7001. 'Width' => '100%',
  7002. 'Height' => '500',
  7003. 'FullPage' => true,
  7004. 'CreateDocumentDir' => $relative_prefix,
  7005. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7006. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7007. );
  7008. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7009. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  7010. //$defaults['content_lp'] = file_get_contents($item_path);
  7011. $defaults['content_lp'] = file_get_contents($content_path);
  7012. }
  7013. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  7014. $form->addElement('hidden', 'post_time', time());
  7015. $form->setDefaults($defaults);
  7016. return $form->return_form();
  7017. }
  7018. /**
  7019. * Returns the form to update or create a document
  7020. * @param string Action (add/edit)
  7021. * @param integer ID of the lp_item (if already exists)
  7022. * @param mixed Integer if document ID, string if info ('new')
  7023. * @return string HTML form
  7024. */
  7025. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7026. {
  7027. $course_id = api_get_course_int_id();
  7028. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7029. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7030. $no_display_edit_textarea = false;
  7031. //If action==edit document
  7032. //We don't display the document form if it's not an editable document (html or txt file)
  7033. if ($action == "edit") {
  7034. if (is_array($extra_info)) {
  7035. $path_parts = pathinfo($extra_info['dir']);
  7036. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7037. $no_display_edit_textarea = true;
  7038. }
  7039. }
  7040. }
  7041. $no_display_add = false;
  7042. // If action==add an existing document
  7043. // We don't display the document form if it's not an editable document (html or txt file).
  7044. if ($action == "add") {
  7045. if (is_numeric($extra_info)) {
  7046. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  7047. $extra_info
  7048. );
  7049. $result = Database::query($sql_doc);
  7050. $path_file = Database :: result($result, 0, 0);
  7051. $path_parts = pathinfo($path_file);
  7052. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7053. $no_display_add = true;
  7054. }
  7055. }
  7056. }
  7057. if ($id != 0 && is_array($extra_info)) {
  7058. $item_title = stripslashes($extra_info['title']);
  7059. $item_description = stripslashes($extra_info['description']);
  7060. $item_terms = stripslashes($extra_info['terms']);
  7061. if (empty ($item_title)) {
  7062. $path_parts = pathinfo($extra_info['path']);
  7063. $item_title = stripslashes($path_parts['filename']);
  7064. }
  7065. } elseif (is_numeric($extra_info)) {
  7066. $sql_doc = "SELECT path, title FROM ".$tbl_doc."
  7067. WHERE c_id = ".$course_id." AND id = ".Database::escape_string($extra_info);
  7068. $result = Database::query($sql_doc);
  7069. $row = Database::fetch_array($result);
  7070. $explode = explode('.', $row['title']);
  7071. if (count($explode) > 1) {
  7072. for ($i = 0; $i < count($explode) - 1; $i++) {
  7073. $item_title .= $explode[$i];
  7074. }
  7075. } else {
  7076. $item_title = $row['title'];
  7077. }
  7078. $item_title = str_replace('_', ' ', $item_title);
  7079. if (empty ($item_title)) {
  7080. $path_parts = pathinfo($row['path']);
  7081. $item_title = stripslashes($path_parts['filename']);
  7082. }
  7083. } else {
  7084. $item_title = '';
  7085. $item_description = '';
  7086. }
  7087. $return = '<legend>';
  7088. if ($id != 0 && is_array($extra_info)) {
  7089. $parent = $extra_info['parent_item_id'];
  7090. } else {
  7091. $parent = 0;
  7092. }
  7093. $sql = "SELECT * FROM ".$tbl_lp_item."
  7094. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7095. $result = Database::query($sql);
  7096. $arrLP = array();
  7097. while ($row = Database :: fetch_array($result)) {
  7098. $arrLP[] = array(
  7099. 'id' => $row['id'],
  7100. 'item_type' => $row['item_type'],
  7101. 'title' => $row['title'],
  7102. 'path' => $row['path'],
  7103. 'description' => $row['description'],
  7104. 'parent_item_id' => $row['parent_item_id'],
  7105. 'previous_item_id' => $row['previous_item_id'],
  7106. 'next_item_id' => $row['next_item_id'],
  7107. 'display_order' => $row['display_order'],
  7108. 'max_score' => $row['max_score'],
  7109. 'min_score' => $row['min_score'],
  7110. 'mastery_score' => $row['mastery_score'],
  7111. 'prerequisite' => $row['prerequisite']
  7112. );
  7113. }
  7114. $this->tree_array($arrLP);
  7115. $arrLP = $this->arrMenu;
  7116. unset ($this->arrMenu);
  7117. if ($action == 'add') {
  7118. $return .= get_lang('CreateTheDocument');
  7119. } elseif ($action == 'move') {
  7120. $return .= get_lang('MoveTheCurrentDocument');
  7121. } else {
  7122. $return .= get_lang('EditTheCurrentDocument');
  7123. }
  7124. $return .= '</legend>';
  7125. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7126. $return .= Display :: return_warning_message(
  7127. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7128. false
  7129. );
  7130. }
  7131. $form = new FormValidator('form', 'POST', api_get_self(
  7132. ).'?'.$_SERVER['QUERY_STRING'], '', array('enctype' => "multipart/form-data"));
  7133. $defaults['title'] = Security :: remove_XSS($item_title);
  7134. if (empty($item_title)) {
  7135. $defaults['title'] = Security::remove_XSS($item_title);
  7136. }
  7137. $defaults['description'] = $item_description;
  7138. $form->addElement('html', $return);
  7139. if ($action != 'move') {
  7140. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  7141. $form->applyFilter('title', 'html_filter');
  7142. }
  7143. //$arrHide = array($id);
  7144. $arrHide[0]['value'] = $this->name;
  7145. $arrHide[0]['padding'] = 3;
  7146. for ($i = 0; $i < count($arrLP); $i++) {
  7147. if ($action != 'add') {
  7148. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7149. $arrLP[$i]['id'],
  7150. $arrHide
  7151. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7152. ) {
  7153. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7154. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7155. if ($parent == $arrLP[$i]['id']) {
  7156. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7157. }
  7158. }
  7159. } else {
  7160. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7161. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7162. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7163. if ($parent == $arrLP[$i]['id']) {
  7164. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7165. }
  7166. }
  7167. }
  7168. }
  7169. $parent_select = $form->addElement(
  7170. 'select',
  7171. 'parent',
  7172. get_lang('Parent'),
  7173. '',
  7174. 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"'
  7175. );
  7176. $my_count = 0;
  7177. foreach ($arrHide as $key => $value) {
  7178. if ($my_count != 0) {
  7179. // The LP name is also the first section and is not in the same charset like the other sections.
  7180. $value['value'] = Security :: remove_XSS($value['value']);
  7181. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7182. } else {
  7183. $value['value'] = Security :: remove_XSS($value['value']);
  7184. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7185. }
  7186. $my_count++;
  7187. }
  7188. if (!empty($id)) {
  7189. $parent_select->setSelected($parent);
  7190. } else {
  7191. $parent_item_id = $_SESSION['parent_item_id'];
  7192. $parent_select->setSelected($parent_item_id);
  7193. }
  7194. if (is_array($arrLP)) {
  7195. reset($arrLP);
  7196. }
  7197. $arrHide = array();
  7198. //POSITION
  7199. for ($i = 0; $i < count($arrLP); $i++) {
  7200. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7201. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7202. $s_selected_position = $arrLP[$i]['id'];
  7203. } elseif ($action == 'add') {
  7204. $s_selected_position = $arrLP[$i]['id'];
  7205. }
  7206. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7207. }
  7208. }
  7209. $position = $form->addElement(
  7210. 'select',
  7211. 'previous',
  7212. get_lang('Position'),
  7213. '',
  7214. 'id="previous" class="learnpath_item_form" style="width:40%;"'
  7215. );
  7216. $position->addOption(get_lang('FirstPosition'), 0);
  7217. foreach ($arrHide as $key => $value) {
  7218. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7219. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7220. }
  7221. $position->setSelected($s_selected_position);
  7222. if (is_array($arrLP)) {
  7223. reset($arrLP);
  7224. }
  7225. if ($action != 'move') {
  7226. $id_prerequisite = 0;
  7227. if (is_array($arrLP)) {
  7228. foreach ($arrLP as $key => $value) {
  7229. if ($value['id'] == $id) {
  7230. $id_prerequisite = $value['prerequisite'];
  7231. break;
  7232. }
  7233. }
  7234. }
  7235. $arrHide = array();
  7236. for ($i = 0; $i < count($arrLP); $i++) {
  7237. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7238. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7239. $s_selected_position = $arrLP[$i]['id'];
  7240. } elseif ($action == 'add') {
  7241. $s_selected_position = $arrLP[$i]['id'];
  7242. }
  7243. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7244. }
  7245. }
  7246. if (!$no_display_add) {
  7247. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  7248. if (isset ($_POST['content'])) {
  7249. $content = stripslashes($_POST['content']);
  7250. } elseif (is_array($extra_info)) {
  7251. //If it's an html document or a text file
  7252. if (!$no_display_edit_textarea) {
  7253. $content = $this->display_document($extra_info['path'], false, false);
  7254. }
  7255. } elseif (is_numeric($extra_info)) {
  7256. $content = $this->display_document($extra_info, false, false);
  7257. } else {
  7258. $content = '';
  7259. }
  7260. if (!$no_display_edit_textarea) {
  7261. // We need to calculate here some specific settings for the online editor.
  7262. // The calculated settings work for documents in the Documents tool
  7263. // (on the root or in subfolders).
  7264. // For documents in native scorm packages it is unclear whether the
  7265. // online editor should be activated or not.
  7266. // A new document, it is in the root of the repository.
  7267. $relative_path = '';
  7268. $relative_prefix = '';
  7269. if (is_array($extra_info) && $extra_info != 'new') {
  7270. // The document already exists. Whe have to determine its relative path towards the repository root.
  7271. $relative_path = explode('/', $extra_info['dir']);
  7272. $cnt = count($relative_path) - 2;
  7273. if ($cnt < 0) {
  7274. $cnt = 0;
  7275. }
  7276. $relative_prefix = str_repeat('../', $cnt);
  7277. $relative_path = array_slice($relative_path, 1, $cnt);
  7278. $relative_path = implode('/', $relative_path);
  7279. if (strlen($relative_path) > 0) {
  7280. $relative_path = $relative_path.'/';
  7281. }
  7282. } else {
  7283. $_course = api_get_course_info();
  7284. $result = $this->generate_lp_folder($_course);
  7285. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7286. $relative_prefix = '../../';
  7287. }
  7288. $editor_config = array(
  7289. 'ToolbarSet' => 'LearningPathDocuments',
  7290. 'Width' => '100%',
  7291. 'Height' => '500',
  7292. 'FullPage' => true,
  7293. 'CreateDocumentDir' => $relative_prefix,
  7294. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7295. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path(
  7296. ).'/document/'.$relative_path
  7297. );
  7298. if ($_GET['action'] == 'add_item') {
  7299. $class = 'add';
  7300. $text = get_lang('LPCreateDocument');
  7301. } else {
  7302. if ($_GET['action'] == 'edit_item') {
  7303. $class = 'save';
  7304. $text = get_lang('SaveDocument');
  7305. }
  7306. }
  7307. $form->addElement('style_submit_button', 'submit_button', $text, 'class="'.$class.'"');
  7308. $renderer = $form->defaultRenderer();
  7309. $renderer->setElementTemplate(
  7310. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7311. 'content_lp'
  7312. );
  7313. $form->addElement('html', '<div>');
  7314. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7315. $form->addElement('html', '</div>');
  7316. $defaults['content_lp'] = $content;
  7317. }
  7318. } elseif (is_numeric($extra_info)) {
  7319. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7320. $return = $this->display_document($extra_info, true, true, true);
  7321. $form->addElement('html', $return);
  7322. }
  7323. }
  7324. }
  7325. if ($action == 'move') {
  7326. $form->addElement('hidden', 'title', $item_title);
  7327. $form->addElement('hidden', 'description', $item_description);
  7328. }
  7329. if (is_numeric($extra_info)) {
  7330. $form->addElement(
  7331. 'style_submit_button',
  7332. 'submit_button',
  7333. get_lang('SaveDocument'),
  7334. 'value="submit_button", class="save"'
  7335. );
  7336. $form->addElement('hidden', 'path', $extra_info);
  7337. } elseif (is_array($extra_info)) {
  7338. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7339. $form->addElement('hidden', 'path', $extra_info['path']);
  7340. }
  7341. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7342. $form->addElement('hidden', 'post_time', time());
  7343. $form->setDefaults($defaults);
  7344. return $form->return_form();
  7345. }
  7346. /**
  7347. * Return HTML form to add/edit a link item
  7348. * @param string Action (add/edit)
  7349. * @param integer Item ID if exists
  7350. * @param mixed Extra info
  7351. * @return string HTML form
  7352. */
  7353. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7354. {
  7355. $course_id = api_get_course_int_id();
  7356. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7357. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7358. if ($id != 0 && is_array($extra_info)) {
  7359. $item_title = stripslashes($extra_info['title']);
  7360. $item_description = stripslashes($extra_info['description']);
  7361. $item_url = stripslashes($extra_info['url']);
  7362. } elseif (is_numeric($extra_info)) {
  7363. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7364. $result = Database::query($sql_link);
  7365. $row = Database :: fetch_array($result);
  7366. $item_title = $row['title'];
  7367. $item_description = $row['description'];
  7368. $item_url = $row['url'];
  7369. } else {
  7370. $item_title = '';
  7371. $item_description = '';
  7372. $item_url = '';
  7373. }
  7374. $legend = '<legend>';
  7375. if ($id != 0 && is_array($extra_info)) {
  7376. $parent = $extra_info['parent_item_id'];
  7377. } else {
  7378. $parent = 0;
  7379. }
  7380. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7381. $result = Database::query($sql);
  7382. $arrLP = array();
  7383. while ($row = Database :: fetch_array($result)) {
  7384. $arrLP[] = array(
  7385. 'id' => $row['id'],
  7386. 'item_type' => $row['item_type'],
  7387. 'title' => $row['title'],
  7388. 'path' => $row['path'],
  7389. 'description' => $row['description'],
  7390. 'parent_item_id' => $row['parent_item_id'],
  7391. 'previous_item_id' => $row['previous_item_id'],
  7392. 'next_item_id' => $row['next_item_id'],
  7393. 'display_order' => $row['display_order'],
  7394. 'max_score' => $row['max_score'],
  7395. 'min_score' => $row['min_score'],
  7396. 'mastery_score' => $row['mastery_score'],
  7397. 'prerequisite' => $row['prerequisite']
  7398. );
  7399. }
  7400. $this->tree_array($arrLP);
  7401. $arrLP = $this->arrMenu;
  7402. unset ($this->arrMenu);
  7403. if ($action == 'add') {
  7404. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7405. } elseif ($action == 'move') {
  7406. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7407. } else {
  7408. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7409. }
  7410. $legend .= '</legend>';
  7411. $return .= '<div class="sectioncomment">';
  7412. $return .= '<form method="POST">';
  7413. $return .= $legend;
  7414. $return .= '<table>';
  7415. if ($action != 'move') {
  7416. $return .= '<tr>';
  7417. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7418. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7419. $return .= '</tr>';
  7420. }
  7421. $return .= '<tr>';
  7422. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7423. $return .= '<td class="input">';
  7424. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7425. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7426. $arrHide = array(
  7427. $id
  7428. );
  7429. $parent_item_id = $_SESSION['parent_item_id'];
  7430. for ($i = 0; $i < count($arrLP); $i++) {
  7431. if ($action != 'add') {
  7432. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7433. $arrLP[$i]['id'],
  7434. $arrHide
  7435. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7436. ) {
  7437. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7438. } else {
  7439. $arrHide[] = $arrLP[$i]['id'];
  7440. }
  7441. } else {
  7442. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7443. $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>';
  7444. }
  7445. }
  7446. }
  7447. if (is_array($arrLP)) {
  7448. reset($arrLP);
  7449. }
  7450. $return .= '</select>';
  7451. $return .= '</td>';
  7452. $return .= '</tr>';
  7453. $return .= '<tr>';
  7454. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7455. $return .= '<td class="input">';
  7456. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7457. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7458. for ($i = 0; $i < count($arrLP); $i++) {
  7459. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7460. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7461. $selected = 'selected="selected" ';
  7462. } elseif ($action == 'add') {
  7463. $selected = 'selected="selected" ';
  7464. } else {
  7465. $selected = '';
  7466. }
  7467. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7468. 'After'
  7469. ).' "'.$arrLP[$i]['title'].'"</option>';
  7470. }
  7471. }
  7472. $return .= '</select>';
  7473. $return .= '</td>';
  7474. $return .= '</tr>';
  7475. if ($action != 'move') {
  7476. $return .= '<tr>';
  7477. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7478. $return .= '<td class="input"><input'.(is_numeric(
  7479. $extra_info
  7480. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7481. $return .= '</tr>';
  7482. $id_prerequisite = 0;
  7483. if (is_array($arrLP)) {
  7484. foreach ($arrLP as $key => $value) {
  7485. if ($value['id'] == $id) {
  7486. $id_prerequisite = $value['prerequisite'];
  7487. break;
  7488. }
  7489. }
  7490. }
  7491. $arrHide = array();
  7492. for ($i = 0; $i < count($arrLP); $i++) {
  7493. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7494. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7495. $s_selected_position = $arrLP[$i]['id'];
  7496. } elseif ($action == 'add') {
  7497. $s_selected_position = 0;
  7498. }
  7499. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7500. }
  7501. }
  7502. $return .= '</tr>';
  7503. }
  7504. $return .= '<tr>';
  7505. if ($action == 'add') {
  7506. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7507. 'AddLinkToCourse'
  7508. ).'</button></td>';
  7509. } else {
  7510. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7511. 'EditCurrentLink'
  7512. ).'</button></td>';
  7513. }
  7514. $return .= '</tr>';
  7515. $return .= '</table>';
  7516. if ($action == 'move') {
  7517. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7518. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7519. }
  7520. if (is_numeric($extra_info)) {
  7521. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7522. } elseif (is_array($extra_info)) {
  7523. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7524. }
  7525. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7526. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7527. $return .= '</form>';
  7528. $return .= '</div>';
  7529. return $return;
  7530. }
  7531. /**
  7532. * Return HTML form to add/edit a student publication (work)
  7533. * @param string Action (add/edit)
  7534. * @param integer Item ID if already exists
  7535. * @param mixed Extra info (work ID if integer)
  7536. * @return string HTML form
  7537. */
  7538. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7539. {
  7540. $course_id = api_get_course_int_id();
  7541. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7542. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7543. if ($id != 0 && is_array($extra_info)) {
  7544. $item_title = stripslashes($extra_info['title']);
  7545. $item_description = stripslashes($extra_info['description']);
  7546. } elseif (is_numeric($extra_info)) {
  7547. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7548. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7549. $result = Database::query($sql_publication);
  7550. $row = Database :: fetch_array($result);
  7551. $item_title = $row['title'];
  7552. } else {
  7553. $item_title = get_lang('Student_publication');
  7554. }
  7555. $legend = '<legend>';
  7556. if ($id != 0 && is_array($extra_info)) {
  7557. $parent = $extra_info['parent_item_id'];
  7558. } else {
  7559. $parent = 0;
  7560. }
  7561. $sql = "SELECT * FROM ".$tbl_lp_item."
  7562. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7563. $result = Database::query($sql);
  7564. $arrLP = array();
  7565. while ($row = Database :: fetch_array($result)) {
  7566. $arrLP[] = array(
  7567. 'id' => $row['id'],
  7568. 'item_type' => $row['item_type'],
  7569. 'title' => $row['title'],
  7570. 'path' => $row['path'],
  7571. 'description' => $row['description'],
  7572. 'parent_item_id' => $row['parent_item_id'],
  7573. 'previous_item_id' => $row['previous_item_id'],
  7574. 'next_item_id' => $row['next_item_id'],
  7575. 'display_order' => $row['display_order'],
  7576. 'max_score' => $row['max_score'],
  7577. 'min_score' => $row['min_score'],
  7578. 'mastery_score' => $row['mastery_score'],
  7579. 'prerequisite' => $row['prerequisite']
  7580. );
  7581. }
  7582. $this->tree_array($arrLP);
  7583. $arrLP = $this->arrMenu;
  7584. unset ($this->arrMenu);
  7585. if ($action == 'add') {
  7586. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7587. } elseif ($action == 'move') {
  7588. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7589. } else {
  7590. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7591. }
  7592. $legend .= '</legend>';
  7593. $return .= '<div class="sectioncomment">';
  7594. $return .= '<form method="POST">';
  7595. $return .= $legend;
  7596. $return .= '<table class="lp_form">';
  7597. if ($action != 'move') {
  7598. $return .= '<tr>';
  7599. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7600. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7601. $return .= '</tr>';
  7602. }
  7603. $return .= '<tr>';
  7604. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7605. $return .= '<td class="input">';
  7606. $return .= "\t\t\t\t".'<select id="idParent" name="parent" style="width:100%;" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7607. //$parent_item_id = $_SESSION['parent_item_id'];
  7608. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7609. $arrHide = array(
  7610. $id
  7611. );
  7612. for ($i = 0; $i < count($arrLP); $i++) {
  7613. if ($action != 'add') {
  7614. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7615. $arrLP[$i]['id'],
  7616. $arrHide
  7617. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7618. ) {
  7619. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7620. } else {
  7621. $arrHide[] = $arrLP[$i]['id'];
  7622. }
  7623. } else {
  7624. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7625. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7626. }
  7627. }
  7628. }
  7629. if (is_array($arrLP)) {
  7630. reset($arrLP);
  7631. }
  7632. $return .= "\t\t\t\t".'</select>';
  7633. $return .= '</td>';
  7634. $return .= '</tr>';
  7635. $return .= '<tr>';
  7636. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7637. $return .= '<td class="input">';
  7638. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7639. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7640. for ($i = 0; $i < count($arrLP); $i++) {
  7641. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7642. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7643. $selected = 'selected="selected" ';
  7644. } elseif ($action == 'add') {
  7645. $selected = 'selected="selected" ';
  7646. } else {
  7647. $selected = '';
  7648. }
  7649. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7650. 'After'
  7651. ).' "'.$arrLP[$i]['title'].'"</option>';
  7652. }
  7653. }
  7654. $return .= "\t\t\t\t".'</select>';
  7655. $return .= '</td>';
  7656. $return .= '</tr>';
  7657. if ($action != 'move') {
  7658. $id_prerequisite = 0;
  7659. if (is_array($arrLP)) {
  7660. foreach ($arrLP as $key => $value) {
  7661. if ($value['id'] == $id) {
  7662. $id_prerequisite = $value['prerequisite'];
  7663. break;
  7664. }
  7665. }
  7666. }
  7667. $arrHide = array();
  7668. for ($i = 0; $i < count($arrLP); $i++) {
  7669. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7670. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7671. $s_selected_position = $arrLP[$i]['id'];
  7672. } elseif ($action == 'add') {
  7673. $s_selected_position = 0;
  7674. }
  7675. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7676. }
  7677. }
  7678. // Commented the prerequisites, only visible in edit (work).
  7679. /*
  7680. $return .= '<tr>';
  7681. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7682. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7683. foreach($arrHide as $key => $value) {
  7684. if ($key == $s_selected_position && $action == 'add') {
  7685. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7686. }
  7687. elseif ($key == $id_prerequisite && $action == 'edit') {
  7688. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7689. }
  7690. else {
  7691. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7692. }
  7693. }
  7694. $return .= "</select></td>";
  7695. */
  7696. $return .= '</tr>';
  7697. }
  7698. $return .= '<tr>';
  7699. if ($action == 'add') {
  7700. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7701. 'AddAssignmentToCourse'
  7702. ).'</button></td>';
  7703. } else {
  7704. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7705. 'EditCurrentStudentPublication'
  7706. ).'</button></td>';
  7707. }
  7708. $return .= '</tr>';
  7709. $return .= '</table>';
  7710. if ($action == 'move') {
  7711. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7712. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7713. }
  7714. if (is_numeric($extra_info)) {
  7715. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7716. } elseif (is_array($extra_info)) {
  7717. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7718. }
  7719. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7720. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7721. $return .= '</form>';
  7722. $return .= '</div>';
  7723. return $return;
  7724. }
  7725. /**
  7726. * Displays the menu for manipulating a step
  7727. * @return string html
  7728. */
  7729. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7730. {
  7731. $course_id = api_get_course_int_id();
  7732. $course_code = api_get_course_id();
  7733. $_course = api_get_course_info();
  7734. $return = '<div class="actions">';
  7735. switch ($item_type) {
  7736. case 'dokeos_chapter' :
  7737. case 'chapter' :
  7738. // Commented the message cause should not show it.
  7739. //$lang = get_lang('TitleManipulateChapter');
  7740. break;
  7741. case 'dokeos_module' :
  7742. case 'module' :
  7743. // Commented the message cause should not show it.
  7744. //$lang = get_lang('TitleManipulateModule');
  7745. break;
  7746. case TOOL_DOCUMENT :
  7747. // Commented the message cause should not show it.
  7748. //$lang = get_lang('TitleManipulateDocument');
  7749. break;
  7750. case TOOL_LINK :
  7751. case 'link' :
  7752. // Commented the message cause should not show it.
  7753. //$lang = get_lang('TitleManipulateLink');
  7754. break;
  7755. case TOOL_QUIZ :
  7756. // Commented the message cause should not show it.
  7757. //$lang = get_lang('TitleManipulateQuiz');
  7758. break;
  7759. case TOOL_STUDENTPUBLICATION :
  7760. // Commented the message cause should not show it.
  7761. //$lang = get_lang('TitleManipulateStudentPublication');
  7762. break;
  7763. }
  7764. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7765. $item_id = intval($item_id);
  7766. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7767. $result = Database::query($sql);
  7768. $row = Database::fetch_assoc($result);
  7769. $audio_player = null;
  7770. // We display an audio player if needed.
  7771. if (!empty($row['audio'])) {
  7772. $audio_player .= '<div class="lp_mediaplayer" id="container"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  7773. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7774. $audio_player .= '<script>
  7775. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7776. s1.addParam("allowscriptaccess","always");
  7777. s1.addParam("flashvars","file=../../courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7778. s1.write("container");
  7779. </script>';
  7780. }
  7781. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7782. $_GET['cidReq']
  7783. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7784. $return .= Display::url(
  7785. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7786. $url.'&action=edit_item&path_item='.$row['path']
  7787. );
  7788. $return .= Display::url(
  7789. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7790. $url.'&action=move_item'
  7791. );
  7792. // Commented for now as prerequisites cannot be added to chapters.
  7793. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7794. $return .= Display::url(
  7795. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7796. $url.'&action=edit_item_prereq'
  7797. );
  7798. }
  7799. $return .= Display::url(
  7800. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7801. $url.'&action=delete_item'
  7802. );
  7803. if ($item_type == TOOL_HOTPOTATOES) {
  7804. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7805. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7806. }
  7807. if ($item_type == TOOL_DOCUMENT) {
  7808. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7809. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7810. }
  7811. $return .= '</div>';
  7812. if (!empty($audio_player)) {
  7813. $return .= '<br />'.$audio_player;
  7814. }
  7815. return $return;
  7816. }
  7817. /**
  7818. * Creates the javascript needed for filling up the checkboxes without page reload
  7819. *
  7820. * @return string
  7821. */
  7822. public function get_js_dropdown_array()
  7823. {
  7824. $course_id = api_get_course_int_id();
  7825. $return = 'var child_name = new Array();'."\n";
  7826. $return .= 'var child_value = new Array();'."\n\n";
  7827. $return .= 'child_name[0] = new Array();'."\n";
  7828. $return .= 'child_value[0] = new Array();'."\n\n";
  7829. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7830. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7831. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7832. ORDER BY display_order ASC";
  7833. $res_zero = Database::query($sql_zero);
  7834. $i = 0;
  7835. while ($row_zero = Database :: fetch_array($res_zero)) {
  7836. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7837. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7838. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7839. }
  7840. $return .= "\n";
  7841. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7842. $res = Database::query($sql);
  7843. while ($row = Database :: fetch_array($res)) {
  7844. $sql_parent = "
  7845. SELECT * FROM ".$tbl_lp_item."
  7846. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7847. ORDER BY display_order ASC";
  7848. $res_parent = Database::query($sql_parent);
  7849. $i = 0;
  7850. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7851. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7852. while ($row_parent = Database :: fetch_array($res_parent)) {
  7853. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7854. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7855. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7856. }
  7857. $return .= "\n";
  7858. }
  7859. return $return;
  7860. }
  7861. /**
  7862. * Display the form to allow moving an item
  7863. * @param integer Item ID
  7864. * @return string HTML form
  7865. */
  7866. public function display_move_item($item_id)
  7867. {
  7868. $course_id = api_get_course_int_id();
  7869. $return = '';
  7870. if (is_numeric($item_id)) {
  7871. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7872. $sql = "SELECT * FROM ".$tbl_lp_item."
  7873. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7874. $res = Database::query($sql);
  7875. $row = Database :: fetch_array($res);
  7876. switch ($row['item_type']) {
  7877. case 'dokeos_chapter' :
  7878. case 'dir' :
  7879. case 'asset' :
  7880. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7881. $return .= $this->display_item_form(
  7882. $row['item_type'],
  7883. get_lang('MoveCurrentChapter'),
  7884. 'move',
  7885. $item_id,
  7886. $row
  7887. );
  7888. break;
  7889. case 'dokeos_module' :
  7890. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7891. $return .= $this->display_item_form(
  7892. $row['item_type'],
  7893. 'Move th current module:',
  7894. 'move',
  7895. $item_id,
  7896. $row
  7897. );
  7898. break;
  7899. case TOOL_DOCUMENT :
  7900. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7901. $return .= $this->display_document_form('move', $item_id, $row);
  7902. break;
  7903. case TOOL_LINK :
  7904. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7905. $return .= $this->display_link_form('move', $item_id, $row);
  7906. break;
  7907. case TOOL_HOTPOTATOES :
  7908. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7909. $return .= $this->display_link_form('move', $item_id, $row);
  7910. break;
  7911. case TOOL_QUIZ :
  7912. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7913. $return .= $this->display_quiz_form('move', $item_id, $row);
  7914. break;
  7915. case TOOL_STUDENTPUBLICATION :
  7916. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7917. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7918. break;
  7919. case TOOL_FORUM :
  7920. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7921. $return .= $this->display_forum_form('move', $item_id, $row);
  7922. break;
  7923. case TOOL_THREAD :
  7924. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7925. $return .= $this->display_forum_form('move', $item_id, $row);
  7926. break;
  7927. }
  7928. }
  7929. return $return;
  7930. }
  7931. /**
  7932. * Displays a basic form on the overview page for changing the item title and the item description.
  7933. * @param string $item_type
  7934. * @param string $title
  7935. * @param array $data
  7936. * @return string
  7937. */
  7938. public function display_item_small_form($item_type, $title = '', $data = array())
  7939. {
  7940. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7941. $form = new FormValidator('small_form', 'post', $url);
  7942. $form->addElement('header', $title);
  7943. $form->addElement('text', 'title', get_lang('Title'));
  7944. $form->addElement('button', 'submit_button', get_lang('Save'));
  7945. $form->addElement('hidden', 'id', $data['id']);
  7946. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7947. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7948. $form->setDefaults(array('title' => $data['title']));
  7949. return $form->toHtml();
  7950. }
  7951. /**
  7952. * Return HTML form to allow prerequisites selection
  7953. * @param integer Item ID
  7954. * @return string HTML form
  7955. */
  7956. public function display_item_prerequisites_form($item_id)
  7957. {
  7958. $course_id = api_get_course_int_id();
  7959. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7960. $item_id = intval($item_id);
  7961. /* Current prerequisite */
  7962. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7963. $result = Database::query($sql);
  7964. $row = Database::fetch_array($result);
  7965. $preq_id = $row['prerequisite'];
  7966. $return = '<legend>';
  7967. $return .= get_lang('AddEditPrerequisites');
  7968. $return .= '</legend>';
  7969. $return .= '<form method="POST">';
  7970. $return .= '<table class="data_table">';
  7971. $return .= '<tr>';
  7972. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7973. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  7974. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  7975. $return .= '</tr>';
  7976. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7977. $return .= '<tr >';
  7978. $return .= '<td colspan="3" class="radio">';
  7979. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7980. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  7981. $return .= '</tr>';
  7982. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7983. $result = Database::query($sql);
  7984. $arrLP = array();
  7985. while ($row = Database :: fetch_array($result)) {
  7986. $arrLP[] = array(
  7987. 'id' => $row['id'],
  7988. 'item_type' => $row['item_type'],
  7989. 'title' => $row['title'],
  7990. 'ref' => $row['ref'],
  7991. 'description' => $row['description'],
  7992. 'parent_item_id' => $row['parent_item_id'],
  7993. 'previous_item_id' => $row['previous_item_id'],
  7994. 'next_item_id' => $row['next_item_id'],
  7995. 'max_score' => $row['max_score'],
  7996. 'min_score' => $row['min_score'],
  7997. 'mastery_score' => $row['mastery_score'],
  7998. 'prerequisite' => $row['prerequisite'],
  7999. 'next_item_id' => $row['next_item_id'],
  8000. 'display_order' => $row['display_order']
  8001. );
  8002. if ($row['ref'] == $preq_id) {
  8003. $preq_mastery = $row['mastery_score'];
  8004. $preq_max = $row['max_score'];
  8005. }
  8006. }
  8007. $this->tree_array($arrLP);
  8008. $arrLP = $this->arrMenu;
  8009. unset ($this->arrMenu);
  8010. for ($i = 0; $i < count($arrLP); $i++) {
  8011. if ($arrLP[$i]['id'] == $item_id) {
  8012. break;
  8013. }
  8014. $return .= '<tr>';
  8015. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8016. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  8017. $depth_multi = $arrLP[$i]['depth'] * 10;
  8018. $return .= '<input'.(($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '').(($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ').'id="id'.$arrLP[$i]['id'].'" name="prerequisites" style="margin-left:'.$depth_multi.'px; margin-right:10px;" type="radio" value="'.$arrLP[$i]['id'].'" />';
  8019. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8020. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8021. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.png" style="margin-right:5px;" title="" />';
  8022. } else {
  8023. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  8024. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.gif" style="margin-right:5px;" title="" />';
  8025. } else {
  8026. $return .= Display::return_icon(
  8027. 'folder_document.gif',
  8028. '',
  8029. array('style' => 'margin-right:5px;')
  8030. );
  8031. }
  8032. }
  8033. $return .= $arrLP[$i]['title'].'</label>';
  8034. $return .= '</td>';
  8035. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8036. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8037. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8038. $return .= '</td>';
  8039. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8040. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8041. $return .= '</td>';
  8042. }
  8043. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8044. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8045. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8046. $return .= '</td>';
  8047. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8048. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8049. $return .= '</td>';
  8050. }
  8051. $return .= '</tr>';
  8052. }
  8053. $return .= '<tr>';
  8054. $return .= '</tr>';
  8055. $return .= '</table>';
  8056. $return .= '<div style="padding-top:3px;">';
  8057. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8058. 'ModifyPrerequisites'
  8059. ).'</button>';
  8060. $return .= '</form>';
  8061. return $return;
  8062. }
  8063. /**
  8064. * Return HTML list to allow prerequisites selection for lp
  8065. * @param integer Item ID
  8066. * @return string HTML form
  8067. */
  8068. public function display_lp_prerequisites_list()
  8069. {
  8070. $course_id = api_get_course_int_id();
  8071. $lp_id = $this->lp_id;
  8072. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8073. // get current prerequisite
  8074. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8075. $result = Database::query($sql);
  8076. $row = Database :: fetch_array($result);
  8077. $preq_id = $row['prerequisite'];
  8078. $session_id = api_get_session_id();
  8079. $session_condition = api_get_session_condition($session_id);
  8080. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8081. $rs = Database::query($sql);
  8082. $return = '';
  8083. $return .= '<select name="prerequisites" >';
  8084. $return .= '<option value="0">'.get_lang('None').'</option>';
  8085. if (Database::num_rows($rs) > 0) {
  8086. while ($row = Database::fetch_array($rs)) {
  8087. if ($row['id'] == $lp_id) {
  8088. continue;
  8089. }
  8090. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8091. }
  8092. }
  8093. $return .= '</select>';
  8094. return $return;
  8095. }
  8096. /**
  8097. * Creates a list with all the documents in it
  8098. * @return string
  8099. */
  8100. public function get_documents()
  8101. {
  8102. $course_info = api_get_course_info();
  8103. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8104. return $document_tree;
  8105. }
  8106. /**
  8107. * Creates a list with all the exercises (quiz) in it
  8108. * @return string
  8109. */
  8110. public function get_exercises()
  8111. {
  8112. $course_id = api_get_course_int_id();
  8113. // New for hotpotatoes.
  8114. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8115. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8116. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8117. $session_id = api_get_session_id();
  8118. $condition_session = api_get_session_condition($session_id);
  8119. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8120. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8121. $res_quiz = Database::query($sql_quiz);
  8122. $res_hot = Database::query($sql_hot);
  8123. $return = '<ul class="lp_resource">';
  8124. $return .= '<li class="lp_resource_element">';
  8125. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  8126. $return .= '<a href="'.api_get_path(
  8127. REL_CODE_PATH
  8128. ).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8129. $return .= '</li>';
  8130. // Display hotpotatoes
  8131. while ($row_hot = Database :: fetch_array($res_hot)) {
  8132. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8133. $return .= '<a class="moved" href="#">';
  8134. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8135. $return .= '</a> ';
  8136. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  8137. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8138. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8139. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8140. $row_hot['title']
  8141. )).'</a>';
  8142. $return .= '</li>';
  8143. }
  8144. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8145. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['iid'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8146. $return .= '<a class="moved" href="#">';
  8147. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8148. $return .= '</a> ';
  8149. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  8150. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8151. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['iid'].'&amp;lp_id='.$this->lp_id.'">'.
  8152. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8153. '</a>';
  8154. $return .= '</li>';
  8155. }
  8156. $return .= '</ul>';
  8157. return $return;
  8158. }
  8159. /**
  8160. * Creates a list with all the links in it
  8161. * @return string
  8162. */
  8163. public function get_links()
  8164. {
  8165. $course_id = api_get_course_int_id();
  8166. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8167. $session_id = api_get_session_id();
  8168. $condition_session = api_get_session_condition($session_id);
  8169. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8170. $res_link = Database::query($sql_link);
  8171. $return = '<ul class="lp_resource">';
  8172. $return .= '<li class="lp_resource_element">';
  8173. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  8174. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8175. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8176. $return .= '</li>';
  8177. $course_info = api_get_course_info();
  8178. while ($row_link = Database :: fetch_array($res_link)) {
  8179. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8180. if ($item_visibility != 2) {
  8181. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8182. $return .= '<a class="moved" href="#">';
  8183. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8184. $return .= '</a> ';
  8185. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  8186. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_LINK.'&amp;file='.$row_link['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8187. $row_link['title'].
  8188. '</a>';
  8189. $return .= '</li>';
  8190. }
  8191. }
  8192. $return .= '</ul>';
  8193. return $return;
  8194. }
  8195. /**
  8196. * Creates a list with all the student publications in it
  8197. * @return unknown
  8198. */
  8199. public function get_student_publications()
  8200. {
  8201. $return = '<div class="lp_resource" >';
  8202. $return .= '<div class="lp_resource_element">';
  8203. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  8204. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8205. ).'&amp;action=add_item&amp;type='.TOOL_STUDENTPUBLICATION.'&amp;lp_id='.$this->lp_id.'">'.get_lang(
  8206. 'AddAssignmentPage'
  8207. ).'</a>';
  8208. $return .= '</div>';
  8209. $return .= '</div>';
  8210. return $return;
  8211. }
  8212. /**
  8213. * Creates a list with all the forums in it
  8214. * @return string
  8215. */
  8216. public function get_forums()
  8217. {
  8218. require_once '../forum/forumfunction.inc.php';
  8219. require_once '../forum/forumconfig.inc.php';
  8220. $a_forums = get_forums();
  8221. $return = '<ul class="lp_resource">';
  8222. //First add link
  8223. $return .= '<li class="lp_resource_element">';
  8224. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  8225. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'forum/index.php?'.api_get_cidreq(
  8226. ).'&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id='.$this->lp_id.'" title="'.get_lang(
  8227. 'CreateANewForum'
  8228. ).'">'.get_lang('CreateANewForum').'</a>';
  8229. $return .= '</li>';
  8230. $return .= '<script>
  8231. function toggle_forum(forum_id){
  8232. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8233. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8234. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8235. } else {
  8236. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8237. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8238. }
  8239. }
  8240. </script>';
  8241. foreach ($a_forums as $forum) {
  8242. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8243. if (!empty($forum['forum_id'])) {
  8244. $return .= '<a class="moved" href="#">';
  8245. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8246. $return .= ' </a>';
  8247. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  8248. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8249. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8250. </a>
  8251. <a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_FORUM.'&amp;forum_id='.$forum['forum_id'].'&amp;lp_id='.$this->lp_id.'" style="vertical-align:middle">'.Security :: remove_XSS(
  8252. $forum['forum_title']
  8253. ).'</a>';
  8254. }
  8255. $return .= '</li>';
  8256. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8257. $a_threads = get_threads($forum['forum_id']);
  8258. if (is_array($a_threads)) {
  8259. foreach ($a_threads as $thread) {
  8260. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8261. $return .= '&nbsp;<a class="moved" href="#">';
  8262. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8263. $return .= ' </a>';
  8264. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8265. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_THREAD.'&amp;thread_id='.$thread['thread_id'].'&amp;lp_id='.$this->lp_id.'">'.Security :: remove_XSS(
  8266. $thread['thread_title']
  8267. ).'</a>';
  8268. $return .= '</li>';
  8269. }
  8270. }
  8271. $return .= '</div>';
  8272. }
  8273. $return .= '</ul>';
  8274. return $return;
  8275. }
  8276. /**
  8277. * // TODO: The output encoding should be equal to the system encoding.
  8278. *
  8279. * Exports the learning path as a SCORM package. This is the main function that
  8280. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8281. * whole thing and returns the zip.
  8282. *
  8283. * This method needs to be called in PHP5, as it will fail with non-adequate
  8284. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8285. * you need to call it on a learnpath object.
  8286. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8287. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8288. * path has been modified, it should use the generic method here below.
  8289. * @TODO link this function with the export_lp() function in the same class
  8290. * @param string Optional name of zip file. If none, title of learnpath is
  8291. * domesticated and trailed with ".zip"
  8292. * @return string Returns the zip package string, or null if error
  8293. */
  8294. public function scorm_export()
  8295. {
  8296. $_course = api_get_course_info();
  8297. $course_id = api_get_course_int_id();
  8298. $links_to_create = null;
  8299. // Remove memory and time limits as much as possible as this might be a long process...
  8300. if (function_exists('ini_set')) {
  8301. api_set_memory_limit('128M');
  8302. ini_set('max_execution_time', 600);
  8303. }
  8304. // Create the zip handler (this will remain available throughout the method).
  8305. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8306. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8307. $temp_dir_short = uniqid();
  8308. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8309. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8310. $zip_folder = new PclZip($temp_zip_file);
  8311. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8312. $root_path = $main_path = api_get_path(SYS_PATH);
  8313. $files_cleanup = array();
  8314. // Place to temporarily stash the zipfiles.
  8315. // create the temp dir if it doesn't exist
  8316. // or do a cleanup befor creating the zipfile.
  8317. if (!is_dir($temp_zip_dir)) {
  8318. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8319. } else {
  8320. // Cleanup: Check the temp dir for old files and delete them.
  8321. $handle = opendir($temp_zip_dir);
  8322. while (false !== ($file = readdir($handle))) {
  8323. if ($file != '.' && $file != '..') {
  8324. unlink("$temp_zip_dir/$file");
  8325. }
  8326. }
  8327. closedir($handle);
  8328. }
  8329. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8330. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8331. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8332. )
  8333. ) {
  8334. // Remove the possible . at the end of the path.
  8335. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8336. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8337. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8338. $zip_files_dist = api_copyr(
  8339. $current_course_path.'/scorm/'.$this->path,
  8340. $dest_path_to_scorm_folder,
  8341. array('imsmanifest'),
  8342. $zip_files
  8343. );
  8344. }
  8345. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8346. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8347. // Aggregation Model official document, secion "2.3 Content Packaging".
  8348. $xmldoc = new DOMDocument('1.0'); // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8349. $root = $xmldoc->createElement('manifest');
  8350. $root->setAttribute('identifier', 'SingleCourseManifest');
  8351. $root->setAttribute('version', '1.1');
  8352. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8353. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8354. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8355. $root->setAttribute(
  8356. 'xsi:schemaLocation',
  8357. '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'
  8358. );
  8359. // Build mandatory sub-root container elements.
  8360. $metadata = $xmldoc->createElement('metadata');
  8361. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8362. $metadata->appendChild($md_schema);
  8363. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8364. $metadata->appendChild($md_schemaversion);
  8365. $root->appendChild($metadata);
  8366. $organizations = $xmldoc->createElement('organizations');
  8367. $resources = $xmldoc->createElement('resources');
  8368. // Build the only organization we will use in building our learnpaths.
  8369. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8370. $organization = $xmldoc->createElement('organization');
  8371. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8372. // To set the title of the SCORM entity (=organization), we take the name given
  8373. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8374. // learning path charset) as it is the encoding that defines how it is stored
  8375. // in the database. Then we convert it to HTML entities again as the "&" character
  8376. // alone is not authorized in XML (must be &amp;).
  8377. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8378. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8379. $organization->appendChild($org_title);
  8380. $folder_name = 'document';
  8381. // Removes the learning_path/scorm_folder path when exporting see #4841
  8382. $path_to_remove = null;
  8383. $result = $this->generate_lp_folder($_course);
  8384. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8385. $path_to_remove = 'document'.$result['dir'];
  8386. $path_to_replace = $folder_name.'/';
  8387. }
  8388. //Fixes chamilo scorm exports
  8389. if ($this->ref == 'chamilo_scorm_export') {
  8390. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8391. }
  8392. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8393. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8394. $link_updates = array();
  8395. foreach ($this->items as $index => $item) {
  8396. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8397. // Get included documents from this item.
  8398. if ($item->type == 'sco') {
  8399. $inc_docs = $item->get_resources_from_source(
  8400. null,
  8401. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8402. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8403. );
  8404. } else {
  8405. $inc_docs = $item->get_resources_from_source();
  8406. }
  8407. // Give a child element <item> to the <organization> element.
  8408. $my_item_id = $item->get_id();
  8409. $my_item = $xmldoc->createElement('item');
  8410. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8411. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8412. $my_item->setAttribute('isvisible', 'true');
  8413. // Give a child element <title> to the <item> element.
  8414. $my_title = $xmldoc->createElement(
  8415. 'title',
  8416. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8417. );
  8418. $my_item->appendChild($my_title);
  8419. // Give a child element <adlcp:prerequisites> to the <item> element.
  8420. $my_prereqs = $xmldoc->createElement(
  8421. 'adlcp:prerequisites',
  8422. $this->get_scorm_prereq_string($my_item_id)
  8423. );
  8424. $my_prereqs->setAttribute('type', 'aicc_script');
  8425. $my_item->appendChild($my_prereqs);
  8426. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8427. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8428. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8429. //$xmldoc->createElement('adlcp:timelimitaction','');
  8430. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8431. //$xmldoc->createElement('adlcp:datafromlms','');
  8432. // Give a child element <adlcp:masteryscore> to the <item> element.
  8433. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8434. $my_item->appendChild($my_masteryscore);
  8435. // Attach this item to the organization element or hits parent if there is one.
  8436. if (!empty($item->parent) && $item->parent != 0) {
  8437. $children = $organization->childNodes;
  8438. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8439. if (is_object($possible_parent)) {
  8440. $possible_parent->appendChild($my_item);
  8441. } else {
  8442. if ($this->debug > 0) {
  8443. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8444. }
  8445. }
  8446. } else {
  8447. if ($this->debug > 0) {
  8448. error_log('No parent');
  8449. }
  8450. $organization->appendChild($my_item);
  8451. }
  8452. // Get the path of the file(s) from the course directory root.
  8453. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8454. if (!empty($path_to_remove)) {
  8455. //From docs
  8456. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8457. //From quiz
  8458. if ($this->ref == 'chamilo_scorm_export') {
  8459. $path_to_remove = 'scorm/'.$this->path.'/';
  8460. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8461. }
  8462. } else {
  8463. $my_xml_file_path = $my_file_path;
  8464. }
  8465. $my_sub_dir = dirname($my_file_path);
  8466. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8467. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8468. $my_xml_sub_dir = $my_sub_dir;
  8469. // Give a <resource> child to the <resources> element
  8470. $my_resource = $xmldoc->createElement('resource');
  8471. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8472. $my_resource->setAttribute('type', 'webcontent');
  8473. $my_resource->setAttribute('href', $my_xml_file_path);
  8474. // adlcp:scormtype can be either 'sco' or 'asset'.
  8475. if ($item->type == 'sco') {
  8476. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8477. } else {
  8478. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8479. }
  8480. // xml:base is the base directory to find the files declared in this resource.
  8481. $my_resource->setAttribute('xml:base', '');
  8482. // Give a <file> child to the <resource> element.
  8483. $my_file = $xmldoc->createElement('file');
  8484. $my_file->setAttribute('href', $my_xml_file_path);
  8485. $my_resource->appendChild($my_file);
  8486. // Dependency to other files - not yet supported.
  8487. $i = 1;
  8488. foreach ($inc_docs as $doc_info) {
  8489. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8490. continue;
  8491. }
  8492. $my_dep = $xmldoc->createElement('resource');
  8493. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8494. $my_dep->setAttribute('identifier', $res_id);
  8495. $my_dep->setAttribute('type', 'webcontent');
  8496. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8497. $my_dep_file = $xmldoc->createElement('file');
  8498. // Check type of URL.
  8499. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8500. if ($doc_info[1] == 'remote') {
  8501. // Remote file. Save url as is.
  8502. $my_dep_file->setAttribute('href', $doc_info[0]);
  8503. $my_dep->setAttribute('xml:base', '');
  8504. } elseif ($doc_info[1] == 'local') {
  8505. switch ($doc_info[2]) {
  8506. case 'url': // Local URL - save path as url for now, don't zip file.
  8507. $abs_path = api_get_path(SYS_PATH).str_replace(
  8508. api_get_path(WEB_PATH),
  8509. '',
  8510. $doc_info[0]
  8511. );
  8512. $current_dir = dirname($abs_path);
  8513. $current_dir = str_replace('\\', '/', $current_dir);
  8514. $file_path = realpath($abs_path);
  8515. $file_path = str_replace('\\', '/', $file_path);
  8516. $my_dep_file->setAttribute('href', $file_path);
  8517. $my_dep->setAttribute('xml:base', '');
  8518. if (strstr($file_path, $main_path) !== false) {
  8519. // The calculated real path is really inside Chamilo's root path.
  8520. // Reduce file path to what's under the DocumentRoot.
  8521. $file_path = substr($file_path, strlen($root_path) - 1);
  8522. //echo $file_path;echo '<br /><br />';
  8523. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8524. $zip_files_abs[] = $file_path;
  8525. $link_updates[$my_file_path][] = array(
  8526. 'orig' => $doc_info[0],
  8527. 'dest' => $file_path
  8528. );
  8529. $my_dep_file->setAttribute('href', $file_path);
  8530. $my_dep->setAttribute('xml:base', '');
  8531. } elseif (empty($file_path)) {
  8532. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8533. if (strpos($document_root, -1) == '/') {
  8534. $document_root = substr(0, -1, $document_root);
  8535. }*/
  8536. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8537. $file_path = str_replace('//', '/', $file_path);
  8538. if (file_exists($file_path)) {
  8539. $file_path = substr(
  8540. $file_path,
  8541. strlen($current_dir)
  8542. ); // We get the relative path.
  8543. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8544. $link_updates[$my_file_path][] = array(
  8545. 'orig' => $doc_info[0],
  8546. 'dest' => $file_path
  8547. );
  8548. $my_dep_file->setAttribute('href', $file_path);
  8549. $my_dep->setAttribute('xml:base', '');
  8550. }
  8551. }
  8552. break;
  8553. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8554. $my_dep_file->setAttribute('href', $doc_info[0]);
  8555. $my_dep->setAttribute('xml:base', '');
  8556. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8557. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8558. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8559. $abs_img_path_without_subdir = $doc_info[0];
  8560. $relp = api_get_path(REL_PATH); // The url-append config param.
  8561. $pos = strpos($abs_img_path_without_subdir, $relp);
  8562. if ($pos === 0) {
  8563. $abs_img_path_without_subdir = '/'.substr(
  8564. $abs_img_path_without_subdir,
  8565. strlen($relp)
  8566. );
  8567. }
  8568. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8569. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8570. $file_path = str_replace('\\', '/', $file_path);
  8571. $file_path = str_replace('//', '/', $file_path);
  8572. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8573. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8574. $cur_path = substr(
  8575. $current_course_path,
  8576. -1
  8577. ) == '/' ? $current_course_path : $current_course_path.'/';
  8578. // Check if the current document is in that path.
  8579. if (strstr($file_path, $cur_path) !== false) {
  8580. // The document is in that path, now get the relative path
  8581. // to the containing document.
  8582. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8583. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8584. $relative_path = '';
  8585. if (strstr($file_path, $cur_path) !== false) {
  8586. $relative_path = substr($file_path, strlen($orig_file_path));
  8587. $file_path = substr($file_path, strlen($cur_path));
  8588. } else {
  8589. // This case is still a problem as it's difficult to calculate a relative path easily
  8590. // might still generate wrong links.
  8591. //$file_path = substr($file_path,strlen($cur_path));
  8592. // Calculate the directory path to the current file (without trailing slash).
  8593. $my_relative_path = dirname($file_path);
  8594. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8595. $my_relative_file = basename($file_path);
  8596. // Calculate the directory path to the containing file (without trailing slash).
  8597. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8598. $dotdot = '';
  8599. $subdir = '';
  8600. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8601. $my_orig_file_path
  8602. ) > 1) && (strlen($my_relative_path) > 1)) {
  8603. $my_relative_path2 = dirname($my_relative_path);
  8604. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8605. $my_orig_file_path = dirname($my_orig_file_path);
  8606. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8607. $subdir = substr(
  8608. $my_relative_path,
  8609. strlen($my_relative_path2) + 1
  8610. ).'/'.$subdir;
  8611. $dotdot += '../';
  8612. $my_relative_path = $my_relative_path2;
  8613. }
  8614. $relative_path = $dotdot.$subdir.$my_relative_file;
  8615. }
  8616. // Put the current document in the zip (this array is the array
  8617. // that will manage documents already in the course folder - relative).
  8618. $zip_files[] = $file_path;
  8619. // Update the links to the current document in the containing document (make them relative).
  8620. $link_updates[$my_file_path][] = array(
  8621. 'orig' => $doc_info[0],
  8622. 'dest' => $relative_path
  8623. );
  8624. $my_dep_file->setAttribute('href', $file_path);
  8625. $my_dep->setAttribute('xml:base', '');
  8626. } elseif (strstr($file_path, $main_path) !== false) {
  8627. // The calculated real path is really inside Chamilo's root path.
  8628. // Reduce file path to what's under the DocumentRoot.
  8629. $file_path = substr($file_path, strlen($root_path));
  8630. //echo $file_path;echo '<br /><br />';
  8631. //error_log('Reduced path: '.$file_path, 0);
  8632. $zip_files_abs[] = $file_path;
  8633. $link_updates[$my_file_path][] = array(
  8634. 'orig' => $doc_info[0],
  8635. 'dest' => $file_path
  8636. );
  8637. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8638. $my_dep->setAttribute('xml:base', '');
  8639. } elseif (empty($file_path)) {
  8640. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8641. if(strpos($document_root,-1) == '/') {
  8642. $document_root = substr(0, -1, $document_root);
  8643. }*/
  8644. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8645. $file_path = str_replace('//', '/', $file_path);
  8646. if (file_exists($file_path)) {
  8647. $file_path = substr(
  8648. $file_path,
  8649. strlen($current_dir)
  8650. ); // We get the relative path.
  8651. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8652. $link_updates[$my_file_path][] = array(
  8653. 'orig' => $doc_info[0],
  8654. 'dest' => $file_path
  8655. );
  8656. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8657. $my_dep->setAttribute('xml:base', '');
  8658. }
  8659. }
  8660. break;
  8661. 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
  8662. if (substr($doc_info[0], 0, 2) == '..') {
  8663. // Relative path going up.
  8664. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8665. $current_dir = str_replace('\\', '/', $current_dir);
  8666. $file_path = realpath($current_dir.$doc_info[0]);
  8667. $file_path = str_replace('\\', '/', $file_path);
  8668. //error_log($file_path.' <-> '.$main_path,0);
  8669. if (strstr($file_path, $main_path) !== false) {
  8670. // The calculated real path is really inside Chamilo's root path.
  8671. // Reduce file path to what's under the DocumentRoot.
  8672. $file_path = substr($file_path, strlen($root_path));
  8673. //error_log('Reduced path: '.$file_path, 0);
  8674. $zip_files_abs[] = $file_path;
  8675. $link_updates[$my_file_path][] = array(
  8676. 'orig' => $doc_info[0],
  8677. 'dest' => $file_path
  8678. );
  8679. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8680. $my_dep->setAttribute('xml:base', '');
  8681. }
  8682. } else {
  8683. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8684. $my_dep_file->setAttribute('href', $doc_info[0]);
  8685. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8686. }
  8687. break;
  8688. default:
  8689. $my_dep_file->setAttribute('href', $doc_info[0]);
  8690. $my_dep->setAttribute('xml:base', '');
  8691. break;
  8692. }
  8693. }
  8694. $my_dep->appendChild($my_dep_file);
  8695. $resources->appendChild($my_dep);
  8696. $dependency = $xmldoc->createElement('dependency');
  8697. $dependency->setAttribute('identifierref', $res_id);
  8698. $my_resource->appendChild($dependency);
  8699. $i++;
  8700. }
  8701. //$my_dependency = $xmldoc->createElement('dependency');
  8702. //$my_dependency->setAttribute('identifierref', '');
  8703. $resources->appendChild($my_resource);
  8704. $zip_files[] = $my_file_path;
  8705. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8706. } else {
  8707. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8708. switch ($item->type) {
  8709. case TOOL_LINK:
  8710. $my_item = $xmldoc->createElement('item');
  8711. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8712. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8713. $my_item->setAttribute('isvisible', 'true');
  8714. // Give a child element <title> to the <item> element.
  8715. $my_title = $xmldoc->createElement(
  8716. 'title',
  8717. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8718. );
  8719. $my_item->appendChild($my_title);
  8720. // Give a child element <adlcp:prerequisites> to the <item> element.
  8721. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8722. $my_prereqs->setAttribute('type', 'aicc_script');
  8723. $my_item->appendChild($my_prereqs);
  8724. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8725. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8726. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8727. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8728. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8729. //$xmldoc->createElement('adlcp:datafromlms', '');
  8730. // Give a child element <adlcp:masteryscore> to the <item> element.
  8731. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8732. $my_item->appendChild($my_masteryscore);
  8733. // Attach this item to the organization element or its parent if there is one.
  8734. if (!empty($item->parent) && $item->parent != 0) {
  8735. $children = $organization->childNodes;
  8736. for ($i = 0; $i < $children->length; $i++) {
  8737. $item_temp = $children->item($i);
  8738. if ($item_temp->nodeName == 'item') {
  8739. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8740. $item_temp->appendChild($my_item);
  8741. }
  8742. }
  8743. }
  8744. } else {
  8745. $organization->appendChild($my_item);
  8746. }
  8747. $my_file_path = 'link_'.$item->get_id().'.html';
  8748. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8749. TABLE_LINK
  8750. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8751. $rs = Database::query($sql);
  8752. if ($link = Database :: fetch_array($rs)) {
  8753. $url = $link['url'];
  8754. $title = stripslashes($link['title']);
  8755. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8756. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8757. $my_xml_file_path = $my_file_path;
  8758. $my_sub_dir = dirname($my_file_path);
  8759. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8760. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8761. $my_xml_sub_dir = $my_sub_dir;
  8762. // Give a <resource> child to the <resources> element.
  8763. $my_resource = $xmldoc->createElement('resource');
  8764. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8765. $my_resource->setAttribute('type', 'webcontent');
  8766. $my_resource->setAttribute('href', $my_xml_file_path);
  8767. // adlcp:scormtype can be either 'sco' or 'asset'.
  8768. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8769. // xml:base is the base directory to find the files declared in this resource.
  8770. $my_resource->setAttribute('xml:base', '');
  8771. // give a <file> child to the <resource> element.
  8772. $my_file = $xmldoc->createElement('file');
  8773. $my_file->setAttribute('href', $my_xml_file_path);
  8774. $my_resource->appendChild($my_file);
  8775. $resources->appendChild($my_resource);
  8776. }
  8777. break;
  8778. case TOOL_QUIZ:
  8779. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8780. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8781. $exe = new Exercise();
  8782. $exe->read($exe_id);
  8783. $my_item = $xmldoc->createElement('item');
  8784. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8785. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8786. $my_item->setAttribute('isvisible', 'true');
  8787. // Give a child element <title> to the <item> element.
  8788. $my_title = $xmldoc->createElement(
  8789. 'title',
  8790. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8791. );
  8792. $my_item->appendChild($my_title);
  8793. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8794. //$my_item->appendChild($my_max_score);
  8795. // Give a child element <adlcp:prerequisites> to the <item> element.
  8796. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8797. $my_prereqs->setAttribute('type', 'aicc_script');
  8798. $my_item->appendChild($my_prereqs);
  8799. // Give a child element <adlcp:masteryscore> to the <item> element.
  8800. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8801. $my_item->appendChild($my_masteryscore);
  8802. // Attach this item to the organization element or hits parent if there is one.
  8803. if (!empty($item->parent) && $item->parent != 0) {
  8804. $children = $organization->childNodes;
  8805. for ($i = 0; $i < $children->length; $i++) {
  8806. $item_temp = $children->item($i);
  8807. if ($item_temp->nodeName == 'item') {
  8808. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8809. $item_temp->appendChild($my_item);
  8810. }
  8811. }
  8812. }
  8813. } else {
  8814. $organization->appendChild($my_item);
  8815. }
  8816. // Include export scripts.
  8817. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8818. // Get the path of the file(s) from the course directory root
  8819. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8820. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8821. // Write the contents of the exported exercise into a (big) html file
  8822. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8823. $contents = export_exercise($exe_id, true);
  8824. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8825. $res = file_put_contents($tmp_file_path, $contents);
  8826. if ($res === false) {
  8827. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8828. }
  8829. $files_cleanup[] = $tmp_file_path;
  8830. //error_log($tmp_path); die();
  8831. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8832. $my_xml_file_path = $my_file_path;
  8833. $my_sub_dir = dirname($my_file_path);
  8834. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8835. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8836. $my_xml_sub_dir = $my_sub_dir;
  8837. // Give a <resource> child to the <resources> element.
  8838. $my_resource = $xmldoc->createElement('resource');
  8839. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8840. $my_resource->setAttribute('type', 'webcontent');
  8841. $my_resource->setAttribute('href', $my_xml_file_path);
  8842. // adlcp:scormtype can be either 'sco' or 'asset'.
  8843. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8844. // xml:base is the base directory to find the files declared in this resource.
  8845. $my_resource->setAttribute('xml:base', '');
  8846. // Give a <file> child to the <resource> element.
  8847. $my_file = $xmldoc->createElement('file');
  8848. $my_file->setAttribute('href', $my_xml_file_path);
  8849. $my_resource->appendChild($my_file);
  8850. // Get included docs.
  8851. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8852. // Dependency to other files - not yet supported.
  8853. $i = 1;
  8854. foreach ($inc_docs as $doc_info) {
  8855. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8856. continue;
  8857. }
  8858. $my_dep = $xmldoc->createElement('resource');
  8859. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8860. $my_dep->setAttribute('identifier', $res_id);
  8861. $my_dep->setAttribute('type', 'webcontent');
  8862. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8863. $my_dep_file = $xmldoc->createElement('file');
  8864. // Check type of URL.
  8865. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8866. if ($doc_info[1] == 'remote') {
  8867. // Remote file. Save url as is.
  8868. $my_dep_file->setAttribute('href', $doc_info[0]);
  8869. $my_dep->setAttribute('xml:base', '');
  8870. } elseif ($doc_info[1] == 'local') {
  8871. switch ($doc_info[2]) {
  8872. case 'url': // Local URL - save path as url for now, don't zip file.
  8873. // Save file but as local file (retrieve from URL).
  8874. $abs_path = api_get_path(SYS_PATH).str_replace(
  8875. api_get_path(WEB_PATH),
  8876. '',
  8877. $doc_info[0]
  8878. );
  8879. $current_dir = dirname($abs_path);
  8880. $current_dir = str_replace('\\', '/', $current_dir);
  8881. $file_path = realpath($abs_path);
  8882. $file_path = str_replace('\\', '/', $file_path);
  8883. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8884. $my_dep->setAttribute('xml:base', '');
  8885. if (strstr($file_path, $main_path) !== false) {
  8886. // The calculated real path is really inside the chamilo root path.
  8887. // Reduce file path to what's under the DocumentRoot.
  8888. $file_path = substr($file_path, strlen($root_path));
  8889. //echo $file_path;echo '<br /><br />';
  8890. //error_log('Reduced path: '.$file_path, 0);
  8891. $zip_files_abs[] = $file_path;
  8892. $link_updates[$my_file_path][] = array(
  8893. 'orig' => $doc_info[0],
  8894. 'dest' => 'document/'.$file_path
  8895. );
  8896. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8897. $my_dep->setAttribute('xml:base', '');
  8898. } elseif (empty($file_path)) {
  8899. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8900. if (strpos($document_root,-1) == '/') {
  8901. $document_root = substr(0, -1, $document_root);
  8902. }*/
  8903. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8904. $file_path = str_replace('//', '/', $file_path);
  8905. if (file_exists($file_path)) {
  8906. $file_path = substr(
  8907. $file_path,
  8908. strlen($current_dir)
  8909. ); // We get the relative path.
  8910. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8911. $link_updates[$my_file_path][] = array(
  8912. 'orig' => $doc_info[0],
  8913. 'dest' => 'document/'.$file_path
  8914. );
  8915. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8916. $my_dep->setAttribute('xml:base', '');
  8917. }
  8918. }
  8919. break;
  8920. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8921. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8922. $current_dir = str_replace('\\', '/', $current_dir);
  8923. $file_path = realpath($doc_info[0]);
  8924. $file_path = str_replace('\\', '/', $file_path);
  8925. $my_dep_file->setAttribute('href', $file_path);
  8926. $my_dep->setAttribute('xml:base', '');
  8927. if (strstr($file_path, $main_path) !== false) {
  8928. // The calculated real path is really inside the chamilo root path.
  8929. // Reduce file path to what's under the DocumentRoot.
  8930. $file_path = substr($file_path, strlen($root_path));
  8931. //echo $file_path;echo '<br /><br />';
  8932. //error_log('Reduced path: '.$file_path, 0);
  8933. $zip_files_abs[] = $file_path;
  8934. $link_updates[$my_file_path][] = array(
  8935. 'orig' => $doc_info[0],
  8936. 'dest' => $file_path
  8937. );
  8938. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8939. $my_dep->setAttribute('xml:base', '');
  8940. } elseif (empty($file_path)) {
  8941. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8942. if (strpos($document_root,-1) == '/') {
  8943. $document_root = substr(0, -1, $document_root);
  8944. }*/
  8945. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8946. $file_path = str_replace('//', '/', $file_path);
  8947. if (file_exists($file_path)) {
  8948. $file_path = substr(
  8949. $file_path,
  8950. strlen($current_dir)
  8951. ); // We get the relative path.
  8952. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8953. $link_updates[$my_file_path][] = array(
  8954. 'orig' => $doc_info[0],
  8955. 'dest' => $file_path
  8956. );
  8957. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8958. $my_dep->setAttribute('xml:base', '');
  8959. }
  8960. }
  8961. break;
  8962. 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
  8963. if (substr($doc_info[0], 0, 2) == '..') {
  8964. // Relative path going up.
  8965. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8966. $current_dir = str_replace('\\', '/', $current_dir);
  8967. $file_path = realpath($current_dir.$doc_info[0]);
  8968. $file_path = str_replace('\\', '/', $file_path);
  8969. //error_log($file_path.' <-> '.$main_path, 0);
  8970. if (strstr($file_path, $main_path) !== false) {
  8971. // The calculated real path is really inside Chamilo's root path.
  8972. // Reduce file path to what's under the DocumentRoot.
  8973. $file_path = substr($file_path, strlen($root_path));
  8974. $file_path_dest = $file_path;
  8975. // File path is courses/CHAMILO/document/....
  8976. $info_file_path = explode('/', $file_path);
  8977. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8978. $file_path_dest = 'document/'.$file_path;
  8979. }
  8980. //error_log('Reduced path: '.$file_path, 0);
  8981. $zip_files_abs[] = $file_path;
  8982. $link_updates[$my_file_path][] = array(
  8983. 'orig' => $doc_info[0],
  8984. 'dest' => $file_path_dest
  8985. );
  8986. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8987. $my_dep->setAttribute('xml:base', '');
  8988. }
  8989. } else {
  8990. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8991. $my_dep_file->setAttribute('href', $doc_info[0]);
  8992. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8993. }
  8994. break;
  8995. default:
  8996. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8997. $my_dep->setAttribute('xml:base', '');
  8998. break;
  8999. }
  9000. }
  9001. $my_dep->appendChild($my_dep_file);
  9002. $resources->appendChild($my_dep);
  9003. $dependency = $xmldoc->createElement('dependency');
  9004. $dependency->setAttribute('identifierref', $res_id);
  9005. $my_resource->appendChild($dependency);
  9006. $i++;
  9007. }
  9008. $resources->appendChild($my_resource);
  9009. $zip_files[] = $my_file_path;
  9010. break;
  9011. default:
  9012. // Get the path of the file(s) from the course directory root
  9013. $my_file_path = 'non_exportable.html';
  9014. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9015. $my_xml_file_path = $my_file_path;
  9016. $my_sub_dir = dirname($my_file_path);
  9017. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9018. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9019. $my_xml_sub_dir = $my_sub_dir;
  9020. // Give a <resource> child to the <resources> element.
  9021. $my_resource = $xmldoc->createElement('resource');
  9022. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9023. $my_resource->setAttribute('type', 'webcontent');
  9024. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9025. // adlcp:scormtype can be either 'sco' or 'asset'.
  9026. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9027. // xml:base is the base directory to find the files declared in this resource.
  9028. $my_resource->setAttribute('xml:base', '');
  9029. // Give a <file> child to the <resource> element.
  9030. $my_file = $xmldoc->createElement('file');
  9031. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9032. $my_resource->appendChild($my_file);
  9033. $resources->appendChild($my_resource);
  9034. break;
  9035. }
  9036. }
  9037. }
  9038. $organizations->appendChild($organization);
  9039. $root->appendChild($organizations);
  9040. $root->appendChild($resources);
  9041. $xmldoc->appendChild($root);
  9042. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9043. // then add the file to the zip, then destroy the file (this is done automatically).
  9044. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9045. //error_log(print_r($zip_files,true), 0);
  9046. foreach ($zip_files as $file_path) {
  9047. if (empty($file_path)) {
  9048. continue;
  9049. }
  9050. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9051. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9052. $this->create_path($dest_file);
  9053. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path,0);
  9054. //echo $main_path.$file_path.'<br />';
  9055. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9056. // Check if the file needs a link update.
  9057. if (in_array($file_path, array_keys($link_updates))) {
  9058. $string = file_get_contents($dest_file);
  9059. unlink($dest_file);
  9060. foreach ($link_updates[$file_path] as $old_new) {
  9061. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9062. // This is an ugly hack that allows .flv files to be found by the flv player that
  9063. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9064. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9065. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9066. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9067. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9068. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9069. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9070. }
  9071. //Fix to avoid problems with default_course_document
  9072. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9073. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9074. } else {
  9075. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9076. $new_dest = $old_new['dest'];
  9077. }
  9078. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9079. $string = str_replace($old_new['orig'], $new_dest, $string);
  9080. //Add files inside the HTMLs
  9081. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9082. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9083. if (file_exists($sys_course_path.$new_path)) {
  9084. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9085. }
  9086. }
  9087. file_put_contents($dest_file, $string);
  9088. }
  9089. }
  9090. foreach ($zip_files_abs as $file_path) {
  9091. if (empty($file_path)) {
  9092. continue;
  9093. }
  9094. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9095. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9096. continue;
  9097. }
  9098. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9099. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9100. $this->create_path($dest_file);
  9101. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9102. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path, 0);
  9103. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9104. copy($main_path.$file_path, $dest_file);
  9105. // Check if the file needs a link update.
  9106. if (in_array($file_path, array_keys($link_updates))) {
  9107. $string = file_get_contents($dest_file);
  9108. unlink($dest_file);
  9109. foreach ($link_updates[$file_path] as $old_new) {
  9110. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9111. // This is an ugly hack that allows .flv files to be found by the flv player that
  9112. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9113. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9114. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9115. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9116. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9117. }
  9118. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9119. }
  9120. file_put_contents($dest_file, $string);
  9121. }
  9122. }
  9123. if (is_array($links_to_create)) {
  9124. foreach ($links_to_create as $file => $link) {
  9125. $file_content = '<!DOCTYPE html>
  9126. <head>
  9127. <meta charset="'.api_get_language_isocode().'" />
  9128. <title>'.$link['title'].'</title>
  9129. </head>
  9130. <body dir="'.api_get_text_direction().'">
  9131. <div style="text-align:center">
  9132. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9133. </body>
  9134. </html>';
  9135. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9136. }
  9137. }
  9138. // Add non exportable message explanation.
  9139. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9140. $file_content = '<!DOCTYPE html>
  9141. <head>
  9142. <meta charset="'.api_get_language_isocode().'" />
  9143. <title>'.$lang_not_exportable.'</title>
  9144. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9145. </head>
  9146. <body dir="'.api_get_text_direction().'">';
  9147. $file_content .=
  9148. <<<EOD
  9149. <style>
  9150. .error-message {
  9151. font-family: arial, verdana, helvetica, sans-serif;
  9152. border-width: 1px;
  9153. border-style: solid;
  9154. left: 50%;
  9155. margin: 10px auto;
  9156. min-height: 30px;
  9157. padding: 5px;
  9158. right: 50%;
  9159. width: 500px;
  9160. background-color: #FFD1D1;
  9161. border-color: #FF0000;
  9162. color: #000;
  9163. }
  9164. </style>
  9165. <body>
  9166. <div class="error-message">
  9167. $lang_not_exportable
  9168. </div>
  9169. </body>
  9170. </html>
  9171. EOD;
  9172. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9173. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9174. }
  9175. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9176. // Add the extra files that go along with a SCORM package.
  9177. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9178. $extra_files = scandir($main_code_path);
  9179. foreach ($extra_files as $extra_file) {
  9180. if (strpos($extra_file, '.') === 0) {
  9181. continue;
  9182. } else {
  9183. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9184. $this->create_path($dest_file);
  9185. copy($main_code_path.$extra_file, $dest_file);
  9186. }
  9187. }
  9188. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9189. $manifest = @$xmldoc->saveXML();
  9190. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9191. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9192. $zip_folder->add(
  9193. $archive_path.'/'.$temp_dir_short,
  9194. PCLZIP_OPT_REMOVE_PATH,
  9195. $archive_path.'/'.$temp_dir_short.'/'
  9196. );
  9197. // Clean possible temporary files.
  9198. foreach ($files_cleanup as $file) {
  9199. $res = unlink($file);
  9200. if ($res === false) {
  9201. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9202. }
  9203. }
  9204. // Send file to client
  9205. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9206. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9207. }
  9208. public function scorm_export_to_pdf($lp_id)
  9209. {
  9210. $lp_id = intval($lp_id);
  9211. $files_to_export = array();
  9212. $course_data = api_get_course_info($this->cc);
  9213. if (!empty($course_data)) {
  9214. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9215. $list = self::get_flat_ordered_items_list($lp_id);
  9216. if (!empty($list)) {
  9217. foreach ($list as $item_id) {
  9218. $item = $this->items[$item_id];
  9219. switch ($item->type) {
  9220. case 'document':
  9221. //Getting documents from a LP with chamilo documents
  9222. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9223. $file_path = api_get_path(
  9224. SYS_COURSE_PATH
  9225. ).$course_data['path'].'/document'.$file_data['path'];
  9226. if (file_exists($file_path)) {
  9227. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9228. }
  9229. break;
  9230. case 'asset': //commes from a scorm package generated by chamilo
  9231. case 'sco':
  9232. $file_path = $scorm_path.'/'.$item->path;
  9233. if (file_exists($file_path)) {
  9234. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9235. }
  9236. break;
  9237. case 'dokeos_chapter':
  9238. case 'dir':
  9239. case 'chapter':
  9240. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9241. break;
  9242. }
  9243. }
  9244. }
  9245. $pdf = new PDF();
  9246. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9247. return $result;
  9248. }
  9249. return false;
  9250. }
  9251. /**
  9252. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9253. * if it doesn't exist
  9254. */
  9255. public function create_path($path)
  9256. {
  9257. $path_bits = explode('/', dirname($path));
  9258. // IS_WINDOWS_OS has been defined in api.lib.php
  9259. $path_built = IS_WINDOWS_OS ? '' : '/';
  9260. foreach ($path_bits as $bit) {
  9261. if (!empty ($bit)) {
  9262. $new_path = $path_built.$bit;
  9263. if (is_dir($new_path)) {
  9264. $path_built = $new_path.'/';
  9265. } else {
  9266. mkdir($new_path, api_get_permissions_for_new_directories());
  9267. $path_built = $new_path.'/';
  9268. }
  9269. }
  9270. }
  9271. }
  9272. /**
  9273. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9274. * @return boolean The results of the unlink function, or false if there was no image to start with
  9275. */
  9276. public function delete_lp_image()
  9277. {
  9278. $img = $this->get_preview_image();
  9279. if ($img != '') {
  9280. $del_file = $this->get_preview_image_path(null, 'sys');
  9281. if (isset($del_file) && file_exists($del_file)) {
  9282. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9283. if (file_exists($del_file_2)) {
  9284. unlink($del_file_2);
  9285. }
  9286. $this->set_preview_image('');
  9287. return @unlink($del_file);
  9288. }
  9289. }
  9290. return false;
  9291. }
  9292. /**
  9293. * Uploads an author image to the upload/learning_path/images directory
  9294. * @param array The image array, coming from the $_FILES superglobal
  9295. * @return boolean True on success, false on error
  9296. */
  9297. public function upload_image($image_array)
  9298. {
  9299. $image_moved = false;
  9300. if (!empty ($image_array['name'])) {
  9301. $upload_ok = FileManager::process_uploaded_file($image_array);
  9302. $has_attachment = true;
  9303. } else {
  9304. $image_moved = true;
  9305. }
  9306. if ($upload_ok) {
  9307. if ($has_attachment) {
  9308. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9309. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9310. $updir = $sys_course_path.$courseDir;
  9311. // Try to add an extension to the file if it hasn't one.
  9312. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9313. if (!FileManager::filter_extension($new_file_name)) {
  9314. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9315. $image_moved = false;
  9316. } else {
  9317. $file_extension = explode('.', $image_array['name']);
  9318. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9319. $filename = uniqid('');
  9320. $new_file_name = $filename.'.'.$file_extension;
  9321. $new_path = $updir.'/'.$new_file_name;
  9322. // Resize the image.
  9323. $temp = new Image($image_array['tmp_name']);
  9324. $picture_infos = $temp->get_image_info();
  9325. if ($picture_infos['width'] > 104) {
  9326. $thumbwidth = 104;
  9327. } else {
  9328. $thumbwidth = $picture_infos['width'];
  9329. }
  9330. if ($picture_infos['height'] > 96) {
  9331. $new_height = 96;
  9332. } else {
  9333. $new_height = $picture_infos['height'];
  9334. }
  9335. $temp->resize($thumbwidth, $new_height, 0);
  9336. $result = $temp->send_image($new_path);
  9337. // Storing the image filename.
  9338. if ($result) {
  9339. $image_moved = true;
  9340. $this->set_preview_image($new_file_name);
  9341. //Resize to 32
  9342. $temp->resize(64, 64, 0);
  9343. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9344. return true;
  9345. }
  9346. }
  9347. }
  9348. }
  9349. return false;
  9350. }
  9351. public function set_autolunch($lp_id, $status)
  9352. {
  9353. $course_id = api_get_course_int_id();
  9354. $lp_id = intval($lp_id);
  9355. $status = intval($status);
  9356. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9357. //Setting everything to autolunch = 0
  9358. $attributes['autolunch'] = 0;
  9359. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9360. Database::update($lp_table, $attributes, $where);
  9361. if ($status == 1) {
  9362. //Setting my lp_id to autolunch = 1
  9363. $attributes['autolunch'] = 1;
  9364. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9365. Database::update($lp_table, $attributes, $where);
  9366. }
  9367. }
  9368. /**
  9369. * Gets previous_item_id for the next element of the lp_item table
  9370. * @author Isaac flores paz
  9371. * @return integer Previous item ID
  9372. */
  9373. function select_previous_item_id()
  9374. {
  9375. $course_id = api_get_course_int_id();
  9376. if ($this->debug > 0) {
  9377. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9378. }
  9379. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9380. // Get the max order of the items
  9381. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'";
  9382. $rs_max_order = Database::query($sql_max_order);
  9383. $row_max_order = Database::fetch_object($rs_max_order);
  9384. $max_order = $row_max_order->display_order;
  9385. // Get the previous item ID
  9386. $sql_max = "SELECT id as previous FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' AND display_order = '".$max_order."' ";
  9387. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9388. $row_max = Database::fetch_object($rs_max);
  9389. // Return the previous item ID
  9390. return $row_max->previous;
  9391. }
  9392. function copy()
  9393. {
  9394. $main_path = api_get_path(SYS_CODE_PATH);
  9395. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9396. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9397. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9398. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9399. //Course builder
  9400. $cb = new CourseBuilder();
  9401. //Setting tools that will be copied
  9402. $cb->set_tools_to_build(array('learnpaths'));
  9403. //Setting elements that will be copied
  9404. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9405. $course = $cb->build();
  9406. //Course restorer
  9407. $course_restorer = new CourseRestorer($course);
  9408. $course_restorer->set_add_text_in_items(true);
  9409. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9410. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9411. }
  9412. function verify_document_size($s)
  9413. {
  9414. $post_max = ini_get('post_max_size');
  9415. $upl_max = ini_get('upload_max_filesize');
  9416. $documents_total_space = DocumentManager::documents_total_space();
  9417. $course_max_space = DocumentManager::get_course_quota();
  9418. $total_size = filesize($s) + $documents_total_space;
  9419. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9420. return true;
  9421. } else {
  9422. return false;
  9423. }
  9424. }
  9425. function clear_prerequisites()
  9426. {
  9427. $course_id = $this->get_course_int_id();
  9428. if ($this->debug > 0) {
  9429. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9430. }
  9431. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9432. $lp_id = $this->get_id();
  9433. //Cleaning prerequisites
  9434. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9435. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9436. Database::query($sql);
  9437. //Cleaning mastery score for exercises
  9438. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9439. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9440. Database::query($sql);
  9441. }
  9442. function set_previous_step_as_prerequisite_for_all_items()
  9443. {
  9444. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9445. $course_id = $this->get_course_int_id();
  9446. $lp_id = $this->get_id();
  9447. if (!empty($this->items)) {
  9448. $old_id = null;
  9449. $old_max = 0;
  9450. $old_type = null;
  9451. foreach ($this->items as $item) {
  9452. if (!empty($old_id)) {
  9453. $current_item_id = $item->get_id();
  9454. if ($old_type == 'quiz') {
  9455. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$old_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$old_id'";
  9456. Database::query($sql);
  9457. }
  9458. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$old_id' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9459. Database::query($sql);
  9460. }
  9461. $old_id = $item->get_id();
  9462. $old_max = $item->get_max();
  9463. $old_type = $item->get_type();
  9464. }
  9465. }
  9466. }
  9467. static function create_category($params)
  9468. {
  9469. global $app;
  9470. $em = $app['orm.ems']['db_write'];
  9471. $item = new Entity\CLpCategory();
  9472. $item->setName($params['name']);
  9473. $item->setCId($params['c_id']);
  9474. $em->persist($item);
  9475. $em->flush();
  9476. }
  9477. static function update_category($params)
  9478. {
  9479. global $app;
  9480. $em = $app['orm.ems']['db_write'];
  9481. $item = $em->find('Entity\CLpCategory', $params['id']);
  9482. if ($item) {
  9483. $item->setName($params['name']);
  9484. $item->setCId($params['c_id']);
  9485. $em->persist($item);
  9486. $em->flush();
  9487. }
  9488. }
  9489. static function move_up_category($id)
  9490. {
  9491. global $app;
  9492. $em = $app['orm.ems']['db_write'];
  9493. $item = $em->find('Entity\CLpCategory', $id);
  9494. if ($item) {
  9495. $position = $item->getPosition() - 1;
  9496. $item->setPosition($position);
  9497. $em->persist($item);
  9498. $em->flush();
  9499. }
  9500. }
  9501. static function move_down_category($id)
  9502. {
  9503. global $app;
  9504. $em = $app['orm.ems']['db_write'];
  9505. $item = $em->find('Entity\CLpCategory', $id);
  9506. if ($item) {
  9507. $position = $item->getPosition() + 1;
  9508. $item->setPosition($position);
  9509. $em->persist($item);
  9510. $em->flush();
  9511. }
  9512. }
  9513. static function get_count_categories($course_id)
  9514. {
  9515. global $app;
  9516. if (empty($course_id)) {
  9517. return 0;
  9518. }
  9519. $em = $app['orm.em'];
  9520. $query = $em->createQuery('SELECT COUNT(u.id) FROM Entity\CLpCategory u WHERE u.cId = :id');
  9521. $query->setParameter('id', $course_id);
  9522. return $query->getSingleScalarResult();
  9523. }
  9524. static function get_categories($course_id)
  9525. {
  9526. global $app;
  9527. $em = $app['orm.em'];
  9528. //Default behaviour
  9529. /*$items = $em->getRepository('Entity\CLpCategory')->findBy(
  9530. array('cId' => $course_id),
  9531. array('name' => 'ASC')
  9532. );*/
  9533. //Using doctrine extensions
  9534. $items = $em->getRepository('Entity\CLpCategory')->getBySortableGroupsQuery(
  9535. array('cId' => $course_id)
  9536. )->getResult();
  9537. return $items;
  9538. }
  9539. static function get_category($id)
  9540. {
  9541. global $app;
  9542. $em = $app['orm.em'];
  9543. $item = $em->find('Entity\CLpCategory', $id);
  9544. return $item;
  9545. }
  9546. static function get_category_by_course($course_id)
  9547. {
  9548. global $app;
  9549. $items = $app['orm.em']->getRepository('Entity\CLpCategory')->findBy(array('cId' => $course_id));
  9550. return $items;
  9551. }
  9552. static function delete_category($id)
  9553. {
  9554. global $app;
  9555. $em = $app['orm.ems']['db_write'];
  9556. $item = $em->find('Entity\CLpCategory', $id);
  9557. if ($item) {
  9558. $courseId = $item->getCId();
  9559. $query = $em->createQuery('SELECT u FROM Entity\CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9560. $query->setParameter('id', $courseId);
  9561. $query->setParameter('catId', $item->getId());
  9562. $lps = $query->getResult();
  9563. // Setting category = 0.
  9564. if ($lps) {
  9565. foreach ($lps as $lpItem) {
  9566. $lpItem->setCategoryId(0);
  9567. }
  9568. }
  9569. // Removing category.
  9570. $em->remove($item);
  9571. $em->flush();
  9572. }
  9573. }
  9574. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9575. {
  9576. $items = self::get_category_by_course($course_id);
  9577. $cats = array();
  9578. if ($addSelectOption) {
  9579. $cats = array(get_lang('SelectACategory'));
  9580. }
  9581. if (!empty($items)) {
  9582. foreach ($items as $cat) {
  9583. $cats[$cat->getId()] = $cat->getName();
  9584. }
  9585. }
  9586. return $cats;
  9587. }
  9588. }
  9589. if (!function_exists('trim_value')) {
  9590. function trim_value(& $value)
  9591. {
  9592. $value = trim($value);
  9593. }
  9594. }