learnpath.class.php 434 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300
  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. */
  11. /**
  12. * Defines the learnpath parent class
  13. * @package chamilo.learnpath
  14. */
  15. class learnpath {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $created_on = '';
  66. public $modified_on = '';
  67. public $publicated_on = '';
  68. public $expired_on = '';
  69. public $ref = null;
  70. /**
  71. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  72. * Also builds the list of items into $this->items.
  73. * @param string Course code
  74. * @param integer Learnpath ID
  75. * @param integer User ID
  76. * @return boolean True on success, false on error
  77. */
  78. public function __construct($course, $lp_id, $user_id) {
  79. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  80. // Check params.
  81. // Check course code.
  82. $course_id = api_get_course_int_id();
  83. if ($this->debug > 0) { error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0); }
  84. if (empty($course)) {
  85. $this->error = 'Course code is empty';
  86. return false;
  87. } else {
  88. $main_table = Database::get_main_table(TABLE_MAIN_COURSE);
  89. $course = Database::escape_string($course);
  90. $sql = "SELECT * FROM $main_table WHERE code = '$course'";
  91. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying course: '.$sql, 0); }
  92. $res = Database::query($sql);
  93. if (Database::num_rows($res) > 0) {
  94. $this->cc = $course;
  95. $row_course = Database::fetch_array($res);
  96. $course_id = $row_course['id'];
  97. } else {
  98. $this->error = 'Course code does not exist in database ('.$sql.')';
  99. return false;
  100. }
  101. }
  102. // Check learnpath ID.
  103. if (empty($lp_id)) {
  104. $this->error = 'Learnpath ID is empty';
  105. return false;
  106. } else {
  107. // TODO: Make it flexible to use any course_code (still using env course code here).
  108. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  109. $lp_id = intval($lp_id);
  110. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  111. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0); }
  112. $res = Database::query($sql);
  113. if (Database::num_rows($res) > 0) {
  114. $this->lp_id = $lp_id;
  115. $row = Database::fetch_array($res);
  116. $this->type = $row['lp_type'];
  117. $this->name = stripslashes($row['name']);
  118. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  119. $this->proximity = $row['content_local'];
  120. $this->theme = $row['theme'];
  121. $this->maker = $row['content_maker'];
  122. $this->prevent_reinit = $row['prevent_reinit'];
  123. $this->seriousgame_mode = $row['seriousgame_mode'];
  124. $this->license = $row['content_license'];
  125. $this->scorm_debug = $row['debug'];
  126. $this->js_lib = $row['js_lib'];
  127. $this->path = $row['path'];
  128. $this->preview_image = $row['preview_image'];
  129. $this->author = $row['author'];
  130. $this->hide_toc_frame = $row['hide_toc_frame'];
  131. $this->lp_session_id = $row['session_id'];
  132. $this->use_max_score = $row['use_max_score'];
  133. $this->created_on = $row['created_on'];
  134. $this->modified_on = $row['modified_on'];
  135. $this->ref = $row['ref'];
  136. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  137. $this->publicated_on = $row['publicated_on'];
  138. }
  139. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  140. $this->expired_on = $row['expired_on'];
  141. }
  142. if ($this->type == 2) {
  143. if ($row['force_commit'] == 1) {
  144. $this->force_commit = true;
  145. }
  146. }
  147. $this->mode = $row['default_view_mod'];
  148. } else {
  149. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  150. return false;
  151. }
  152. }
  153. // Check user ID.
  154. if (empty($user_id)) {
  155. $this->error = 'User ID is empty';
  156. return false;
  157. } else {
  158. $user_info = api_get_user_info($user_id);
  159. if (!empty($user_info)) {
  160. $this->user_id = $user_info['user_id'];
  161. } else {
  162. $this->error = 'User ID does not exist in database ('.$sql.')';
  163. return false;
  164. }
  165. }
  166. // End of variables checking.
  167. $session_id = api_get_session_id();
  168. // Get the session condition for learning paths of the base + session.
  169. $session = api_get_session_condition($session_id);
  170. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  171. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  172. // Selecting by view_count descending allows to get the highest view_count first.
  173. $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";
  174. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0); }
  175. $res = Database::query($sql);
  176. $view_id = 0; // Used later to query lp_item_view.
  177. if (Database :: num_rows($res) > 0) {
  178. if ($this->debug > 2) {
  179. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  180. }
  181. $row = Database :: fetch_array($res);
  182. $this->attempt = $row['view_count'];
  183. $this->lp_view_id = $row['id'];
  184. $this->last_item_seen = $row['last_item'];
  185. $this->progress_db = $row['progress'];
  186. $this->lp_view_session_id = $row['session_id'];
  187. } else {
  188. if ($this->debug > 2) {
  189. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  190. }
  191. $this->attempt = 1;
  192. $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)";
  193. $res_ins = Database::query($sql_ins);
  194. $this->lp_view_id = Database :: insert_id();
  195. if ($this->debug > 2) {
  196. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
  197. }
  198. }
  199. // Initialise items.
  200. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  201. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  202. $res = Database::query($sql);
  203. if ($this->debug > 2) {
  204. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  205. error_log('-- Start while--', 0);
  206. }
  207. $lp_item_id_list = array();
  208. while ($row = Database::fetch_array($res)) {
  209. $oItem = '';
  210. $lp_item_id_list[] = $row['id'];
  211. switch ($this->type) {
  212. case 3: //aicc
  213. $oItem = new aiccItem('db', $row['id'], $course_id);
  214. if (is_object($oItem)) {
  215. $my_item_id = $oItem->get_id();
  216. $oItem->set_lp_view($this->lp_view_id, $course_id);
  217. $oItem->set_prevent_reinit($this->prevent_reinit);
  218. // Don't use reference here as the next loop will make the pointed object change.
  219. $this->items[$my_item_id] = $oItem;
  220. $this->refs_list[$oItem->ref] = $my_item_id;
  221. if ($this->debug > 2) {
  222. error_log('New LP - learnpath::__construct() - aicc object with id ' . $my_item_id . ' set in items[]', 0);
  223. }
  224. }
  225. break;
  226. case 2:
  227. require_once 'scorm.class.php';
  228. require_once 'scormItem.class.php';
  229. $oItem = new scormItem('db', $row['id'], $course_id);
  230. if (is_object($oItem)) {
  231. $my_item_id = $oItem->get_id();
  232. $oItem->set_lp_view($this->lp_view_id, $course_id);
  233. $oItem->set_prevent_reinit($this->prevent_reinit);
  234. // Don't use reference here as the next loop will make the pointed object change.
  235. $this->items[$my_item_id] = $oItem;
  236. $this->refs_list[$oItem->ref] = $my_item_id;
  237. if ($this->debug > 2) {
  238. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  239. }
  240. }
  241. break;
  242. case 1:
  243. default:
  244. if ($this->debug > 2) {
  245. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  246. }
  247. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  248. if ($this->debug > 2) {
  249. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  250. }
  251. if (is_object($oItem)) {
  252. $my_item_id = $oItem->get_id();
  253. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  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[$my_item_id] = $my_item_id;
  258. if ($this->debug > 2) {
  259. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - object with id ' . $my_item_id . ' set in items[]', 0);
  260. }
  261. }
  262. break;
  263. }
  264. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  265. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  266. if (is_object($this->items[$row['id']])) {
  267. $this->items[$row['id']]->set_level(0);
  268. }
  269. } else {
  270. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  271. $this->items[$row['id']]->set_level($level);
  272. if (is_object($this->items[$row['parent_item_id']])) {
  273. // Items is a list of pointers from item DB ids to item objects.
  274. $this->items[$row['parent_item_id']]->add_child($row['id']);
  275. } else {
  276. if ($this->debug > 2) {
  277. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - The parent item (' . $row['parent_item_id'] . ') of item ' . $row['id'] . ' could not be found', 0);
  278. }
  279. }
  280. }
  281. // Setting the view in the item object.
  282. if (is_object($this->items[$row['id']])) {
  283. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  284. }
  285. }
  286. if ($this->debug > 2) {
  287. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  288. }
  289. if (!empty($lp_item_id_list)) {
  290. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  291. // Get last viewing vars.
  292. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  293. // This query should only return one or zero result.
  294. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  295. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  296. ORDER BY view_count DESC ";
  297. if ($this->debug > 2) {
  298. error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
  299. }
  300. $status_list = array();
  301. $res = Database::query($sql);
  302. while ($row = Database :: fetch_array($res) ) {
  303. $status_list[$row['lp_item_id']] = $row['status'];
  304. }
  305. foreach ($lp_item_id_list as $item_id) {
  306. if (isset($status_list[$item_id])) {
  307. $status = $status_list[$item_id];
  308. if (is_object($this->items[$item_id])) {
  309. $this->items[$item_id]->set_status($status);
  310. if (empty ($status)) {
  311. $this->items[$item_id]->set_status($this->default_status);
  312. }
  313. }
  314. } else {
  315. if (is_object($this->items[$item_id])) {
  316. $this->items[$item_id]->set_status($this->default_status);
  317. }
  318. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  319. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  320. VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ",1,'not attempted')";
  321. if ($this->debug > 2) {
  322. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql_ins, 0);
  323. }
  324. $res_ins = Database::query($sql_ins);
  325. }
  326. }
  327. }
  328. $this->ordered_items = $this->get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  329. $this->max_ordered_items = 0;
  330. foreach ($this->ordered_items as $index => $dummy) {
  331. if ($index > $this->max_ordered_items && !empty($dummy)) {
  332. $this->max_ordered_items = $index;
  333. }
  334. }
  335. // TODO: Define the current item better.
  336. $this->first();
  337. if ($this->debug > 2) {
  338. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  339. }
  340. }
  341. /**
  342. * 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
  343. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  344. *
  345. * @param int $parent
  346. * @param int $previous
  347. * @param string $type
  348. * @param int resource ID (ref)
  349. * @param string $title
  350. * @param string $description
  351. * @return int
  352. */
  353. public function add_item($parent, $previous, $type = 'dokeos_chapter', $id, $title, $description, $prerequisites = 0, $max_time_allowed = 0) {
  354. $course_id = api_get_course_int_id();
  355. if ($this->debug > 0) {
  356. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  357. }
  358. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  359. $parent = intval($parent);
  360. $previous = intval($previous);
  361. $type = Database::escape_string($type);
  362. $id = intval($id);
  363. $max_time_allowed = Database::escape_string(htmlentities($max_time_allowed));
  364. if (empty ($max_time_allowed)) {
  365. $max_time_allowed = 0;
  366. }
  367. $title = Database::escape_string($title);
  368. $description = Database::escape_string($description);
  369. $sql_count = " SELECT COUNT(id) AS num
  370. FROM $tbl_lp_item
  371. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND parent_item_id = " . $parent;
  372. $res_count = Database::query($sql_count);
  373. $row = Database :: fetch_array($res_count);
  374. $num = $row['num'];
  375. if ($num > 0) {
  376. if ($previous == 0) {
  377. $sql = "SELECT id, next_item_id, display_order
  378. FROM " . $tbl_lp_item . "
  379. WHERE c_id = $course_id AND
  380. lp_id = " . $this->get_id() . " AND
  381. parent_item_id = " . $parent . " AND
  382. previous_item_id = 0 OR previous_item_id=" . $parent;
  383. $result = Database::query($sql);
  384. $row = Database :: fetch_array($result);
  385. $tmp_previous = 0;
  386. $next = $row['id'];
  387. $display_order = 0;
  388. } else {
  389. $previous = (int) $previous;
  390. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  391. FROM $tbl_lp_item
  392. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND id = " . $previous;
  393. $result = Database::query($sql);
  394. $row = Database :: fetch_array($result);
  395. $tmp_previous = $row['id'];
  396. $next = $row['next_item_id'];
  397. $display_order = $row['display_order'];
  398. }
  399. } else {
  400. $tmp_previous = 0;
  401. $next = 0;
  402. $display_order = 0;
  403. }
  404. $new_item_id = -1;
  405. $id = Database::escape_string($id);
  406. if ($type == 'quiz') {
  407. $sql = 'SELECT SUM(ponderation)
  408. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  409. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  410. ON quiz_question.id = quiz_rel_question.question_id
  411. WHERE quiz_rel_question.exercice_id = '.$id." AND
  412. quiz_question.c_id = $course_id AND
  413. quiz_rel_question.c_id = $course_id ";
  414. $rsQuiz = Database::query($sql);
  415. $max_score = Database :: result($rsQuiz, 0, 0);
  416. //Disabling the exercise if we add it inside a LP
  417. $exercise = new Exercise();
  418. $exercise->read($id);
  419. $exercise->disable();
  420. $exercise->save();
  421. } else {
  422. $max_score = 100;
  423. }
  424. if ($prerequisites != 0) {
  425. $sql_ins = "INSERT INTO " . $tbl_lp_item . " (
  426. c_id,
  427. lp_id, ".
  428. "item_type, ".
  429. "ref, ".
  430. "title, ".
  431. "description, ".
  432. "path, ".
  433. "max_score, ".
  434. "parent_item_id, ".
  435. "previous_item_id, ".
  436. "next_item_id, ".
  437. "display_order, ".
  438. "prerequisite, ".
  439. "max_time_allowed ".
  440. ") VALUES (
  441. $course_id ,
  442. ".$this->get_id() . ", ".
  443. "'" . $type . "', ".
  444. "'', ".
  445. "'" . $title . "', ".
  446. "'" . $description . "', ".
  447. "'" . $id . "', ".
  448. "'" . $max_score . "', ".
  449. $parent . ", ".
  450. $previous . ", ".
  451. $next . ", ".
  452. ($display_order +1) . ", ".
  453. $prerequisites . ", ".
  454. $max_time_allowed .
  455. ")";
  456. } else {
  457. // Insert new item.
  458. $sql_ins = "
  459. INSERT INTO " . $tbl_lp_item . " ( ".
  460. "c_id, ".
  461. "lp_id, ".
  462. "item_type, ".
  463. "ref, ".
  464. "title, ".
  465. "description, ".
  466. "path, ".
  467. "max_score, ".
  468. "parent_item_id, ".
  469. "previous_item_id, ".
  470. "next_item_id, ".
  471. "display_order, ".
  472. "max_time_allowed ".
  473. ") VALUES (".
  474. $course_id. ",".
  475. $this->get_id() . ",".
  476. "'" . $type . "',".
  477. "'',".
  478. "'" . $title . "',".
  479. "'" . $description . "',".
  480. "'" . $id . "',".
  481. "'" . $max_score . "',".
  482. $parent . ",".
  483. $previous . ",".
  484. $next . ",".
  485. ($display_order +1) . ",".
  486. $max_time_allowed .
  487. ")";
  488. }
  489. if ($this->debug > 2) {
  490. error_log('New LP - Inserting dokeos_chapter: ' . $sql_ins, 0);
  491. }
  492. $res_ins = Database::query($sql_ins);
  493. if ($res_ins > 0) {
  494. $new_item_id = Database :: insert_id($res_ins);
  495. // Update the item that should come after the new item.
  496. $sql_update_next = "
  497. UPDATE " . $tbl_lp_item . "
  498. SET previous_item_id = " . $new_item_id . "
  499. WHERE c_id = $course_id AND id = " . $next;
  500. Database::query($sql_update_next);
  501. // Update the item that should be before the new item.
  502. $sql_update_previous = "
  503. UPDATE " . $tbl_lp_item . "
  504. SET next_item_id = " . $new_item_id . "
  505. WHERE c_id = $course_id AND id = " . $tmp_previous;
  506. Database::query($sql_update_previous);
  507. // Update all the items after the new item.
  508. $sql_update_order = "
  509. UPDATE " . $tbl_lp_item . "
  510. SET display_order = display_order + 1
  511. WHERE
  512. c_id = $course_id AND
  513. lp_id = " . $this->get_id() . " AND
  514. id <> " . $new_item_id . " AND
  515. parent_item_id = " . $parent . " AND
  516. display_order > " . $display_order;
  517. Database::query($sql_update_order);
  518. // Update the item that should come after the new item.
  519. $sql_update_ref = "UPDATE " . $tbl_lp_item . "
  520. SET ref = " . $new_item_id . "
  521. WHERE c_id = $course_id AND id = " . $new_item_id;
  522. Database::query($sql_update_ref);
  523. }
  524. // Upload audio.
  525. if (!empty ($_FILES['mp3']['name'])) {
  526. // Create the audio folder if it does not exist yet.
  527. global $_course;
  528. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  529. if (!is_dir($filepath . 'audio')) {
  530. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  531. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  532. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  533. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  534. }
  535. // Upload the file in the documents tool.
  536. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  537. $file_path = handle_uploaded_document($_course, $_FILES['mp3'], api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document', '/audio', api_get_user_id(), '', '', '', '', false);
  538. // Getting the filename only.
  539. $file_components = explode('/', $file_path);
  540. $file = $file_components[count($file_components) - 1];
  541. // Store the mp3 file in the lp_item table.
  542. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '" . Database::escape_string($file) . "' WHERE id = '" . Database::escape_string($new_item_id) . "'";
  543. Database::query($sql_insert_audio);
  544. }
  545. return $new_item_id;
  546. }
  547. /**
  548. * Static admin function allowing addition of a learnpath to a course.
  549. * @param string Course code
  550. * @param string Learnpath name
  551. * @param string Learnpath description string, if provided
  552. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  553. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  554. * @param string Zip file containing the learnpath or directory containing the learnpath
  555. * @return integer The new learnpath ID on success, 0 on failure
  556. */
  557. public static function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '', $publicated_on = '', $expired_on = '') {
  558. global $charset;
  559. $course_id = api_get_course_int_id();
  560. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  561. // Check course code exists.
  562. // Check lp_name doesn't exist, otherwise append something.
  563. $i = 0;
  564. $name = Database::escape_string($name);
  565. // Session id.
  566. $session_id = api_get_session_id();
  567. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  568. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  569. $res_name = Database::query($check_name);
  570. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  571. //by default the publication date is the same that the creation date
  572. //The behaviour above was changed due BT#2800
  573. global $_custom;
  574. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  575. $publicated_on = '';
  576. } else {
  577. $publicated_on = api_get_utc_datetime();
  578. }
  579. } else {
  580. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  581. }
  582. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  583. $expired_on = '';
  584. } else {
  585. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  586. }
  587. while (Database :: num_rows($res_name)) {
  588. // There is already one such name, update the current one a bit.
  589. $i++;
  590. $name = $name . ' - ' . $i;
  591. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  592. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  593. $res_name = Database::query($check_name);
  594. }
  595. // New name does not exist yet; keep it.
  596. // Escape description.
  597. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset)); // Kevin: added htmlentities().
  598. $type = 1;
  599. switch ($learnpath) {
  600. case 'guess':
  601. break;
  602. case 'dokeos':
  603. case 'chamilo':
  604. $type = 1;
  605. break;
  606. case 'aicc':
  607. break;
  608. }
  609. switch ($origin) {
  610. case 'zip':
  611. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  612. break;
  613. case 'manual':
  614. default:
  615. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  616. $res_max = Database::query($get_max);
  617. if (Database :: num_rows($res_max) < 1) {
  618. $dsp = 1;
  619. } else {
  620. $row = Database :: fetch_array($res_max);
  621. $dsp = $row[0] + 1;
  622. }
  623. $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) " .
  624. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
  625. Database::query($sql_insert);
  626. $id = Database :: insert_id();
  627. if ($id > 0) {
  628. $course_info = api_get_course_info();
  629. // Insert into item_property.
  630. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  631. api_set_default_visibility($id, TOOL_LEARNPATH);
  632. return $id;
  633. }
  634. break;
  635. }
  636. }
  637. /**
  638. * Appends a message to the message attribute
  639. * @param string Message to append.
  640. */
  641. public function append_message($string) {
  642. if ($this->debug > 0) {
  643. error_log('New LP - In learnpath::append_message()', 0);
  644. }
  645. $this->message .= $string;
  646. }
  647. /**
  648. * Autocompletes the parents of an item in case it's been completed or passed
  649. * @param integer Optional ID of the item from which to look for parents
  650. */
  651. public function autocomplete_parents($item) {
  652. $course_id = api_get_course_int_id();
  653. if ($this->debug > 0) {
  654. error_log('New LP - In learnpath::autocomplete_parents()', 0);
  655. }
  656. if (empty ($item)) {
  657. $item = $this->current;
  658. }
  659. $parent_id = $this->items[$item]->get_parent();
  660. if ($this->debug > 2) {
  661. error_log('New LP - autocompleting parent of item ' . $item . ' (item ' . $parent_id . ')', 0);
  662. }
  663. if (is_object($this->items[$item]) and !empty ($parent_id)) {
  664. // if $item points to an object and there is a parent.
  665. if ($this->debug > 2) {
  666. error_log('New LP - ' . $item . ' is an item, proceed', 0);
  667. }
  668. $current_item = & $this->items[$item];
  669. $parent = & $this->items[$parent_id]; // Get the parent.
  670. // New experiment including failed and browsed in completed status.
  671. $current_status = $current_item->get_status();
  672. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  673. // If the current item is completed or passes or succeeded.
  674. $completed = true;
  675. if ($this->debug > 2) {
  676. error_log('New LP - Status of current item is alright', 0);
  677. }
  678. foreach ($parent->get_children() as $child) {
  679. // Check all his brothers (his parent's children) for completion status.
  680. if ($child != $item) {
  681. if ($this->debug > 2) {
  682. error_log('New LP - Looking at brother with ID ' . $child . ', status is ' . $this->items[$child]->get_status(), 0);
  683. }
  684. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  685. // Trying completing parents of failed and browsed items as well.
  686. if ($this->items[$child]->status_is(array (
  687. 'completed',
  688. 'passed',
  689. 'succeeded',
  690. 'browsed',
  691. 'failed'
  692. ))) {
  693. // Keep completion status to true.
  694. } else {
  695. if ($this->debug > 2) {
  696. error_log('New LP - Found one incomplete child of ' . $parent_id . ': ' . $child . ' is ' . $this->items[$child]->get_status(), 0);
  697. }
  698. $completed = false;
  699. }
  700. }
  701. }
  702. if ($completed) { // If all the children were completed:
  703. $parent->set_status('completed');
  704. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  705. $this->update_queue[$parent->get_id()] = $parent->get_status();
  706. if ($this->debug > 2) {
  707. error_log('New LP - Added parent to update queue ' . print_r($this->update_queue, true), 0);
  708. }
  709. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  710. }
  711. } else {
  712. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  713. }
  714. }
  715. }
  716. /**
  717. * Autosaves the current results into the database for the whole learnpath
  718. */
  719. public function autosave() {
  720. if ($this->debug > 0) {
  721. error_log('New LP - In learnpath::autosave()', 0);
  722. }
  723. // TODO: Add save operations for the learnpath itself.
  724. }
  725. /**
  726. * Clears the message attribute
  727. */
  728. public function clear_message() {
  729. if ($this->debug > 0) {
  730. error_log('New LP - In learnpath::clear_message()', 0);
  731. }
  732. $this->message = '';
  733. }
  734. /**
  735. * Closes the current resource
  736. *
  737. * Stops the timer
  738. * Saves into the database if required
  739. * Clears the current resource data from this object
  740. * @return boolean True on success, false on failure
  741. */
  742. public function close() {
  743. $course_id = api_get_course_int_id();
  744. if ($this->debug > 0) {
  745. error_log('New LP - In learnpath::close()', 0);
  746. }
  747. if (empty ($this->lp_id)) {
  748. $this->error = 'Trying to close this learnpath but no ID is set';
  749. return false;
  750. }
  751. $this->current_time_stop = time();
  752. if ($this->save) {
  753. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  754. /*
  755. $sql = "UPDATE $learnpath_view_table " .
  756. "SET " .
  757. "stop_time = ".$this->current_time_stop.", " .
  758. "score = ".$this->current_score.", ".
  759. "WHERE learnpath_id = '".$this->lp_id."'";
  760. //$res = Database::query($sql);
  761. $res = Database::query($res);
  762. if (Database::affected_rows($res) < 1) {
  763. $this->error = 'Could not update learnpath_view table while closing learnpath';
  764. return false;
  765. }
  766. */
  767. }
  768. $this->ordered_items = array ();
  769. $this->index = 0;
  770. unset ($this->lp_id);
  771. //unset other stuff
  772. return true;
  773. }
  774. /**
  775. * Static admin function allowing removal of a learnpath
  776. * @param string Course code
  777. * @param integer Learnpath ID
  778. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  779. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  780. */
  781. public function delete($course = null, $id = null, $delete = 'keep') {
  782. $course_id = api_get_course_int_id();
  783. // TODO: Implement a way of getting this to work when the current object is not set.
  784. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  785. //if (empty($course)) { $course = api_get_course_id(); }
  786. //if (empty($id)) { $id = $this->get_id(); }
  787. // If an ID is specifically given and the current LP is not the same, prevent delete.
  788. if (!empty ($id) && ($id != $this->lp_id)) {
  789. return false;
  790. }
  791. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  792. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  793. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  794. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  795. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  796. // Delete lp item id.
  797. foreach ($this->items as $id => $dummy) {
  798. //$this->items[$id]->delete();
  799. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  800. $res_del_item_view = Database::query($sql_del_view);
  801. }
  802. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  803. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  804. $res_del_item = Database::query($sql_del_item);
  805. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  806. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  807. $res_del_view = Database::query($sql_del_view);
  808. self::toggle_publish($this->lp_id, 'i');
  809. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  810. if ($this->type == 2 || $this->type == 3) {
  811. // This is a scorm learning path, delete the files as well.
  812. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  813. $res = Database::query($sql);
  814. if (Database :: num_rows($res) > 0) {
  815. $row = Database :: fetch_array($res);
  816. $path = $row['path'];
  817. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  818. $res = Database::query($sql);
  819. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  820. if ($this->debug > 2) {
  821. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  822. }
  823. } else {
  824. // No other LP uses that directory, delete it.
  825. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  826. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  827. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  828. if ($this->debug > 2) {
  829. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  830. }
  831. // Proposed by Christophe (clefevre).
  832. if (strcmp(substr($path, -2), "/.") == 0) {
  833. $path = substr($path, 0, -1); // Remove "." at the end.
  834. }
  835. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  836. rmdirr($course_scorm_dir . $path);
  837. }
  838. }
  839. }
  840. }
  841. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  842. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  843. $res_del_lp = Database::query($sql_del_lp);
  844. $this->update_display_order(); // Updates the display order of all lps.
  845. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  846. require_once '../gradebook/lib/be.inc.php';
  847. // Delete link of gradebook tool
  848. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  849. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  850. $result = Database::query($sql);
  851. $row = Database :: fetch_array($result, 'ASSOC');*/
  852. // Fixing gradebook link deleted see #5229.
  853. /*
  854. if (!empty($row['id'])) {
  855. $link = LinkFactory :: load($row['id']);
  856. if ($link[0] != null) {
  857. $link[0]->delete();
  858. }
  859. }*/
  860. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  861. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  862. if ($link_info !== false) {
  863. remove_resource_from_course_gradebook($link_info['id']);
  864. }
  865. if (api_get_setting('search_enabled') == 'true') {
  866. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  867. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  868. }
  869. }
  870. /**
  871. * Removes all the children of one item - dangerous!
  872. * @param integer Element ID of which children have to be removed
  873. * @return integer Total number of children removed
  874. */
  875. public function delete_children_items($id) {
  876. $course_id = api_get_course_int_id();
  877. if ($this->debug > 0) {
  878. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  879. }
  880. $num = 0;
  881. if (empty ($id) || $id != strval(intval($id))) {
  882. return false;
  883. }
  884. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  885. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  886. $res = Database::query($sql);
  887. while ($row = Database :: fetch_array($res)) {
  888. $num += $this->delete_children_items($row['id']);
  889. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  890. $res_del = Database::query($sql_del);
  891. $num++;
  892. }
  893. return $num;
  894. }
  895. /**
  896. * Removes an item from the current learnpath
  897. * @param integer Elem ID (0 if first)
  898. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  899. * @return integer Number of elements moved
  900. * @todo implement resource removal
  901. */
  902. public function delete_item($id, $remove = 'keep') {
  903. $course_id = api_get_course_int_id();
  904. if ($this->debug > 0) {
  905. error_log('New LP - In learnpath::delete_item()', 0);
  906. }
  907. // TODO: Implement the resource removal.
  908. if (empty ($id) || $id != strval(intval($id))) {
  909. return false;
  910. }
  911. // First select item to get previous, next, and display order.
  912. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  913. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  914. $res_sel = Database::query($sql_sel);
  915. if (Database :: num_rows($res_sel) < 1) {
  916. return false;
  917. }
  918. $row = Database :: fetch_array($res_sel);
  919. $previous = $row['previous_item_id'];
  920. $next = $row['next_item_id'];
  921. $display = $row['display_order'];
  922. $parent = $row['parent_item_id'];
  923. $lp = $row['lp_id'];
  924. // Delete children items.
  925. $num = $this->delete_children_items($id);
  926. if ($this->debug > 2) {
  927. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  928. }
  929. // Now delete the item.
  930. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  931. if ($this->debug > 2) {
  932. error_log('New LP - Deleting item: ' . $sql_del, 0);
  933. }
  934. $res_del = Database::query($sql_del);
  935. // Now update surrounding items.
  936. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  937. $res_upd = Database::query($sql_upd);
  938. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  939. $res_upd = Database::query($sql_upd);
  940. // Now update all following items with new display order.
  941. $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";
  942. $res_all = Database::query($sql_all);
  943. //Removing prerequisites since the item will not longer exist
  944. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  945. $res_all = Database::query($sql_all);
  946. // Remove from search engine if enabled.
  947. if (api_get_setting('search_enabled') == 'true') {
  948. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  949. $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';
  950. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  951. $res = Database::query($sql);
  952. if (Database :: num_rows($res) > 0) {
  953. $row2 = Database :: fetch_array($res);
  954. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  955. $di = new ChamiloIndexer();
  956. $di->remove_document((int) $row2['search_did']);
  957. }
  958. $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';
  959. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  960. Database::query($sql);
  961. }
  962. }
  963. /**
  964. * Updates an item's content in place
  965. * @param integer Element ID
  966. * @param integer Parent item ID
  967. * @param integer Previous item ID
  968. * @param string Item title
  969. * @param string Item description
  970. * @param string Prerequisites (optional)
  971. * @param string Indexing terms (optional)
  972. * @param array The array resulting of the $_FILES[mp3] element
  973. * @return boolean True on success, false on error
  974. */
  975. public function edit_item($id, $parent, $previous, $title, $description, $prerequisites = 0, $audio = null, $max_time_allowed = 0) {
  976. $course_id = api_get_course_int_id();
  977. if ($this->debug > 0) {
  978. error_log('New LP - In learnpath::edit_item()', 0);
  979. }
  980. if (empty ($max_time_allowed)) {
  981. $max_time_allowed = 0;
  982. }
  983. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  984. return false;
  985. }
  986. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  987. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  988. $res_select = Database::query($sql_select);
  989. $row_select = Database :: fetch_array($res_select);
  990. $audio_update_sql = '';
  991. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  992. // Create the audio folder if it does not exist yet.
  993. global $_course;
  994. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  995. if (!is_dir($filepath . 'audio')) {
  996. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  997. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  998. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  999. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  1000. }
  1001. // Upload file in documents.
  1002. $pi = pathinfo($audio['name']);
  1003. if ($pi['extension'] == 'mp3') {
  1004. $c_det = api_get_course_info($this->cc);
  1005. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1006. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1007. $path = substr($path, 7);
  1008. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1009. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1010. }
  1011. }
  1012. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1013. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1014. // TODO: htmlspecialchars to be checked for encoding related problems.
  1015. if ($same_parent && $same_previous) {
  1016. // Only update title and description.
  1017. $sql_update = " UPDATE " . $tbl_lp_item . "
  1018. SET title = '" . Database::escape_string($title) . "',
  1019. prerequisite = '" . $prerequisites . "',
  1020. description = '" . Database::escape_string($description) . "'
  1021. " . $audio_update_sql . ",
  1022. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1023. WHERE c_id = ".$course_id." AND id = " . $id;
  1024. $res_update = Database::query($sql_update);
  1025. } else {
  1026. $old_parent = $row_select['parent_item_id'];
  1027. $old_previous = $row_select['previous_item_id'];
  1028. $old_next = $row_select['next_item_id'];
  1029. $old_order = $row_select['display_order'];
  1030. $old_prerequisite = $row_select['prerequisite'];
  1031. $old_max_time_allowed = $row_select['max_time_allowed'];
  1032. /* BEGIN -- virtually remove the current item id */
  1033. /* for the next and previous item it is like the current item doesn't exist anymore */
  1034. if ($old_previous != 0) {
  1035. $sql_update_next = "
  1036. UPDATE " . $tbl_lp_item . "
  1037. SET next_item_id = " . $old_next . "
  1038. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1039. $res_update_next = Database::query($sql_update_next);
  1040. //echo '<p>' . $sql_update_next . '</p>';
  1041. }
  1042. if ($old_next != 0) {
  1043. $sql_update_previous = "
  1044. UPDATE " . $tbl_lp_item . "
  1045. SET previous_item_id = " . $old_previous . "
  1046. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1047. $res_update_previous = Database::query($sql_update_previous);
  1048. //echo '<p>' . $sql_update_previous . '</p>';
  1049. }
  1050. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1051. $sql_update_order = "
  1052. UPDATE " . $tbl_lp_item . "
  1053. SET display_order = display_order - 1
  1054. WHERE
  1055. c_id = ".$course_id." AND
  1056. display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
  1057. parent_item_id = " . $old_parent;
  1058. $res_update_order = Database::query($sql_update_order);
  1059. //echo '<p>' . $sql_update_order . '</p>';
  1060. /* END -- virtually remove the current item id */
  1061. /* BEGIN -- update the current item id to his new location */
  1062. if ($previous == 0) {
  1063. // Select the data of the item that should come after the current item.
  1064. $sql_select_old = "SELECT id, display_order
  1065. FROM " . $tbl_lp_item . "
  1066. WHERE
  1067. c_id = ".$course_id." AND
  1068. lp_id = " . $this->lp_id . " AND
  1069. parent_item_id = " . $parent . " AND
  1070. previous_item_id = " . $previous;
  1071. $res_select_old = Database::query($sql_select_old);
  1072. $row_select_old = Database :: fetch_array($res_select_old);
  1073. //echo '<p>' . $sql_select_old . '</p>';
  1074. // If the new parent didn't have children before.
  1075. if (Database :: num_rows($res_select_old) == 0) {
  1076. $new_next = 0;
  1077. $new_order = 1;
  1078. } else {
  1079. $new_next = $row_select_old['id'];
  1080. $new_order = $row_select_old['display_order'];
  1081. }
  1082. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1083. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1084. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1085. } else {
  1086. // Select the data of the item that should come before the current item.
  1087. $sql_select_old = " SELECT next_item_id, display_order
  1088. FROM " . $tbl_lp_item . "
  1089. WHERE c_id = ".$course_id." AND id = " . $previous;
  1090. $res_select_old = Database::query($sql_select_old);
  1091. $row_select_old = Database :: fetch_array($res_select_old);
  1092. //echo '<p>' . $sql_select_old . '</p>';
  1093. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1094. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1095. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1096. $new_next = $row_select_old['next_item_id'];
  1097. $new_order = $row_select_old['display_order'] + 1;
  1098. }
  1099. // TODO: htmlspecialchars to be checked for encoding related problems.
  1100. // Update the current item with the new data.
  1101. $sql_update = "UPDATE " . $tbl_lp_item . "
  1102. SET
  1103. title = '" . Database::escape_string($title) . "',
  1104. description = '" . Database::escape_string($description) . "',
  1105. parent_item_id = " . $parent . ",
  1106. previous_item_id = " . $previous . ",
  1107. next_item_id = " . $new_next . ",
  1108. display_order = " . $new_order . "
  1109. " . $audio_update_sql . "
  1110. WHERE c_id = ".$course_id." AND id = " . $id;
  1111. $res_update_next = Database::query($sql_update);
  1112. //echo '<p>' . $sql_update . '</p>';
  1113. if ($previous != 0) {
  1114. // Update the previous item's next_item_id.
  1115. $sql_update_previous = "
  1116. UPDATE " . $tbl_lp_item . "
  1117. SET next_item_id = " . $id . "
  1118. WHERE c_id = ".$course_id." AND id = " . $previous;
  1119. $res_update_next = Database::query($sql_update_previous);
  1120. //echo '<p>' . $sql_update_previous . '</p>';
  1121. }
  1122. if ($new_next != 0) {
  1123. // Update the next item's previous_item_id.
  1124. $sql_update_next = "
  1125. UPDATE " . $tbl_lp_item . "
  1126. SET previous_item_id = " . $id . "
  1127. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1128. $res_update_next = Database::query($sql_update_next);
  1129. //echo '<p>' . $sql_update_next . '</p>';
  1130. }
  1131. if ($old_prerequisite != $prerequisites) {
  1132. $sql_update_next = "
  1133. UPDATE " . $tbl_lp_item . "
  1134. SET prerequisite = " . $prerequisites . "
  1135. WHERE c_id = ".$course_id." AND id = " . $id;
  1136. $res_update_next = Database::query($sql_update_next);
  1137. }
  1138. if ($old_max_time_allowed != $max_time_allowed) {
  1139. $sql_update_max_time_allowed = "
  1140. UPDATE " . $tbl_lp_item . "
  1141. SET max_time_allowed = " . $max_time_allowed . "
  1142. WHERE c_id = ".$course_id." AND id = " . $id;
  1143. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1144. }
  1145. // Update all the items with the same or a bigger display_order than the current item.
  1146. $sql_update_order = "
  1147. UPDATE " . $tbl_lp_item . "
  1148. SET display_order = display_order + 1
  1149. WHERE
  1150. c_id = ".$course_id." AND
  1151. lp_id = " . $this->get_id() . " AND
  1152. id <> " . $id . " AND
  1153. parent_item_id = " . $parent . " AND
  1154. display_order >= " . $new_order;
  1155. $res_update_next = Database::query($sql_update_order);
  1156. }
  1157. }
  1158. /**
  1159. * Updates an item's prereq in place
  1160. * @param integer Element ID
  1161. * @param string Prerequisite Element ID
  1162. * @param string Prerequisite item type
  1163. * @param string Prerequisite min score
  1164. * @param string Prerequisite max score
  1165. * @return boolean True on success, false on error
  1166. */
  1167. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100) {
  1168. $course_id = api_get_course_int_id();
  1169. if ($this->debug > 0) {
  1170. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1171. }
  1172. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1173. return false;
  1174. }
  1175. $prerequisite_id = Database::escape_string($prerequisite_id);
  1176. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1177. if (!is_numeric($mastery_score) || $mastery_score < 0)
  1178. $mastery_score = 0;
  1179. if (!is_numeric($max_score) || $max_score < 0)
  1180. $max_score = 100;
  1181. if ($mastery_score > $max_score)
  1182. $max_score = $mastery_score;
  1183. if (!is_numeric($prerequisite_id))
  1184. $prerequisite_id = 'NULL';
  1185. $sql_upd = " UPDATE " . $tbl_lp_item . "
  1186. SET prerequisite = " . $prerequisite_id . " WHERE c_id = ".$course_id." AND id = " . $id;
  1187. $res_upd = Database::query($sql_upd);
  1188. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1189. $sql_upd = " UPDATE " . $tbl_lp_item . " SET
  1190. mastery_score = " . $mastery_score .
  1191. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1192. " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
  1193. $res_upd = Database::query($sql_upd);
  1194. }
  1195. // TODO: Update the item object (can be ignored for now because refreshed).
  1196. return true;
  1197. }
  1198. /**
  1199. * Escapes a string with the available database escape function
  1200. * @param string String to escape
  1201. * @return string String escaped
  1202. * @deprecated use Database::escape_string
  1203. */
  1204. public function escape_string($string) {
  1205. //if ($this->debug > 0) { error_log('New LP - In learnpath::escape_string('.$string.')', 0); }
  1206. return Database::escape_string($string);
  1207. }
  1208. /**
  1209. * Static admin function exporting a learnpath into a zip file
  1210. * @param string Export type (scorm, zip, cd)
  1211. * @param string Course code
  1212. * @param integer Learnpath ID
  1213. * @param string Zip file name
  1214. * @return string Zip file path (or false on error)
  1215. */
  1216. public function export_lp($type, $course, $id, $zipname) {
  1217. $course_id = api_get_course_int_id();
  1218. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1219. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1220. return false;
  1221. }
  1222. $url = '';
  1223. switch ($type) {
  1224. case 'scorm':
  1225. break;
  1226. case 'zip':
  1227. break;
  1228. case 'cdrom':
  1229. break;
  1230. }
  1231. return $url;
  1232. }
  1233. /**
  1234. * Gets all the chapters belonging to the same parent as the item/chapter given
  1235. * Can also be called as abstract method
  1236. * @param integer Item ID
  1237. * @return array A list of all the "brother items" (or an empty array on failure)
  1238. */
  1239. public function get_brother_chapters($id) {
  1240. $course_id = api_get_course_int_id();
  1241. if ($this->debug > 0) {
  1242. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1243. }
  1244. if (empty ($id) OR $id != strval(intval($id))) {
  1245. return array ();
  1246. }
  1247. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1248. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1249. $res_parent = Database::query($sql_parent);
  1250. if (Database :: num_rows($res_parent) > 0) {
  1251. $row_parent = Database :: fetch_array($res_parent);
  1252. $parent = $row_parent['parent_item_id'];
  1253. $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";
  1254. $res_bros = Database::query($sql_bros);
  1255. $list = array ();
  1256. while ($row_bro = Database :: fetch_array($res_bros)) {
  1257. $list[] = $row_bro;
  1258. }
  1259. return $list;
  1260. }
  1261. return array ();
  1262. }
  1263. /**
  1264. * Gets all the items belonging to the same parent as the item given
  1265. * Can also be called as abstract method
  1266. * @param integer Item ID
  1267. * @return array A list of all the "brother items" (or an empty array on failure)
  1268. */
  1269. public function get_brother_items($id) {
  1270. $course_id = api_get_course_int_id();
  1271. if ($this->debug > 0) {
  1272. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1273. }
  1274. if (empty ($id) OR $id != strval(intval($id))) {
  1275. return array ();
  1276. }
  1277. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1278. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1279. $res_parent = Database::query($sql_parent);
  1280. if (Database :: num_rows($res_parent) > 0) {
  1281. $row_parent = Database :: fetch_array($res_parent);
  1282. $parent = $row_parent['parent_item_id'];
  1283. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1284. $res_bros = Database::query($sql_bros);
  1285. $list = array ();
  1286. while ($row_bro = Database :: fetch_array($res_bros)) {
  1287. $list[] = $row_bro;
  1288. }
  1289. return $list;
  1290. }
  1291. return array ();
  1292. }
  1293. /**
  1294. * Get the specific prefix index terms of this learning path
  1295. * @return array Array of terms
  1296. */
  1297. public function get_common_index_terms_by_prefix($prefix) {
  1298. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1299. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1300. $prefix_terms = array();
  1301. if (!empty($terms)) {
  1302. foreach ($terms as $term) {
  1303. $prefix_terms[] = $term['value'];
  1304. }
  1305. }
  1306. return $prefix_terms;
  1307. }
  1308. /**
  1309. * Gets the number of items currently completed
  1310. * @return integer The number of items currently completed
  1311. */
  1312. public function get_complete_items_count() {
  1313. if ($this->debug > 0) {
  1314. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1315. }
  1316. $i = 0;
  1317. foreach ($this->items as $id => $dummy) {
  1318. // Trying failed and browsed considered "progressed" as well.
  1319. if ($this->items[$id]->status_is(array (
  1320. 'completed',
  1321. 'passed',
  1322. 'succeeded',
  1323. 'browsed',
  1324. 'failed'
  1325. )) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir') {
  1326. $i++;
  1327. }
  1328. }
  1329. return $i;
  1330. }
  1331. /**
  1332. * Gets the current item ID
  1333. * @return integer The current learnpath item id
  1334. */
  1335. public function get_current_item_id() {
  1336. $current = 0;
  1337. if ($this->debug > 0) {
  1338. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1339. }
  1340. if (!empty ($this->current)) {
  1341. $current = $this->current;
  1342. }
  1343. if ($this->debug > 2) {
  1344. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1345. }
  1346. return $current;
  1347. }
  1348. /**
  1349. * Force to get the first learnpath item id
  1350. * @return integer The current learnpath item id
  1351. */
  1352. public function get_first_item_id() {
  1353. $current = 0;
  1354. if (is_array($this->ordered_items)) {
  1355. $current = $this->ordered_items[0];
  1356. }
  1357. return $current;
  1358. }
  1359. /**
  1360. * Gets the total number of items available for viewing in this SCORM
  1361. * @return integer The total number of items
  1362. */
  1363. public function get_total_items_count() {
  1364. if ($this->debug > 0) {
  1365. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1366. }
  1367. return count($this->items);
  1368. }
  1369. /**
  1370. * Gets the total number of items available for viewing in this SCORM but without chapters
  1371. * @return integer The total no-chapters number of items
  1372. */
  1373. public function get_total_items_count_without_chapters() {
  1374. if ($this->debug > 0) {
  1375. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1376. }
  1377. $total = 0;
  1378. foreach ($this->items as $temp2) {
  1379. if (!in_array($temp2->get_type(), array (
  1380. 'dokeos_chapter',
  1381. 'chapter',
  1382. 'dir'
  1383. )))
  1384. $total++;
  1385. }
  1386. return $total;
  1387. }
  1388. /**
  1389. * Gets the first element URL.
  1390. * @return string URL to load into the viewer
  1391. */
  1392. public function first() {
  1393. if ($this->debug > 0) {
  1394. error_log('New LP - In learnpath::first()', 0);
  1395. error_log('$this->last_item_seen '.$this->last_item_seen);
  1396. //error_log('$this->items '.print_r($this->items, 1));
  1397. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1398. }
  1399. // Test if the last_item_seen exists and is not a dir.
  1400. if (count($this->ordered_items) == 0) {
  1401. $this->index = 0;
  1402. }
  1403. if ($this->debug > 0) {
  1404. if (isset($this->items[$this->last_item_seen])) {
  1405. $status = $this->items[$this->last_item_seen]->get_status();
  1406. }
  1407. error_log('status '.$status);
  1408. }
  1409. if (!empty($this->last_item_seen) &&
  1410. !empty($this->items[$this->last_item_seen]) &&
  1411. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1412. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1413. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1414. //&& !$this->items[$this->last_item_seen]->is_done()
  1415. ) {
  1416. if ($this->debug > 2) {
  1417. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1418. }
  1419. $index = -1;
  1420. foreach ($this->ordered_items as $myindex => $item_id) {
  1421. if ($item_id == $this->last_item_seen) {
  1422. $index = $myindex;
  1423. break;
  1424. }
  1425. }
  1426. if ($index == -1) {
  1427. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1428. if ($this->debug > 2) {
  1429. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1430. }
  1431. return false;
  1432. } else {
  1433. $this->last = $this->last_item_seen;
  1434. $this->current = $this->last_item_seen;
  1435. $this->index = $index;
  1436. }
  1437. } else {
  1438. if ($this->debug > 2) {
  1439. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1440. }
  1441. $index = 0;
  1442. // Loop through all ordered items and stop at the first item that is
  1443. // not a directory *and* that has not been completed yet.
  1444. while ( !empty($this->ordered_items[$index]) AND
  1445. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1446. (
  1447. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1448. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1449. $this->items[$this->ordered_items[$index]]->is_done() === true
  1450. ) AND $index < $this->max_ordered_items) {
  1451. $index++;
  1452. }
  1453. $this->last = $this->current;
  1454. // current is
  1455. $this->current = $this->ordered_items[$index];
  1456. $this->index = $index;
  1457. if ($this->debug > 2) {
  1458. error_log('$index ' . $index);
  1459. }
  1460. if ($this->debug > 2) {
  1461. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1462. }
  1463. }
  1464. if ($this->debug > 2) {
  1465. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1466. }
  1467. }
  1468. /**
  1469. * Gets the information about an item in a format usable as JavaScript to update
  1470. * the JS API by just printing this content into the <head> section of the message frame
  1471. * @param integer Item ID
  1472. * @return string
  1473. */
  1474. public function get_js_info($item_id = '') {
  1475. if ($this->debug > 0) {
  1476. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1477. }
  1478. $info = '';
  1479. $item_id = Database::escape_string($item_id);
  1480. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1481. //if item is defined, return values from DB
  1482. $oItem = $this->items[$item_id];
  1483. $info .= '<script language="javascript">';
  1484. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1485. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1486. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1487. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1488. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1489. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1490. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1491. $info .= "top.set_flag_synchronized();";
  1492. $info .= '</script>';
  1493. if ($this->debug > 2) {
  1494. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1495. }
  1496. return $info;
  1497. } else {
  1498. // If item_id is empty, just update to default SCORM data.
  1499. $info .= '<script language="javascript">';
  1500. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1501. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1502. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1503. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1504. $info .= "top.set_session_time('" . learnpathItem :: get_scorm_time('js') . "');";
  1505. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1506. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1507. $info .= "top.set_flag_synchronized();";
  1508. $info .= '</script>';
  1509. if ($this->debug > 2) {
  1510. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1511. }
  1512. return $info;
  1513. }
  1514. }
  1515. /**
  1516. * Gets the js library from the database
  1517. * @return string The name of the javascript library to be used
  1518. */
  1519. public function get_js_lib() {
  1520. $lib = '';
  1521. if (!empty ($this->js_lib)) {
  1522. $lib = $this->js_lib;
  1523. }
  1524. return $lib;
  1525. }
  1526. /**
  1527. * Gets the learnpath database ID
  1528. * @return integer Learnpath ID in the lp table
  1529. */
  1530. public function get_id() {
  1531. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1532. if (!empty ($this->lp_id)) {
  1533. return $this->lp_id;
  1534. } else {
  1535. return 0;
  1536. }
  1537. }
  1538. /**
  1539. * Gets the last element URL.
  1540. * @return string URL to load into the viewer
  1541. */
  1542. public function get_last() {
  1543. if ($this->debug > 0) {
  1544. error_log('New LP - In learnpath::get_last()', 0);
  1545. }
  1546. $this->index = count($this->ordered_items) - 1;
  1547. return $this->ordered_items[$this->index];
  1548. }
  1549. /**
  1550. * Gets the navigation bar for the learnpath display screen
  1551. * @return string The HTML string to use as a navigation bar
  1552. */
  1553. public function get_navigation_bar() {
  1554. if ($this->debug > 0) {
  1555. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1556. }
  1557. // TODO: Find a good value for the following variables.
  1558. $file = '';
  1559. $openDir = '';
  1560. $edoceo = '';
  1561. $time = 0;
  1562. $navbar = '';
  1563. $RequestUri = '';
  1564. $mycurrentitemid = $this->get_current_item_id();
  1565. if ($this->mode == 'fullscreen') {
  1566. $navbar = '
  1567. <div class="buttons">
  1568. <a href="lp_controller.php?action=stats" 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('Reporting') . '"></a>
  1569. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1570. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>.
  1571. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
  1572. </div>';
  1573. } else {
  1574. $navbar = '
  1575. <div class="buttons">
  1576. <a href="lp_controller.php?action=stats" 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('Reporting') . '"></a>
  1577. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1578. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>
  1579. </div>';
  1580. }
  1581. return $navbar;
  1582. }
  1583. /**
  1584. * Gets the next resource in queue (url).
  1585. * @return string URL to load into the viewer
  1586. */
  1587. public function get_next_index() {
  1588. if ($this->debug > 0) {
  1589. error_log('New LP - In learnpath::get_next_index()', 0);
  1590. }
  1591. // TODO
  1592. $index = $this->index;
  1593. $index++;
  1594. if ($this->debug > 2) {
  1595. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1596. }
  1597. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1598. $index++;
  1599. if ($index == $this->max_ordered_items){
  1600. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1601. return $this->index;
  1602. } else {
  1603. return $index;
  1604. }
  1605. }
  1606. }
  1607. if (empty ($this->ordered_items[$index])) {
  1608. return $this->index;
  1609. }
  1610. if ($this->debug > 2) {
  1611. error_log('New LP - index is now ' . $index, 0);
  1612. }
  1613. return $index;
  1614. }
  1615. /**
  1616. * Gets item_id for the next element
  1617. * @return integer Next item (DB) ID
  1618. */
  1619. public function get_next_item_id() {
  1620. if ($this->debug > 0) {
  1621. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1622. }
  1623. $new_index = $this->get_next_index();
  1624. if (!empty ($new_index)) {
  1625. if (isset ($this->ordered_items[$new_index])) {
  1626. if ($this->debug > 2) {
  1627. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1628. }
  1629. return $this->ordered_items[$new_index];
  1630. }
  1631. }
  1632. if ($this->debug > 2) {
  1633. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1634. }
  1635. return 0;
  1636. }
  1637. /**
  1638. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1639. *
  1640. * Generally, the package provided is in the form of a zip file, so the function
  1641. * has been written to test a zip file. If not a zip, the function will return the
  1642. * default return value: ''
  1643. * @param string the path to the file
  1644. * @param string the original name of the file
  1645. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1646. */
  1647. public static function get_package_type($file_path, $file_name) {
  1648. // Get name of the zip file without the extension.
  1649. $file_info = pathinfo($file_name);
  1650. $filename = $file_info['basename']; // Name including extension.
  1651. $extension = $file_info['extension']; // Extension only.
  1652. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1653. 'dll',
  1654. 'exe'
  1655. ))) {
  1656. return 'oogie';
  1657. }
  1658. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1659. 'dll',
  1660. 'exe'
  1661. ))) {
  1662. return 'woogie';
  1663. }
  1664. $file_base_name = str_replace('.' . $extension, '', $filename); // Filename without its extension.
  1665. $zipFile = new PclZip($file_path);
  1666. // Check the zip content (real size and file extension).
  1667. $zipContentArray = $zipFile->listContent();
  1668. $package_type = '';
  1669. $at_root = false;
  1670. $manifest = '';
  1671. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1672. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1673. foreach ($zipContentArray as $thisContent) {
  1674. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1675. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1676. }
  1677. elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1678. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1679. $package_type = 'scorm';
  1680. break; // Exit the foreach loop.
  1681. }
  1682. elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1683. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1684. $package_type = 'aicc';
  1685. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1686. } else {
  1687. $package_type = '';
  1688. }
  1689. }
  1690. }
  1691. return $package_type;
  1692. }
  1693. /**
  1694. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1695. * @return string URL to load into the viewer
  1696. */
  1697. public function get_previous_index() {
  1698. if ($this->debug > 0) {
  1699. error_log('New LP - In learnpath::get_previous_index()', 0);
  1700. }
  1701. $index = $this->index;
  1702. if (isset ($this->ordered_items[$index -1])) {
  1703. $index--;
  1704. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1705. $index--;
  1706. if ($index < 0) {
  1707. return $this->index;
  1708. }
  1709. }
  1710. } else {
  1711. if ($this->debug > 2) {
  1712. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1713. }
  1714. // There is no previous item.
  1715. }
  1716. return $index;
  1717. }
  1718. /**
  1719. * Gets item_id for the next element
  1720. * @return integer Previous item (DB) ID
  1721. */
  1722. public function get_previous_item_id() {
  1723. if ($this->debug > 0) {
  1724. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1725. }
  1726. $new_index = $this->get_previous_index();
  1727. return $this->ordered_items[$new_index];
  1728. }
  1729. /**
  1730. * Gets the progress value from the progress_db attribute
  1731. * @return integer Current progress value
  1732. */
  1733. public function get_progress() {
  1734. if ($this->debug > 0) {
  1735. error_log('New LP - In learnpath::get_progress()', 0);
  1736. }
  1737. if (!empty ($this->progress_db)) {
  1738. return $this->progress_db;
  1739. }
  1740. return 0;
  1741. }
  1742. /**
  1743. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1744. * @param integer Learnpath ID
  1745. * @param integer User ID
  1746. * @param string Mode of display ('%','abs' or 'both')
  1747. * @param string Course database name (optional, defaults to '')
  1748. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1749. * @return integer Current progress value as found in the database
  1750. */
  1751. public static function get_db_progress($lp_id, $user_id, $mode = '%', $course_code = '', $sincere = false,$session_id = 0) {
  1752. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1753. $session_id = intval($session_id);
  1754. $course_info = api_get_course_info($course_code);
  1755. $session_condition = api_get_session_condition($session_id);
  1756. $course_id = $course_info['real_id'];
  1757. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1758. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1759. $res = Database::query($sql);
  1760. $view_id = 0;
  1761. if (Database :: num_rows($res) > 0) {
  1762. $row = Database :: fetch_array($res);
  1763. $progress = $row['progress'];
  1764. $view_id = $row['id'];
  1765. } else {
  1766. if ($sincere) {
  1767. return null;
  1768. }
  1769. }
  1770. if (empty ($progress)) {
  1771. $progress = '0';
  1772. }
  1773. if ($mode == '%') {
  1774. return $progress . '%';
  1775. } else {
  1776. // Get the number of items completed and the number of items total.
  1777. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  1778. $sql = "SELECT count(*) FROM $tbl
  1779. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = " . $lp_id . " AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  1780. $res = Database::query($sql);
  1781. $row = Database :: fetch_array($res);
  1782. $total = $row[0];
  1783. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1784. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  1785. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  1786. // Trying as also counting browsed and failed items.
  1787. $sql = "SELECT count(distinct(lp_item_id))
  1788. FROM $tbl_item_view as item_view
  1789. INNER JOIN $tbl_item as item
  1790. ON item.id = item_view.lp_item_id
  1791. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  1792. WHERE
  1793. item_view.c_id = $course_id AND
  1794. item.c_id = $course_id AND
  1795. lp_view_id = " . $view_id . " AND
  1796. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  1797. $res = Database::query($sql);
  1798. $row = Database :: fetch_array($res);
  1799. $completed = $row[0];
  1800. if ($mode == 'abs') {
  1801. return $completed . '/' . $total;
  1802. } elseif ($mode == 'both') {
  1803. if ($progress < ($completed / ($total ? $total : 1))) {
  1804. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  1805. }
  1806. return $progress . '% (' . $completed . '/' . $total . ')';
  1807. }
  1808. }
  1809. return $progress;
  1810. }
  1811. /**
  1812. * Returns the HTML necessary to print a mediaplayer block inside a page
  1813. * @return string The mediaplayer HTML
  1814. */
  1815. public function get_mediaplayer($autostart='true') {
  1816. $course_id = api_get_course_int_id();
  1817. global $_course;
  1818. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1819. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1820. // Getting all the information about the item.
  1821. $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 " .
  1822. "WHERE lp.id = '" . $_SESSION['oLP']->current . "' AND
  1823. lp.c_id = $course_id AND
  1824. lp_view.c_id = $course_id";
  1825. $result = Database::query($sql);
  1826. $row = Database::fetch_assoc($result);
  1827. $output = '';
  1828. if (!empty ($row['audio'])) {
  1829. $list = $_SESSION['oLP']->get_toc();
  1830. $type_quiz = false;
  1831. foreach($list as $toc) {
  1832. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  1833. $type_quiz = true;
  1834. }
  1835. }
  1836. if ($type_quiz) {
  1837. if ($_SESSION['oLP']->prevent_reinit == 1) {
  1838. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  1839. } else {
  1840. $autostart_audio = $autostart;
  1841. }
  1842. } else {
  1843. $autostart_audio = 'true';
  1844. }
  1845. // The mp3 player.
  1846. $output = '<div id="container">';
  1847. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  1848. $output .= '<script type="text/javascript">
  1849. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  1850. s1.addParam("allowscriptaccess","always");
  1851. s1.addParam("flashvars","file=' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=' . $autostart_audio.'");
  1852. s1.write("container");
  1853. </script>
  1854. </div>';
  1855. }
  1856. return $output;
  1857. }
  1858. /**
  1859. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  1860. * @param int Learnpath id
  1861. * @param int Student id
  1862. * @param string Course code (optional)
  1863. * @return bool True if
  1864. */
  1865. public static function is_lp_visible_for_student($lp_id, $student_id, $course = null) {
  1866. $lp_id = (int)$lp_id;
  1867. $course = api_get_course_info($course);
  1868. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  1869. // Get current prerequisite
  1870. $sql = "SELECT id, prerequisite, publicated_on, expired_on FROM $tbl_learnpath WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  1871. $rs = Database::query($sql);
  1872. $now = time();
  1873. if (Database::num_rows($rs)>0) {
  1874. $row = Database::fetch_array($rs, 'ASSOC');
  1875. $prerequisite = $row['prerequisite'];
  1876. $is_visible = true;
  1877. $progress = 0;
  1878. if (!empty($prerequisite)) {
  1879. $progress = self::get_db_progress($prerequisite,$student_id,'%', '', false, api_get_session_id());
  1880. $progress = intval($progress);
  1881. if ($progress < 100) {
  1882. $is_visible = false;
  1883. }
  1884. }
  1885. // Also check the time availability of the LP
  1886. if ($is_visible) {
  1887. //Adding visibility reestrinctions
  1888. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  1889. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  1890. //api_not_allowed();
  1891. $is_visible = false;
  1892. }
  1893. }
  1894. //Blocking empty start times see BT#2800
  1895. global $_custom;
  1896. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  1897. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  1898. //api_not_allowed();
  1899. $is_visible = false;
  1900. }
  1901. }
  1902. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  1903. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  1904. //api_not_allowed();
  1905. $is_visible = false;
  1906. }
  1907. }
  1908. }
  1909. return $is_visible;
  1910. }
  1911. return false;
  1912. }
  1913. /**
  1914. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  1915. * completed so far.
  1916. * @param string Mode in which we want the values
  1917. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  1918. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  1919. * @param boolean true if it comes from a Diplay LP view
  1920. * @return string HTML string containing the progress bar
  1921. */
  1922. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false) {
  1923. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  1924. global $lp_theme_css;
  1925. // Setting up the CSS path of the current style if exists.
  1926. if (!empty ($lp_theme_css)) {
  1927. $css_path = api_get_path(WEB_CODE_PATH) . 'css/' . $lp_theme_css . '/images/';
  1928. } else {
  1929. $css_path = '../img/';
  1930. }
  1931. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  1932. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  1933. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  1934. }
  1935. $text = $percentage . $text_add;
  1936. //@todo use Display::display_progress();
  1937. $output .= '<div class="progress progress-striped">
  1938. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  1939. </div>
  1940. <div class="progresstext" id="progress_text">' . $text . '</div>';
  1941. return $output;
  1942. }
  1943. /**
  1944. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  1945. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  1946. * @param integer Additional steps to fake as completed
  1947. * @return list Percentage or number and symbol (% or /xx)
  1948. */
  1949. public function get_progress_bar_text($mode = '', $add = 0) {
  1950. if ($this->debug > 0) {
  1951. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  1952. }
  1953. if (empty ($mode)) {
  1954. $mode = $this->progress_bar_mode;
  1955. }
  1956. $total_items = $this->get_total_items_count_without_chapters();
  1957. if ($this->debug > 2) {
  1958. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  1959. }
  1960. $i = $this->get_complete_items_count();
  1961. if ($this->debug > 2) {
  1962. error_log('New LP - Items completed so far: ' . $i, 0);
  1963. }
  1964. if ($add != 0) {
  1965. $i += $add;
  1966. if ($this->debug > 2) {
  1967. error_log('New LP - Items completed so far (+modifier): ' . $i, 0);
  1968. }
  1969. }
  1970. $text = '';
  1971. if ($i > $total_items) {
  1972. $i = $total_items;
  1973. }
  1974. if ($mode == '%') {
  1975. if ($total_items > 0) {
  1976. $percentage = ((float) $i / (float) $total_items) * 100;
  1977. } else {
  1978. $percentage = 0;
  1979. }
  1980. $percentage = number_format($percentage, 0);
  1981. $text = '%';
  1982. }
  1983. elseif ($mode == 'abs') {
  1984. $percentage = $i;
  1985. $text = '/' . $total_items;
  1986. }
  1987. return array (
  1988. $percentage,
  1989. $text
  1990. );
  1991. }
  1992. /**
  1993. * Gets the progress bar mode
  1994. * @return string The progress bar mode attribute
  1995. */
  1996. public function get_progress_bar_mode() {
  1997. if ($this->debug > 0) {
  1998. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  1999. }
  2000. if (!empty ($this->progress_bar_mode)) {
  2001. return $this->progress_bar_mode;
  2002. } else {
  2003. return '%';
  2004. }
  2005. }
  2006. /**
  2007. * Gets the learnpath proximity (remote or local)
  2008. * @return string Learnpath proximity
  2009. */
  2010. public function get_proximity() {
  2011. if ($this->debug > 0) {
  2012. error_log('New LP - In learnpath::get_proximity()', 0);
  2013. }
  2014. if (!empty ($this->proximity)) {
  2015. return $this->proximity;
  2016. } else {
  2017. return '';
  2018. }
  2019. }
  2020. /**
  2021. * Gets the learnpath theme (remote or local)
  2022. * @return string Learnpath theme
  2023. */
  2024. public function get_theme() {
  2025. if ($this->debug > 0) {
  2026. error_log('New LP - In learnpath::get_theme()', 0);
  2027. }
  2028. if (!empty ($this->theme)) {
  2029. return $this->theme;
  2030. } else {
  2031. return '';
  2032. }
  2033. }
  2034. /**
  2035. * Gets the learnpath session id
  2036. * @return string Learnpath theme
  2037. */
  2038. public function get_lp_session_id() {
  2039. if ($this->debug > 0) {
  2040. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2041. }
  2042. if (!empty ($this->lp_session_id)) {
  2043. return $this->lp_session_id;
  2044. } else {
  2045. return 0;
  2046. }
  2047. }
  2048. /**
  2049. * Gets the learnpath image
  2050. * @return string Web URL of the LP image
  2051. */
  2052. public function get_preview_image() {
  2053. if ($this->debug > 0) {
  2054. error_log('New LP - In learnpath::get_preview_image()', 0);
  2055. }
  2056. if (!empty ($this->preview_image)) {
  2057. return $this->preview_image;
  2058. } else {
  2059. return '';
  2060. }
  2061. }
  2062. /**
  2063. * Gets the learnpath author
  2064. * @return string LP's author
  2065. */
  2066. public function get_author() {
  2067. if ($this->debug > 0) {
  2068. error_log('New LP - In learnpath::get_author()', 0);
  2069. }
  2070. if (!empty ($this->author)) {
  2071. return $this->author;
  2072. } else {
  2073. return '';
  2074. }
  2075. }
  2076. /**
  2077. * Gets the learnpath author
  2078. * @return string LP's author
  2079. */
  2080. public function get_hide_toc_frame() {
  2081. if ($this->debug > 0) {
  2082. error_log('New LP - In learnpath::get_author()', 0);
  2083. }
  2084. if (!empty ($this->hide_toc_frame)) {
  2085. return $this->hide_toc_frame;
  2086. } else {
  2087. return '';
  2088. }
  2089. }
  2090. /**
  2091. * Generate a new prerequisites string for a given item. If this item was a sco and
  2092. * its prerequisites were strings (instead of IDs), then transform those strings into
  2093. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2094. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2095. * same rule as the scorm_export() method
  2096. * @param integer Item ID
  2097. * @return string Prerequisites string ready for the export as SCORM
  2098. */
  2099. public function get_scorm_prereq_string($item_id) {
  2100. if ($this->debug > 0) {
  2101. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2102. }
  2103. if (!is_object($this->items[$item_id])) {
  2104. return false;
  2105. }
  2106. $oItem = $this->items[$item_id];
  2107. $prereq = $oItem->get_prereq_string();
  2108. if (empty ($prereq)) {
  2109. return '';
  2110. }
  2111. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) { // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2112. // then simply return it (with the ITEM_ prefix).
  2113. return 'ITEM_' . $prereq;
  2114. } else {
  2115. if (isset ($this->refs_list[$prereq])) {
  2116. // It's a simple string item from which the ID can be found in the refs list,
  2117. // so we can transform it directly to an ID for export.
  2118. return 'ITEM_' . $this->refs_list[$prereq];
  2119. } else {
  2120. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2121. // and replace them, one by one, by the internal IDs (chamilo db)
  2122. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2123. // by a space as well.
  2124. $find = array (
  2125. '&',
  2126. '|',
  2127. '~',
  2128. '=',
  2129. '<>',
  2130. '{',
  2131. '}',
  2132. '*',
  2133. '(',
  2134. ')'
  2135. );
  2136. $replace = array (
  2137. ' ',
  2138. ' ',
  2139. ' ',
  2140. ' ',
  2141. ' ',
  2142. ' ',
  2143. ' ',
  2144. ' ',
  2145. ' ',
  2146. ' '
  2147. );
  2148. $prereq_mod = str_replace($find, $replace, $prereq);
  2149. $ids = split(' ', $prereq_mod);
  2150. foreach ($ids as $id) {
  2151. $id = trim($id);
  2152. if (isset ($this->refs_list[$id])) {
  2153. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2154. }
  2155. }
  2156. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2157. return $prereq;
  2158. }
  2159. }
  2160. }
  2161. /**
  2162. * Returns the XML DOM document's node
  2163. * @param resource Reference to a list of objects to search for the given ITEM_*
  2164. * @param string The identifier to look for
  2165. * @return mixed The reference to the element found with that identifier. False if not found
  2166. */
  2167. public function get_scorm_xml_node(& $children, $id) {
  2168. for ($i = 0; $i < $children->length; $i++) {
  2169. $item_temp = $children->item($i);
  2170. if ($item_temp->nodeName == 'item') {
  2171. if ($item_temp->getAttribute('identifier') == $id) {
  2172. return $item_temp;
  2173. }
  2174. }
  2175. $subchildren = $item_temp->childNodes;
  2176. if ($subchildren->length > 0) {
  2177. $val = $this->get_scorm_xml_node($subchildren, $id);
  2178. if (is_object($val)) {
  2179. return $val;
  2180. }
  2181. }
  2182. }
  2183. return false;
  2184. }
  2185. /**
  2186. * Returns a usable array of stats related to the current learnpath and user
  2187. * @return array Well-formatted array containing status for the current learnpath
  2188. */
  2189. public function get_stats() {
  2190. if ($this->debug > 0) {
  2191. error_log('New LP - In learnpath::get_stats()', 0);
  2192. }
  2193. // TODO
  2194. }
  2195. /**
  2196. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2197. * the given course (for all learnpaths and all users)
  2198. * @param string Course code
  2199. * @return array Well-formatted array containing status for the course's learnpaths
  2200. */
  2201. public function get_stats_course($course) {
  2202. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2203. // TODO
  2204. }
  2205. /**
  2206. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2207. * the given course and learnpath (for all users)
  2208. * @param string Course code
  2209. * @param integer Learnpath ID
  2210. * @return array Well-formatted array containing status for the specified learnpath
  2211. */
  2212. public function get_stats_lp($course, $lp) {
  2213. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2214. // TODO
  2215. }
  2216. /**
  2217. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2218. * the given course, learnpath and user.
  2219. * @param string Course code
  2220. * @param integer Learnpath ID
  2221. * @param integer User ID
  2222. * @return array Well-formatted array containing status for the specified learnpath and user
  2223. */
  2224. public function get_stats_lp_user($course, $lp, $user) {
  2225. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2226. // TODO
  2227. }
  2228. /**
  2229. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2230. * the given course and learnpath (for all users)
  2231. * @param string Course code
  2232. * @param integer User ID
  2233. * @return array Well-formatted array containing status for the user's learnpaths
  2234. */
  2235. public function get_stats_user($course, $user) {
  2236. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2237. // TODO
  2238. }
  2239. /**
  2240. * Gets the status list for all LP's items
  2241. * @return array Array of [index] => [item ID => current status]
  2242. */
  2243. public function get_items_status_list() {
  2244. if ($this->debug > 0) {
  2245. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2246. }
  2247. $list = array ();
  2248. foreach ($this->ordered_items as $item_id) {
  2249. $list[] = array (
  2250. $item_id => $this->items[$item_id]->get_status()
  2251. );
  2252. }
  2253. return $list;
  2254. }
  2255. /**
  2256. * Return the number of interactions for the given learnpath Item View ID.
  2257. * This method can be used as static.
  2258. * @param integer Item View ID
  2259. * @param integer course id
  2260. * @return integer Number of interactions
  2261. */
  2262. public static function get_interactions_count_from_db($lp_iv_id, $course_id) {
  2263. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2264. $lp_iv_id = intval($lp_iv_id);
  2265. $course_id = intval($course_id);
  2266. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2267. $res = Database::query($sql);
  2268. $res = 0;
  2269. if (Database::num_rows($res)) {
  2270. $row = Database::fetch_array($res);
  2271. $num = $row[0];
  2272. }
  2273. return $num;
  2274. }
  2275. /**
  2276. * Return the interactions as an array for the given lp_iv_id.
  2277. * This method can be used as static.
  2278. * @param integer Learnpath Item View ID
  2279. * @return array
  2280. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2281. */
  2282. public static function get_iv_interactions_array($lp_iv_id = 0) {
  2283. $course_id = api_get_course_int_id();
  2284. $list = array ();
  2285. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2286. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2287. $res = Database::query($sql);
  2288. $num = Database :: num_rows($res);
  2289. if ($num > 0) {
  2290. $list[] = array (
  2291. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2292. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2293. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2294. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2295. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2296. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2297. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2298. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2299. );
  2300. while ($row = Database :: fetch_array($res)) {
  2301. $list[] = array (
  2302. 'order_id' => ($row['order_id'] + 1),
  2303. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2304. 'type' => $row['interaction_type'],
  2305. 'time' => $row['completion_time'],
  2306. //'correct_responses' => $row['correct_responses'],
  2307. 'correct_responses' => '', // Hide correct responses from students.
  2308. 'student_response' => $row['student_response'],
  2309. 'result' => $row['result'],
  2310. 'latency' => $row['latency']
  2311. );
  2312. }
  2313. }
  2314. return $list;
  2315. }
  2316. /**
  2317. * Return the number of objectives for the given learnpath Item View ID.
  2318. * This method can be used as static.
  2319. * @param integer Item View ID
  2320. * @return integer Number of objectives
  2321. */
  2322. public static function get_objectives_count_from_db($lp_iv_id, $course_id) {
  2323. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2324. $course_id = intval($course_id);
  2325. $lp_iv_id = intval($lp_iv_id);
  2326. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2327. //@todo seems that this always returns 0
  2328. $res = Database::query($sql);
  2329. $res = 0;
  2330. if (Database::num_rows($res)) {
  2331. $row = Database :: fetch_array($res);
  2332. $num = $row[0];
  2333. }
  2334. return $num;
  2335. }
  2336. /**
  2337. * Return the objectives as an array for the given lp_iv_id.
  2338. * This method can be used as static.
  2339. * @param integer Learnpath Item View ID
  2340. * @return array
  2341. * @todo Translate labels
  2342. */
  2343. public function get_iv_objectives_array($lp_iv_id = 0) {
  2344. $course_id = api_get_course_int_id();
  2345. $list = array();
  2346. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2347. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2348. $res = Database::query($sql);
  2349. $num = Database :: num_rows($res);
  2350. if ($num > 0) {
  2351. $list[] = array (
  2352. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2353. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2354. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2355. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2356. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2357. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2358. );
  2359. while ($row = Database :: fetch_array($res)) {
  2360. $list[] = array (
  2361. 'order_id' => ($row['order_id'] + 1),
  2362. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2363. 'score_raw' => $row['score_raw'],
  2364. 'score_max' => $row['score_max'],
  2365. 'score_min' => $row['score_min'],
  2366. 'status' => $row['status']
  2367. );
  2368. }
  2369. }
  2370. return $list;
  2371. }
  2372. /**
  2373. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2374. * used by get_html_toc() to be ready to display
  2375. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2376. */
  2377. public function get_toc() {
  2378. if ($this->debug > 0) {
  2379. error_log('New LP - In learnpath::get_toc()', 0);
  2380. }
  2381. $toc = array();
  2382. //echo "<pre>".print_r($this->items,true)."</pre>";
  2383. foreach ($this->ordered_items as $item_id) {
  2384. if ($this->debug > 2) {
  2385. error_log('New LP - learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2386. }
  2387. // TODO: Change this link generation and use new function instead.
  2388. $toc[] = array (
  2389. 'id' => $item_id,
  2390. 'title' => $this->items[$item_id]->get_title(),
  2391. //'link' => get_addedresource_link_in_learnpath('document', $item_id, 1),
  2392. 'status' => $this->items[$item_id]->get_status(),
  2393. 'level' => $this->items[$item_id]->get_level(),
  2394. 'type' => $this->items[$item_id]->get_type(),
  2395. 'description' => $this->items[$item_id]->get_description(),
  2396. 'path' => $this->items[$item_id]->get_path(),
  2397. );
  2398. }
  2399. if ($this->debug > 2) {
  2400. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2401. }
  2402. return $toc;
  2403. }
  2404. /**
  2405. * Generate and return the table of contents for this learnpath. The JS
  2406. * table returned is used inside of scorm_api.php
  2407. * @return string A JS array vairiable construction
  2408. */
  2409. public function get_items_details_as_js($varname = 'olms.lms_item_types') {
  2410. if ($this->debug > 0) {
  2411. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2412. }
  2413. $toc = $varname.' = new Array();';
  2414. //echo "<pre>".print_r($this->items,true)."</pre>";
  2415. foreach ($this->ordered_items as $item_id) {
  2416. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2417. }
  2418. if ($this->debug > 2) {
  2419. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2420. }
  2421. return $toc;
  2422. }
  2423. /**
  2424. * Gets the learning path type
  2425. * @param boolean Return the name? If false, return the ID. Default is false.
  2426. * @return mixed Type ID or name, depending on the parameter
  2427. */
  2428. public function get_type($get_name = false) {
  2429. $res = false;
  2430. if ($this->debug > 0) {
  2431. error_log('New LP - In learnpath::get_type()', 0);
  2432. }
  2433. if (!empty ($this->type)) {
  2434. if ($get_name) {
  2435. // Get it from the lp_type table in main db.
  2436. } else {
  2437. $res = $this->type;
  2438. }
  2439. }
  2440. if ($this->debug > 2) {
  2441. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2442. }
  2443. return $res;
  2444. }
  2445. /**
  2446. * Gets the learning path type as static method
  2447. * @param boolean Return the name? If false, return the ID. Default is false.
  2448. * @return mixed Type ID or name, depending on the parameter
  2449. */
  2450. public static function get_type_static($lp_id = 0) {
  2451. $course_id = api_get_course_int_id();
  2452. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2453. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2454. $res = Database::query($sql);
  2455. if ($res === false) {
  2456. return null;
  2457. }
  2458. if (Database :: num_rows($res) <= 0) {
  2459. return null;
  2460. }
  2461. $row = Database :: fetch_array($res);
  2462. return $row['lp_type'];
  2463. }
  2464. /**
  2465. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2466. * This method can be used as abstract and is recursive
  2467. * @param integer Learnpath ID
  2468. * @param integer Parent ID of the items to look for
  2469. * @return mixed Ordered list of item IDs or false on error
  2470. */
  2471. public function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null) {
  2472. if (empty($course_id)) {
  2473. $course_id = api_get_course_int_id();
  2474. } else {
  2475. $course_id = intval($course_id);
  2476. }
  2477. $list = array();
  2478. if (empty ($lp)) {
  2479. return false;
  2480. }
  2481. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2482. $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";
  2483. $res = Database::query($sql);
  2484. while ($row = Database :: fetch_array($res)) {
  2485. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2486. $list[] = $row['id'];
  2487. foreach ($sublist as $item) {
  2488. $list[] = $item;
  2489. }
  2490. }
  2491. return $list;
  2492. }
  2493. /**
  2494. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2495. * @return string HTML TOC ready to display
  2496. */
  2497. public function get_html_toc($toc_list = null) {
  2498. $course_id = api_get_course_int_id();
  2499. $course_code = api_get_course_id();
  2500. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  2501. $charset = api_get_system_encoding();
  2502. $display_action_links_with_icons = false;
  2503. if ($this->debug > 0) {
  2504. error_log('New LP - In learnpath::get_html_toc()', 0);
  2505. }
  2506. if (empty($toc_list)) {
  2507. $toc_list = $this->get_toc();
  2508. }
  2509. //echo $this->current;
  2510. //$parent = $this->items[$this->current]->get_parent();
  2511. //if (empty($parent)) { $parent = $this->ordered_items[$this->items[$this->current]->get_previous_index()]; }
  2512. $html = '<div id="scorm_title" class="scorm_title">' . Security::remove_XSS($this->get_name()) . '</div>';
  2513. // Build, display.
  2514. if ($is_allowed_to_edit) {
  2515. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2516. if ($this->get_lp_session_id() == api_get_session_id()) {
  2517. $html .= '<div id="actions_lp" class="actions_lp">';
  2518. if ($display_action_links_with_icons) {
  2519. $html .= '<div class = "btn-group">';
  2520. $html .= "<a href='lp_controller.php?" . api_get_cidreq() . "&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . Display :: return_icon('build_learnpath.png', get_lang('Build'),'',ICON_SIZE_MEDIUM)."</a>";
  2521. //$html .= "<a href='lp_controller.php?" . api_get_cidreq() . "&amp;action=admin_view&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM)."</a>";
  2522. //$html .= '<span>' . Display :: return_icon('view_remove_na.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</span><br />';
  2523. $html .= '<a href="lp_controller.php?' . api_get_cidreq() . '">'. get_lang('ReturnToLPList') . '</a>';
  2524. $html .= '</div>';
  2525. } else {
  2526. $html .= '<div class="btn-group">';
  2527. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2528. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;action=add_item&amp;type=step&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2529. //$html .= '<span><b>' . get_lang('Display') . '</b></span><br />';
  2530. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2531. $html .= '</div>';
  2532. }
  2533. $html .= '</div>';
  2534. }
  2535. }
  2536. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2537. require_once 'resourcelinker.inc.php';
  2538. // Temporary variables.
  2539. $mycurrentitemid = $this->get_current_item_id();
  2540. $color_counter = 0;
  2541. $i = 0;
  2542. foreach ($toc_list as $item) {
  2543. if ($this->debug > 2) {
  2544. //error_log('New LP - learnpath::get_html_toc(): using item ' . $item['id'], 0);
  2545. }
  2546. // TODO: Complete this.
  2547. $icon_name = array (
  2548. 'not attempted' => '../img/notattempted.gif',
  2549. 'incomplete' => '../img/incomplete.png',
  2550. 'failed' => '../img/delete.png',
  2551. 'completed' => '../img/completed.png',
  2552. 'passed' => '../img/passed.png',
  2553. 'succeeded' => '../img/succeeded.png',
  2554. 'browsed' => '../img/completed.png',
  2555. );
  2556. $style = 'scorm_item';
  2557. $scorm_color_background = 'scorm_item';
  2558. $style_item = 'scorm_item';
  2559. $current = false;
  2560. if ($item['id'] == $this->current) {
  2561. $style = 'scorm_item_highlight';
  2562. $scorm_color_background = 'scorm_item_highlight';
  2563. } else {
  2564. if ($color_counter % 2 == 0) {
  2565. $scorm_color_background = 'scorm_item_1';
  2566. } else {
  2567. $scorm_color_background = 'scorm_item_2';
  2568. }
  2569. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2570. $scorm_color_background =' scorm_item_section ';
  2571. }
  2572. }
  2573. if ($scorm_color_background != '') {
  2574. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . '">';
  2575. }
  2576. // The anchor will let us center the TOC on the currently viewed item &^D
  2577. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2578. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 1.5) . 'em; padding-right:' . ($item['level'] / 2) . 'em" title="' . $item['description'] . '" >';
  2579. $html .= '<a name="atoc_' . $item['id'] . '" />';
  2580. } else {
  2581. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 2) . 'em; padding-right:' . ($item['level'] * 1.5) . 'em" title="' . $item['description'] . '" >';
  2582. }
  2583. $title = $item['title'];
  2584. if (empty ($title)) {
  2585. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2586. }
  2587. $title = Security::remove_XSS($title);
  2588. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2589. //$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>" ;
  2590. $url = $this->get_link('http', $item['id'], $toc_list);
  2591. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2592. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2593. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2594. $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2595. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2596. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;" . stripslashes($title);
  2597. } elseif ($item['type'] == 'dir') {
  2598. $html .= stripslashes($title);
  2599. }
  2600. /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2601. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2602. $user_id = api_get_user_id();
  2603. $sql = "SELECT path FROM $tbl_track_e_exercises, $tbl_lp_item
  2604. WHERE c_id = $course_id AND
  2605. path = '" . $item['path'] . "' AND
  2606. exe_user_id = '$user_id' AND
  2607. exe_cours_id = '$course_code' AND
  2608. path = exe_exo_id AND
  2609. status <> 'incomplete'";
  2610. $result = Database::query($sql);
  2611. $count = Database :: num_rows($result);*/
  2612. if ($item['type'] == 'quiz') {
  2613. if ($item['status'] == 'completed') {
  2614. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2615. }
  2616. } else {
  2617. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2618. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2619. }
  2620. }
  2621. $html .= "</div>";
  2622. if ($scorm_color_background != '') {
  2623. $html .= '</div>';
  2624. }
  2625. $color_counter++;
  2626. }
  2627. $html .= "</div>";
  2628. return $html;
  2629. }
  2630. /**
  2631. * Gets the learnpath maker name - generally the editor's name
  2632. * @return string Learnpath maker name
  2633. */
  2634. public function get_maker() {
  2635. if ($this->debug > 0) {
  2636. error_log('New LP - In learnpath::get_maker()', 0);
  2637. }
  2638. if (!empty ($this->maker)) {
  2639. return $this->maker;
  2640. } else {
  2641. return '';
  2642. }
  2643. }
  2644. /**
  2645. * Gets the user-friendly message stored in $this->message
  2646. * @return string Message
  2647. */
  2648. public function get_message() {
  2649. if ($this->debug > 0) {
  2650. error_log('New LP - In learnpath::get_message()', 0);
  2651. }
  2652. return $this->message;
  2653. }
  2654. /**
  2655. * Gets the learnpath name/title
  2656. * @return string Learnpath name/title
  2657. */
  2658. public function get_name() {
  2659. if ($this->debug > 0) {
  2660. error_log('New LP - In learnpath::get_name()', 0);
  2661. }
  2662. if (!empty ($this->name)) {
  2663. return $this->name;
  2664. } else {
  2665. return 'N/A';
  2666. }
  2667. }
  2668. /**
  2669. * Gets a link to the resource from the present location, depending on item ID.
  2670. * @param string Type of link expected
  2671. * @param integer Learnpath item ID
  2672. * @return string Link to the lp_item resource
  2673. */
  2674. public function get_link($type = 'http', $item_id = null, $provided_toc = false) {
  2675. $course_id = api_get_course_int_id();
  2676. if ($this->debug > 0) {
  2677. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  2678. }
  2679. if (empty($item_id)) {
  2680. if ($this->debug > 2) {
  2681. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  2682. }
  2683. $item_id = $this->get_current_item_id();
  2684. }
  2685. if (empty($item_id)) {
  2686. if ($this->debug > 2) {
  2687. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  2688. }
  2689. //still empty, this means there was no item_id given and we are not in an object context or
  2690. //the object property is empty, return empty link
  2691. $item_id = $this->first();
  2692. return '';
  2693. }
  2694. $file = '';
  2695. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  2696. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  2697. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2698. $item_id = Database::escape_string($item_id);
  2699. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  2700. FROM $lp_table l, $lp_item_table li
  2701. WHERE l.c_id = $course_id AND
  2702. li.c_id = $course_id AND
  2703. li.id = $item_id AND
  2704. li.lp_id = l.id";
  2705. if ($this->debug > 2) {
  2706. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  2707. }
  2708. $res = Database::query($sql);
  2709. if (Database :: num_rows($res) > 0) {
  2710. $row = Database :: fetch_array($res);
  2711. $lp_type = $row['ltype'];
  2712. $lp_path = $row['lpath'];
  2713. $lp_item_type = $row['litype'];
  2714. $lp_item_path = $row['lipath'];
  2715. $lp_item_params = $row['liparams'];
  2716. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  2717. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  2718. }
  2719. //$lp_item_params = '?'.$lp_item_params;
  2720. //add ? if none - left commented to give freedom to scorm implementation
  2721. //if(substr($lp_item_params,0,1)!='?'){
  2722. // $lp_item_params = '?'.$lp_item_params;
  2723. //}
  2724. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  2725. if ($type == 'http') {
  2726. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  2727. } else {
  2728. $course_path = $sys_course_path; //system path
  2729. }
  2730. // 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.
  2731. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  2732. $lp_type = 1;
  2733. }
  2734. if ($this->debug > 2) {
  2735. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  2736. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  2737. }
  2738. // Now go through the specific cases to get the end of the path.
  2739. // @todo Use constants instead of int values.
  2740. switch ($lp_type) {
  2741. case 1 :
  2742. if ($lp_item_type == 'dokeos_chapter') {
  2743. $file = 'lp_content.php?type=dir';
  2744. } else {
  2745. require_once 'resourcelinker.inc.php';
  2746. $file = rl_get_resource_link_for_learnpath(api_get_course_id(), $this->get_id(), $item_id);
  2747. if ($this->debug > 0) {
  2748. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  2749. }
  2750. if ($lp_item_type == 'link') {
  2751. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  2752. if (is_youtube_link($file)) {
  2753. $src = get_youtube_video_id($file);
  2754. $file = 'embed.php?type=youtube&src='.$src;
  2755. }
  2756. } else {
  2757. // check how much attempts of a exercise exits in lp
  2758. $lp_item_id = $this->get_current_item_id();
  2759. $lp_view_id = $this->get_view_id();
  2760. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  2761. if (empty($provided_toc)) {
  2762. if ($this->debug > 0) {
  2763. error_log('In learnpath::get_link() Loading get_toc ', 0);
  2764. }
  2765. $list = $this->get_toc();
  2766. } else {
  2767. if ($this->debug > 0) {
  2768. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  2769. }
  2770. $list = $provided_toc;
  2771. }
  2772. $type_quiz = false;
  2773. foreach ($list as $toc) {
  2774. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  2775. $type_quiz = true;
  2776. }
  2777. }
  2778. if ($type_quiz) {
  2779. $lp_item_id = Database::escape_string($lp_item_id);
  2780. $lp_view_id = Database::escape_string($lp_view_id);
  2781. $sql = "SELECT count(*) FROM $lp_item_view_table
  2782. WHERE c_id = $course_id AND lp_item_id='" . (int) $lp_item_id . "' AND lp_view_id ='" . (int) $lp_view_id . "' AND status='completed'";
  2783. $result = Database::query($sql);
  2784. $row_count = Database :: fetch_row($result);
  2785. $count_item_view = (int) $row_count[0];
  2786. $not_multiple_attempt = 0;
  2787. if ($prevent_reinit === 1 && $count_item_view > 0) {
  2788. $not_multiple_attempt = 1;
  2789. }
  2790. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  2791. }
  2792. $tmp_array = explode('/', $file);
  2793. $document_name = $tmp_array[count($tmp_array) - 1];
  2794. if (strpos($document_name, '_DELETED_')) {
  2795. $file = 'blank.php?error=document_deleted';
  2796. }
  2797. }
  2798. }
  2799. break;
  2800. case 2 :
  2801. if ($this->debug > 2) {
  2802. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2803. }
  2804. if ($lp_item_type != 'dir') {
  2805. // Quite complex here:
  2806. // We want to make sure 'http://' (and similar) links can
  2807. // be loaded as is (withouth the Chamilo path in front) but
  2808. // some contents use this form: resource.htm?resource=http://blablabla
  2809. // which means we have to find a protocol at the path's start, otherwise
  2810. // it should not be considered as an external URL.
  2811. //if ($this->prerequisites_match($item_id)) {
  2812. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2813. if ($this->debug > 2) {
  2814. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2815. }
  2816. // Distant url, return as is.
  2817. $file = $lp_item_path;
  2818. } else {
  2819. if ($this->debug > 2) {
  2820. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2821. }
  2822. // Prevent getting untranslatable urls.
  2823. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2824. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2825. // Prepare the path.
  2826. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2827. // TODO: Fix this for urls with protocol header.
  2828. $file = str_replace('//', '/', $file);
  2829. $file = str_replace(':/', '://', $file);
  2830. if (substr($lp_path, -1) == '/') {
  2831. $lp_path = substr($lp_path, 0, -1);
  2832. }
  2833. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  2834. // if file not found.
  2835. $decoded = html_entity_decode($lp_item_path);
  2836. list ($decoded) = explode('?', $decoded);
  2837. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  2838. require_once 'resourcelinker.inc.php';
  2839. $file = rl_get_resource_link_for_learnpath(api_get_course_id(), $this->get_id(), $item_id);
  2840. if (empty($file)) {
  2841. $file = 'blank.php?error=document_not_found';
  2842. } else {
  2843. $tmp_array = explode('/', $file);
  2844. $document_name = $tmp_array[count($tmp_array) - 1];
  2845. if (strpos($document_name, '_DELETED_')) {
  2846. $file = 'blank.php?error=document_deleted';
  2847. } else {
  2848. $file = 'blank.php?error=document_not_found';
  2849. }
  2850. }
  2851. } else {
  2852. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  2853. }
  2854. }
  2855. }
  2856. //}else{
  2857. //prerequisites did not match
  2858. //$file = 'blank.php';
  2859. //}
  2860. // We want to use parameters if they were defined in the imsmanifest
  2861. if (strpos($file, 'blank.php') === false) {
  2862. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  2863. }
  2864. } else {
  2865. $file = 'lp_content.php?type=dir';
  2866. }
  2867. break;
  2868. case 3 :
  2869. if ($this->debug > 2) {
  2870. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2871. }
  2872. // Formatting AICC HACP append URL.
  2873. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  2874. if ($lp_item_type != 'dir') {
  2875. // Quite complex here:
  2876. // We want to make sure 'http://' (and similar) links can
  2877. // be loaded as is (withouth the Chamilo path in front) but
  2878. // some contents use this form: resource.htm?resource=http://blablabla
  2879. // which means we have to find a protocol at the path's start, otherwise
  2880. // it should not be considered as an external URL.
  2881. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2882. if ($this->debug > 2) {
  2883. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2884. }
  2885. // Distant url, return as is.
  2886. $file = $lp_item_path;
  2887. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  2888. /*
  2889. if (stristr($file,'<servername>') !== false) {
  2890. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  2891. }
  2892. */
  2893. if (stripos($file, '<servername>') !== false) {
  2894. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  2895. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  2896. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  2897. }
  2898. //
  2899. $file .= $aicc_append;
  2900. } else {
  2901. if ($this->debug > 2) {
  2902. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2903. }
  2904. // Prevent getting untranslatable urls.
  2905. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2906. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2907. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  2908. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2909. // TODO: Fix this for urls with protocol header.
  2910. $file = str_replace('//', '/', $file);
  2911. $file = str_replace(':/', '://', $file);
  2912. $file .= $aicc_append;
  2913. }
  2914. } else {
  2915. $file = 'lp_content.php?type=dir';
  2916. }
  2917. break;
  2918. case 4 :
  2919. break;
  2920. default :
  2921. break;
  2922. }
  2923. }
  2924. if ($this->debug > 2) {
  2925. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  2926. }
  2927. return $file;
  2928. }
  2929. /**
  2930. * Gets the latest usable view or generate a new one
  2931. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  2932. * @return integer DB lp_view id
  2933. */
  2934. public function get_view($attempt_num = 0) {
  2935. if ($this->debug > 0) {
  2936. error_log('New LP - In learnpath::get_view()', 0);
  2937. }
  2938. $search = '';
  2939. // Use $attempt_num to enable multi-views management (disabled so far).
  2940. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  2941. $search = 'AND view_count = ' . $attempt_num;
  2942. }
  2943. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  2944. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  2945. $course_id = api_get_course_int_id();
  2946. $sql = "SELECT id, view_count FROM $lp_view_table
  2947. WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() ." AND user_id = " . $this->get_user_id() . " " .$search .
  2948. " ORDER BY view_count DESC";
  2949. $res = Database::query($sql);
  2950. if (Database :: num_rows($res) > 0) {
  2951. $row = Database :: fetch_array($res);
  2952. $this->lp_view_id = $row['id'];
  2953. } else {
  2954. // There is no database record, create one.
  2955. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  2956. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ",1)";
  2957. $res = Database::query($sql);
  2958. $id = Database :: insert_id();
  2959. $this->lp_view_id = $id;
  2960. }
  2961. return $this->lp_view_id;
  2962. }
  2963. /**
  2964. * Gets the current view id
  2965. * @return integer View ID (from lp_view)
  2966. */
  2967. public function get_view_id() {
  2968. if ($this->debug > 0) {
  2969. error_log('New LP - In learnpath::get_view_id()', 0);
  2970. }
  2971. if (!empty ($this->lp_view_id)) {
  2972. return $this->lp_view_id;
  2973. } else {
  2974. return 0;
  2975. }
  2976. }
  2977. /**
  2978. * Gets the update queue
  2979. * @return array Array containing IDs of items to be updated by JavaScript
  2980. */
  2981. public function get_update_queue() {
  2982. if ($this->debug > 0) {
  2983. error_log('New LP - In learnpath::get_update_queue()', 0);
  2984. }
  2985. return $this->update_queue;
  2986. }
  2987. /**
  2988. * Gets the user ID
  2989. * @return integer User ID
  2990. */
  2991. public function get_user_id() {
  2992. if ($this->debug > 0) {
  2993. error_log('New LP - In learnpath::get_user_id()', 0);
  2994. }
  2995. if (!empty ($this->user_id)) {
  2996. return $this->user_id;
  2997. } else {
  2998. return false;
  2999. }
  3000. }
  3001. /**
  3002. * Checks if any of the items has an audio element attached
  3003. * @return bool True or false
  3004. */
  3005. public function has_audio() {
  3006. if ($this->debug > 1) {
  3007. error_log('New LP - In learnpath::has_audio()', 0);
  3008. }
  3009. $has = false;
  3010. foreach ($this->items as $i => $item) {
  3011. if (!empty ($this->items[$i]->audio)) {
  3012. $has = true;
  3013. break;
  3014. }
  3015. }
  3016. return $has;
  3017. }
  3018. /**
  3019. * Logs a message into a file
  3020. * @param string Message to log
  3021. * @return boolean True on success, false on error or if msg empty
  3022. */
  3023. public function log($msg) {
  3024. if ($this->debug > 0) {
  3025. error_log('New LP - In learnpath::log()', 0);
  3026. }
  3027. // TODO
  3028. $this->error .= $msg;
  3029. return true;
  3030. }
  3031. /**
  3032. * Moves an item up and down at its level
  3033. * @param integer Item to move up and down
  3034. * @param string Direction 'up' or 'down'
  3035. * @return integer New display order, or false on error
  3036. */
  3037. public function move_item($id, $direction) {
  3038. $course_id = api_get_course_int_id();
  3039. if ($this->debug > 0) {
  3040. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3041. }
  3042. if (empty ($id) or empty ($direction)) {
  3043. return false;
  3044. }
  3045. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3046. $sql_sel = "SELECT *
  3047. FROM " . $tbl_lp_item . "
  3048. WHERE c_id = ".$course_id." AND id = " . $id;
  3049. $res_sel = Database::query($sql_sel);
  3050. // Check if elem exists.
  3051. if (Database :: num_rows($res_sel) < 1) {
  3052. return false;
  3053. }
  3054. // Gather data.
  3055. $row = Database :: fetch_array($res_sel);
  3056. $previous = $row['previous_item_id'];
  3057. $next = $row['next_item_id'];
  3058. $display = $row['display_order'];
  3059. $parent = $row['parent_item_id'];
  3060. $lp = $row['lp_id'];
  3061. // Update the item (switch with previous/next one).
  3062. switch ($direction) {
  3063. case 'up' :
  3064. if ($this->debug > 2) {
  3065. error_log('Movement up detected', 0);
  3066. }
  3067. if ($display <= 1) { /*do nothing*/
  3068. } else {
  3069. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3070. WHERE c_id = ".$course_id." AND id = $previous";
  3071. if ($this->debug > 2) {
  3072. error_log('Selecting previous: ' . $sql_sel2, 0);
  3073. }
  3074. $res_sel2 = Database::query($sql_sel2);
  3075. if (Database :: num_rows($res_sel2) < 1) {
  3076. $previous_previous = 0;
  3077. }
  3078. // Gather data.
  3079. $row2 = Database :: fetch_array($res_sel2);
  3080. $previous_previous = $row2['previous_item_id'];
  3081. // Update previous_previous item (switch "next" with current).
  3082. if ($previous_previous != 0) {
  3083. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3084. if ($this->debug > 2) {
  3085. error_log($sql_upd2, 0);
  3086. }
  3087. $res_upd2 = Database::query($sql_upd2);
  3088. }
  3089. // Update previous item (switch with current).
  3090. if ($previous != 0) {
  3091. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3092. WHERE c_id = ".$course_id." AND id = $previous";
  3093. if ($this->debug > 2) {
  3094. error_log($sql_upd2, 0);
  3095. }
  3096. $res_upd2 = Database::query($sql_upd2);
  3097. }
  3098. // Update current item (switch with previous).
  3099. if ($id != 0) {
  3100. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3101. WHERE c_id = ".$course_id." AND id = $id";
  3102. if ($this->debug > 2) {
  3103. error_log($sql_upd2, 0);
  3104. }
  3105. $res_upd2 = Database::query($sql_upd2);
  3106. }
  3107. // Update next item (new previous item).
  3108. if ($next != 0) {
  3109. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3110. WHERE c_id = ".$course_id." AND id = $next";
  3111. if ($this->debug > 2) {
  3112. error_log($sql_upd2, 0);
  3113. }
  3114. $res_upd2 = Database::query($sql_upd2);
  3115. }
  3116. $display = $display -1;
  3117. }
  3118. break;
  3119. case 'down' :
  3120. if ($this->debug > 2) {
  3121. error_log('Movement down detected', 0);
  3122. }
  3123. if ($next == 0) { /* Do nothing. */
  3124. } else {
  3125. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3126. if ($this->debug > 2) {
  3127. error_log('Selecting next: ' . $sql_sel2, 0);
  3128. }
  3129. $res_sel2 = Database::query($sql_sel2);
  3130. if (Database :: num_rows($res_sel2) < 1) {
  3131. $next_next = 0;
  3132. }
  3133. // Gather data.
  3134. $row2 = Database :: fetch_array($res_sel2);
  3135. $next_next = $row2['next_item_id'];
  3136. // Update previous item (switch with current).
  3137. if ($previous != 0) {
  3138. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3139. WHERE c_id = ".$course_id." AND id = $previous";
  3140. $res_upd2 = Database::query($sql_upd2);
  3141. }
  3142. // Update current item (switch with previous).
  3143. if ($id != 0) {
  3144. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3145. WHERE c_id = ".$course_id." AND id = $id";
  3146. $res_upd2 = Database::query($sql_upd2);
  3147. }
  3148. // Update next item (new previous item).
  3149. if ($next != 0) {
  3150. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3151. WHERE c_id = ".$course_id." AND id = $next";
  3152. $res_upd2 = Database::query($sql_upd2);
  3153. }
  3154. // Update next_next item (switch "previous" with current).
  3155. if ($next_next != 0) {
  3156. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3157. WHERE c_id = ".$course_id." AND id = $next_next";
  3158. $res_upd2 = Database::query($sql_upd2);
  3159. }
  3160. $display = $display +1;
  3161. }
  3162. break;
  3163. default :
  3164. return false;
  3165. }
  3166. return $display;
  3167. }
  3168. /**
  3169. * Move a learnpath up (display_order)
  3170. * @param integer Learnpath ID
  3171. */
  3172. public function move_up($lp_id) {
  3173. $course_id = api_get_course_int_id();
  3174. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3175. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3176. $res = Database::query($sql);
  3177. if ($res === false)
  3178. return false;
  3179. $lps = array ();
  3180. $lp_order = array ();
  3181. $num = Database :: num_rows($res);
  3182. // First check the order is correct, globally (might be wrong because
  3183. // of versions < 1.8.4)
  3184. if ($num > 0) {
  3185. $i = 1;
  3186. while ($row = Database :: fetch_array($res)) {
  3187. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3188. $need_fix = true;
  3189. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3190. $res_u = Database::query($sql_u);
  3191. }
  3192. $row['display_order'] = $i;
  3193. $lps[$row['id']] = $row;
  3194. $lp_order[$i] = $row['id'];
  3195. $i++;
  3196. }
  3197. }
  3198. if ($num > 1) { // If there's only one element, no need to sort.
  3199. $order = $lps[$lp_id]['display_order'];
  3200. if ($order > 1) { // If it's the first element, no need to move up.
  3201. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3202. $res_u1 = Database::query($sql_u1);
  3203. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . " WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3204. $res_u2 = Database::query($sql_u2);
  3205. }
  3206. }
  3207. }
  3208. /**
  3209. * Move a learnpath down (display_order)
  3210. * @param integer Learnpath ID
  3211. */
  3212. public function move_down($lp_id) {
  3213. $course_id = api_get_course_int_id();
  3214. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3215. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3216. $res = Database::query($sql);
  3217. if ($res === false)
  3218. return false;
  3219. $lps = array ();
  3220. $lp_order = array ();
  3221. $num = Database :: num_rows($res);
  3222. $max = 0;
  3223. // First check the order is correct, globally (might be wrong because
  3224. // of versions < 1.8.4).
  3225. if ($num > 0) {
  3226. $i = 1;
  3227. while ($row = Database :: fetch_array($res)) {
  3228. $max = $i;
  3229. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3230. $need_fix = true;
  3231. $sql_u = "UPDATE $lp_table SET display_order = $i
  3232. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3233. $res_u = Database::query($sql_u);
  3234. }
  3235. $row['display_order'] = $i;
  3236. $lps[$row['id']] = $row;
  3237. $lp_order[$i] = $row['id'];
  3238. $i++;
  3239. }
  3240. }
  3241. if ($num > 1) { // If there's only one element, no need to sort.
  3242. $order = $lps[$lp_id]['display_order'];
  3243. if ($order < $max) { // If it's the first element, no need to move up.
  3244. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3245. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3246. $res_u1 = Database::query($sql_u1);
  3247. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3248. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3249. $res_u2 = Database::query($sql_u2);
  3250. }
  3251. }
  3252. }
  3253. /**
  3254. * Updates learnpath attributes to point to the next element
  3255. * The last part is similar to set_current_item but processing the other way around
  3256. */
  3257. public function next() {
  3258. if ($this->debug > 0) {
  3259. error_log('New LP - In learnpath::next()', 0);
  3260. }
  3261. $this->last = $this->get_current_item_id();
  3262. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3263. $this->autocomplete_parents($this->last);
  3264. $new_index = $this->get_next_index();
  3265. if ($this->debug > 2) {
  3266. error_log('New LP - New index: ' . $new_index, 0);
  3267. }
  3268. $this->index = $new_index;
  3269. if ($this->debug > 2) {
  3270. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3271. }
  3272. $this->current = $this->ordered_items[$new_index];
  3273. if ($this->debug > 2) {
  3274. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3275. }
  3276. }
  3277. /**
  3278. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3279. * class, this might be redefined to allow several behaviours depending on the document type.
  3280. * @param integer Resource ID
  3281. * @return boolean True on success, false otherwise
  3282. */
  3283. public function open($id) {
  3284. if ($this->debug > 0) {
  3285. error_log('New LP - In learnpath::open()', 0);
  3286. }
  3287. // TODO:
  3288. // set the current resource attribute to this resource
  3289. // switch on element type (redefine in child class?)
  3290. // set status for this item to "opened"
  3291. // start timer
  3292. // initialise score
  3293. $this->index = 0; //or = the last item seen (see $this->last)
  3294. }
  3295. /**
  3296. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3297. * and the prerequisite string on error.
  3298. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3299. * @param integer Optional item ID. If none given, uses the current open item.
  3300. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3301. */
  3302. public function prerequisites_match($item = null) {
  3303. if ($this->debug > 0) {
  3304. error_log('New LP - In learnpath::prerequisites_match()', 0);
  3305. }
  3306. if (empty ($item)) {
  3307. $item = $this->current;
  3308. }
  3309. if (is_object($this->items[$item])) {
  3310. $prereq_string = $this->items[$item]->get_prereq_string();
  3311. if (empty ($prereq_string)) {
  3312. return true;
  3313. }
  3314. // Clean spaces.
  3315. $prereq_string = str_replace(' ', '', $prereq_string);
  3316. if ($this->debug > 0) {
  3317. error_log('Found prereq_string: ' . $prereq_string, 0);
  3318. }
  3319. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3320. $result = $this->items[$item]->parse_prereq($prereq_string, $this->items, $this->refs_list, $this->get_user_id());
  3321. if ($result === false) {
  3322. $this->set_error_msg($this->items[$item]->prereq_alert);
  3323. }
  3324. } else {
  3325. $result = true;
  3326. if ($this->debug > 1) {
  3327. error_log('New LP - $this->items[' . $item . '] was not an object', 0);
  3328. }
  3329. }
  3330. if ($this->debug > 1) {
  3331. error_log('New LP - End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3332. }
  3333. return $result;
  3334. }
  3335. /**
  3336. * Updates learnpath attributes to point to the previous element
  3337. * The last part is similar to set_current_item but processing the other way around
  3338. */
  3339. public function previous() {
  3340. if ($this->debug > 0) {
  3341. error_log('New LP - In learnpath::previous()', 0);
  3342. }
  3343. $this->last = $this->get_current_item_id();
  3344. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3345. $this->autocomplete_parents($this->last);
  3346. $new_index = $this->get_previous_index();
  3347. $this->index = $new_index;
  3348. $this->current = $this->ordered_items[$new_index];
  3349. }
  3350. /**
  3351. * Publishes a learnpath. This basically means show or hide the learnpath
  3352. * to normal users.
  3353. * Can be used as abstract
  3354. * @param integer Learnpath ID
  3355. * @param string New visibility
  3356. */
  3357. public function toggle_visibility($lp_id, $set_visibility = 1) {
  3358. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3359. $action = 'visible';
  3360. if ($set_visibility != 1) {
  3361. $action = 'invisible';
  3362. }
  3363. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3364. }
  3365. /**
  3366. * Publishes a learnpath. This basically means show or hide the learnpath
  3367. * on the course homepage
  3368. * Can be used as abstract
  3369. * @param integer Learnpath id
  3370. * @param string New visibility (v/s - visible/invisible)
  3371. */
  3372. public static function toggle_publish($lp_id, $set_visibility = 'v') {
  3373. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_publish()', 0); }
  3374. $course_id = api_get_course_int_id();
  3375. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3376. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3377. $result = Database::query($sql);
  3378. $row = Database :: fetch_array($result);
  3379. $name = domesticate($row['name']);
  3380. if ($set_visibility == 'i') {
  3381. $s = $name . " " . get_lang('LearnpathNotPublished');
  3382. $dialogBox = $s;
  3383. $v = 0;
  3384. }
  3385. if ($set_visibility == 'v') {
  3386. $s = $name . " " . get_lang('LearnpathPublished');
  3387. $dialogBox = $s;
  3388. $v = 1;
  3389. }
  3390. $session_id = api_get_session_id();
  3391. $session_condition = api_get_session_condition($session_id);
  3392. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3393. $course_id = api_get_course_int_id();
  3394. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  3395. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND name='$name' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3396. $result = Database::query($sql);
  3397. $num = Database :: num_rows($result);
  3398. $row2 = Database :: fetch_array($result);
  3399. //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
  3400. if (($set_visibility == 'i') && ($num > 0)) {
  3401. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (name='$name' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition)";
  3402. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3403. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3404. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3405. } else {
  3406. // Parameter and database incompatible, do nothing.
  3407. }
  3408. $result = Database::query($sql);
  3409. //if ($this->debug > 2) { error_log('New LP - Leaving learnpath::toggle_visibility: '.$sql, 0); }
  3410. }
  3411. /**
  3412. * Restart the whole learnpath. Return the URL of the first element.
  3413. * Make sure the results are saved with anoter method. This method should probably be
  3414. * redefined in children classes.
  3415. * To use a similar method statically, use the create_new_attempt() method
  3416. * @return string URL to load in the viewer
  3417. */
  3418. public function restart() {
  3419. if ($this->debug > 0) {
  3420. error_log('New LP - In learnpath::restart()', 0);
  3421. }
  3422. // TODO
  3423. // Call autosave method to save the current progress.
  3424. //$this->index = 0;
  3425. $session_id = api_get_session_id();
  3426. $course_id = api_get_course_int_id();
  3427. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3428. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
  3429. "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3430. if ($this->debug > 2) {
  3431. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3432. }
  3433. $res = Database::query($sql);
  3434. if ($view_id = Database :: insert_id($res)) {
  3435. $this->lp_view_id = $view_id;
  3436. $this->attempt = $this->attempt + 1;
  3437. } else {
  3438. $this->error = 'Could not insert into item_view table...';
  3439. return false;
  3440. }
  3441. $this->autocomplete_parents($this->current);
  3442. foreach ($this->items as $index => $dummy) {
  3443. $this->items[$index]->restart();
  3444. $this->items[$index]->set_lp_view($this->lp_view_id);
  3445. }
  3446. $this->first();
  3447. return true;
  3448. }
  3449. /**
  3450. * Saves the current item
  3451. * @return boolean
  3452. */
  3453. public function save_current() {
  3454. if ($this->debug > 0) {
  3455. error_log('New LP - In learnpath::save_current()', 0);
  3456. }
  3457. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3458. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3459. if ($this->debug > 2) {
  3460. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3461. }
  3462. if ($this->debug > 2) {
  3463. error_log('' . print_r($this->items, true), 0);
  3464. }
  3465. if (is_object($this->items[$this->current])) {
  3466. //$res = $this->items[$this->current]->save(false);
  3467. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3468. $this->autocomplete_parents($this->current);
  3469. $status = $this->items[$this->current]->get_status();
  3470. $this->append_message('new_item_status: ' . $status);
  3471. $this->update_queue[$this->current] = $status;
  3472. return $res;
  3473. }
  3474. return false;
  3475. }
  3476. /**
  3477. * Saves the given item
  3478. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3479. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3480. * @return boolean
  3481. */
  3482. public function save_item($item_id = null, $from_outside = true) {
  3483. $course_id = api_get_course_int_id();
  3484. if ($this->debug > 0) {
  3485. error_log('New LP - In learnpath::save_item(' . $item_id . ',' . $from_outside . ')', 0);
  3486. }
  3487. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3488. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3489. if (empty ($item_id)) {
  3490. $item_id = Database::escape_string($_REQUEST['id']);
  3491. }
  3492. if (empty ($item_id)) {
  3493. $item_id = $this->get_current_item_id();
  3494. }
  3495. if ($this->debug > 2) {
  3496. error_log('New LP - save_current() saving item ' . $item_id, 0);
  3497. }
  3498. if (is_object($this->items[$item_id])) {
  3499. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3500. //$res = $this->items[$item_id]->save($from_outside);
  3501. $this->autocomplete_parents($item_id);
  3502. $status = $this->items[$item_id]->get_status();
  3503. $this->append_message('new_item_status: ' . $status);
  3504. $this->update_queue[$item_id] = $status;
  3505. return $res;
  3506. }
  3507. return false;
  3508. }
  3509. /**
  3510. * Saves the last item seen's ID only in case
  3511. */
  3512. public function save_last() {
  3513. $course_id = api_get_course_int_id();
  3514. if ($this->debug > 0) {
  3515. error_log('New LP - In learnpath::save_last()', 0);
  3516. }
  3517. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3518. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3519. if (isset ($this->current)) {
  3520. if ($this->debug > 2) {
  3521. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3522. }
  3523. $sql = "UPDATE $table SET last_item = " . Database::escape_string($this->get_current_item_id()). " " .
  3524. "WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() . " AND user_id = " . $this->get_user_id().' '.$session_condition;
  3525. if ($this->debug > 2) {
  3526. error_log('New LP - Saving last item seen : ' . $sql, 0);
  3527. }
  3528. $res = Database::query($sql);
  3529. }
  3530. // Save progress.
  3531. list($progress, $text) = $this->get_progress_bar_text('%');
  3532. if ($progress >= 0 && $progress <= 100) {
  3533. $progress = (int) $progress;
  3534. $sql = "UPDATE $table SET progress = $progress " .
  3535. "WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() . " AND " .
  3536. "user_id = " . $this->get_user_id().' '.$session_condition;
  3537. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3538. $this->progress_db = $progress;
  3539. }
  3540. }
  3541. /**
  3542. * Sets the current item ID (checks if valid and authorized first)
  3543. * @param integer New item ID. If not given or not authorized, defaults to current
  3544. */
  3545. public function set_current_item($item_id = null) {
  3546. if ($this->debug > 0) {
  3547. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  3548. }
  3549. if (empty ($item_id)) {
  3550. if ($this->debug > 2) {
  3551. error_log('New LP - No new current item given, ignore...', 0);
  3552. }
  3553. // Do nothing.
  3554. } else {
  3555. if ($this->debug > 2) {
  3556. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  3557. }
  3558. if (is_numeric($item_id)) {
  3559. $item_id = Database::escape_string($item_id);
  3560. // TODO: Check in database here.
  3561. $this->last = $this->current;
  3562. $this->current = $item_id;
  3563. // TODO: Update $this->index as well.
  3564. foreach ($this->ordered_items as $index => $item) {
  3565. if ($item == $this->current) {
  3566. $this->index = $index;
  3567. break;
  3568. }
  3569. }
  3570. if ($this->debug > 2) {
  3571. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  3572. }
  3573. } else {
  3574. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  3575. }
  3576. }
  3577. }
  3578. /**
  3579. * Sets the encoding
  3580. * @param string New encoding
  3581. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3582. */
  3583. public function set_encoding($enc = 'UTF-8') {
  3584. if ($this->debug > 0) {
  3585. error_log('New LP - In learnpath::set_encoding()', 0);
  3586. }
  3587. $course_id = api_get_course_int_id();
  3588. /* // Deprecated code (Chamilo 1.8.8).
  3589. $enc = strtoupper($enc);
  3590. $encodings = array (
  3591. 'UTF-8',
  3592. 'ISO-8859-1',
  3593. 'ISO-8859-15',
  3594. 'cp1251',
  3595. 'cp1252',
  3596. 'KOI8-R',
  3597. 'BIG5',
  3598. 'GB2312',
  3599. 'Shift_JIS',
  3600. 'EUC-JP',
  3601. ''
  3602. );
  3603. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  3604. $lp = $this->get_id();
  3605. if ($lp != 0) {
  3606. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3607. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  3608. $res = Database::query($sql);
  3609. return $res;
  3610. }
  3611. }
  3612. return false;
  3613. */
  3614. $enc = api_refine_encoding_id($enc);
  3615. if (empty($enc)) {
  3616. $enc = api_get_system_encoding();
  3617. }
  3618. if (api_is_encoding_supported($enc)) {
  3619. $lp = $this->get_id();
  3620. if ($lp != 0) {
  3621. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3622. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  3623. $res = Database::query($sql);
  3624. return $res;
  3625. }
  3626. }
  3627. return false;
  3628. }
  3629. /**
  3630. * Sets the JS lib setting in the database directly.
  3631. * This is the JavaScript library file this lp needs to load on startup
  3632. * @param string Proximity setting
  3633. * @return boolean True on update success. False otherwise.
  3634. */
  3635. public function set_jslib($lib = '') {
  3636. if ($this->debug > 0) {
  3637. error_log('New LP - In learnpath::set_jslib()', 0);
  3638. }
  3639. $lp = $this->get_id();
  3640. $course_id = api_get_course_int_id();
  3641. if ($lp != 0) {
  3642. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3643. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  3644. $res = Database::query($sql);
  3645. return $res;
  3646. } else {
  3647. return false;
  3648. }
  3649. }
  3650. /**
  3651. * Sets the name of the LP maker (publisher) (and save)
  3652. * @param string Optional string giving the new content_maker of this learnpath
  3653. * @return boolean True
  3654. */
  3655. public function set_maker($name = '') {
  3656. if ($this->debug > 0) {
  3657. error_log('New LP - In learnpath::set_maker()', 0);
  3658. }
  3659. if (empty ($name))
  3660. return false;
  3661. $this->maker = Database::escape_string($name);
  3662. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3663. $course_id = api_get_course_int_id();
  3664. $lp_id = $this->get_id();
  3665. $sql = "UPDATE $lp_table SET content_maker = '" . $this->maker . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3666. if ($this->debug > 2) {
  3667. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  3668. }
  3669. $res = Database::query($sql);
  3670. return true;
  3671. }
  3672. /**
  3673. * Sets the name of the current learnpath (and save)
  3674. * @param string Optional string giving the new name of this learnpath
  3675. * @return boolean True/False
  3676. */
  3677. public function set_name($name = '') {
  3678. if ($this->debug > 0) {
  3679. error_log('New LP - In learnpath::set_name()', 0);
  3680. }
  3681. if (empty ($name))
  3682. return false;
  3683. $this->name = Database::escape_string($name);
  3684. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3685. $lp_id = $this->get_id();
  3686. $course_id = api_get_course_int_id();
  3687. $sql = "UPDATE $lp_table SET name = '" . $this->name . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3688. if ($this->debug > 2) {
  3689. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  3690. }
  3691. $res = Database::query($sql);
  3692. // If the lp is visible on the homepage, change his name there.
  3693. if (Database::affected_rows()) {
  3694. $table = Database :: get_course_table(TABLE_TOOL_LIST);
  3695. $sql = 'UPDATE ' . $table . ' SET
  3696. name = "' . $this->name . '"
  3697. WHERE c_id = '.$course_id.' AND link = "newscorm/lp_controller.php?action=view&lp_id=' . $lp_id . '"';
  3698. Database::query($sql);
  3699. }
  3700. return true;
  3701. }
  3702. /**
  3703. * Set index specified prefix terms for all items in this path
  3704. * @param string Comma-separated list of terms
  3705. * @param char Xapian term prefix
  3706. * @return boolean False on error, true otherwise
  3707. */
  3708. public function set_terms_by_prefix($terms_string, $prefix) {
  3709. $course_id = api_get_course_int_id();
  3710. if (api_get_setting('search_enabled') !== 'true')
  3711. return false;
  3712. if (!extension_loaded('xapian')) {
  3713. return false;
  3714. }
  3715. $terms_string = trim($terms_string);
  3716. $terms = explode(',', $terms_string);
  3717. array_walk($terms, 'trim_value');
  3718. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  3719. // Don't do anything if no change, verify only at DB, not the search engine.
  3720. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  3721. return false;
  3722. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  3723. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  3724. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  3725. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  3726. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  3727. // TODO: Make query secure agains XSS : use member attr instead of post var.
  3728. $lp_id = intval($_POST['lp_id']);
  3729. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  3730. $result = Database::query($sql);
  3731. $di = new ChamiloIndexer();
  3732. while ($lp_item = Database :: fetch_array($result)) {
  3733. // Get search_did.
  3734. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  3735. $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';
  3736. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  3737. //echo $sql; echo '<br>';
  3738. $res = Database::query($sql);
  3739. if (Database::num_rows($res) > 0) {
  3740. $se_ref = Database :: fetch_array($res);
  3741. // Compare terms.
  3742. $doc = $di->get_document($se_ref['search_did']);
  3743. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  3744. $xterms = array ();
  3745. foreach ($xapian_terms as $xapian_term) {
  3746. $xterms[] = substr($xapian_term['name'], 1);
  3747. }
  3748. $dterms = $terms;
  3749. $missing_terms = array_diff($dterms, $xterms);
  3750. $deprecated_terms = array_diff($xterms, $dterms);
  3751. // Save it to search engine.
  3752. foreach ($missing_terms as $term) {
  3753. $doc->add_term($prefix . $term, 1);
  3754. }
  3755. foreach ($deprecated_terms as $term) {
  3756. $doc->remove_term($prefix . $term);
  3757. }
  3758. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  3759. $di->getDb()->flush();
  3760. } else {
  3761. //@todo What we should do here?
  3762. }
  3763. }
  3764. return true;
  3765. }
  3766. /**
  3767. * Sets the theme of the LP (local/remote) (and save)
  3768. * @param string Optional string giving the new theme of this learnpath
  3769. * @return bool Returns true if theme name is not empty
  3770. */
  3771. public function set_theme($name = '') {
  3772. $course_id = api_get_course_int_id();
  3773. if ($this->debug > 0) {
  3774. error_log('New LP - In learnpath::set_theme()', 0);
  3775. }
  3776. $this->theme = Database::escape_string($name);
  3777. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3778. $lp_id = $this->get_id();
  3779. $sql = "UPDATE $lp_table SET theme = '" . $this->theme . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3780. if ($this->debug > 2) {
  3781. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  3782. }
  3783. //$res = Database::query($sql);
  3784. $res = Database::query($sql);
  3785. return true;
  3786. }
  3787. /**
  3788. * Sets the image of an LP (and save)
  3789. * @param string Optional string giving the new image of this learnpath
  3790. * @return bool Returns true if theme name is not empty
  3791. */
  3792. public function set_preview_image($name = '') {
  3793. $course_id = api_get_course_int_id();
  3794. if ($this->debug > 0) {
  3795. error_log('New LP - In learnpath::set_preview_image()', 0);
  3796. }
  3797. $this->preview_image = Database::escape_string($name);
  3798. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3799. $lp_id = $this->get_id();
  3800. $sql = "UPDATE $lp_table SET preview_image = '" . $this->preview_image . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3801. if ($this->debug > 2) {
  3802. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  3803. }
  3804. $res = Database::query($sql);
  3805. return true;
  3806. }
  3807. /**
  3808. * Sets the author of a LP (and save)
  3809. * @param string Optional string giving the new author of this learnpath
  3810. * @return bool Returns true if author's name is not empty
  3811. */
  3812. public function set_author($name = '') {
  3813. $course_id = api_get_course_int_id();
  3814. if ($this->debug > 0) {
  3815. error_log('New LP - In learnpath::set_author()', 0);
  3816. }
  3817. $this->author = Database::escape_string($name);
  3818. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3819. $lp_id = $this->get_id();
  3820. $sql = "UPDATE $lp_table SET author = '" . $this->author . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3821. if ($this->debug > 2) {
  3822. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  3823. }
  3824. $res = Database::query($sql);
  3825. return true;
  3826. }
  3827. /**
  3828. * Sets the hide_toc_frame parameter of a LP (and save)
  3829. * @param int 1 if frame is hiddent 0 thenelse
  3830. * @return bool Returns true if author's name is not empty
  3831. */
  3832. public function set_hide_toc_frame($hide) {
  3833. $course_id = api_get_course_int_id();
  3834. if ($this->debug > 0) {
  3835. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  3836. }
  3837. if (intval($hide) == $hide){
  3838. $this->hide_toc_frame = $hide;
  3839. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3840. $lp_id = $this->get_id();
  3841. $sql = "UPDATE $lp_table SET hide_toc_frame = '" . $this->hide_toc_frame . "'
  3842. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3843. if ($this->debug > 2) {
  3844. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  3845. }
  3846. $res = Database::query($sql);
  3847. return true;
  3848. } else {
  3849. return false;
  3850. }
  3851. }
  3852. /**
  3853. * Sets the prerequisite of a LP (and save)
  3854. * @param int integer giving the new prerequisite of this learnpath
  3855. * @return bool returns true if prerequisite is not empty
  3856. */
  3857. public function set_prerequisite($prerequisite) {
  3858. $course_id = api_get_course_int_id();
  3859. if ($this->debug > 0) {
  3860. error_log('New LP - In learnpath::set_prerequisite()', 0);
  3861. }
  3862. $this->prerequisite = intval($prerequisite);
  3863. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3864. $lp_id = $this->get_id();
  3865. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  3866. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3867. if ($this->debug > 2) {
  3868. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  3869. }
  3870. $res = Database::query($sql);
  3871. return true;
  3872. }
  3873. /**
  3874. * Sets the location/proximity of the LP (local/remote) (and save)
  3875. * @param string Optional string giving the new location of this learnpath
  3876. * @return boolean True on success / False on error
  3877. */
  3878. public function set_proximity($name = '') {
  3879. $course_id = api_get_course_int_id();
  3880. if ($this->debug > 0) {
  3881. error_log('New LP - In learnpath::set_proximity()', 0);
  3882. }
  3883. if (empty ($name))
  3884. return false;
  3885. $this->proximity = Database::escape_string($name);
  3886. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3887. $lp_id = $this->get_id();
  3888. $sql = "UPDATE $lp_table SET content_local = '" . $this->proximity . "'
  3889. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3890. if ($this->debug > 2) {
  3891. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  3892. }
  3893. $res = Database::query($sql);
  3894. return true;
  3895. }
  3896. /**
  3897. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  3898. * @param integer DB ID of the item
  3899. */
  3900. public function set_previous_item($id) {
  3901. if ($this->debug > 0) {
  3902. error_log('New LP - In learnpath::set_previous_item()', 0);
  3903. }
  3904. $this->last = $id;
  3905. }
  3906. /**
  3907. * Sets use_max_score
  3908. * @param string Optional string giving the new location of this learnpath
  3909. * @return boolean True on success / False on error
  3910. */
  3911. public function set_use_max_score($use_max_score = 1) {
  3912. $course_id = api_get_course_int_id();
  3913. if ($this->debug > 0) {
  3914. error_log('New LP - In learnpath::set_use_max_score()', 0);
  3915. }
  3916. $use_max_score = intval($use_max_score);
  3917. $this->use_max_score = $use_max_score;
  3918. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3919. $lp_id = $this->get_id();
  3920. $sql = "UPDATE $lp_table SET use_max_score = '" . $this->use_max_score . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3921. if ($this->debug > 2) {
  3922. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  3923. }
  3924. $res = Database::query($sql);
  3925. return true;
  3926. }
  3927. /**
  3928. * Sets and saves the expired_on date
  3929. * @param string Optional string giving the new author of this learnpath
  3930. * @return bool Returns true if author's name is not empty
  3931. */
  3932. public function set_expired_on($expired_on) {
  3933. $course_id = api_get_course_int_id();
  3934. if ($this->debug > 0) {
  3935. error_log('New LP - In learnpath::set_expired_on()', 0);
  3936. }
  3937. if (!empty($expired_on)) {
  3938. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  3939. } else {
  3940. $this->expired_on = '';
  3941. }
  3942. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3943. $lp_id = $this->get_id();
  3944. $sql = "UPDATE $lp_table SET expired_on = '" . $this->expired_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3945. if ($this->debug > 2) {
  3946. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  3947. }
  3948. $res = Database::query($sql);
  3949. return true;
  3950. }
  3951. /**
  3952. * Sets and saves the publicated_on date
  3953. * @param string Optional string giving the new author of this learnpath
  3954. * @return bool Returns true if author's name is not empty
  3955. */
  3956. public function set_publicated_on($publicated_on) {
  3957. $course_id = api_get_course_int_id();
  3958. if ($this->debug > 0) {
  3959. error_log('New LP - In learnpath::set_expired_on()', 0);
  3960. }
  3961. if (!empty($publicated_on)) {
  3962. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  3963. } else {
  3964. $this->publicated_on = '';
  3965. }
  3966. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3967. $lp_id = $this->get_id();
  3968. $sql = "UPDATE $lp_table SET publicated_on = '" . $this->publicated_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3969. if ($this->debug > 2) {
  3970. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  3971. }
  3972. $res = Database::query($sql);
  3973. return true;
  3974. }
  3975. /**
  3976. * Sets and saves the expired_on date
  3977. * @param string Optional string giving the new author of this learnpath
  3978. * @return bool Returns true if author's name is not empty
  3979. */
  3980. public function set_modified_on() {
  3981. $course_id = api_get_course_int_id();
  3982. if ($this->debug > 0) {
  3983. error_log('New LP - In learnpath::set_expired_on()', 0);
  3984. }
  3985. $this->modified_on = api_get_utc_datetime();
  3986. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3987. $lp_id = $this->get_id();
  3988. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3989. if ($this->debug > 2) {
  3990. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  3991. }
  3992. $res = Database::query($sql);
  3993. return true;
  3994. }
  3995. /**
  3996. * Sets the object's error message
  3997. * @param string Error message. If empty, reinits the error string
  3998. * @return void
  3999. */
  4000. public function set_error_msg($error = '') {
  4001. if ($this->debug > 0) {
  4002. error_log('New LP - In learnpath::set_error_msg()', 0);
  4003. }
  4004. if (empty ($error)) {
  4005. $this->error = '';
  4006. } else {
  4007. $this->error .= $error;
  4008. }
  4009. }
  4010. /**
  4011. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4012. * @param boolean Whether to allow a new attempt or not
  4013. * @return boolean True
  4014. */
  4015. public function start_current_item($allow_new_attempt = false) {
  4016. if ($this->debug > 0) {
  4017. error_log('New LP - In learnpath::start_current_item()', 0);
  4018. }
  4019. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4020. $type = $this->get_type();
  4021. $item_type = $this->items[$this->current]->get_type();
  4022. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4023. $this->items[$this->current]->open($allow_new_attempt);
  4024. $this->autocomplete_parents($this->current);
  4025. $prereq_check = $this->prerequisites_match($this->current);
  4026. $this->items[$this->current]->save(false, $prereq_check);
  4027. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4028. } else {
  4029. // If sco, then it is supposed to have been updated by some other call.
  4030. }
  4031. if ($item_type == 'sco') {
  4032. $this->items[$this->current]->restart();
  4033. }
  4034. }
  4035. if ($this->debug > 0) {
  4036. error_log('New LP - End of learnpath::start_current_item()', 0);
  4037. }
  4038. return true;
  4039. }
  4040. /**
  4041. * Stops the processing and counters for the old item (as held in $this->last)
  4042. * @return boolean True/False
  4043. */
  4044. public function stop_previous_item() {
  4045. if ($this->debug > 0) {
  4046. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4047. }
  4048. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4049. if ($this->debug > 2) {
  4050. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4051. }
  4052. switch ($this->get_type()) {
  4053. case '3' :
  4054. if ($this->items[$this->last]->get_type() != 'au') {
  4055. if ($this->debug > 2) {
  4056. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4057. }
  4058. $this->items[$this->last]->close();
  4059. //$this->autocomplete_parents($this->last);
  4060. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4061. } else {
  4062. if ($this->debug > 2) {
  4063. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4064. }
  4065. }
  4066. case '2' :
  4067. if ($this->items[$this->last]->get_type() != 'sco') {
  4068. if ($this->debug > 2) {
  4069. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4070. }
  4071. $this->items[$this->last]->close();
  4072. //$this->autocomplete_parents($this->last);
  4073. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4074. } else {
  4075. if ($this->debug > 2) {
  4076. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4077. }
  4078. }
  4079. break;
  4080. case '1' :
  4081. default :
  4082. if ($this->debug > 2) {
  4083. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4084. }
  4085. $this->items[$this->last]->close();
  4086. break;
  4087. }
  4088. } else {
  4089. if ($this->debug > 2) {
  4090. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4091. }
  4092. return false;
  4093. }
  4094. return true;
  4095. }
  4096. /**
  4097. * Updates the default view mode from fullscreen to embedded and inversely
  4098. * @return string The current default view mode ('fullscreen' or 'embedded')
  4099. */
  4100. public function update_default_view_mode() {
  4101. $course_id = api_get_course_int_id();
  4102. if ($this->debug > 0) {
  4103. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4104. }
  4105. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4106. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4107. $res = Database::query($sql);
  4108. if (Database :: num_rows($res) > 0) {
  4109. $row = Database :: fetch_array($res);
  4110. $view_mode = $row['default_view_mod'];
  4111. if ($view_mode == 'fullscreen') {
  4112. $view_mode = 'embedded';
  4113. } elseif ($view_mode == 'embedded') {
  4114. $view_mode = 'embedframe';
  4115. } elseif ($view_mode == 'embedframe') {
  4116. $view_mode = 'fullscreen';
  4117. }
  4118. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4119. $res = Database::query($sql);
  4120. $this->mode = $view_mode;
  4121. return $view_mode;
  4122. } else {
  4123. if ($this->debug > 2) {
  4124. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4125. }
  4126. }
  4127. return -1;
  4128. }
  4129. /**
  4130. * Updates the default behaviour about auto-commiting SCORM updates
  4131. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4132. */
  4133. public function update_default_scorm_commit() {
  4134. $course_id = api_get_course_int_id();
  4135. if ($this->debug > 0) {
  4136. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4137. }
  4138. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4139. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4140. $res = Database::query($sql);
  4141. if (Database :: num_rows($res) > 0) {
  4142. $row = Database :: fetch_array($res);
  4143. $force = $row['force_commit'];
  4144. if ($force == 1) {
  4145. $force = 0;
  4146. $force_return = false;
  4147. } elseif ($force == 0) {
  4148. $force = 1;
  4149. $force_return = true;
  4150. }
  4151. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4152. $res = Database::query($sql);
  4153. $this->force_commit = $force_return;
  4154. return $force_return;
  4155. } else {
  4156. if ($this->debug > 2) {
  4157. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4158. }
  4159. }
  4160. return -1;
  4161. }
  4162. /**
  4163. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4164. * @return bool True on success, false on failure
  4165. */
  4166. public function update_display_order() {
  4167. $course_id = api_get_course_int_id();
  4168. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4169. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4170. $res = Database::query($sql);
  4171. if ($res === false)
  4172. return false;
  4173. $lps = array ();
  4174. $lp_order = array ();
  4175. $num = Database :: num_rows($res);
  4176. // First check the order is correct, globally (might be wrong because
  4177. // of versions < 1.8.4).
  4178. if ($num > 0) {
  4179. $i = 1;
  4180. while ($row = Database :: fetch_array($res)) {
  4181. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4182. $need_fix = true;
  4183. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4184. $res_u = Database::query($sql_u);
  4185. }
  4186. $i++;
  4187. }
  4188. }
  4189. return true;
  4190. }
  4191. /**
  4192. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4193. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4194. */
  4195. public function update_reinit() {
  4196. $course_id = api_get_course_int_id();
  4197. if ($this->debug > 0) {
  4198. error_log('New LP - In learnpath::update_reinit()', 0);
  4199. }
  4200. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4201. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4202. $res = Database::query($sql);
  4203. if (Database :: num_rows($res) > 0) {
  4204. $row = Database :: fetch_array($res);
  4205. $force = $row['prevent_reinit'];
  4206. if ($force == 1) {
  4207. $force = 0;
  4208. } elseif ($force == 0) {
  4209. $force = 1;
  4210. }
  4211. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4212. $res = Database::query($sql);
  4213. $this->prevent_reinit = $force;
  4214. return $force;
  4215. } else {
  4216. if ($this->debug > 2) {
  4217. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4218. }
  4219. }
  4220. return -1;
  4221. }
  4222. /**
  4223. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4224. *
  4225. * @return string 'single', 'multi' or 'seriousgame'
  4226. * @author ndiechburg <noel@cblue.be>
  4227. **/
  4228. public function get_attempt_mode() {
  4229. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4230. $this->seriousgame_mode=0;
  4231. }
  4232. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4233. $this->prevent_reinit =1;
  4234. }
  4235. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4236. return 'seriousgame';
  4237. }
  4238. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4239. return 'single';
  4240. }
  4241. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4242. return 'multiple';
  4243. }
  4244. return 'single';
  4245. }
  4246. /**
  4247. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4248. *
  4249. * @param string 'seriousgame', 'single' or 'multiple'
  4250. * @return boolean
  4251. * @author ndiechburg <noel@cblue.be>
  4252. **/
  4253. public function set_attempt_mode($mode) {
  4254. $course_id = api_get_course_int_id();
  4255. switch ($mode) {
  4256. case 'seriousgame' :
  4257. $sg_mode = 1;
  4258. $prevent_reinit = 1;
  4259. break;
  4260. case 'single' :
  4261. $sg_mode = 0;
  4262. $prevent_reinit = 1;
  4263. break;
  4264. case 'multiple' :
  4265. $sg_mode = 0;
  4266. $prevent_reinit = 0;
  4267. break;
  4268. default :
  4269. $sg_mode = 0;
  4270. $prevent_reinit = 0;
  4271. break;
  4272. }
  4273. $this->prevent_reinit = $prevent_reinit;
  4274. $this->seriousgame_mode = $sg_mode;
  4275. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4276. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4277. $res = Database::query($sql);
  4278. if ($res) {
  4279. return true;
  4280. } else {
  4281. return false;
  4282. }
  4283. }
  4284. /**
  4285. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4286. *
  4287. * @return boolean
  4288. * @author ndiechburg <noel@cblue.be>
  4289. **/
  4290. public function switch_attempt_mode() {
  4291. if ($this->debug > 0) {
  4292. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4293. }
  4294. $mode = $this->get_attempt_mode();
  4295. switch ($mode) {
  4296. case 'single' :
  4297. $next_mode = 'multiple';
  4298. break;
  4299. case 'multiple' :
  4300. $next_mode = 'seriousgame';
  4301. break;
  4302. case 'seriousgame' :
  4303. $next_mode = 'single';
  4304. break;
  4305. default :
  4306. $next_mode = 'single';
  4307. break;
  4308. }
  4309. $this->set_attempt_mode($next_mode);
  4310. }
  4311. /**
  4312. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4313. *
  4314. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4315. * @author ndiechburg <noel@cblue.be>
  4316. **/
  4317. public function set_seriousgame_mode() {
  4318. $course_id = api_get_course_int_id();
  4319. if ($this->debug > 0) {
  4320. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4321. }
  4322. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4323. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4324. $res = Database::query($sql);
  4325. if (Database :: num_rows($res) > 0) {
  4326. $row = Database :: fetch_array($res);
  4327. $force = $row['seriousgame_mode'];
  4328. if ($force == 1) {
  4329. $force = 0;
  4330. } elseif ($force == 0) {
  4331. $force = 1;
  4332. }
  4333. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4334. $res = Database::query($sql);
  4335. $this->seriousgame_mode = $force;
  4336. return $force;
  4337. } else {
  4338. if ($this->debug > 2) {
  4339. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4340. }
  4341. }
  4342. return -1;
  4343. }
  4344. /**
  4345. * Updates the "scorm_debug" value that shows or hide the debug window
  4346. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4347. */
  4348. public function update_scorm_debug() {
  4349. $course_id = api_get_course_int_id();
  4350. if ($this->debug > 0) {
  4351. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4352. }
  4353. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4354. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4355. $res = Database::query($sql);
  4356. if (Database :: num_rows($res) > 0) {
  4357. $row = Database :: fetch_array($res);
  4358. $force = $row['debug'];
  4359. if ($force == 1) {
  4360. $force = 0;
  4361. } elseif ($force == 0) {
  4362. $force = 1;
  4363. }
  4364. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4365. $res = Database::query($sql);
  4366. $this->scorm_debug = $force;
  4367. return $force;
  4368. } else {
  4369. if ($this->debug > 2) {
  4370. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4371. }
  4372. }
  4373. return -1;
  4374. }
  4375. /**
  4376. * Function that makes a call to the function sort_tree_array and create_tree_array
  4377. * @author Kevin Van Den Haute
  4378. * @param array
  4379. */
  4380. public function tree_array($array) {
  4381. if ($this->debug > 1) {
  4382. error_log('New LP - In learnpath::tree_array()', 0);
  4383. }
  4384. $array = $this->sort_tree_array($array);
  4385. $this->create_tree_array($array);
  4386. }
  4387. /**
  4388. * Creates an array with the elements of the learning path tree in it
  4389. *
  4390. * @author Kevin Van Den Haute
  4391. * @param array $array
  4392. * @param int $parent
  4393. * @param int $depth
  4394. * @param array $tmp
  4395. */
  4396. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ()) {
  4397. if ($this->debug > 1) {
  4398. error_log('New LP - In learnpath::create_tree_array())', 0);
  4399. }
  4400. if (is_array($array)) {
  4401. for ($i = 0; $i < count($array); $i++) {
  4402. if ($array[$i]['parent_item_id'] == $parent) {
  4403. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4404. $tmp[] = $array[$i]['parent_item_id'];
  4405. $depth++;
  4406. }
  4407. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4408. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4409. $this->arrMenu[] = array (
  4410. 'id' => $array[$i]['id'],
  4411. 'item_type' => $array[$i]['item_type'],
  4412. 'title' => $array[$i]['title'],
  4413. 'path' => $array[$i]['path'],
  4414. 'description' => $array[$i]['description'],
  4415. 'parent_item_id' => $array[$i]['parent_item_id'],
  4416. 'previous_item_id' => $array[$i]['previous_item_id'],
  4417. 'next_item_id' => $array[$i]['next_item_id'],
  4418. 'min_score' => $array[$i]['min_score'],
  4419. 'max_score' => $array[$i]['max_score'],
  4420. 'mastery_score' => $array[$i]['mastery_score'],
  4421. 'display_order' => $array[$i]['display_order'],
  4422. 'prerequisite' => $preq,
  4423. 'depth' => $depth,
  4424. 'audio' => $audio
  4425. );
  4426. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4427. }
  4428. }
  4429. }
  4430. }
  4431. /**
  4432. * Sorts a multi dimensional array by parent id and display order
  4433. * @author Kevin Van Den Haute
  4434. *
  4435. * @param array $array (array with al the learning path items in it)
  4436. *
  4437. * @return array
  4438. */
  4439. public function sort_tree_array($array) {
  4440. foreach ($array as $key => $row) {
  4441. $parent[$key] = $row['parent_item_id'];
  4442. $position[$key] = $row['display_order'];
  4443. }
  4444. if (count($array) > 0)
  4445. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4446. return $array;
  4447. }
  4448. /**
  4449. * Function that creates a table structure with a learning path his modules, chapters and documents.
  4450. * Also the actions for the modules, chapters and documents are in this table.
  4451. * @author Kevin Van Den Haute
  4452. * @param int $lp_id
  4453. * @return string
  4454. */
  4455. public function overview() {
  4456. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4457. if ($this->debug > 0) {
  4458. error_log('New LP - In learnpath::overview()', 0);
  4459. }
  4460. global $_course;
  4461. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4462. $return = '';
  4463. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  4464. // we need to start a form when we want to update all the mp3 files
  4465. if ($update_audio == 'true') {
  4466. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&amp;action=' . Security :: remove_XSS($_GET['action']) . '&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  4467. }
  4468. $return .= '<div id="message"></div>';
  4469. $return_audio = '<table class="data_table">';
  4470. $return_audio .= '<tr>';
  4471. $return_audio .= '<th width="60%">' . get_lang('Title') . '</th>';
  4472. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  4473. $return_audio .= '</tr>';
  4474. if ($update_audio != 'true') {
  4475. $return .= '<div class="span12">';
  4476. $return .= self::return_new_tree($update_audio);
  4477. $return .='</div>';
  4478. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn')), array('style'=>'float:left; margin-top:15px;width:100%'));
  4479. } else {
  4480. $return_audio .= self::return_new_tree($update_audio);
  4481. $return .= $return_audio.'</table>';
  4482. }
  4483. // We need to close the form when we are updating the mp3 files.
  4484. if ($update_audio == 'true') {
  4485. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  4486. }
  4487. // We need to close the form when we are updating the mp3 files.
  4488. if ($update_audio == 'true' && count($arrLP) != 0) {
  4489. $return .= '</form>';
  4490. }
  4491. return $return;
  4492. }
  4493. public function return_new_tree($update_audio = 'false', $drop_element_here = false) {
  4494. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  4495. echo '<script>
  4496. var newOrderData= "";
  4497. function processChildren(parentId) {
  4498. //Loop through the children of the UL element defined by the parentId
  4499. var ulParentID= "UL_" + parentId;
  4500. $("#" + ulParentID).children().each(function () {
  4501. /*Only process elements with an id attribute (in order to skip the blank,
  4502. unmovable <li> elements.*/
  4503. if ($(this).attr("id")) {
  4504. /*Build a string of data with the childs ID and parent ID,
  4505. using the "|" as a delimiter between the two IDs and the "^"
  4506. as a record delimiter (these delimiters were chosen in case the data
  4507. involved includes more common delimiters like commas within the content)
  4508. */
  4509. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  4510. //Determine if this child is a containter
  4511. if ($(this).is(".container")) {
  4512. //Process the child elements of the container
  4513. processChildren($(this).attr("id"));
  4514. }
  4515. }
  4516. }); //end of children loop
  4517. } //end of processChildren function
  4518. $(function() {
  4519. $(".item_data").live("mouseover", function(event) {
  4520. $(".button_actions", this).show();
  4521. });
  4522. $(".item_data").live("mouseout", function() {
  4523. $(".button_actions",this).hide();
  4524. });
  4525. $(".button_actions").hide();
  4526. $( ".lp_resource" ).sortable({
  4527. items: ".lp_resource_element ",
  4528. handle: ".moved", //only the class "moved"
  4529. cursor: "move",
  4530. connectWith: "#lp_item_list",
  4531. placeholder: "ui-state-highlight", //defines the yellow highlight
  4532. start: function(event, ui) {
  4533. $(ui.item).css("width", "160px");
  4534. $(ui.item).find(".item_data").attr("style", "");
  4535. },
  4536. stop: function(event, ui) {
  4537. $(ui.item).css("width", "100%");
  4538. },
  4539. });
  4540. $("#lp_item_list").sortable({
  4541. items: "li",
  4542. handle: ".moved", //only the class "moved"
  4543. cursor: "move",
  4544. placeholder: "ui-state-highlight", //defines the yellow highlight
  4545. update: function(event, ui) {
  4546. //Walk through the direct descendants of the lp_item_list <ul>
  4547. $("#lp_item_list").children().each(function () {
  4548. /*Only process elements with an id attribute (in order to skip the blank,
  4549. unmovable <li> elements.*/
  4550. if ($(this).attr("id")) {
  4551. /*Build a string of data with the child s ID and parent ID,
  4552. using the "|" as a delimiter between the two IDs and the "^"
  4553. as a record delimiter (these delimiters were chosen in case the data
  4554. involved includes more common delimiters like commas within the content)
  4555. */
  4556. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  4557. //Determine if this child is a containter
  4558. if ($(this).is(".li_container")) {
  4559. //Process the child elements of the container
  4560. processChildren($(this).attr("id"));
  4561. }
  4562. }
  4563. }); //end of lp_item_list children loop
  4564. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  4565. $.post("'.$ajax_url.'", order, function(reponse){
  4566. $("#message").html(reponse);
  4567. });
  4568. },
  4569. receive: function(event, ui) {
  4570. var id = $(ui.item).attr("data_id");
  4571. var type = $(ui.item).attr("data_type");
  4572. var title = $(ui.item).attr("title");
  4573. if (ui.item.parent()[0]) {
  4574. var parent_id = $(ui.item.parent()[0]).attr("id");
  4575. var previous_id = $(ui.item.prev()).attr("id");
  4576. if (parent_id) {
  4577. parent_id = parent_id.split("_")[1];
  4578. var params = {
  4579. "a": "add_lp_item",
  4580. "id": id,
  4581. "parent_id": parent_id,
  4582. "previous_id": previous_id,
  4583. "type": type,
  4584. "title" : title
  4585. };
  4586. $.ajax({
  4587. type: "GET",
  4588. url: "'.$ajax_url.'",
  4589. data: params,
  4590. async: false,
  4591. success: function(data) {
  4592. if (data == -1) {
  4593. } else {
  4594. $(".normal-message").hide();
  4595. $(ui.item).attr("id", data);
  4596. $(ui.item).addClass("lp_resource_element_new");
  4597. $(ui.item).find(".item_data").attr("style", "");
  4598. $(ui.item).addClass("record li_container");
  4599. $(ui.item).removeClass("lp_resource_element");
  4600. $(ui.item).removeClass("doc_resource");
  4601. }
  4602. }
  4603. });
  4604. }
  4605. }//
  4606. }//end receive
  4607. });
  4608. });
  4609. </script>';
  4610. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4611. $course_id = api_get_course_int_id();
  4612. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  4613. $sql = "SELECT * FROM $tbl_lp_item
  4614. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  4615. $result = Database::query($sql);
  4616. $arrLP = array();
  4617. while ($row = Database :: fetch_array($result)) {
  4618. $row['title'] = Security :: remove_XSS($row['title']);
  4619. $row['description'] = Security :: remove_XSS($row['description']);
  4620. $arrLP[] = array (
  4621. 'id' => $row['id'],
  4622. 'item_type' => $row['item_type'],
  4623. 'title' => $row['title'],
  4624. 'path' => $row['path'],
  4625. 'description' => $row['description'],
  4626. 'parent_item_id' => $row['parent_item_id'],
  4627. 'previous_item_id' => $row['previous_item_id'],
  4628. 'next_item_id' => $row['next_item_id'],
  4629. 'max_score' => $row['max_score'],
  4630. 'min_score' => $row['min_score'],
  4631. 'mastery_score' => $row['mastery_score'],
  4632. 'prerequisite' => $row['prerequisite'],
  4633. 'display_order' => $row['display_order'],
  4634. 'audio' => $row['audio']
  4635. );
  4636. }
  4637. $this->tree_array($arrLP);
  4638. $arrLP = $this->arrMenu;
  4639. unset ($this->arrMenu);
  4640. $elements = array();
  4641. for ($i = 0; $i < count($arrLP); $i++) {
  4642. $title = $arrLP[$i]['title'];
  4643. $title_cut = cut($arrLP[$i]['title'], 25);
  4644. if (($i % 2) == 0) {
  4645. $oddclass = 'row_odd';
  4646. } else {
  4647. $oddclass = 'row_even';
  4648. }
  4649. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddclass . '">';
  4650. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4651. $icon = '';
  4652. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4653. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  4654. } else {
  4655. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4656. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  4657. } else {
  4658. $icon = '<img src="../img/folder_document.gif" />';
  4659. }
  4660. }
  4661. // The audio column.
  4662. $return_audio .= '<td align="center">';
  4663. $audio = '';
  4664. if (!$update_audio OR $update_audio <> 'true') {
  4665. if (!empty($arrLP[$i]['audio'])) {
  4666. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  4667. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  4668. $audio .= '<script type="text/javascript">
  4669. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  4670. s1.addParam("allowscriptaccess","always");
  4671. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  4672. s1.write("container' . $i . '");
  4673. </script>';*/
  4674. } else {
  4675. $audio .= '';
  4676. }
  4677. } else {
  4678. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  4679. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  4680. if (!empty ($arrLP[$i]['audio'])) {
  4681. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br /><input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  4682. }
  4683. }
  4684. }
  4685. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  4686. $return_audio .= '</td>';
  4687. $move_icon = '';
  4688. $move_item_icon = '';
  4689. $edit_icon = '';
  4690. $delete_icon = '';
  4691. $audio_icon = '';$prerequisities_icon = '';
  4692. if ($is_allowed_to_edit) {
  4693. if (!$update_audio OR $update_audio <> 'true') {
  4694. $move_icon .= '<a class="moved" href="#">';
  4695. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  4696. $move_icon .= '</a>';
  4697. }
  4698. // No edit for this item types
  4699. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  4700. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  4701. $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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'] . '">';
  4702. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4703. $edit_icon .= '</a>';
  4704. } else {
  4705. $edit_icon .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=edit_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
  4706. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4707. $edit_icon .= '</a>';
  4708. }
  4709. }
  4710. $delete_icon .= ' <a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=delete_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
  4711. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  4712. $delete_icon .= '</a>';
  4713. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  4714. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  4715. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('Prerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  4716. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  4717. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  4718. }
  4719. }
  4720. if ($update_audio != 'true') {
  4721. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
  4722. } else {
  4723. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  4724. }
  4725. $parent_id = $arrLP[$i]['parent_item_id'];
  4726. $default_data[$arrLP[$i]['id']] = $row;
  4727. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  4728. if (empty($parent_id)) {
  4729. $elements[$arrLP[$i]['id']]['data'] = $row;
  4730. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4731. } else {
  4732. $parent_arrays = array();
  4733. if ($arrLP[$i]['depth'] > 1) {
  4734. //Getting list of parents
  4735. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  4736. foreach($arrLP as $item) {
  4737. if ($item['id'] == $parent_id) {
  4738. if ($item['parent_item_id'] == 0) {
  4739. $parent_id = $item['id'];
  4740. break;
  4741. } else {
  4742. $parent_id = $item['parent_item_id'];
  4743. if (empty($parent_arrays)) {
  4744. $parent_arrays[] = intval($item['id']);
  4745. }
  4746. $parent_arrays[] = $parent_id;
  4747. break;
  4748. }
  4749. }
  4750. }
  4751. }
  4752. }
  4753. if (!empty($parent_arrays)) {
  4754. $parent_arrays = array_reverse($parent_arrays);
  4755. $val = '$elements';
  4756. $x = 0;
  4757. foreach($parent_arrays as $item) {
  4758. if ($x != count($parent_arrays) -1) {
  4759. $val .= '["'.$item.'"]["children"]';
  4760. } else {
  4761. $val .= '["'.$item.'"]["children"]';
  4762. }
  4763. $x++;
  4764. }
  4765. $val .= "";
  4766. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  4767. eval($code_str);
  4768. } else {
  4769. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  4770. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4771. }
  4772. }
  4773. }
  4774. $return = '<div class="lp_tree well">';
  4775. $return .= '<ul id="lp_item_list">';
  4776. $return .='<h4>'.$this->name.'</h4><br>';
  4777. $tree = self::print_recursive($elements, $default_data, $default_content);
  4778. if (!empty($tree)) {
  4779. $return .= $tree;
  4780. } else {
  4781. if ($drop_element_here) {
  4782. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  4783. }
  4784. }
  4785. $return .= '</ul>';
  4786. if ($update_audio == 'true') {
  4787. $return = $return_audio;
  4788. } else {
  4789. $return .= '</div>';
  4790. }
  4791. return $return;
  4792. }
  4793. function print_recursive($elements, $default_data, $default_content) {
  4794. $return = '';
  4795. foreach ($elements as $key => $item) {
  4796. if (isset($item['load_data']) || empty($item['data'])) {
  4797. $item['data'] = $default_data[$item['load_data']];
  4798. $item['type'] = $default_content[$item['load_data']]['item_type'];
  4799. }
  4800. $sub_list = '';
  4801. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  4802. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  4803. }
  4804. if (empty($item['children'])) {
  4805. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4806. $active = null;
  4807. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  4808. $active = 'active';
  4809. }
  4810. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4811. } else {
  4812. //sections
  4813. if (isset($item['children'])) {
  4814. $data = self::print_recursive($item['children'], $default_data, $default_content);
  4815. }
  4816. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4817. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4818. }
  4819. }
  4820. return $return;
  4821. }
  4822. /**
  4823. * This function builds the action menu
  4824. * @return void
  4825. */
  4826. public function build_action_menu() {
  4827. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4828. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4829. $return = '<div class="actions">';
  4830. $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>';
  4831. if ($is_allowed_to_edit) {
  4832. }
  4833. //$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>';
  4834. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('view_left_right.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  4835. $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>';
  4836. // 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>';
  4837. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  4838. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;action=edit&amp;lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  4839. $return .= '</div>';
  4840. echo $return;
  4841. }
  4842. /**
  4843. * This functions builds the LP tree based on data from the database.
  4844. * @return string
  4845. * @deprecated use the return_new_tree() function
  4846. * @uses dtree.js :: necessary javascript for building this tree
  4847. */
  4848. public function build_tree() {
  4849. $course_id = api_get_course_int_id();
  4850. $return = "<script type=\"text/javascript\">\n";
  4851. $return .= "\tm = new dTree('m');\n\n";
  4852. $return .= "\tm.config.folderLinks = true;\n";
  4853. $return .= "\tm.config.useCookies = true;\n";
  4854. $return .= "\tm.config.useIcons = true;\n";
  4855. $return .= "\tm.config.useLines = true;\n";
  4856. $return .= "\tm.config.useSelection = true;\n";
  4857. $return .= "\tm.config.useStatustext = false;\n\n";
  4858. $menu = 0;
  4859. $parent = '';
  4860. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  4861. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  4862. $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
  4863. FROM $tbl_lp_item
  4864. WHERE c_id = ".$course_id." AND lp_id = " . Database::escape_string($this->lp_id);
  4865. $result = Database::query($sql);
  4866. $arrLP = array ();
  4867. while ($row = Database :: fetch_array($result)) {
  4868. $row['title'] = Security :: remove_XSS($row['title']);
  4869. $row['description'] = Security :: remove_XSS($row['description']);
  4870. $arrLP[] = array (
  4871. 'id' => $row['id'],
  4872. 'item_type' => $row['item_type'],
  4873. 'title' => $row['title'],
  4874. 'path' => $row['path'],
  4875. 'description' => $row['description'],
  4876. 'parent_item_id' => $row['parent_item_id'],
  4877. 'previous_item_id' => $row['previous_item_id'],
  4878. 'next_item_id' => $row['next_item_id'],
  4879. 'max_score' => $row['max_score'],
  4880. 'min_score' => $row['min_score'],
  4881. 'mastery_score' => $row['mastery_score'],
  4882. 'display_order' => $row['display_order']
  4883. );
  4884. }
  4885. $this->tree_array($arrLP);
  4886. $arrLP = $this->arrMenu;
  4887. unset ($this->arrMenu);
  4888. $title = '';
  4889. for ($i = 0; $i < count($arrLP); $i++) {
  4890. $title = addslashes($arrLP[$i]['title']);
  4891. $menu_page = api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=view_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $_SESSION['oLP']->lp_id;
  4892. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4893. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4894. $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";
  4895. } else
  4896. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4897. $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";
  4898. } else {
  4899. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  4900. }
  4901. if ($menu < $arrLP[$i]['id'])
  4902. $menu = $arrLP[$i]['id'];
  4903. }
  4904. $return .= "\n\tdocument.write(m);\n";
  4905. $return .= "\t if(!m.selectedNode) m.s(1);";
  4906. $return .= "</script>\n";
  4907. return $return;
  4908. }
  4909. /**
  4910. * Creates the default learning path folder
  4911. */
  4912. public static function generate_learning_path_folder($course) {
  4913. //Creating learning_path folder
  4914. $dir = '/learning_path';
  4915. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4916. $folder = false;
  4917. if (!is_dir($filepath.'/'.$dir)) {
  4918. $folder = create_unexisting_directory($course, api_get_user_id(), api_get_session_id(), 0, 0, $filepath, $dir , get_lang('LearningPaths'), 0);
  4919. } else {
  4920. $folder = true;
  4921. }
  4922. return $folder;
  4923. }
  4924. public function generate_lp_folder($course, $lp_name = null) {
  4925. $filepath = '';
  4926. $dir = '/learning_path/';
  4927. if (empty($lp_name)) {
  4928. $lp_name = $this->name;
  4929. }
  4930. $folder = self::generate_learning_path_folder($course);
  4931. //Creating LP folder
  4932. if ($folder) {
  4933. //Limits title size
  4934. $title = api_substr(replace_dangerous_char($lp_name), 0 , 80);
  4935. $dir = $dir.$title;
  4936. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4937. if (!is_dir($filepath.'/'.$dir)) {
  4938. $folder = create_unexisting_directory($course, api_get_user_id(), api_get_session_id(), 0, 0, $filepath, $dir , $lp_name);
  4939. } else {
  4940. $folder = true;
  4941. }
  4942. $dir = $dir.'/';
  4943. if ($folder) {
  4944. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  4945. }
  4946. }
  4947. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  4948. return $array;
  4949. }
  4950. /**
  4951. * Create a new document //still needs some finetuning
  4952. * @param array $_course
  4953. * @return string
  4954. */
  4955. public function create_document($_course) {
  4956. $course_id = api_get_course_int_id();
  4957. global $charset;
  4958. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  4959. if (strstr($dir, '..'))
  4960. $dir = '/';
  4961. if ($dir[0] == '.')
  4962. $dir = substr($dir, 1);
  4963. if ($dir[0] != '/')
  4964. $dir = '/' . $dir;
  4965. if ($dir[strlen($dir) - 1] != '/')
  4966. $dir .= '/';
  4967. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  4968. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  4969. //Generates folder
  4970. $result = $this->generate_lp_folder($_course);
  4971. $dir = $result['dir'];
  4972. $filepath = $result['filepath'];
  4973. }
  4974. if (!is_dir($filepath)) {
  4975. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  4976. $dir = '/';
  4977. }
  4978. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  4979. // is already escaped twice when it gets here.
  4980. $title = replace_dangerous_char(stripslashes($_POST['title']));
  4981. $title = disable_dangerous_file($title);
  4982. $filename = $title;
  4983. $content = $_POST['content_lp'];
  4984. $tmp_filename = $filename;
  4985. $i = 0;
  4986. while (file_exists($filepath . $tmp_filename . '.html'))
  4987. $tmp_filename = $filename . '_' . ++ $i;
  4988. $filename = $tmp_filename . '.html';
  4989. $content = stripslashes($content);
  4990. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  4991. // Change the path of mp3 to absolute.
  4992. // The first regexp deals with ../../../ urls.
  4993. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  4994. // The second regexp deals with audio/ urls.
  4995. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  4996. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  4997. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  4998. if (!file_exists($filepath . $filename)) {
  4999. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5000. fputs($fp, $content);
  5001. fclose($fp);
  5002. $file_size = filesize($filepath . $filename);
  5003. $save_file_path = $dir.$filename;
  5004. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5005. if ($document_id) {
  5006. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id(), null, null, null, null, api_get_session_id());
  5007. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5008. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5009. if ($new_comment || $new_title) {
  5010. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5011. $ct = '';
  5012. if ($new_comment)
  5013. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5014. if ($new_title)
  5015. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5016. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5017. Database::query($sql_update);
  5018. }
  5019. }
  5020. return $document_id;
  5021. }
  5022. }
  5023. }
  5024. /**
  5025. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5026. * @param array $_course array
  5027. * @return void
  5028. */
  5029. public function edit_document($_course) {
  5030. $course_id = api_get_course_int_id();
  5031. global $_configuration;
  5032. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5033. if (strstr($dir, '..'))
  5034. $dir = '/';
  5035. if ($dir[0] == '.')
  5036. $dir = substr($dir, 1);
  5037. if ($dir[0] != '/')
  5038. $dir = '/' . $dir;
  5039. if ($dir[strlen($dir) - 1] != '/')
  5040. $dir .= '/';
  5041. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5042. if (!is_dir($filepath)) {
  5043. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5044. $dir = '/';
  5045. }
  5046. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5047. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5048. $document_id = intval($_POST['path']);
  5049. $sql = "SELECT path FROM " . $table_doc . " WHERE c_id = $course_id AND id = " . $document_id;
  5050. $res = Database::query($sql);
  5051. $row = Database :: fetch_array($res);
  5052. $content = stripslashes($_POST['content_lp']);
  5053. $file = $filepath . $row['path'];
  5054. if ($fp = @ fopen($file, 'w')) {
  5055. $content = text_filter($content);
  5056. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5057. // Change the path of mp3 to absolute.
  5058. // The first regexp deals with ../../../ urls.
  5059. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5060. // The second regexp deals with audio/ urls.
  5061. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5062. fputs($fp, $content);
  5063. fclose($fp);
  5064. $sql_update = "UPDATE " . $table_doc ." SET title='".Database::escape_string($_POST['title'])."' WHERE c_id = ".$course_id." AND id = " . $document_id;
  5065. Database::query($sql_update);
  5066. }
  5067. }
  5068. }
  5069. /**
  5070. * Displays the selected item, with a panel for manipulating the item
  5071. * @param int $item_id
  5072. * @param string $msg
  5073. * @return string
  5074. */
  5075. public function display_item($item_id, $iframe = true, $msg = '') {
  5076. $course_id = api_get_course_int_id();
  5077. $return = '';
  5078. if (is_numeric($item_id)) {
  5079. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5080. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5081. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5082. WHERE c_id = ".$course_id." AND lp.id = " . Database::escape_string($item_id);
  5083. $result = Database::query($sql);
  5084. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5085. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5086. // Prevents wrong parent selection for document, see Bug#1251.
  5087. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5088. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5089. }
  5090. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5091. $return .= '<div style="padding:10px;">';
  5092. if ($msg != '')
  5093. $return .= $msg;
  5094. $return .= '<h3>'.$row['title'].'</h3>';
  5095. switch ($row['item_type']) {
  5096. case TOOL_QUIZ:
  5097. if (!empty($row['path'])) {
  5098. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5099. $exercise = new Exercise();
  5100. $exercise->read($row['path']);
  5101. $return .= $exercise->description.'<br />';
  5102. }
  5103. break;
  5104. case TOOL_DOCUMENT:
  5105. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5106. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($row['path']);
  5107. $result = Database::query($sql_doc);
  5108. $path_file = Database::result($result, 0, 0);
  5109. $path_parts = pathinfo($path_file);
  5110. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5111. if (in_array($path_parts['extension'], array (
  5112. 'html',
  5113. 'txt',
  5114. 'png',
  5115. 'jpg',
  5116. 'JPG',
  5117. 'jpeg',
  5118. 'JPEG',
  5119. 'gif',
  5120. 'swf'
  5121. ))) {
  5122. $return .= $this->display_document($row['path'], true, true);
  5123. }
  5124. break;
  5125. }
  5126. $return .= '</div>';
  5127. }
  5128. }
  5129. return $return;
  5130. }
  5131. /**
  5132. * Shows the needed forms for editing a specific item
  5133. * @param int $item_id
  5134. * @return string
  5135. */
  5136. public function display_edit_item($item_id) {
  5137. global $_course; // It will disappear.
  5138. $course_id = api_get_course_int_id();
  5139. $return = '';
  5140. if (is_numeric($item_id)) {
  5141. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5142. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = " . Database::escape_string($item_id);
  5143. $res = Database::query($sql);
  5144. $row = Database::fetch_array($res);
  5145. switch ($row['item_type']) {
  5146. case 'dokeos_chapter' :
  5147. case 'dir' :
  5148. case 'asset' :
  5149. case 'sco' :
  5150. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5151. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5152. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5153. } else {
  5154. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5155. }
  5156. break;
  5157. case TOOL_DOCUMENT :
  5158. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5159. $sql_step = " SELECT lp.*, doc.path as dir
  5160. FROM " . $tbl_lp_item . " as lp
  5161. LEFT JOIN " . $tbl_doc . " as doc ON doc.id = lp.path
  5162. WHERE lp.c_id = $course_id AND
  5163. doc.c_id = $course_id AND
  5164. lp.id = " . Database::escape_string($item_id);
  5165. $res_step = Database::query($sql_step);
  5166. $row_step = Database :: fetch_array($res_step);
  5167. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5168. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5169. break;
  5170. case TOOL_LINK :
  5171. $link_id = (string) $row['path'];
  5172. if (ctype_digit($link_id)) {
  5173. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5174. $sql_select = 'SELECT url FROM ' . $tbl_link . ' WHERE c_id = '.$course_id.' AND id = ' . Database::escape_string($link_id);
  5175. $res_link = Database::query($sql_select);
  5176. $row_link = Database :: fetch_array($res_link);
  5177. if (is_array($row_link)) {
  5178. $row['url'] = $row_link['url'];
  5179. }
  5180. }
  5181. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5182. $return .= $this->display_link_form('edit', $item_id, $row);
  5183. break;
  5184. case 'dokeos_module' :
  5185. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5186. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5187. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5188. } else {
  5189. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5190. }
  5191. break;
  5192. case TOOL_QUIZ :
  5193. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5194. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5195. break;
  5196. case TOOL_HOTPOTATOES :
  5197. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5198. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5199. break;
  5200. case TOOL_STUDENTPUBLICATION :
  5201. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5202. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5203. break;
  5204. case TOOL_FORUM :
  5205. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5206. $return .= $this->display_forum_form('edit', $item_id, $row);
  5207. break;
  5208. case TOOL_THREAD :
  5209. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5210. $return .= $this->display_thread_form('edit', $item_id, $row);
  5211. break;
  5212. }
  5213. }
  5214. return $return;
  5215. }
  5216. /**
  5217. * Function that displays a list with al the resources that could be added to the learning path
  5218. * @return string
  5219. */
  5220. public function display_resources() {
  5221. global $_course; // TODO: Don't use globals.
  5222. $course_code = api_get_course_id();
  5223. //Get all the docs
  5224. $documents = $this->get_documents();
  5225. //Get all the exercises
  5226. $exercises = $this->get_exercises();
  5227. // Get all the links
  5228. $links = $this->get_links();
  5229. //Get al the student publications
  5230. $works = $this->get_student_publications();
  5231. //Get al the forums
  5232. $forums = $this->get_forums(null, $course_code);
  5233. $headers = array( Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  5234. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  5235. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  5236. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  5237. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  5238. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  5239. );
  5240. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5241. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  5242. return true;
  5243. }
  5244. /**
  5245. * Returns the extension of a document
  5246. * @param string filename
  5247. * @return string Extension (part after the last dot)
  5248. */
  5249. public function get_extension($filename) {
  5250. $explode = explode('.', $filename);
  5251. return $explode[count($explode) - 1];
  5252. }
  5253. /**
  5254. * Displays a document by id
  5255. *
  5256. * @param unknown_type $id
  5257. * @return unknown
  5258. */
  5259. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false) {
  5260. global $_course; // It is temporary.
  5261. $course_id = api_get_course_int_id();
  5262. $return = '';
  5263. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5264. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5265. WHERE c_id = ".$course_id." AND id = " . $id;
  5266. $res_doc = Database::query($sql_doc);
  5267. $row_doc = Database :: fetch_array($res_doc);
  5268. //if ($show_title)
  5269. //$return .= '<p class="lp_title">' . $row_doc['title'] . ($edit_link ? ' [ <a href="' .api_get_self(). '?cidReq=' . $_GET['cidReq'] . '&amp;action=add_item&amp;type=' . TOOL_DOCUMENT . '&amp;file=' . $_GET['file'] . '&amp;edit=true&amp;lp_id=' . $_GET['lp_id'] . '">Edit this document</a> ]' : '') . '</p>';
  5270. // TODO: Add a path filter.
  5271. if ($iframe) {
  5272. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document' . str_replace('%2F', '/', urlencode($row_doc['path'])) . '?' . api_get_cidreq() . '"></iframe>';
  5273. } else {
  5274. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5275. }
  5276. return $return;
  5277. }
  5278. /**
  5279. * Return HTML form to add/edit a quiz
  5280. * @param string Action (add/edit)
  5281. * @param integer Item ID if already exists
  5282. * @param mixed Extra information (quiz ID if integer)
  5283. * @return string HTML form
  5284. */
  5285. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '') {
  5286. $course_id = api_get_course_int_id();
  5287. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5288. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5289. if ($id != 0 && is_array($extra_info)) {
  5290. $item_title = $extra_info['title'];
  5291. $item_description = $extra_info['description'];
  5292. } elseif (is_numeric($extra_info)) {
  5293. $sql_quiz = "SELECT title, description FROM " . $tbl_quiz . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5294. $result = Database::query($sql_quiz);
  5295. $row = Database :: fetch_array($result);
  5296. $item_title = $row['title'];
  5297. $item_description = $row['description'];
  5298. } else {
  5299. $item_title = '';
  5300. $item_description = '';
  5301. }
  5302. $item_title = Security::remove_XSS($item_title);
  5303. $item_description = Security::remove_XSS($item_description);
  5304. $legend = '<legend>';
  5305. if ($id != 0 && is_array($extra_info))
  5306. $parent = $extra_info['parent_item_id'];
  5307. else
  5308. $parent = 0;
  5309. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5310. $result = Database::query($sql);
  5311. $arrLP = array ();
  5312. while ($row = Database :: fetch_array($result)) {
  5313. $arrLP[] = array (
  5314. 'id' => $row['id'],
  5315. 'item_type' => $row['item_type'],
  5316. 'title' => $row['title'],
  5317. 'path' => $row['path'],
  5318. 'description' => $row['description'],
  5319. 'parent_item_id' => $row['parent_item_id'],
  5320. 'previous_item_id' => $row['previous_item_id'],
  5321. 'next_item_id' => $row['next_item_id'],
  5322. 'display_order' => $row['display_order'],
  5323. 'max_score' => $row['max_score'],
  5324. 'min_score' => $row['min_score'],
  5325. 'mastery_score' => $row['mastery_score'],
  5326. 'prerequisite' => $row['prerequisite'],
  5327. 'max_time_allowed' => $row['max_time_allowed']
  5328. );
  5329. }
  5330. $this->tree_array($arrLP);
  5331. $arrLP = $this->arrMenu;
  5332. unset ($this->arrMenu);
  5333. if ($action == 'add')
  5334. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5335. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5336. else
  5337. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5338. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5339. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5340. }
  5341. $legend .= '</legend>';
  5342. $return .= '<div class="sectioncomment">';
  5343. $return .= '<form method="POST">';
  5344. $return .= $legend;
  5345. $return .= '<table class="lp_form">';
  5346. if ($action != 'move') {
  5347. $return .= '<tr>';
  5348. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5349. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5350. $return .= '</tr>';
  5351. }
  5352. $return .= '<tr>';
  5353. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5354. $return .= '<td class="input">';
  5355. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5356. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5357. $arrHide = array (
  5358. $id
  5359. );
  5360. //$parent_item_id = $_SESSION['parent_item_id'];
  5361. for ($i = 0; $i < count($arrLP); $i++) {
  5362. if ($action != 'add') {
  5363. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5364. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5365. } else {
  5366. $arrHide[] = $arrLP[$i]['id'];
  5367. }
  5368. } else {
  5369. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5370. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5371. }
  5372. }
  5373. if (is_array($arrLP)) {
  5374. reset($arrLP);
  5375. }
  5376. $return .= '</select>';
  5377. $return .= '</td>';
  5378. $return .= '</tr>';
  5379. $return .= '<tr>';
  5380. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5381. $return .= '<td class="input">';
  5382. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5383. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5384. for ($i = 0; $i < count($arrLP); $i++) {
  5385. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5386. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5387. $selected = 'selected="selected" ';
  5388. elseif ($action == 'add') $selected = 'selected="selected" ';
  5389. else
  5390. $selected = '';
  5391. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5392. }
  5393. }
  5394. $return .= '</select>';
  5395. $return .= '</td>';
  5396. $return .= '</tr>';
  5397. if ($action != 'move') {
  5398. $id_prerequisite = 0;
  5399. if (is_array($arrLP)) {
  5400. foreach ($arrLP as $key => $value) {
  5401. if ($value['id'] == $id) {
  5402. $id_prerequisite = $value['prerequisite'];
  5403. break;
  5404. }
  5405. }
  5406. }
  5407. $arrHide = array ();
  5408. for ($i = 0; $i < count($arrLP); $i++) {
  5409. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5410. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5411. $s_selected_position = $arrLP[$i]['id'];
  5412. elseif ($action == 'add') $s_selected_position = 0;
  5413. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5414. }
  5415. }
  5416. /*// Commented the prerequisites, only visible in edit (exercise).
  5417. $return .= '<tr>';
  5418. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('Prerequisites').'</label></td>';
  5419. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5420. foreach($arrHide as $key => $value){
  5421. if($key==$s_selected_position && $action == 'add'){
  5422. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5423. }
  5424. elseif($key==$id_prerequisite && $action == 'edit'){
  5425. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5426. }
  5427. else{
  5428. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5429. }
  5430. }
  5431. $return .= "</select></td>";
  5432. */
  5433. $return .= '</tr>';
  5434. /*$return .= '<tr>';
  5435. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5436. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5437. // Remove temporarily the test description.
  5438. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5439. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5440. $return .= '</tr>'; */
  5441. }
  5442. $return .= '<tr>';
  5443. if ($action == 'add') {
  5444. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5445. } else {
  5446. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5447. }
  5448. $return .= '</tr>';
  5449. $return .= '</table>';
  5450. if ($action == 'move') {
  5451. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5452. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5453. }
  5454. if (is_numeric($extra_info)) {
  5455. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5456. }
  5457. elseif (is_array($extra_info)) {
  5458. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5459. }
  5460. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5461. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5462. $return .= '</form>';
  5463. $return .= '</div>';
  5464. return $return;
  5465. }
  5466. /**
  5467. * Addition of Hotpotatoes tests
  5468. * @param string Action
  5469. * @param integer Internal ID of the item
  5470. * @param mixed Extra information - can be an array with title and description indexes
  5471. * @return string HTML structure to display the hotpotatoes addition formular
  5472. */
  5473. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') {
  5474. $course_id = api_get_course_int_id();
  5475. global $charset;
  5476. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5477. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5478. if ($id != 0 && is_array($extra_info)) {
  5479. $item_title = stripslashes($extra_info['title']);
  5480. $item_description = stripslashes($extra_info['description']);
  5481. } elseif (is_numeric($extra_info)) {
  5482. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5483. $sql_hot = "SELECT * FROM " . $TBL_DOCUMENT . "
  5484. WHERE c_id = ".$course_id." AND
  5485. path LIKE '" . $uploadPath . "/%/%htm%' AND
  5486. id = " . (int) $extra_info . "
  5487. ORDER BY id ASC";
  5488. $res_hot = Database::query($sql_hot);
  5489. $row = Database::fetch_array($res_hot);
  5490. $item_title = $row['title'];
  5491. $item_description = $row['description'];
  5492. if (!empty ($row['comment'])) {
  5493. $item_title = $row['comment'];
  5494. }
  5495. } else {
  5496. $item_title = '';
  5497. $item_description = '';
  5498. }
  5499. if ($id != 0 && is_array($extra_info))
  5500. $parent = $extra_info['parent_item_id'];
  5501. else
  5502. $parent = 0;
  5503. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5504. $result = Database::query($sql);
  5505. $arrLP = array ();
  5506. while ($row = Database :: fetch_array($result)) {
  5507. $arrLP[] = array (
  5508. 'id' => $row['id'],
  5509. 'item_type' => $row['item_type'],
  5510. 'title' => $row['title'],
  5511. 'path' => $row['path'],
  5512. 'description' => $row['description'],
  5513. 'parent_item_id' => $row['parent_item_id'],
  5514. 'previous_item_id' => $row['previous_item_id'],
  5515. 'next_item_id' => $row['next_item_id'],
  5516. 'display_order' => $row['display_order'],
  5517. 'max_score' => $row['max_score'],
  5518. 'min_score' => $row['min_score'],
  5519. 'mastery_score' => $row['mastery_score'],
  5520. 'prerequisite' => $row['prerequisite'],
  5521. 'max_time_allowed' => $row['max_time_allowed']
  5522. );
  5523. }
  5524. $legend = '<legend>';
  5525. if ($action == 'add')
  5526. $legend .= get_lang('CreateTheExercise');
  5527. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  5528. else
  5529. $legend .= get_lang('EditCurrentExecice');
  5530. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5531. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5532. }
  5533. $legend .= '</legend>';
  5534. $return .= '<form method="POST">';
  5535. $return .= $legend;
  5536. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5537. $return .= '<tr>';
  5538. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  5539. $return .= '<td class="input">';
  5540. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5541. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5542. $arrHide = array (
  5543. $id
  5544. );
  5545. if (count($arrLP) > 0) {
  5546. for ($i = 0; $i < count($arrLP); $i++) {
  5547. if ($action != 'add') {
  5548. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5549. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5550. } else {
  5551. $arrHide[] = $arrLP[$i]['id'];
  5552. }
  5553. } else {
  5554. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5555. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5556. }
  5557. }
  5558. reset($arrLP);
  5559. }
  5560. $return .= "\t\t\t\t" . '</select>';
  5561. $return .= '</td>';
  5562. $return .= '</tr>';
  5563. $return .= '<tr>';
  5564. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  5565. $return .= '<td class="input">';
  5566. $return .= "\t\t\t\t" . '<select id="previous" name="previous" size="1">';
  5567. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5568. for ($i = 0; $i < count($arrLP); $i++) {
  5569. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5570. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5571. $selected = 'selected="selected" ';
  5572. elseif ($action == 'add') $selected = 'selected="selected" ';
  5573. else
  5574. $selected = '';
  5575. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5576. }
  5577. }
  5578. $return .= "\t\t\t\t" . '</select>';
  5579. $return .= '</td>';
  5580. $return .= '</tr>';
  5581. if ($action != 'move') {
  5582. $return .= '<tr>';
  5583. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  5584. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5585. $return .= '</tr>';
  5586. $id_prerequisite = 0;
  5587. if (is_array($arrLP) && count($arrLP) > 0) {
  5588. foreach ($arrLP as $key => $value) {
  5589. if ($value['id'] == $id) {
  5590. $id_prerequisite = $value['prerequisite'];
  5591. break;
  5592. }
  5593. }
  5594. $arrHide = array ();
  5595. for ($i = 0; $i < count($arrLP); $i++) {
  5596. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5597. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5598. $s_selected_position = $arrLP[$i]['id'];
  5599. elseif ($action == 'add') $s_selected_position = 0;
  5600. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5601. }
  5602. }
  5603. }
  5604. //$return .= '<tr>';
  5605. //$return .= '<td class="label"><label for="idPrerequisites">'.get_lang('Prerequisites').' :</label></td>';
  5606. //$return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5607. /*
  5608. foreach ($arrHide as $key => $value) {
  5609. if ($key == $s_selected_position && $action == 'add') {
  5610. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5611. }
  5612. elseif ($key == $id_prerequisite && $action == 'edit') {
  5613. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5614. } else {
  5615. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  5616. }
  5617. }
  5618. */
  5619. //$return .= "</select></td>";
  5620. //$return .= '</tr>';
  5621. //$return .= '<tr>';
  5622. //$return .= '</tr>';
  5623. }
  5624. $return .= '<tr>';
  5625. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  5626. $return .= '</tr>';
  5627. $return .= '</table>';
  5628. if ($action == 'move') {
  5629. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5630. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5631. }
  5632. if (is_numeric($extra_info)) {
  5633. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5634. }
  5635. elseif (is_array($extra_info)) {
  5636. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5637. }
  5638. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  5639. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5640. $return .= '</form>';
  5641. return $return;
  5642. }
  5643. /**
  5644. * Return the form to display the forum edit/add option
  5645. * @param string Action (add/edit)
  5646. * @param integer ID of the lp_item if already exists
  5647. * @param mixed Forum ID or title
  5648. * @return string HTML form
  5649. */
  5650. public function display_forum_form($action = 'add', $id = 0, $extra_info = '') {
  5651. $course_id = api_get_course_int_id();
  5652. global $charset;
  5653. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5654. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  5655. if ($id != 0 && is_array($extra_info)) {
  5656. $item_title = stripslashes($extra_info['title']);
  5657. }
  5658. elseif (is_numeric($extra_info)) {
  5659. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  5660. FROM " . $tbl_forum . "
  5661. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  5662. $result = Database::query($sql_forum);
  5663. $row = Database :: fetch_array($result);
  5664. $item_title = $row['title'];
  5665. $item_description = $row['comment'];
  5666. } else {
  5667. $item_title = '';
  5668. $item_description = '';
  5669. }
  5670. $legend = '<legend>';
  5671. if ($id != 0 && is_array($extra_info))
  5672. $parent = $extra_info['parent_item_id'];
  5673. else
  5674. $parent = 0;
  5675. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5676. WHERE c_id = ".$course_id." AND
  5677. lp_id = " . $this->lp_id;
  5678. $result = Database::query($sql);
  5679. $arrLP = array ();
  5680. while ($row = Database :: fetch_array($result)) {
  5681. $arrLP[] = array (
  5682. 'id' => $row['id'],
  5683. 'item_type' => $row['item_type'],
  5684. 'title' => $row['title'],
  5685. 'path' => $row['path'],
  5686. 'description' => $row['description'],
  5687. 'parent_item_id' => $row['parent_item_id'],
  5688. 'previous_item_id' => $row['previous_item_id'],
  5689. 'next_item_id' => $row['next_item_id'],
  5690. 'display_order' => $row['display_order'],
  5691. 'max_score' => $row['max_score'],
  5692. 'min_score' => $row['min_score'],
  5693. 'mastery_score' => $row['mastery_score'],
  5694. 'prerequisite' => $row['prerequisite']
  5695. );
  5696. }
  5697. $this->tree_array($arrLP);
  5698. $arrLP = $this->arrMenu;
  5699. unset ($this->arrMenu);
  5700. if ($action == 'add')
  5701. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  5702. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  5703. else
  5704. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  5705. $legend .= '</legend>';
  5706. $return .= '<div class="sectioncomment">';
  5707. $return .= '<form method="POST">';
  5708. $return .= $legend;
  5709. $return .= '<table class="lp_form">';
  5710. if ($action != 'move') {
  5711. $return .= '<tr>';
  5712. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5713. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  5714. $return .= '</tr>';
  5715. }
  5716. $return .= '<tr>';
  5717. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5718. $return .= '<td class="input">';
  5719. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  5720. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5721. $arrHide = array (
  5722. $id
  5723. );
  5724. //$parent_item_id = $_SESSION['parent_item_id'];
  5725. for ($i = 0; $i < count($arrLP); $i++) {
  5726. if ($action != 'add') {
  5727. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5728. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5729. } else {
  5730. $arrHide[] = $arrLP[$i]['id'];
  5731. }
  5732. } else {
  5733. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5734. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5735. }
  5736. }
  5737. if (is_array($arrLP)) {
  5738. reset($arrLP);
  5739. }
  5740. $return .= "\t\t\t\t" . '</select>';
  5741. $return .= '</td>';
  5742. $return .= '</tr>';
  5743. $return .= '<tr>';
  5744. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5745. $return .= '<td class="input">';
  5746. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  5747. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5748. for ($i = 0; $i < count($arrLP); $i++) {
  5749. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5750. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5751. $selected = 'selected="selected" ';
  5752. elseif ($action == 'add') $selected = 'selected="selected" ';
  5753. else
  5754. $selected = '';
  5755. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5756. }
  5757. }
  5758. $return .= "\t\t\t\t" . '</select>';
  5759. $return .= '</td>';
  5760. $return .= '</tr>';
  5761. if ($action != 'move') {
  5762. $return .= '<tr>';
  5763. $return .= '</tr>';
  5764. $id_prerequisite = 0;
  5765. if (is_array($arrLP)) {
  5766. foreach ($arrLP as $key => $value) {
  5767. if ($value['id'] == $id) {
  5768. $id_prerequisite = $value['prerequisite'];
  5769. break;
  5770. }
  5771. }
  5772. }
  5773. $arrHide = array ();
  5774. for ($i = 0; $i < count($arrLP); $i++) {
  5775. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5776. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5777. $s_selected_position = $arrLP[$i]['id'];
  5778. elseif ($action == 'add') $s_selected_position = 0;
  5779. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5780. }
  5781. }
  5782. $return .= '</tr>';
  5783. }
  5784. $return .= '<tr>';
  5785. if ($action == 'add') {
  5786. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  5787. } else {
  5788. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  5789. }
  5790. $return .= '</tr>';
  5791. $return .= '</table>';
  5792. if ($action == 'move') {
  5793. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5794. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5795. }
  5796. if (is_numeric($extra_info)) {
  5797. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5798. }
  5799. elseif (is_array($extra_info)) {
  5800. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5801. }
  5802. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  5803. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5804. $return .= '</form>';
  5805. $return .= '</div>';
  5806. return $return;
  5807. }
  5808. /**
  5809. * Return HTML form to add/edit forum threads
  5810. * @param string Action (add/edit)
  5811. * @param integer Item ID if already exists in learning path
  5812. * @param mixed Extra information (thread ID if integer)
  5813. * @return string HTML form
  5814. */
  5815. public function display_thread_form($action = 'add', $id = 0, $extra_info = '') {
  5816. $course_id = api_get_course_int_id();
  5817. if (empty($course_id)) {
  5818. return null;
  5819. }
  5820. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5821. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  5822. if ($id != 0 && is_array($extra_info)) {
  5823. $item_title = stripslashes($extra_info['title']);
  5824. } elseif (is_numeric($extra_info)) {
  5825. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  5826. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  5827. $result = Database::query($sql_forum);
  5828. $row = Database :: fetch_array($result);
  5829. $item_title = $row['title'];
  5830. $item_description = '';
  5831. } else {
  5832. $item_title = '';
  5833. $item_description = '';
  5834. }
  5835. $return = null;
  5836. if ($id != 0 && is_array($extra_info))
  5837. $parent = $extra_info['parent_item_id'];
  5838. else
  5839. $parent = 0;
  5840. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5841. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5842. $result = Database::query($sql);
  5843. $arrLP = array ();
  5844. while ($row = Database :: fetch_array($result)) {
  5845. $arrLP[] = array (
  5846. 'id' => $row['id'],
  5847. 'item_type' => $row['item_type'],
  5848. 'title' => $row['title'],
  5849. 'path' => $row['path'],
  5850. 'description' => $row['description'],
  5851. 'parent_item_id' => $row['parent_item_id'],
  5852. 'previous_item_id' => $row['previous_item_id'],
  5853. 'next_item_id' => $row['next_item_id'],
  5854. 'display_order' => $row['display_order'],
  5855. 'max_score' => $row['max_score'],
  5856. 'min_score' => $row['min_score'],
  5857. 'mastery_score' => $row['mastery_score'],
  5858. 'prerequisite' => $row['prerequisite']
  5859. );
  5860. }
  5861. $this->tree_array($arrLP);
  5862. $arrLP = $this->arrMenu;
  5863. unset ($this->arrMenu);
  5864. $return .= '<form method="POST">';
  5865. if ($action == 'add')
  5866. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  5867. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  5868. else
  5869. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  5870. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5871. $return .= '<tr>';
  5872. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5873. $return .= '<td class="input">';
  5874. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5875. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5876. $arrHide = array (
  5877. $id
  5878. );
  5879. for ($i = 0; $i < count($arrLP); $i++) {
  5880. if ($action != 'add') {
  5881. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5882. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5883. } else {
  5884. $arrHide[] = $arrLP[$i]['id'];
  5885. }
  5886. } else {
  5887. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5888. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5889. }
  5890. }
  5891. if ($arrLP != null) {
  5892. reset($arrLP);
  5893. }
  5894. $return .= '</select>';
  5895. $return .= '</td>';
  5896. $return .= '</tr>';
  5897. $return .= '<tr>';
  5898. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5899. $return .= '<td class="input">';
  5900. $return .= "\t\t\t\t" . '<select id="previous" name="previous" size="1">';
  5901. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5902. for ($i = 0; $i < count($arrLP); $i++) {
  5903. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5904. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5905. $selected = 'selected="selected" ';
  5906. elseif ($action == 'add') $selected = 'selected="selected" ';
  5907. else
  5908. $selected = '';
  5909. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5910. }
  5911. }
  5912. $return .= "\t\t\t\t" . '</select>';
  5913. $return .= '</td>';
  5914. $return .= '</tr>';
  5915. if ($action != 'move') {
  5916. $return .= '<tr>';
  5917. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5918. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5919. $return .= '</tr>';
  5920. $return .= '<tr>';
  5921. $return .= '</tr>';
  5922. $id_prerequisite = 0;
  5923. if ($arrLP != null) {
  5924. foreach ($arrLP as $key => $value) {
  5925. if ($value['id'] == $id) {
  5926. $id_prerequisite = $value['prerequisite'];
  5927. break;
  5928. }
  5929. }
  5930. }
  5931. $arrHide = array();
  5932. for ($i = 0; $i < count($arrLP); $i++) {
  5933. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5934. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5935. $s_selected_position = $arrLP[$i]['id'];
  5936. elseif ($action == 'add') $s_selected_position = 0;
  5937. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5938. }
  5939. }
  5940. $return .= '<tr>';
  5941. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('Prerequisites') . '</label></td>';
  5942. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  5943. foreach ($arrHide as $key => $value) {
  5944. if ($key == $s_selected_position && $action == 'add') {
  5945. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5946. }
  5947. elseif ($key == $id_prerequisite && $action == 'edit') {
  5948. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5949. } else {
  5950. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  5951. }
  5952. }
  5953. $return .= "</select></td>";
  5954. $return .= '</tr>';
  5955. }
  5956. $return .= '<tr>';
  5957. $return .= '<td></td><td>
  5958. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  5959. $return .= '</tr>';
  5960. $return .= '</table>';
  5961. if ($action == 'move') {
  5962. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5963. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5964. }
  5965. if (is_numeric($extra_info)) {
  5966. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5967. }
  5968. elseif (is_array($extra_info)) {
  5969. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5970. }
  5971. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  5972. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5973. $return .= '</form>';
  5974. $return .= '</div>';
  5975. return $return;
  5976. }
  5977. /**
  5978. * Return the HTML form to display an item (generally a section/module item)
  5979. * @param string Item type (module/dokeos_module)
  5980. * @param string Title (optional, only when creating)
  5981. * @param string Action ('add'/'edit')
  5982. * @param integer lp_item ID
  5983. * @param mixed Extra info
  5984. * @return string HTML form
  5985. */
  5986. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new') {
  5987. $course_id = api_get_course_int_id();
  5988. global $_course;
  5989. global $charset;
  5990. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5991. if ($id != 0 && is_array($extra_info)) {
  5992. $item_title = $extra_info['title'];
  5993. $item_description = $extra_info['description'];
  5994. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  5995. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  5996. } else {
  5997. $item_title = '';
  5998. $item_description = '';
  5999. $item_path_fck = '';
  6000. }
  6001. $legend = '<legend>';
  6002. if ($id != 0 && is_array($extra_info))
  6003. $parent = $extra_info['parent_item_id'];
  6004. else
  6005. $parent = 0;
  6006. $id = intval($id);
  6007. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6008. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND id != $id";
  6009. if ($item_type == 'module')
  6010. $sql .= " AND parent_item_id = 0";
  6011. $result = Database::query($sql);
  6012. $arrLP = array ();
  6013. while ($row = Database :: fetch_array($result)) {
  6014. $arrLP[] = array (
  6015. 'id' => $row['id'],
  6016. 'item_type' => $row['item_type'],
  6017. 'title' => $row['title'],
  6018. 'path' => $row['path'],
  6019. 'description' => $row['description'],
  6020. 'parent_item_id' => $row['parent_item_id'],
  6021. 'previous_item_id' => $row['previous_item_id'],
  6022. 'next_item_id' => $row['next_item_id'],
  6023. 'max_score' => $row['max_score'],
  6024. 'min_score' => $row['min_score'],
  6025. 'mastery_score' => $row['mastery_score'],
  6026. 'prerequisite' => $row['prerequisite'],
  6027. 'display_order' => $row['display_order']
  6028. );
  6029. }
  6030. $this->tree_array($arrLP);
  6031. $arrLP = $this->arrMenu;
  6032. unset ($this->arrMenu);
  6033. $legend .= $title;
  6034. $legend .= '</legend>';
  6035. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6036. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6037. $form = new FormValidator('form', 'POST', $url);
  6038. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6039. $defaults['description'] = $item_description;
  6040. $form->addElement('html', $legend);
  6041. //$arrHide = array($id);
  6042. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6043. $arrHide[0]['padding'] = 3;
  6044. $charset = api_get_system_encoding();
  6045. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6046. for ($i = 0; $i < count($arrLP); $i++) {
  6047. if ($action != 'add') {
  6048. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6049. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6050. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6051. if ($parent == $arrLP[$i]['id']) {
  6052. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6053. }
  6054. }
  6055. } else {
  6056. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6057. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6058. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6059. if ($parent == $arrLP[$i]['id']) {
  6060. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6061. }
  6062. }
  6063. }
  6064. }
  6065. if ($action != 'move') {
  6066. $form->addElement('text', 'title', get_lang('Title'), 'id="idTitle" class="learnpath_chapter_form" size="40%"');
  6067. $form->applyFilter('title', 'html_filter');
  6068. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6069. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6070. } else {
  6071. $form->addElement('hidden', 'title');
  6072. }
  6073. $parent_select = & $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"');
  6074. foreach ($arrHide as $key => $value) {
  6075. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6076. }
  6077. if (!empty($s_selected_parent)) {
  6078. $parent_select->setSelected($s_selected_parent);
  6079. }
  6080. }
  6081. if (is_array($arrLP)) {
  6082. reset($arrLP);
  6083. }
  6084. $arrHide = array();
  6085. // POSITION
  6086. for ($i = 0; $i < count($arrLP); $i++) {
  6087. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6088. //this is the same!
  6089. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6090. $s_selected_position = $arrLP[$i]['id'];
  6091. } elseif ($action == 'add') {
  6092. $s_selected_position = $arrLP[$i]['id'];
  6093. }
  6094. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6095. }
  6096. }
  6097. $position = & $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_chapter_form" style="width:37%;"');
  6098. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6099. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6100. foreach ($arrHide as $key => $value) {
  6101. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6102. }
  6103. if (!empty ($s_selected_position)) {
  6104. $position->setSelected($s_selected_position);
  6105. }
  6106. if (is_array($arrLP)) {
  6107. reset($arrLP);
  6108. }
  6109. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6110. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6111. $form->addElement('hidden', 'parent', '0');
  6112. }
  6113. //fix in order to use the tab
  6114. if ($item_type == 'chapter') {
  6115. $form->addElement('hidden', 'type', 'chapter');
  6116. }
  6117. $extension = null;
  6118. if (!empty($item_path)) {
  6119. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6120. }
  6121. //assets can't be modified
  6122. //$item_type == 'asset' ||
  6123. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6124. if ($item_type == 'sco') {
  6125. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6126. }
  6127. $renderer = $form->defaultRenderer();
  6128. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6129. $relative_prefix = '';
  6130. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6131. 'Width' => '100%',
  6132. 'Height' => '500',
  6133. 'FullPage' => true,
  6134. 'CreateDocumentDir' => $relative_prefix,
  6135. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6136. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6137. );
  6138. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6139. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6140. //$defaults['content_lp'] = file_get_contents($item_path);
  6141. $defaults['content_lp'] = file_get_contents($content_path);
  6142. }
  6143. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6144. $form->addElement('hidden', 'post_time', time());
  6145. $form->setDefaults($defaults);
  6146. return $form->return_form();
  6147. }
  6148. /**
  6149. * Returns the form to update or create a document
  6150. * @param string Action (add/edit)
  6151. * @param integer ID of the lp_item (if already exists)
  6152. * @param mixed Integer if document ID, string if info ('new')
  6153. * @return string HTML form
  6154. */
  6155. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new') {
  6156. $course_id = api_get_course_int_id();
  6157. global $charset;
  6158. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6159. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6160. $no_display_edit_textarea = false;
  6161. //If action==edit document
  6162. //We don't display the document form if it's not an editable document (html or txt file)
  6163. if ($action == "edit") {
  6164. if (is_array($extra_info)) {
  6165. $path_parts = pathinfo($extra_info['dir']);
  6166. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6167. $no_display_edit_textarea = true;
  6168. }
  6169. }
  6170. }
  6171. $no_display_add = false;
  6172. // If action==add an existing document
  6173. // We don't display the document form if it's not an editable document (html or txt file).
  6174. if ($action == "add") {
  6175. if (is_numeric($extra_info)) {
  6176. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6177. $result = Database::query($sql_doc);
  6178. $path_file = Database :: result($result, 0, 0);
  6179. $path_parts = pathinfo($path_file);
  6180. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6181. $no_display_add = true;
  6182. }
  6183. }
  6184. }
  6185. if ($id != 0 && is_array($extra_info)) {
  6186. $item_title = stripslashes($extra_info['title']);
  6187. $item_description = stripslashes($extra_info['description']);
  6188. $item_terms = stripslashes($extra_info['terms']);
  6189. if (empty ($item_title)) {
  6190. $path_parts = pathinfo($extra_info['path']);
  6191. $item_title = stripslashes($path_parts['filename']);
  6192. }
  6193. } elseif (is_numeric($extra_info)) {
  6194. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6195. WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6196. $result = Database::query($sql_doc);
  6197. $row = Database::fetch_array($result);
  6198. $explode = explode('.', $row['title']);
  6199. if (count($explode) > 1) {
  6200. for ($i = 0; $i < count($explode) - 1; $i++)
  6201. $item_title .= $explode[$i];
  6202. } else {
  6203. $item_title = $row['title'];
  6204. }
  6205. $item_title = str_replace('_', ' ', $item_title);
  6206. if (empty ($item_title)) {
  6207. $path_parts = pathinfo($row['path']);
  6208. $item_title = stripslashes($path_parts['filename']);
  6209. }
  6210. } else {
  6211. $item_title = '';
  6212. $item_description = '';
  6213. }
  6214. $return = '<legend>';
  6215. if ($id != 0 && is_array($extra_info))
  6216. $parent = $extra_info['parent_item_id'];
  6217. else
  6218. $parent = 0;
  6219. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6220. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6221. $result = Database::query($sql);
  6222. $arrLP = array ();
  6223. while ($row = Database :: fetch_array($result)) {
  6224. $arrLP[] = array (
  6225. 'id' => $row['id'],
  6226. 'item_type' => $row['item_type'],
  6227. 'title' => $row['title'],
  6228. 'path' => $row['path'],
  6229. 'description' => $row['description'],
  6230. 'parent_item_id' => $row['parent_item_id'],
  6231. 'previous_item_id' => $row['previous_item_id'],
  6232. 'next_item_id' => $row['next_item_id'],
  6233. 'display_order' => $row['display_order'],
  6234. 'max_score' => $row['max_score'],
  6235. 'min_score' => $row['min_score'],
  6236. 'mastery_score' => $row['mastery_score'],
  6237. 'prerequisite' => $row['prerequisite']
  6238. );
  6239. }
  6240. $this->tree_array($arrLP);
  6241. $arrLP = $this->arrMenu;
  6242. unset ($this->arrMenu);
  6243. if ($action == 'add') {
  6244. $return .= get_lang('CreateTheDocument');
  6245. } elseif ($action == 'move') {
  6246. $return .= get_lang('MoveTheCurrentDocument');
  6247. } else {
  6248. $return .= get_lang('EditTheCurrentDocument');
  6249. }
  6250. $return .= '</legend>';
  6251. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6252. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6253. }
  6254. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6255. $defaults['title'] = Security :: remove_XSS($item_title);
  6256. if (empty($item_title)) {
  6257. $defaults['title'] = Security::remove_XSS($item_title);
  6258. }
  6259. $defaults['description'] = $item_description;
  6260. $form->addElement('html', $return);
  6261. if ($action != 'move') {
  6262. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6263. $form->applyFilter('title', 'html_filter');
  6264. }
  6265. //$arrHide = array($id);
  6266. $arrHide[0]['value'] = $this->name;
  6267. $arrHide[0]['padding'] = 3;
  6268. for ($i = 0; $i < count($arrLP); $i++) {
  6269. if ($action != 'add') {
  6270. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6271. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6272. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6273. if ($parent == $arrLP[$i]['id']) {
  6274. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6275. }
  6276. }
  6277. } else {
  6278. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6279. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6280. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6281. if ($parent == $arrLP[$i]['id']) {
  6282. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6283. }
  6284. }
  6285. }
  6286. }
  6287. $parent_select = & $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6288. $my_count=0;
  6289. foreach ($arrHide as $key => $value) {
  6290. if ($my_count!=0) {
  6291. // The LP name is also the first section and is not in the same charset like the other sections.
  6292. $value['value'] = Security :: remove_XSS($value['value']);
  6293. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6294. } else {
  6295. $value['value'] = Security :: remove_XSS($value['value']);
  6296. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6297. }
  6298. $my_count++;
  6299. }
  6300. if (!empty($id)) {
  6301. $parent_select->setSelected($parent);
  6302. } else {
  6303. $parent_item_id = $_SESSION['parent_item_id'];
  6304. $parent_select->setSelected($parent_item_id);
  6305. }
  6306. if (is_array($arrLP)) {
  6307. reset($arrLP);
  6308. }
  6309. $arrHide = array ();
  6310. //POSITION
  6311. for ($i = 0; $i < count($arrLP); $i++) {
  6312. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6313. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6314. $s_selected_position = $arrLP[$i]['id'];
  6315. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6316. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6317. }
  6318. }
  6319. $position = & $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6320. $position->addOption(get_lang('FirstPosition'), 0);
  6321. foreach ($arrHide as $key => $value) {
  6322. $padding = isset($value['padding']) ? $value['padding']: 0;
  6323. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6324. }
  6325. $position->setSelected($s_selected_position);
  6326. if (is_array($arrLP)) {
  6327. reset($arrLP);
  6328. }
  6329. if ($action != 'move') {
  6330. $id_prerequisite = 0;
  6331. if (is_array($arrLP)) {
  6332. foreach ($arrLP as $key => $value) {
  6333. if ($value['id'] == $id) {
  6334. $id_prerequisite = $value['prerequisite'];
  6335. break;
  6336. }
  6337. }
  6338. }
  6339. $arrHide = array ();
  6340. for ($i = 0; $i < count($arrLP); $i++) {
  6341. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6342. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6343. $s_selected_position = $arrLP[$i]['id'];
  6344. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6345. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6346. }
  6347. }
  6348. if (!$no_display_add) {
  6349. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6350. if (isset ($_POST['content']))
  6351. $content = stripslashes($_POST['content']);
  6352. elseif (is_array($extra_info)) {
  6353. //If it's an html document or a text file
  6354. if (!$no_display_edit_textarea) {
  6355. $content = $this->display_document($extra_info['path'], false, false);
  6356. }
  6357. } elseif (is_numeric($extra_info))
  6358. $content = $this->display_document($extra_info, false, false);
  6359. else
  6360. $content = '';
  6361. if (!$no_display_edit_textarea) {
  6362. // We need to calculate here some specific settings for the online editor.
  6363. // The calculated settings work for documents in the Documents tool
  6364. // (on the root or in subfolders).
  6365. // For documents in native scorm packages it is unclear whether the
  6366. // online editor should be activated or not.
  6367. // A new document, it is in the root of the repository.
  6368. $relative_path = '';
  6369. $relative_prefix = '';
  6370. if (is_array($extra_info) && $extra_info != 'new') {
  6371. // The document already exists. Whe have to determine its relative path towards the repository root.
  6372. $relative_path = explode('/', $extra_info['dir']);
  6373. $cnt = count($relative_path) - 2;
  6374. if ($cnt < 0) {
  6375. $cnt = 0;
  6376. }
  6377. $relative_prefix = str_repeat('../', $cnt);
  6378. $relative_path = array_slice($relative_path, 1, $cnt);
  6379. $relative_path = implode('/', $relative_path);
  6380. if (strlen($relative_path) > 0) {
  6381. $relative_path = $relative_path . '/';
  6382. }
  6383. } else {
  6384. global $_course;
  6385. $result = $this->generate_lp_folder($_course);
  6386. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6387. $relative_prefix = '../../';
  6388. }
  6389. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6390. 'Width' => '100%',
  6391. 'Height' => '500',
  6392. 'FullPage' => true,
  6393. 'CreateDocumentDir' => $relative_prefix,
  6394. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6395. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6396. );
  6397. if ($_GET['action'] == 'add_item') {
  6398. $class = 'add';
  6399. $text = get_lang('LPCreateDocument');
  6400. } else
  6401. if ($_GET['action'] == 'edit_item') {
  6402. $class = 'save';
  6403. $text = get_lang('SaveDocument');
  6404. }
  6405. $form->addElement('style_submit_button', 'submit_button', $text, 'class="' . $class . '"');
  6406. $renderer = $form->defaultRenderer();
  6407. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6408. $form->addElement('html', '<div>');
  6409. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6410. $form->addElement('html', '</div>');
  6411. $defaults['content_lp'] = $content;
  6412. }
  6413. } elseif (is_numeric($extra_info)) {
  6414. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6415. $return = $this->display_document($extra_info, true, true, true);
  6416. $form->addElement('html', $return);
  6417. }
  6418. }
  6419. }
  6420. if ($action == 'move') {
  6421. $form->addElement('hidden', 'title', $item_title);
  6422. $form->addElement('hidden', 'description', $item_description);
  6423. }
  6424. if (is_numeric($extra_info)) {
  6425. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'value="submit_button", class="save"');
  6426. $form->addElement('hidden', 'path', $extra_info);
  6427. } elseif (is_array($extra_info)) {
  6428. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6429. $form->addElement('hidden', 'path', $extra_info['path']);
  6430. }
  6431. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6432. $form->addElement('hidden', 'post_time', time());
  6433. $form->setDefaults($defaults);
  6434. return $form->return_form();
  6435. }
  6436. /**
  6437. * Return HTML form to add/edit a link item
  6438. * @param string Action (add/edit)
  6439. * @param integer Item ID if exists
  6440. * @param mixed Extra info
  6441. * @return string HTML form
  6442. */
  6443. public function display_link_form($action = 'add', $id = 0, $extra_info = '') {
  6444. $course_id = api_get_course_int_id();
  6445. global $charset;
  6446. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6447. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6448. if ($id != 0 && is_array($extra_info)) {
  6449. $item_title = stripslashes($extra_info['title']);
  6450. $item_description = stripslashes($extra_info['description']);
  6451. $item_url = stripslashes($extra_info['url']);
  6452. } elseif (is_numeric($extra_info)) {
  6453. $sql_link = "SELECT title, description, url FROM " . $tbl_link . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6454. $result = Database::query($sql_link);
  6455. $row = Database :: fetch_array($result);
  6456. $item_title = $row['title'];
  6457. $item_description = $row['description'];
  6458. $item_url = $row['url'];
  6459. } else {
  6460. $item_title = '';
  6461. $item_description = '';
  6462. $item_url = '';
  6463. }
  6464. $legend = '<legend>';
  6465. if ($id != 0 && is_array($extra_info))
  6466. $parent = $extra_info['parent_item_id'];
  6467. else
  6468. $parent = 0;
  6469. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6470. $result = Database::query($sql);
  6471. $arrLP = array ();
  6472. while ($row = Database :: fetch_array($result)) {
  6473. $arrLP[] = array (
  6474. 'id' => $row['id'],
  6475. 'item_type' => $row['item_type'],
  6476. 'title' => $row['title'],
  6477. 'path' => $row['path'],
  6478. 'description' => $row['description'],
  6479. 'parent_item_id' => $row['parent_item_id'],
  6480. 'previous_item_id' => $row['previous_item_id'],
  6481. 'next_item_id' => $row['next_item_id'],
  6482. 'display_order' => $row['display_order'],
  6483. 'max_score' => $row['max_score'],
  6484. 'min_score' => $row['min_score'],
  6485. 'mastery_score' => $row['mastery_score'],
  6486. 'prerequisite' => $row['prerequisite']
  6487. );
  6488. }
  6489. $this->tree_array($arrLP);
  6490. $arrLP = $this->arrMenu;
  6491. unset ($this->arrMenu);
  6492. if ($action == 'add')
  6493. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6494. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6495. else
  6496. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  6497. $legend .= '</legend>';
  6498. $return .= '<div class="sectioncomment">';
  6499. $return .= '<form method="POST">';
  6500. $return .= $legend;
  6501. $return .= '<table>';
  6502. if ($action != 'move') {
  6503. $return .= '<tr>';
  6504. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6505. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  6506. $return .= '</tr>';
  6507. }
  6508. $return .= '<tr>';
  6509. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6510. $return .= '<td class="input">';
  6511. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6512. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6513. $arrHide = array (
  6514. $id
  6515. );
  6516. $parent_item_id = $_SESSION['parent_item_id'];
  6517. for ($i = 0; $i < count($arrLP); $i++) {
  6518. if ($action != 'add') {
  6519. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6520. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6521. } else {
  6522. $arrHide[] = $arrLP[$i]['id'];
  6523. }
  6524. } else {
  6525. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6526. $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>';
  6527. }
  6528. }
  6529. if (is_array($arrLP)) {
  6530. reset($arrLP);
  6531. }
  6532. $return .= '</select>';
  6533. $return .= '</td>';
  6534. $return .= '</tr>';
  6535. $return .= '<tr>';
  6536. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6537. $return .= '<td class="input">';
  6538. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6539. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6540. for ($i = 0; $i < count($arrLP); $i++) {
  6541. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  6542. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6543. $selected = 'selected="selected" ';
  6544. elseif ($action == 'add')
  6545. $selected = 'selected="selected" ';
  6546. else
  6547. $selected = '';
  6548. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6549. }
  6550. }
  6551. $return .= '</select>';
  6552. $return .= '</td>';
  6553. $return .= '</tr>';
  6554. if ($action != 'move') {
  6555. $return .= '<tr>';
  6556. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  6557. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  6558. $return .= '</tr>';
  6559. $id_prerequisite = 0;
  6560. if (is_array($arrLP)) {
  6561. foreach ($arrLP as $key => $value) {
  6562. if ($value['id'] == $id) {
  6563. $id_prerequisite = $value['prerequisite'];
  6564. break;
  6565. }
  6566. }
  6567. }
  6568. $arrHide = array();
  6569. for ($i = 0; $i < count($arrLP); $i++) {
  6570. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6571. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6572. $s_selected_position = $arrLP[$i]['id'];
  6573. elseif ($action == 'add') $s_selected_position = 0;
  6574. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6575. }
  6576. }
  6577. $return .= '</tr>';
  6578. }
  6579. $return .= '<tr>';
  6580. if ($action == 'add') {
  6581. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  6582. } else {
  6583. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  6584. }
  6585. $return .= '</tr>';
  6586. $return .= '</table>';
  6587. if ($action == 'move') {
  6588. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6589. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6590. }
  6591. if (is_numeric($extra_info)) {
  6592. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6593. } elseif (is_array($extra_info)) {
  6594. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6595. }
  6596. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  6597. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6598. $return .= '</form>';
  6599. $return .= '</div>';
  6600. return $return;
  6601. }
  6602. /**
  6603. * Return HTML form to add/edit a student publication (work)
  6604. * @param string Action (add/edit)
  6605. * @param integer Item ID if already exists
  6606. * @param mixed Extra info (work ID if integer)
  6607. * @return string HTML form
  6608. */
  6609. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '') {
  6610. $course_id = api_get_course_int_id();
  6611. global $charset;
  6612. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6613. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  6614. if ($id != 0 && is_array($extra_info)) {
  6615. $item_title = stripslashes($extra_info['title']);
  6616. $item_description = stripslashes($extra_info['description']);
  6617. }
  6618. elseif (is_numeric($extra_info)) {
  6619. $sql_publication = "SELECT title, description FROM " . $tbl_publication . "
  6620. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6621. $result = Database::query($sql_publication);
  6622. $row = Database :: fetch_array($result);
  6623. $item_title = $row['title'];
  6624. } else {
  6625. $item_title = get_lang('Student_publication');
  6626. }
  6627. $legend = '<legend>';
  6628. if ($id != 0 && is_array($extra_info))
  6629. $parent = $extra_info['parent_item_id'];
  6630. else
  6631. $parent = 0;
  6632. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6633. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6634. $result = Database::query($sql);
  6635. $arrLP = array ();
  6636. while ($row = Database :: fetch_array($result)) {
  6637. $arrLP[] = array (
  6638. 'id' => $row['id'],
  6639. 'item_type' => $row['item_type'],
  6640. 'title' => $row['title'],
  6641. 'path' => $row['path'],
  6642. 'description' => $row['description'],
  6643. 'parent_item_id' => $row['parent_item_id'],
  6644. 'previous_item_id' => $row['previous_item_id'],
  6645. 'next_item_id' => $row['next_item_id'],
  6646. 'display_order' => $row['display_order'],
  6647. 'max_score' => $row['max_score'],
  6648. 'min_score' => $row['min_score'],
  6649. 'mastery_score' => $row['mastery_score'],
  6650. 'prerequisite' => $row['prerequisite']
  6651. );
  6652. }
  6653. $this->tree_array($arrLP);
  6654. $arrLP = $this->arrMenu;
  6655. unset ($this->arrMenu);
  6656. if ($action == 'add')
  6657. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  6658. elseif ($action == 'move') $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  6659. else
  6660. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  6661. $legend .= '</legend>';
  6662. $return .= '<div class="sectioncomment">';
  6663. $return .= '<form method="POST">';
  6664. $return .= $legend;
  6665. $return .= '<table class="lp_form">';
  6666. if ($action != 'move') {
  6667. $return .= '<tr>';
  6668. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6669. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6670. $return .= '</tr>';
  6671. }
  6672. $return .= '<tr>';
  6673. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6674. $return .= '<td class="input">';
  6675. $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">';
  6676. //$parent_item_id = $_SESSION['parent_item_id'];
  6677. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6678. $arrHide = array (
  6679. $id
  6680. );
  6681. for ($i = 0; $i < count($arrLP); $i++) {
  6682. if ($action != 'add') {
  6683. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6684. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6685. } else {
  6686. $arrHide[] = $arrLP[$i]['id'];
  6687. }
  6688. } else {
  6689. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6690. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6691. }
  6692. }
  6693. if (is_array($arrLP)) {
  6694. reset($arrLP);
  6695. }
  6696. $return .= "\t\t\t\t" . '</select>';
  6697. $return .= '</td>';
  6698. $return .= '</tr>';
  6699. $return .= '<tr>';
  6700. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6701. $return .= '<td class="input">';
  6702. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6703. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6704. for ($i = 0; $i < count($arrLP); $i++) {
  6705. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6706. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6707. $selected = 'selected="selected" ';
  6708. elseif ($action == 'add') $selected = 'selected="selected" ';
  6709. else
  6710. $selected = '';
  6711. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6712. }
  6713. }
  6714. $return .= "\t\t\t\t" . '</select>';
  6715. $return .= '</td>';
  6716. $return .= '</tr>';
  6717. if ($action != 'move') {
  6718. $id_prerequisite = 0;
  6719. if (is_array($arrLP)) {
  6720. foreach ($arrLP as $key => $value) {
  6721. if ($value['id'] == $id) {
  6722. $id_prerequisite = $value['prerequisite'];
  6723. break;
  6724. }
  6725. }
  6726. }
  6727. $arrHide = array ();
  6728. for ($i = 0; $i < count($arrLP); $i++) {
  6729. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6730. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6731. $s_selected_position = $arrLP[$i]['id'];
  6732. elseif ($action == 'add') $s_selected_position = 0;
  6733. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6734. }
  6735. }
  6736. // Commented the prerequisites, only visible in edit (work).
  6737. /*
  6738. $return .= '<tr>';
  6739. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('Prerequisites').'</label></td>';
  6740. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6741. foreach($arrHide as $key => $value) {
  6742. if ($key == $s_selected_position && $action == 'add') {
  6743. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6744. }
  6745. elseif ($key == $id_prerequisite && $action == 'edit') {
  6746. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6747. }
  6748. else {
  6749. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6750. }
  6751. }
  6752. $return .= "</select></td>";
  6753. */
  6754. $return .= '</tr>';
  6755. }
  6756. $return .= '<tr>';
  6757. if ($action == 'add') {
  6758. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  6759. } else {
  6760. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  6761. }
  6762. $return .= '</tr>';
  6763. $return .= '</table>';
  6764. if ($action == 'move') {
  6765. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6766. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6767. }
  6768. if (is_numeric($extra_info)) {
  6769. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6770. } elseif (is_array($extra_info)) {
  6771. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6772. }
  6773. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  6774. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6775. $return .= '</form>';
  6776. $return .= '</div>';
  6777. return $return;
  6778. }
  6779. /**
  6780. * Displays the menu for manipulating a step
  6781. * @return string html
  6782. */
  6783. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT) {
  6784. $course_id = api_get_course_int_id();
  6785. $course_code = api_get_course_id();
  6786. global $charset, $_course;
  6787. $return = '<div class="actions">';
  6788. switch ($item_type) {
  6789. case 'dokeos_chapter' :
  6790. case 'chapter' :
  6791. // Commented the message cause should not show it.
  6792. //$lang = get_lang('TitleManipulateChapter');
  6793. break;
  6794. case 'dokeos_module' :
  6795. case 'module' :
  6796. // Commented the message cause should not show it.
  6797. //$lang = get_lang('TitleManipulateModule');
  6798. break;
  6799. case TOOL_DOCUMENT :
  6800. // Commented the message cause should not show it.
  6801. //$lang = get_lang('TitleManipulateDocument');
  6802. break;
  6803. case TOOL_LINK :
  6804. case 'link' :
  6805. // Commented the message cause should not show it.
  6806. //$lang = get_lang('TitleManipulateLink');
  6807. break;
  6808. case TOOL_QUIZ :
  6809. // Commented the message cause should not show it.
  6810. //$lang = get_lang('TitleManipulateQuiz');
  6811. break;
  6812. case TOOL_STUDENTPUBLICATION :
  6813. // Commented the message cause should not show it.
  6814. //$lang = get_lang('TitleManipulateStudentPublication');
  6815. break;
  6816. }
  6817. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6818. $item_id = intval($item_id);
  6819. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  6820. $result = Database::query($sql);
  6821. $row = Database::fetch_assoc($result);
  6822. $audio_player = null;
  6823. // We display an audio player if needed.
  6824. if (!empty($row['audio'])) {
  6825. $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>';
  6826. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  6827. $audio_player .= '<script type="text/javascript">
  6828. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  6829. s1.addParam("allowscriptaccess","always");
  6830. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  6831. s1.write("container");
  6832. </script>';
  6833. }
  6834. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  6835. $return .= Display::url(Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item&path_item=' . $row['path']);
  6836. $return .= Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL), $url.'&action=move_item');
  6837. // Commented for now as prerequisites cannot be added to chapters.
  6838. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  6839. $return .= Display::url(Display::return_icon('accept.png', get_lang('Prerequisites'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item_prereq');
  6840. }
  6841. $return .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), $url.'&action=delete_item');
  6842. if ($item_type == TOOL_HOTPOTATOES ) {
  6843. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6844. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6845. }
  6846. if ($item_type == TOOL_DOCUMENT ) {
  6847. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6848. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6849. }
  6850. $return .= '</div>';
  6851. if (!empty($audio_player)) {
  6852. $return .= '<br />'.$audio_player;
  6853. }
  6854. return $return;
  6855. }
  6856. /**
  6857. * Creates the javascript needed for filling up the checkboxes without page reload
  6858. *
  6859. * @return string
  6860. */
  6861. public function get_js_dropdown_array() {
  6862. $course_id = api_get_course_int_id();
  6863. $return = 'var child_name = new Array();' . "\n";
  6864. $return .= 'var child_value = new Array();' . "\n\n";
  6865. $return .= 'child_name[0] = new Array();' . "\n";
  6866. $return .= 'child_value[0] = new Array();' . "\n\n";
  6867. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6868. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  6869. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  6870. ORDER BY display_order ASC";
  6871. $res_zero = Database::query($sql_zero);
  6872. global $charset;
  6873. $i = 0;
  6874. while ($row_zero = Database :: fetch_array($res_zero)) {
  6875. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  6876. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  6877. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  6878. }
  6879. $return .= "\n";
  6880. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6881. $res = Database::query($sql);
  6882. while ($row = Database :: fetch_array($res)) {
  6883. $sql_parent = "
  6884. SELECT * FROM " . $tbl_lp_item . "
  6885. WHERE c_id = ".$course_id." AND parent_item_id = " . $row['id'] . "
  6886. ORDER BY display_order ASC";
  6887. $res_parent = Database::query($sql_parent);
  6888. $i = 0;
  6889. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  6890. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  6891. while ($row_parent = Database :: fetch_array($res_parent)) {
  6892. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  6893. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  6894. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  6895. }
  6896. $return .= "\n";
  6897. }
  6898. return $return;
  6899. }
  6900. /**
  6901. * Display the form to allow moving an item
  6902. * @param integer Item ID
  6903. * @return string HTML form
  6904. */
  6905. public function display_move_item($item_id) {
  6906. $course_id = api_get_course_int_id();
  6907. global $_course; //will disappear
  6908. global $charset;
  6909. $return = '';
  6910. if (is_numeric($item_id)) {
  6911. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6912. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6913. WHERE c_id = ".$course_id." AND id = " . $item_id;
  6914. $res = Database::query($sql);
  6915. $row = Database :: fetch_array($res);
  6916. switch ($row['item_type']) {
  6917. case 'dokeos_chapter' :
  6918. case 'dir' :
  6919. case 'asset' :
  6920. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6921. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  6922. break;
  6923. case 'dokeos_module' :
  6924. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6925. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  6926. break;
  6927. case TOOL_DOCUMENT :
  6928. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6929. $return .= $this->display_document_form('move', $item_id, $row);
  6930. break;
  6931. case TOOL_LINK :
  6932. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6933. $return .= $this->display_link_form('move', $item_id, $row);
  6934. break;
  6935. case TOOL_HOTPOTATOES :
  6936. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6937. $return .= $this->display_link_form('move', $item_id, $row);
  6938. break;
  6939. case TOOL_QUIZ :
  6940. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6941. $return .= $this->display_quiz_form('move', $item_id, $row);
  6942. break;
  6943. case TOOL_STUDENTPUBLICATION :
  6944. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6945. $return .= $this->display_student_publication_form('move', $item_id, $row);
  6946. break;
  6947. case TOOL_FORUM :
  6948. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6949. $return .= $this->display_forum_form('move', $item_id, $row);
  6950. break;
  6951. case TOOL_THREAD :
  6952. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6953. $return .= $this->display_forum_form('move', $item_id, $row);
  6954. break;
  6955. }
  6956. }
  6957. return $return;
  6958. }
  6959. /**
  6960. * Displays a basic form on the overview page for changing the item title and the item description.
  6961. * @param string $item_type
  6962. * @param string $title
  6963. * @param array $data
  6964. * @return string
  6965. */
  6966. public function display_item_small_form($item_type, $title = '', $data) {
  6967. $return = '<div class="lp_small_form">';
  6968. $return .= '<p class="lp_title">' . $title . '</p>';
  6969. $return .= '<form method="post">';
  6970. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6971. $return .= '<tr>';
  6972. $return .= '<td class="label"><label for="idTitle">Title&nbsp;:</label></td>';
  6973. $return .= '<td class="input"><input class="small_form" id="idTitle" name="title" type="text" value="' . api_html_entity_decode($data['title'], ENT_QUOTES) . '" /></td>';
  6974. $return .= '</tr>';
  6975. $return .= '<tr>';
  6976. $return .= '<td colspan="2"><button class="save" name="submit_button" type="submit">' . get_lang('Save') . '</button></td>';
  6977. $return .= '</tr>';
  6978. $return .= '</table>';
  6979. $return .= '<input name="parent" type="hidden" value="' . $data['parent_item_id'] . '"/>';
  6980. $return .= '<input name="previous" type="hidden" value="' . $data['previous_item_id'] . '"/>';
  6981. $return .= '</form>';
  6982. $return .= '</div>';
  6983. return $return;
  6984. }
  6985. /**
  6986. * Return HTML form to allow prerequisites selection
  6987. * @param integer Item ID
  6988. * @return string HTML form
  6989. */
  6990. public function display_item_prerequisites_form($item_id) {
  6991. $course_id = api_get_course_int_id();
  6992. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6993. $item_id = intval($item_id);
  6994. /* Current prerequisite */
  6995. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = " . $item_id;
  6996. $result = Database::query($sql);
  6997. $row = Database::fetch_array($result);
  6998. $preq_id = $row['prerequisite'];
  6999. //$preq_mastery = $row['mastery_score'];
  7000. //$preq_max = $row['max_score'];
  7001. $return = $this->display_manipulate($item_id, TOOL_DOCUMENT);
  7002. $return = '<legend>';
  7003. $return .= get_lang('AddEditPrerequisites');
  7004. $return .= '</legend>';
  7005. $return .= '<div class="sectioncomment">';
  7006. $return .= '<form method="POST">';
  7007. $return .= '<table class="data_table" style="width:650px">';
  7008. $return .= '<tr>';
  7009. $return .= '<th height="24">' . get_lang('Prerequisites') . '</th>';
  7010. $return .= '<th width="70" height="24">' . get_lang('Minimum') . '</th>';
  7011. $return .= '<th width="70" height="24">' . get_lang('Maximum') . '</th>';
  7012. $return .= '</tr>';
  7013. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7014. $return .= '<tr >';
  7015. $return .= '<td colspan="3" class="radio">';
  7016. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7017. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7018. $return .= '</tr>';
  7019. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7020. $result = Database::query($sql);
  7021. $arrLP = array ();
  7022. while ($row = Database :: fetch_array($result)) {
  7023. $arrLP[] = array (
  7024. 'id' => $row['id'],
  7025. 'item_type' => $row['item_type'],
  7026. 'title' => $row['title'],
  7027. 'ref' => $row['ref'],
  7028. 'description' => $row['description'],
  7029. 'parent_item_id' => $row['parent_item_id'],
  7030. 'previous_item_id' => $row['previous_item_id'],
  7031. 'next_item_id' => $row['next_item_id'],
  7032. 'max_score' => $row['max_score'],
  7033. 'min_score' => $row['min_score'],
  7034. 'mastery_score' => $row['mastery_score'],
  7035. 'prerequisite' => $row['prerequisite'],
  7036. 'next_item_id' => $row['next_item_id'],
  7037. 'display_order' => $row['display_order']
  7038. );
  7039. if ($row['ref'] == $preq_id) {
  7040. $preq_mastery = $row['mastery_score'];
  7041. $preq_max = $row['max_score'];
  7042. }
  7043. }
  7044. $this->tree_array($arrLP);
  7045. $arrLP = $this->arrMenu;
  7046. unset ($this->arrMenu);
  7047. for ($i = 0; $i < count($arrLP); $i++) {
  7048. if ($arrLP[$i]['id'] == $item_id)
  7049. break;
  7050. $return .= '<tr>';
  7051. $return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7052. $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:' . $arrLP[$i]['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $arrLP[$i]['id'] . '" />';
  7053. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  7054. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7055. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7056. } else
  7057. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7058. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7059. } else {
  7060. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7061. }
  7062. $return .= '<label for="id' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</label>';
  7063. $return .= '</td>';
  7064. //$return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . ' />';
  7065. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  7066. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7067. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7068. $return .= '</td>';
  7069. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7070. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7071. $return .= '</td>';
  7072. }
  7073. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  7074. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7075. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7076. $return .= '</td>';
  7077. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7078. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7079. $return .= '</td>';
  7080. }
  7081. $return .= '</tr>';
  7082. }
  7083. $return .= '<tr>';
  7084. $return .= '</tr>';
  7085. $return .= '</table>';
  7086. $return .= '<div style="padding-top:3px;">';
  7087. $return .= '<button class="save" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7088. $return .= '</div>';
  7089. $return .= '</form>';
  7090. // $return .= '</div>';
  7091. return $return;
  7092. }
  7093. /**
  7094. * Return HTML list to allow prerequisites selection for lp
  7095. * @param integer Item ID
  7096. * @return string HTML form
  7097. */
  7098. public function display_lp_prerequisites_list() {
  7099. $course_id = api_get_course_int_id();
  7100. $lp_id = $this->lp_id;
  7101. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7102. // get current prerequisite
  7103. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7104. $result = Database::query($sql);
  7105. $row = Database :: fetch_array($result);
  7106. $preq_id = $row['prerequisite'];
  7107. $session_id = api_get_session_id();
  7108. $session_condition = api_get_session_condition($session_id);
  7109. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  7110. $rs = Database::query($sql);
  7111. $return = '';
  7112. $return .= '<select name="prerequisites" >';
  7113. $return .= '<option value="0">'.get_lang('None').'</option>';
  7114. if (Database::num_rows($rs) > 0) {
  7115. while ($row = Database::fetch_array($rs)) {
  7116. if ($row['id'] == $lp_id) {
  7117. continue;
  7118. }
  7119. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$preq_id)?' selected ' : '').'>'.$row['name'].'</option>';
  7120. }
  7121. }
  7122. $return .= '</select>';
  7123. return $return;
  7124. }
  7125. /**
  7126. * Creates a list with all the documents in it
  7127. * @return string
  7128. */
  7129. public function get_documents() {
  7130. $course_info = api_get_course_info();
  7131. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  7132. return $document_tree;
  7133. }
  7134. /**
  7135. * Creates a list with all the exercises (quiz) in it
  7136. * @return string
  7137. */
  7138. public function get_exercises() {
  7139. $course_id = api_get_course_int_id();
  7140. // New for hotpotatoes.
  7141. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7142. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7143. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7144. $session_id = api_get_session_id();
  7145. $condition_session = api_get_session_condition($session_id);
  7146. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  7147. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session ORDER BY id ASC";
  7148. $res_quiz = Database::query($sql_quiz);
  7149. $res_hot = Database::query($sql_hot);
  7150. $return = '<ul class="lp_resource">';
  7151. $return .= '<li class="lp_resource_element">';
  7152. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7153. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?lp_id=' . $this->lp_id . '">' . get_lang('NewExercise') . '</a>';
  7154. $return .= '</li>';
  7155. // Display quizhotpotatoes.
  7156. while ($row_hot = Database :: fetch_array($res_hot)) {
  7157. $return .= '<li>';
  7158. $return .= '<img alt="hp" src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="18px" height="18px" />';
  7159. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_HOTPOTATOES . '&amp;file=' . $row_hot['id'] . '&amp;lp_id=' . $this->lp_id . '">' . ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7160. $return .= '</li>';
  7161. }
  7162. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7163. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7164. $return .= '<a class="moved" href="#">';
  7165. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7166. $return .= '</a> ';
  7167. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7168. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_QUIZ . '&amp;file=' . $row_quiz['id'] . '&amp;lp_id=' . $this->lp_id . '">' .
  7169. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7170. '</a>';
  7171. $return .= '</li>';
  7172. }
  7173. $return .= '</ul>';
  7174. return $return;
  7175. }
  7176. /**
  7177. * Creates a list with all the links in it
  7178. * @return string
  7179. */
  7180. public function get_links() {
  7181. $course_id = api_get_course_int_id();
  7182. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7183. $session_id = api_get_session_id();
  7184. $condition_session = api_get_session_condition($session_id);
  7185. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  7186. $res_link = Database::query($sql_link);
  7187. $return = '<ul class="lp_resource">';
  7188. $return .= '<li class="lp_resource_element">';
  7189. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  7190. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'link/link.php?' . api_get_cidreq() . '&action=addlink&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('LinkAdd') . '">' . get_lang('LinkAdd') . '</a>';
  7191. $return .= '</li>';
  7192. $course_info = api_get_course_info();
  7193. while ($row_link = Database :: fetch_array($res_link)) {
  7194. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  7195. if ($item_visibility != 2) {
  7196. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  7197. $return .= '<a class="moved" href="#">';
  7198. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7199. $return .= '</a> ';
  7200. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  7201. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_LINK . '&amp;file=' . $row_link['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7202. $row_link['title'].
  7203. '</a>';
  7204. $return .= '</li>';
  7205. }
  7206. }
  7207. $return .= '</ul>';
  7208. return $return;
  7209. }
  7210. /**
  7211. * Creates a list with all the student publications in it
  7212. * @return unknown
  7213. */
  7214. public function get_student_publications() {
  7215. //$course_id = api_get_course_int_id();
  7216. //$tbl_student = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7217. //$session_id = api_get_session_id();
  7218. //$condition_session = api_get_session_condition($session_id);
  7219. //$sql_student = "SELECT * FROM $tbl_student WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  7220. //$res_student = Database::query($sql_student);
  7221. //$return .= '<div class="lp_resource_header"' . " onclick=\"javascript: if(document.getElementById('resStudent').style.display == 'block') {document.getElementById('resStudent').style.display = 'none';} else {document.getElementById('resStudent').style.display = 'block';}\"" . '><img alt="" src="../img/lp_' . TOOL_STUDENTPUBLICATION . '.gif" style="margin-right:5px;" title="" />' . get_lang('Student_publication') . '</div>';
  7222. $return = '<div class="lp_resource" >';
  7223. $return .= '<div class="lp_resource_element">';
  7224. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7225. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_STUDENTPUBLICATION . '&amp;lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7226. $return .= '</div>';
  7227. $return .= '</div>';
  7228. return $return;
  7229. }
  7230. /**
  7231. * Creates a list with all the forums in it
  7232. * @return string
  7233. */
  7234. public function get_forums() {
  7235. require_once '../forum/forumfunction.inc.php';
  7236. require_once '../forum/forumconfig.inc.php';
  7237. $a_forums = get_forums();
  7238. $return = '<ul class="lp_resource">';
  7239. //First add link
  7240. $return .= '<li class="lp_resource_element">';
  7241. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7242. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7243. $return .= '</li>';
  7244. $return .= '<script>
  7245. function toggle_forum(forum_id){
  7246. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7247. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7248. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7249. } else {
  7250. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7251. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7252. }
  7253. }
  7254. </script>';
  7255. foreach ($a_forums as $forum) {
  7256. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7257. if (!empty($forum['forum_id'])) {
  7258. $return .= '<a class="moved" href="#">';
  7259. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7260. $return .= ' </a>';
  7261. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7262. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7263. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7264. </a>
  7265. <a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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($forum['forum_title']) . '</a>';
  7266. }
  7267. $return .= '</li>';
  7268. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7269. $a_threads = get_threads($forum['forum_id']);
  7270. if (is_array($a_threads)) {
  7271. foreach ($a_threads as $thread) {
  7272. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7273. $return .= '&nbsp;<a class="moved" href="#">';
  7274. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7275. $return .= ' </a>';
  7276. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7277. $return .= '<a href="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_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($thread['thread_title']) . '</a>';
  7278. $return .= '</li>';
  7279. }
  7280. }
  7281. $return .= '</div>';
  7282. }
  7283. $return .= '</ul>';
  7284. return $return;
  7285. }
  7286. /**
  7287. * // TODO: The output encoding should be equal to the system encoding.
  7288. *
  7289. * Exports the learning path as a SCORM package. This is the main function that
  7290. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7291. * whole thing and returns the zip.
  7292. *
  7293. * This method needs to be called in PHP5, as it will fail with non-adequate
  7294. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7295. * you need to call it on a learnpath object.
  7296. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7297. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7298. * path has been modified, it should use the generic method here below.
  7299. * @TODO link this function with the export_lp() function in the same class
  7300. * @param string Optional name of zip file. If none, title of learnpath is
  7301. * domesticated and trailed with ".zip"
  7302. * @return string Returns the zip package string, or null if error
  7303. */
  7304. public function scorm_export() {
  7305. global $_course, $debug;
  7306. $course_id = api_get_course_int_id();
  7307. // Remove memory and time limits as much as possible as this might be a long process...
  7308. if (function_exists('ini_set')) {
  7309. $mem = ini_get('memory_limit');
  7310. if (substr($mem, -1, 1) == 'M') {
  7311. $mem_num = substr($mem, 0, -1);
  7312. if ($mem_num < 128) {
  7313. ini_set('memory_limit', '128M');
  7314. }
  7315. } else {
  7316. ini_set('memory_limit', '128M');
  7317. }
  7318. ini_set('max_execution_time', 600);
  7319. }
  7320. // Create the zip handler (this will remain available throughout the method).
  7321. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7322. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7323. $temp_dir_short = uniqid();
  7324. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7325. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7326. $zip_folder = new PclZip($temp_zip_file);
  7327. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7328. $root_path = $main_path = api_get_path(SYS_PATH);
  7329. $files_cleanup = array();
  7330. // Place to temporarily stash the zipfiles.
  7331. // create the temp dir if it doesn't exist
  7332. // or do a cleanup befor creating the zipfile.
  7333. if (!is_dir($temp_zip_dir)) {
  7334. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7335. } else {
  7336. // Cleanup: Check the temp dir for old files and delete them.
  7337. $handle = opendir($temp_zip_dir);
  7338. while (false !== ($file = readdir($handle))) {
  7339. if ($file != '.' && $file != '..') {
  7340. unlink("$temp_zip_dir/$file");
  7341. }
  7342. }
  7343. closedir($handle);
  7344. }
  7345. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7346. //Removes ./ at the end of the path
  7347. $this->path = str_replace('/.', '', $this->path);
  7348. //If scorm comes from a Chamilo scorm package
  7349. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7350. // Remove the possible . at the end of the path
  7351. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7352. //$dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7353. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir);
  7354. if (!is_dir($dest_path_to_scorm_folder)) {
  7355. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7356. }
  7357. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7358. }
  7359. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  7360. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7361. // Aggregation Model official document, secion "2.3 Content Packaging".
  7362. $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.
  7363. $root = $xmldoc->createElement('manifest');
  7364. $root->setAttribute('identifier', 'SingleCourseManifest');
  7365. $root->setAttribute('version', '1.1');
  7366. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7367. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7368. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7369. $root->setAttribute('xsi:schemaLocation', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd');
  7370. // Build mandatory sub-root container elements.
  7371. $metadata = $xmldoc->createElement('metadata');
  7372. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7373. $metadata->appendChild($md_schema);
  7374. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7375. $metadata->appendChild($md_schemaversion);
  7376. $root->appendChild($metadata);
  7377. $organizations = $xmldoc->createElement('organizations');
  7378. $resources = $xmldoc->createElement('resources');
  7379. // Build the only organization we will use in building our learnpaths.
  7380. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7381. $organization = $xmldoc->createElement('organization');
  7382. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7383. // To set the title of the SCORM entity (=organization), we take the name given
  7384. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7385. // learning path charset) as it is the encoding that defines how it is stored
  7386. // in the database. Then we convert it to HTML entities again as the "&" character
  7387. // alone is not authorized in XML (must be &amp;).
  7388. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7389. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7390. $organization->appendChild($org_title);
  7391. $folder_name = 'document';
  7392. // Removes the learning_path/scorm_folder path when exporting see #4841
  7393. $path_to_remove = null;
  7394. $result = $this->generate_lp_folder($_course);
  7395. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7396. $path_to_remove = 'document'.$result['dir'];
  7397. $path_to_replace = $folder_name.'/';
  7398. }
  7399. //Fixes chamilo scorm exports
  7400. if ($this->ref == 'chamilo_scorm_export') {
  7401. $path_to_remove = 'scorm/'.$this->path.'/document/';
  7402. }
  7403. // For each element, add it to the imsmanifest structure, then add it to the zip.
  7404. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  7405. $link_updates = array();
  7406. foreach ($this->items as $index => $item) {
  7407. if ($debug) { echo '<h3>'.$item->name.'</h3>';var_dump($item->type); }
  7408. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  7409. // Get included documents from this item
  7410. if ($item->type == 'sco')
  7411. $inc_docs = $item->get_resources_from_source(null, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.'scorm/'.$this->path.'/'.$item->get_path());
  7412. else
  7413. $inc_docs = $item->get_resources_from_source();
  7414. if ($debug) { echo 'Files inside file';var_dump($inc_docs); }
  7415. // Give a child element <item> to the <organization> element.
  7416. $my_item_id = $item->get_id();
  7417. $my_item = $xmldoc->createElement('item');
  7418. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  7419. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  7420. $my_item->setAttribute('isvisible', 'true');
  7421. // Give a child element <title> to the <item> element.
  7422. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7423. $my_item->appendChild($my_title);
  7424. // Give a child element <adlcp:prerequisites> to the <item> element.
  7425. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  7426. $my_prereqs->setAttribute('type', 'aicc_script');
  7427. $my_item->appendChild($my_prereqs);
  7428. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7429. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  7430. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7431. //$xmldoc->createElement('adlcp:timelimitaction','');
  7432. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7433. //$xmldoc->createElement('adlcp:datafromlms','');
  7434. // Give a child element <adlcp:masteryscore> to the <item> element.
  7435. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7436. $my_item->appendChild($my_masteryscore);
  7437. // Attach this item to the organization element or hits parent if there is one.
  7438. if (!empty($item->parent) && $item->parent != 0) {
  7439. $children = $organization->childNodes;
  7440. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  7441. if (is_object($possible_parent)) {
  7442. $possible_parent->appendChild($my_item);
  7443. } else {
  7444. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  7445. }
  7446. } else {
  7447. if ($this->debug > 0) { error_log('No parent'); }
  7448. $organization->appendChild($my_item);
  7449. }
  7450. // Get the path of the file(s) from the course directory root.
  7451. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7452. if (!empty($path_to_remove)) {
  7453. //From docs
  7454. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  7455. //From quiz
  7456. if ($this->ref == 'chamilo_scorm_export') {
  7457. $path_to_remove = 'scorm/'.$this->path.'/';
  7458. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  7459. }
  7460. }
  7461. $my_sub_dir = dirname($my_file_path);
  7462. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7463. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7464. $my_xml_sub_dir = $my_sub_dir;
  7465. // Give a <resource> child to the <resources> element
  7466. $my_resource = $xmldoc->createElement('resource');
  7467. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7468. $my_resource->setAttribute('type', 'webcontent');
  7469. $my_resource->setAttribute('href', $my_xml_file_path);
  7470. // adlcp:scormtype can be either 'sco' or 'asset'.
  7471. if ($item->type == 'sco') {
  7472. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7473. } else {
  7474. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7475. }
  7476. // xml:base is the base directory to find the files declared in this resource.
  7477. $my_resource->setAttribute('xml:base', '');
  7478. // Give a <file> child to the <resource> element.
  7479. $my_file = $xmldoc->createElement('file');
  7480. $my_file->setAttribute('href', $my_xml_file_path);
  7481. $my_resource->appendChild($my_file);
  7482. // Dependency to other files - not yet supported.
  7483. $i = 1;
  7484. if ($debug) echo 'Looping docs';
  7485. foreach ($inc_docs as $doc_info) {
  7486. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7487. if ($debug) var_dump($doc_info);
  7488. $my_dep = $xmldoc->createElement('resource');
  7489. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7490. $my_dep->setAttribute('identifier', $res_id);
  7491. $my_dep->setAttribute('type', 'webcontent');
  7492. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7493. $my_dep_file = $xmldoc->createElement('file');
  7494. // Check type of URL.
  7495. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7496. if ($doc_info[1] == 'remote') {
  7497. // Remote file. Save url as is.
  7498. $my_dep_file->setAttribute('href', $doc_info[0]);
  7499. $my_dep->setAttribute('xml:base', '');
  7500. } elseif ($doc_info[1] == 'local') {
  7501. switch ($doc_info[2]) {
  7502. case 'url': // Local URL - save path as url for now, don't zip file.
  7503. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7504. $current_dir = dirname($abs_path);
  7505. $current_dir = str_replace('\\', '/', $current_dir);
  7506. $file_path = realpath($abs_path);
  7507. $file_path = str_replace('\\', '/', $file_path);
  7508. $my_dep_file->setAttribute('href', $file_path);
  7509. $my_dep->setAttribute('xml:base', '');
  7510. if (strstr($file_path, $main_path) !== false) {
  7511. // The calculated real path is really inside Chamilo's root path.
  7512. // Reduce file path to what's under the DocumentRoot.
  7513. $file_path = substr($file_path, strlen($root_path) - 1);
  7514. //echo $file_path;echo '<br /><br />';
  7515. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  7516. $zip_files_abs[] = $file_path;
  7517. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7518. $my_dep_file->setAttribute('href', $file_path);
  7519. $my_dep->setAttribute('xml:base', '');
  7520. } elseif (empty($file_path)) {
  7521. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7522. if (strpos($document_root, -1) == '/') {
  7523. $document_root = substr(0, -1, $document_root);
  7524. }*/
  7525. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7526. $file_path = str_replace('//', '/', $file_path);
  7527. if (file_exists($file_path)) {
  7528. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7529. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7530. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7531. $my_dep_file->setAttribute('href', $file_path);
  7532. $my_dep->setAttribute('xml:base', '');
  7533. }
  7534. }
  7535. break;
  7536. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7537. $my_dep_file->setAttribute('href', $doc_info[0]);
  7538. $my_dep->setAttribute('xml:base', '');
  7539. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  7540. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  7541. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  7542. $abs_img_path_without_subdir = $doc_info[0];
  7543. $relp = api_get_path(REL_PATH); // The url-append config param.
  7544. $pos = strpos($abs_img_path_without_subdir, $relp);
  7545. if ($pos === 0) {
  7546. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  7547. }
  7548. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  7549. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  7550. $file_path = str_replace('\\', '/', $file_path);
  7551. $file_path = str_replace('//', '/', $file_path);
  7552. // Prepare the current directory path (until just under 'document') with a trailing slash.
  7553. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  7554. if ($debug) { echo 'file path '; var_dump($file_path); }
  7555. if ($debug) { echo '$cur_path '; var_dump($cur_path); }
  7556. // Check if the current document is in that path.
  7557. if (strstr($file_path, $cur_path) !== false) {
  7558. // The document is in that path, now get the relative path
  7559. // to the containing document.
  7560. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  7561. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  7562. $relative_path = '';
  7563. if (strstr($file_path, $cur_path) !== false) {
  7564. if (strpos($orig_file_path, $file_path) == false) {
  7565. //no need to do something
  7566. $file_path = $relative_path = substr($file_path, strlen($cur_path));
  7567. //$file_path = substr($file_path, strlen($cur_path));
  7568. //$relative_path = str_replace('document/', '', $file_path);
  7569. } else {
  7570. $relative_path = substr($file_path, strlen($orig_file_path));
  7571. $file_path = substr($file_path, strlen($cur_path));
  7572. }
  7573. } else {
  7574. // This case is still a problem as it's difficult to calculate a relative path easily
  7575. // might still generate wrong links.
  7576. //$file_path = substr($file_path,strlen($cur_path));
  7577. // Calculate the directory path to the current file (without trailing slash).
  7578. $my_relative_path = dirname($file_path);
  7579. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  7580. $my_relative_file = basename($file_path);
  7581. // Calculate the directory path to the containing file (without trailing slash).
  7582. $my_orig_file_path = substr($orig_file_path, 0, -1);
  7583. $dotdot = '';
  7584. $subdir = '';
  7585. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  7586. $my_relative_path2 = dirname($my_relative_path);
  7587. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  7588. $my_orig_file_path = dirname($my_orig_file_path);
  7589. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  7590. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  7591. $dotdot += '../';
  7592. $my_relative_path = $my_relative_path2;
  7593. }
  7594. $relative_path = $dotdot.$subdir.$my_relative_file;
  7595. }
  7596. if ($debug) { echo '$relative_path'; var_dump($relative_path); }
  7597. if ($debug) { echo '$file_path'; var_dump($file_path); }
  7598. // Put the current document in the zip (this array is the array
  7599. // that will manage documents already in the course folder - relative).
  7600. $zip_files[] = $file_path;
  7601. // Update the links to the current document in the containing document (make them relative).
  7602. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  7603. if ($debug) { echo 'orig '; var_dump($doc_info[0]); }
  7604. if ($debug) { echo 'dest '; var_dump($relative_path); }
  7605. $my_dep_file->setAttribute('href', $file_path);
  7606. $my_dep->setAttribute('xml:base', '');
  7607. } elseif (strstr($file_path, $main_path) !== false) {
  7608. // The calculated real path is really inside Chamilo's root path.
  7609. // Reduce file path to what's under the DocumentRoot.
  7610. $file_path = substr($file_path, strlen($root_path));
  7611. //echo $file_path;echo '<br /><br />';
  7612. //error_log('Reduced path: '.$file_path, 0);
  7613. $zip_files_abs[] = $file_path;
  7614. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7615. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7616. $my_dep->setAttribute('xml:base', '');
  7617. } elseif (empty($file_path)) {
  7618. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7619. if(strpos($document_root,-1) == '/') {
  7620. $document_root = substr(0, -1, $document_root);
  7621. }*/
  7622. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7623. $file_path = str_replace('//', '/', $file_path);
  7624. if (file_exists($file_path)) {
  7625. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7626. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7627. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7628. $my_dep_file->setAttribute('href','document/'.$file_path);
  7629. $my_dep->setAttribute('xml:base', '');
  7630. }
  7631. }
  7632. break;
  7633. 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
  7634. if (substr($doc_info[0], 0, 2) == '..') {
  7635. // Relative path going up.
  7636. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7637. $current_dir = str_replace('\\', '/', $current_dir);
  7638. $file_path = realpath($current_dir.$doc_info[0]);
  7639. $file_path = str_replace('\\', '/', $file_path);
  7640. //error_log($file_path.' <-> '.$main_path,0);
  7641. if (strstr($file_path, $main_path) !== false) {
  7642. // The calculated real path is really inside Chamilo's root path.
  7643. // Reduce file path to what's under the DocumentRoot.
  7644. $file_path = substr($file_path, strlen($root_path));
  7645. //error_log('Reduced path: '.$file_path, 0);
  7646. $zip_files_abs[] = $file_path;
  7647. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7648. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7649. $my_dep->setAttribute('xml:base', '');
  7650. }
  7651. } else {
  7652. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7653. $my_dep_file->setAttribute('href', $doc_info[0]);
  7654. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7655. }
  7656. break;
  7657. default:
  7658. $my_dep_file->setAttribute('href', $doc_info[0]);
  7659. $my_dep->setAttribute('xml:base', '');
  7660. break;
  7661. }
  7662. }
  7663. $my_dep->appendChild($my_dep_file);
  7664. $resources->appendChild($my_dep);
  7665. $dependency = $xmldoc->createElement('dependency');
  7666. $dependency->setAttribute('identifierref', $res_id);
  7667. $my_resource->appendChild($dependency);
  7668. $i++;
  7669. }
  7670. //$my_dependency = $xmldoc->createElement('dependency');
  7671. //$my_dependency->setAttribute('identifierref', '');
  7672. $resources->appendChild($my_resource);
  7673. $zip_files[] = $my_file_path;
  7674. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  7675. } else {
  7676. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  7677. switch ($item->type) {
  7678. case TOOL_LINK:
  7679. $my_item = $xmldoc->createElement('item');
  7680. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7681. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7682. $my_item->setAttribute('isvisible', 'true');
  7683. // Give a child element <title> to the <item> element.
  7684. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7685. $my_item->appendChild($my_title);
  7686. // Give a child element <adlcp:prerequisites> to the <item> element.
  7687. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7688. $my_prereqs->setAttribute('type', 'aicc_script');
  7689. $my_item->appendChild($my_prereqs);
  7690. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7691. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  7692. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7693. //$xmldoc->createElement('adlcp:timelimitaction', '');
  7694. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7695. //$xmldoc->createElement('adlcp:datafromlms', '');
  7696. // Give a child element <adlcp:masteryscore> to the <item> element.
  7697. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7698. $my_item->appendChild($my_masteryscore);
  7699. // Attach this item to the organization element or its parent if there is one.
  7700. if (!empty($item->parent) && $item->parent != 0) {
  7701. $children = $organization->childNodes;
  7702. for ($i = 0; $i < $children->length; $i++) {
  7703. $item_temp = $children->item($i);
  7704. if ($item_temp -> nodeName == 'item') {
  7705. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7706. $item_temp -> appendChild($my_item);
  7707. }
  7708. }
  7709. }
  7710. } else {
  7711. $organization->appendChild($my_item);
  7712. }
  7713. $my_file_path = 'link_'.$item->get_id().'.html';
  7714. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  7715. $rs = Database::query($sql);
  7716. if ($link = Database :: fetch_array($rs)) {
  7717. $url = $link['url'];
  7718. $title = stripslashes($link['title']);
  7719. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  7720. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7721. $my_xml_file_path = $my_file_path;
  7722. $my_sub_dir = dirname($my_file_path);
  7723. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7724. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7725. $my_xml_sub_dir = $my_sub_dir;
  7726. // Give a <resource> child to the <resources> element.
  7727. $my_resource = $xmldoc->createElement('resource');
  7728. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7729. $my_resource->setAttribute('type', 'webcontent');
  7730. $my_resource->setAttribute('href', $my_xml_file_path);
  7731. // adlcp:scormtype can be either 'sco' or 'asset'.
  7732. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7733. // xml:base is the base directory to find the files declared in this resource.
  7734. $my_resource->setAttribute('xml:base', '');
  7735. // give a <file> child to the <resource> element.
  7736. $my_file = $xmldoc->createElement('file');
  7737. $my_file->setAttribute('href', $my_xml_file_path);
  7738. $my_resource->appendChild($my_file);
  7739. $resources->appendChild($my_resource);
  7740. }
  7741. break;
  7742. case TOOL_QUIZ:
  7743. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  7744. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  7745. $exe = new Exercise();
  7746. $exe->read($exe_id);
  7747. $my_item = $xmldoc->createElement('item');
  7748. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7749. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7750. $my_item->setAttribute('isvisible', 'true');
  7751. // Give a child element <title> to the <item> element.
  7752. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7753. $my_item->appendChild($my_title);
  7754. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  7755. //$my_item->appendChild($my_max_score);
  7756. // Give a child element <adlcp:prerequisites> to the <item> element.
  7757. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7758. $my_prereqs->setAttribute('type','aicc_script');
  7759. $my_item->appendChild($my_prereqs);
  7760. // Give a child element <adlcp:masteryscore> to the <item> element.
  7761. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7762. $my_item->appendChild($my_masteryscore);
  7763. // Attach this item to the organization element or hits parent if there is one.
  7764. if (!empty($item->parent) && $item->parent != 0) {
  7765. $children = $organization->childNodes;
  7766. for ($i = 0; $i < $children->length; $i++) {
  7767. $item_temp = $children->item($i);
  7768. if ($item_temp -> nodeName == 'item') {
  7769. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7770. $item_temp -> appendChild($my_item);
  7771. }
  7772. }
  7773. }
  7774. } else {
  7775. $organization->appendChild($my_item);
  7776. }
  7777. // Include export scripts.
  7778. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  7779. // Get the path of the file(s) from the course directory root
  7780. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7781. $my_file_path = 'quiz_'.$item->get_id().'.html';
  7782. // Write the contents of the exported exercise into a (big) html file
  7783. // to later pack it into the exported SCORM. The file will be removed afterwards.
  7784. $contents = export_exercise($exe_id, true);
  7785. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  7786. $res = file_put_contents($tmp_file_path, $contents);
  7787. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  7788. $files_cleanup[] = $tmp_file_path;
  7789. //error_log($tmp_path); die();
  7790. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7791. $my_xml_file_path = $my_file_path;
  7792. $my_sub_dir = dirname($my_file_path);
  7793. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7794. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7795. $my_xml_sub_dir = $my_sub_dir;
  7796. // Give a <resource> child to the <resources> element.
  7797. $my_resource = $xmldoc->createElement('resource');
  7798. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7799. $my_resource->setAttribute('type', 'webcontent');
  7800. $my_resource->setAttribute('href', $my_xml_file_path);
  7801. // adlcp:scormtype can be either 'sco' or 'asset'.
  7802. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7803. // xml:base is the base directory to find the files declared in this resource.
  7804. $my_resource->setAttribute('xml:base', '');
  7805. // Give a <file> child to the <resource> element.
  7806. $my_file = $xmldoc->createElement('file');
  7807. $my_file->setAttribute('href', $my_xml_file_path);
  7808. $my_resource->appendChild($my_file);
  7809. // Get included docs.
  7810. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  7811. // Dependency to other files - not yet supported.
  7812. $i = 1;
  7813. foreach ($inc_docs as $doc_info) {
  7814. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7815. $my_dep = $xmldoc->createElement('resource');
  7816. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7817. $my_dep->setAttribute('identifier', $res_id);
  7818. $my_dep->setAttribute('type', 'webcontent');
  7819. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7820. $my_dep_file = $xmldoc->createElement('file');
  7821. // Check type of URL.
  7822. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7823. if ($doc_info[1] == 'remote') {
  7824. // Remote file. Save url as is.
  7825. $my_dep_file->setAttribute('href', $doc_info[0]);
  7826. $my_dep->setAttribute('xml:base', '');
  7827. } elseif ($doc_info[1] == 'local') {
  7828. switch ($doc_info[2]) {
  7829. case 'url': // Local URL - save path as url for now, don't zip file.
  7830. // Save file but as local file (retrieve from URL).
  7831. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7832. $current_dir = dirname($abs_path);
  7833. $current_dir = str_replace('\\', '/', $current_dir);
  7834. $file_path = realpath($abs_path);
  7835. $file_path = str_replace('\\', '/', $file_path);
  7836. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7837. $my_dep->setAttribute('xml:base', '');
  7838. if (strstr($file_path, $main_path) !== false) {
  7839. // The calculated real path is really inside the chamilo root path.
  7840. // Reduce file path to what's under the DocumentRoot.
  7841. $file_path = substr($file_path, strlen($root_path));
  7842. //echo $file_path;echo '<br /><br />';
  7843. //error_log('Reduced path: '.$file_path, 0);
  7844. $zip_files_abs[] = $file_path;
  7845. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7846. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7847. $my_dep->setAttribute('xml:base', '');
  7848. } elseif (empty($file_path)) {
  7849. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  7850. if (strpos($document_root,-1) == '/') {
  7851. $document_root = substr(0, -1, $document_root);
  7852. }*/
  7853. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7854. $file_path = str_replace('//', '/', $file_path);
  7855. if (file_exists($file_path)) {
  7856. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7857. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7858. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7859. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7860. $my_dep->setAttribute('xml:base', '');
  7861. }
  7862. }
  7863. break;
  7864. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7865. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7866. $current_dir = str_replace('\\', '/', $current_dir);
  7867. $file_path = realpath($doc_info[0]);
  7868. $file_path = str_replace('\\', '/', $file_path);
  7869. $my_dep_file->setAttribute('href', $file_path);
  7870. $my_dep->setAttribute('xml:base', '');
  7871. if (strstr($file_path,$main_path) !== false) {
  7872. // The calculated real path is really inside the chamilo root path.
  7873. // Reduce file path to what's under the DocumentRoot.
  7874. $file_path = substr($file_path, strlen($root_path));
  7875. //echo $file_path;echo '<br /><br />';
  7876. //error_log('Reduced path: '.$file_path, 0);
  7877. $zip_files_abs[] = $file_path;
  7878. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7879. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7880. $my_dep->setAttribute('xml:base', '');
  7881. } elseif (empty($file_path)) {
  7882. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7883. if (strpos($document_root,-1) == '/') {
  7884. $document_root = substr(0, -1, $document_root);
  7885. }*/
  7886. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7887. $file_path = str_replace('//', '/', $file_path);
  7888. if (file_exists($file_path)) {
  7889. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7890. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7891. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7892. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7893. $my_dep->setAttribute('xml:base', '');
  7894. }
  7895. }
  7896. break;
  7897. 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
  7898. if (substr($doc_info[0], 0, 2) == '..') {
  7899. // Relative path going up.
  7900. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7901. $current_dir = str_replace('\\', '/', $current_dir);
  7902. $file_path = realpath($current_dir.$doc_info[0]);
  7903. $file_path = str_replace('\\', '/', $file_path);
  7904. //error_log($file_path.' <-> '.$main_path, 0);
  7905. if (strstr($file_path, $main_path) !== false) {
  7906. // The calculated real path is really inside Chamilo's root path.
  7907. // Reduce file path to what's under the DocumentRoot.
  7908. $file_path = substr($file_path, strlen($root_path));
  7909. $file_path_dest = $file_path;
  7910. // File path is courses/CHAMILO/document/....
  7911. $info_file_path = explode('/', $file_path);
  7912. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  7913. $file_path_dest = 'document/'.$file_path;
  7914. }
  7915. //error_log('Reduced path: '.$file_path, 0);
  7916. $zip_files_abs[] = $file_path;
  7917. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  7918. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7919. $my_dep->setAttribute('xml:base', '');
  7920. }
  7921. } else {
  7922. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7923. $my_dep_file->setAttribute('href', $doc_info[0]);
  7924. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7925. }
  7926. break;
  7927. default:
  7928. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  7929. $my_dep->setAttribute('xml:base', '');
  7930. break;
  7931. }
  7932. }
  7933. $my_dep->appendChild($my_dep_file);
  7934. $resources->appendChild($my_dep);
  7935. $dependency = $xmldoc->createElement('dependency');
  7936. $dependency->setAttribute('identifierref', $res_id);
  7937. $my_resource->appendChild($dependency);
  7938. $i++;
  7939. }
  7940. $resources->appendChild($my_resource);
  7941. $zip_files[] = $my_file_path;
  7942. break;
  7943. default:
  7944. // Get the path of the file(s) from the course directory root
  7945. $my_file_path = 'non_exportable.html';
  7946. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  7947. $my_xml_file_path = $my_file_path;
  7948. $my_sub_dir = dirname($my_file_path);
  7949. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7950. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  7951. $my_xml_sub_dir = $my_sub_dir;
  7952. // Give a <resource> child to the <resources> element.
  7953. $my_resource = $xmldoc->createElement('resource');
  7954. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7955. $my_resource->setAttribute('type', 'webcontent');
  7956. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  7957. // adlcp:scormtype can be either 'sco' or 'asset'.
  7958. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7959. // xml:base is the base directory to find the files declared in this resource.
  7960. $my_resource->setAttribute('xml:base', '');
  7961. // Give a <file> child to the <resource> element.
  7962. $my_file = $xmldoc->createElement('file');
  7963. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  7964. $my_resource->appendChild($my_file);
  7965. $resources->appendChild($my_resource);
  7966. break;
  7967. }
  7968. }
  7969. }
  7970. $organizations->appendChild($organization);
  7971. $root->appendChild($organizations);
  7972. $root->appendChild($resources);
  7973. $xmldoc->appendChild($root);
  7974. // TODO: Add a readme file here, with a short description and a link to the Reload player
  7975. // then add the file to the zip, then destroy the file (this is done automatically).
  7976. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  7977. //error_log(print_r($zip_files,true), 0);
  7978. $root = api_get_path(SYS_PATH);
  7979. if ($debug) { echo '<h3> zip_files</h3>'; var_dump($zip_files); }
  7980. foreach ($zip_files as $file_path) {
  7981. if (empty($file_path)) { continue; }
  7982. //if (!file_exists($file_path)) continue;
  7983. //Fixes chamilo scorm exports
  7984. if ($this->ref == 'chamilo_scorm_export') {
  7985. $pos = strpos($file_path, 'document');
  7986. $replace = null;
  7987. if ($pos) {
  7988. $replace = substr($file_path, 0, $pos);
  7989. }
  7990. $file_path = str_replace($replace, '', $file_path);
  7991. $pos = strpos($file_path, 'quiz_');
  7992. if ($pos) {
  7993. $replace = substr($file_path, 0, $pos);
  7994. }
  7995. $file_path = str_replace($replace, '', $file_path);
  7996. }
  7997. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  7998. if (!empty($path_to_remove)) {
  7999. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8000. }
  8001. $this->create_path($dest_file);
  8002. if (file_exists($sys_course_path.$_course['path'].'/'.$file_path)) {
  8003. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8004. }
  8005. // Check if the file needs a link update
  8006. if (in_array($file_path, array_keys($link_updates))) {
  8007. $count_parents = explode('/', $file_path);
  8008. $count_parents = count($count_parents) - 1;
  8009. $mult = str_repeat('../', $count_parents);
  8010. $string = file_get_contents($dest_file);
  8011. unlink($dest_file);
  8012. foreach ($link_updates[$file_path] as $old_new) {
  8013. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  8014. // This is an ugly hack that allows .flv files to be found by the flv player that
  8015. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8016. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8017. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8018. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8019. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8020. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8021. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8022. }
  8023. //Fix to avoid problems with default_course_document
  8024. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8025. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  8026. } else {
  8027. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  8028. $new_dest = $old_new['dest'];
  8029. }
  8030. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8031. $string = str_replace($old_new['orig'], $new_dest, $string);
  8032. //Add files inside the HTMLs
  8033. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8034. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  8035. if (file_exists($sys_course_path.$new_path)) {
  8036. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  8037. }
  8038. }
  8039. file_put_contents($dest_file, $string);
  8040. }
  8041. }
  8042. foreach ($zip_files_abs as $file_path) {
  8043. if (empty($file_path)) { continue; }
  8044. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  8045. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) { continue; }
  8046. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  8047. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8048. $this->create_path($dest_file);
  8049. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  8050. //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);
  8051. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  8052. copy($main_path.$file_path, $dest_file);
  8053. // Check if the file needs a link update.
  8054. if (in_array($file_path, array_keys($link_updates))) {
  8055. $string = file_get_contents($dest_file);
  8056. unlink($dest_file);
  8057. foreach ($link_updates[$file_path] as $old_new) {
  8058. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  8059. // This is an ugly hack that allows .flv files to be found by the flv player that
  8060. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8061. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8062. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8063. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8064. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8065. }
  8066. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8067. }
  8068. file_put_contents($dest_file, $string);
  8069. }
  8070. }
  8071. if (is_array($links_to_create)) {
  8072. foreach ($links_to_create as $file => $link) {
  8073. $file_content = '<!DOCTYPE html>
  8074. <head>
  8075. <meta charset="'.api_get_language_isocode().'" />
  8076. <title>'.$link['title'].'</title>
  8077. </head>
  8078. <body dir="'.api_get_text_direction().'">
  8079. <div style="text-align:center">
  8080. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8081. </body>
  8082. </html>';
  8083. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8084. }
  8085. }
  8086. // Add non exportable message explanation.
  8087. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8088. $file_content = '<!DOCTYPE html>
  8089. <head>
  8090. <meta charset="'.api_get_language_isocode().'" />
  8091. <title>'.$lang_not_exportable.'</title>
  8092. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8093. </head>
  8094. <body dir="'.api_get_text_direction().'">';
  8095. $file_content .=
  8096. <<<EOD
  8097. <style>
  8098. .error-message {
  8099. font-family: arial, verdana, helvetica, sans-serif;
  8100. border-width: 1px;
  8101. border-style: solid;
  8102. left: 50%;
  8103. margin: 10px auto;
  8104. min-height: 30px;
  8105. padding: 5px;
  8106. right: 50%;
  8107. width: 500px;
  8108. background-color: #FFD1D1;
  8109. border-color: #FF0000;
  8110. color: #000;
  8111. }
  8112. </style>
  8113. <body>
  8114. <div class="error-message">
  8115. $lang_not_exportable
  8116. </div>
  8117. </body>
  8118. </html>
  8119. EOD;
  8120. if (!is_dir($archive_path.$temp_dir_short.'/'.$folder_name)) {
  8121. @mkdir($archive_path.$temp_dir_short.'/'.$folder_name, api_get_permissions_for_new_directories());
  8122. }
  8123. file_put_contents($archive_path.$temp_dir_short.'/'.$folder_name.'/non_exportable.html', $file_content);
  8124. // Add the extra files that go along with a SCORM package.
  8125. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8126. $extra_files = scandir($main_code_path);
  8127. foreach ($extra_files as $extra_file) {
  8128. if (strpos($extra_file, '.') === 0)
  8129. continue;
  8130. else {
  8131. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8132. $this->create_path($dest_file);
  8133. copy($main_code_path.$extra_file, $dest_file);
  8134. }
  8135. }
  8136. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8137. $manifest = @$xmldoc->saveXML();
  8138. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8139. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8140. if ($this->ref == 'chamilo_scorm_export') {
  8141. // $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/scorm/'.$this->path);
  8142. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8143. } else {
  8144. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8145. }
  8146. // Clean possible temporary files.
  8147. foreach ($files_cleanup as $file) {
  8148. $res = unlink($file);
  8149. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8150. }
  8151. // Send file to client.
  8152. //$name = 'scorm_export_'.$this->lp_id.'.zip';
  8153. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  8154. $name = replace_dangerous_char($this->get_name()).'.zip';
  8155. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8156. }
  8157. public function scorm_export_to_pdf($lp_id) {
  8158. $lp_id = intval($lp_id);
  8159. $files_to_export = array();
  8160. $course_data = api_get_course_info($this->cc);
  8161. if (!empty($course_data)) {
  8162. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8163. $list = $this->get_flat_ordered_items_list($lp_id);
  8164. if (!empty($list)) {
  8165. foreach ($list as $item_id) {
  8166. $item = $this->items[$item_id];
  8167. switch ($item->type) {
  8168. case 'document':
  8169. //Getting documents from a LP with chamilo documents
  8170. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8171. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8172. if (file_exists($file_path)) {
  8173. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8174. }
  8175. break;
  8176. case 'asset': //commes from a scorm package generated by chamilo
  8177. case 'sco':
  8178. $file_path = $scorm_path.'/'.$item->path;
  8179. if (file_exists($file_path)) {
  8180. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8181. }
  8182. break;
  8183. case 'dokeos_chapter':
  8184. case 'dir':
  8185. case 'chapter':
  8186. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8187. break;
  8188. }
  8189. }
  8190. }
  8191. $pdf = new PDF();
  8192. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8193. return $result;
  8194. }
  8195. return false;
  8196. }
  8197. /**
  8198. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  8199. * if it doesn't exist
  8200. */
  8201. public function create_path($path) {
  8202. $path_bits = split('/', dirname($path));
  8203. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8204. $path_built = IS_WINDOWS_OS ? '' : '/';
  8205. foreach ($path_bits as $bit) {
  8206. if (!empty ($bit)) {
  8207. $new_path = $path_built . $bit;
  8208. if (is_dir($new_path)) {
  8209. $path_built = $new_path . '/';
  8210. } else {
  8211. mkdir($new_path, api_get_permissions_for_new_directories());
  8212. $path_built = $new_path . '/';
  8213. }
  8214. }
  8215. }
  8216. }
  8217. /**
  8218. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8219. * @return boolean The results of the unlink function, or false if there was no image to start with
  8220. */
  8221. public function delete_lp_image() {
  8222. $img = $this->get_preview_image();
  8223. if ($img != '') {
  8224. $del_file = api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/upload/learning_path/images/' . $img;
  8225. $this->set_preview_image('');
  8226. return @ unlink($del_file);
  8227. } else {
  8228. return false;
  8229. }
  8230. }
  8231. /**
  8232. * Uploads an author image to the upload/learning_path/images directory
  8233. * @param array The image array, coming from the $_FILES superglobal
  8234. * @return boolean True on success, false on error
  8235. */
  8236. public function upload_image($image_array) {
  8237. $image_moved = false;
  8238. if (!empty ($image_array['name'])) {
  8239. $upload_ok = process_uploaded_file($image_array);
  8240. $has_attachment = true;
  8241. } else {
  8242. $image_moved = true;
  8243. }
  8244. if ($upload_ok) {
  8245. if ($has_attachment) {
  8246. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8247. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8248. $updir = $sys_course_path . $courseDir;
  8249. // Try to add an extension to the file if it hasn't one.
  8250. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8251. if (!filter_extension($new_file_name)) {
  8252. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8253. $image_moved = false;
  8254. } else {
  8255. $file_extension = explode('.', $image_array['name']);
  8256. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8257. $new_file_name = uniqid('') . '.' . $file_extension;
  8258. $new_path = $updir . '/' . $new_file_name;
  8259. // Resize the image.
  8260. $temp = new Image($image_array['tmp_name']);
  8261. $picture_infos = $temp->get_image_info();
  8262. if ($picture_infos['width'] > 104) {
  8263. $thumbwidth = 104;
  8264. } else {
  8265. $thumbwidth = $picture_infos['width'];
  8266. }
  8267. if ($picture_infos['height'] > 96) {
  8268. $new_height = 96;
  8269. } else {
  8270. $new_height = $picture_infos['height'];
  8271. }
  8272. $temp->resize($thumbwidth, $new_height, 0);
  8273. $result = $temp->send_image($new_path);
  8274. // Storing the image filename.
  8275. if ($result) {
  8276. $image_moved = true;
  8277. $this->set_preview_image($new_file_name);
  8278. return true;
  8279. }
  8280. }
  8281. }
  8282. }
  8283. return false;
  8284. }
  8285. public function set_autolunch($lp_id, $status) {
  8286. $course_id = api_get_course_int_id();
  8287. $lp_id = intval($lp_id);
  8288. $status = intval($status);
  8289. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8290. //Setting everything to autolunch = 0
  8291. $attributes['autolunch'] = 0;
  8292. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8293. Database::update($lp_table, $attributes, $where);
  8294. if ($status == 1) {
  8295. //Setting my lp_id to autolunch = 1
  8296. $attributes['autolunch'] = 1;
  8297. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8298. Database::update($lp_table, $attributes, $where );
  8299. }
  8300. }
  8301. /**
  8302. * Gets previous_item_id for the next element of the lp_item table
  8303. * @author Isaac flores paz
  8304. * @return integer Previous item ID
  8305. */
  8306. function select_previous_item_id() {
  8307. $course_id = api_get_course_int_id();
  8308. if ($this->debug > 0) {
  8309. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8310. }
  8311. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8312. // Get the max order of the items
  8313. $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 . "'";
  8314. $rs_max_order = Database::query($sql_max_order);
  8315. $row_max_order = Database::fetch_object($rs_max_order);
  8316. $max_order = $row_max_order->display_order;
  8317. // Get the previous item ID
  8318. $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."' ";
  8319. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  8320. $row_max = Database::fetch_object($rs_max);
  8321. // Return the previous item ID
  8322. return $row_max->previous;
  8323. }
  8324. function copy() {
  8325. $main_path = api_get_path(SYS_CODE_PATH);
  8326. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8327. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8328. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8329. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8330. //Course builder
  8331. $cb = new CourseBuilder();
  8332. //Setting tools that will be copied
  8333. $cb->set_tools_to_build(array('learnpaths'));
  8334. //Setting elements that will be copied
  8335. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  8336. $course = $cb->build();
  8337. //Course restorer
  8338. $course_restorer = new CourseRestorer($course);
  8339. $course_restorer->set_add_text_in_items(true);
  8340. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8341. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8342. }
  8343. function verify_document_size($s) {
  8344. $post_max = ini_get('post_max_size');
  8345. $upl_max = ini_get('upload_max_filesize');
  8346. $documents_total_space = DocumentManager::documents_total_space();
  8347. $course_max_space = DocumentManager::get_course_quota();
  8348. $total_size = filesize($s) + $documents_total_space;
  8349. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8350. return true;
  8351. } else{
  8352. return false;
  8353. }
  8354. }
  8355. }
  8356. if (!function_exists('trim_value')) {
  8357. function trim_value(& $value) {
  8358. $value = trim($value);
  8359. }
  8360. }