learnpath.class.php 426 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171
  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. public $course_int_id;
  71. public function get_course_int_id() {
  72. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  73. }
  74. public function set_course_int_id($course_id) {
  75. return $this->course_int_id = intval($course_id);
  76. }
  77. /**
  78. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  79. * Also builds the list of items into $this->items.
  80. * @param string Course code
  81. * @param integer Learnpath ID
  82. * @param integer User ID
  83. * @return boolean True on success, false on error
  84. */
  85. public function __construct($course, $lp_id, $user_id) {
  86. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  87. // Check course code.
  88. $course_id = api_get_course_int_id();
  89. if ($this->debug > 0) { error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0); }
  90. if (empty($course)) {
  91. $this->error = 'Course code is empty';
  92. return false;
  93. } else {
  94. $main_table = Database::get_main_table(TABLE_MAIN_COURSE);
  95. $course = Database::escape_string($course);
  96. $sql = "SELECT * FROM $main_table WHERE code = '$course'";
  97. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying course: '.$sql, 0); }
  98. $res = Database::query($sql);
  99. if (Database::num_rows($res) > 0) {
  100. $this->cc = $course;
  101. $row_course = Database::fetch_array($res);
  102. $course_id = $row_course['id'];
  103. } else {
  104. $this->error = 'Course code does not exist in database ('.$sql.')';
  105. return false;
  106. }
  107. }
  108. // Check learnpath ID.
  109. if (empty($lp_id)) {
  110. $this->error = 'Learnpath ID is empty';
  111. return false;
  112. } else {
  113. // TODO: Make it flexible to use any course_code (still using env course code here).
  114. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  115. $lp_id = intval($lp_id);
  116. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  117. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0); }
  118. $res = Database::query($sql);
  119. if (Database::num_rows($res) > 0) {
  120. $this->lp_id = $lp_id;
  121. $row = Database::fetch_array($res);
  122. $this->type = $row['lp_type'];
  123. $this->name = stripslashes($row['name']);
  124. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  125. $this->proximity = $row['content_local'];
  126. $this->theme = $row['theme'];
  127. $this->maker = $row['content_maker'];
  128. $this->prevent_reinit = $row['prevent_reinit'];
  129. $this->seriousgame_mode = $row['seriousgame_mode'];
  130. $this->license = $row['content_license'];
  131. $this->scorm_debug = $row['debug'];
  132. $this->js_lib = $row['js_lib'];
  133. $this->path = $row['path'];
  134. $this->preview_image = $row['preview_image'];
  135. $this->author = $row['author'];
  136. $this->hide_toc_frame = $row['hide_toc_frame'];
  137. $this->lp_session_id = $row['session_id'];
  138. $this->use_max_score = $row['use_max_score'];
  139. $this->created_on = $row['created_on'];
  140. $this->modified_on = $row['modified_on'];
  141. $this->ref = $row['ref'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session ORDER BY view_count DESC";
  180. $res = Database::query($sql);
  181. if ($this->debug > 2) { error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0); }
  182. if (Database :: num_rows($res) > 0) {
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  185. }
  186. $row = Database :: fetch_array($res);
  187. $this->attempt = $row['view_count'];
  188. $this->lp_view_id = $row['id'];
  189. $this->last_item_seen = $row['last_item'];
  190. $this->progress_db = $row['progress'];
  191. $this->lp_view_session_id = $row['session_id'];
  192. } else {
  193. if ($this->debug > 2) {
  194. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  195. }
  196. $this->attempt = 1;
  197. $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)";
  198. $res_ins = Database::query($sql_ins);
  199. $this->lp_view_id = Database :: insert_id();
  200. if ($this->debug > 2) {
  201. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
  202. }
  203. }
  204. // Initialise items.
  205. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  206. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  207. $res = Database::query($sql);
  208. if ($this->debug > 2) {
  209. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  210. error_log('-- Start while--', 0);
  211. }
  212. $lp_item_id_list = array();
  213. while ($row = Database::fetch_array($res)) {
  214. $oItem = '';
  215. $lp_item_id_list[] = $row['id'];
  216. switch ($this->type) {
  217. case 3: //aicc
  218. $oItem = new aiccItem('db', $row['id'], $course_id);
  219. if (is_object($oItem)) {
  220. $my_item_id = $oItem->get_id();
  221. $oItem->set_lp_view($this->lp_view_id, $course_id);
  222. $oItem->set_prevent_reinit($this->prevent_reinit);
  223. // Don't use reference here as the next loop will make the pointed object change.
  224. $this->items[$my_item_id] = $oItem;
  225. $this->refs_list[$oItem->ref] = $my_item_id;
  226. if ($this->debug > 2) {
  227. error_log('New LP - learnpath::__construct() - aicc object with id ' . $my_item_id . ' set in items[]', 0);
  228. }
  229. }
  230. break;
  231. case 2:
  232. require_once 'scorm.class.php';
  233. require_once 'scormItem.class.php';
  234. $oItem = new scormItem('db', $row['id'], $course_id);
  235. if (is_object($oItem)) {
  236. $my_item_id = $oItem->get_id();
  237. $oItem->set_lp_view($this->lp_view_id, $course_id);
  238. $oItem->set_prevent_reinit($this->prevent_reinit);
  239. // Don't use reference here as the next loop will make the pointed object change.
  240. $this->items[$my_item_id] = $oItem;
  241. $this->refs_list[$oItem->ref] = $my_item_id;
  242. if ($this->debug > 2) {
  243. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  244. }
  245. }
  246. break;
  247. case 1:
  248. default:
  249. if ($this->debug > 2) {
  250. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  251. }
  252. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  253. if ($this->debug > 2) {
  254. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  255. }
  256. if (is_object($oItem)) {
  257. $my_item_id = $oItem->get_id();
  258. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  259. $oItem->set_prevent_reinit($this->prevent_reinit);
  260. // Don't use reference here as the next loop will make the pointed object change.
  261. $this->items[$my_item_id] = $oItem;
  262. $this->refs_list[$my_item_id] = $my_item_id;
  263. if ($this->debug > 2) {
  264. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - object with id ' . $my_item_id . ' set in items[]', 0);
  265. }
  266. }
  267. break;
  268. }
  269. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  270. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  271. if (is_object($this->items[$row['id']])) {
  272. $this->items[$row['id']]->set_level(0);
  273. }
  274. } else {
  275. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  276. $this->items[$row['id']]->set_level($level);
  277. if (is_object($this->items[$row['parent_item_id']])) {
  278. // Items is a list of pointers from item DB ids to item objects.
  279. $this->items[$row['parent_item_id']]->add_child($row['id']);
  280. } else {
  281. if ($this->debug > 2) {
  282. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - The parent item (' . $row['parent_item_id'] . ') of item ' . $row['id'] . ' could not be found', 0);
  283. }
  284. }
  285. }
  286. // Setting the view in the item object.
  287. if (is_object($this->items[$row['id']])) {
  288. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  289. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  290. $this->items[$row['id']]->current_start_time = 0;
  291. $this->items[$row['id']]->current_stop_time = 0;
  292. }
  293. }
  294. }
  295. if ($this->debug > 2) {
  296. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  297. }
  298. if (!empty($lp_item_id_list)) {
  299. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  300. // Get last viewing vars.
  301. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  302. // This query should only return one or zero result.
  303. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  304. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  305. ORDER BY view_count DESC ";
  306. if ($this->debug > 2) {
  307. error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
  308. }
  309. $status_list = array();
  310. $res = Database::query($sql);
  311. while ($row = Database :: fetch_array($res) ) {
  312. $status_list[$row['lp_item_id']] = $row['status'];
  313. }
  314. foreach ($lp_item_id_list as $item_id) {
  315. if (isset($status_list[$item_id])) {
  316. $status = $status_list[$item_id];
  317. if (is_object($this->items[$item_id])) {
  318. $this->items[$item_id]->set_status($status);
  319. if (empty($status)) {
  320. $this->items[$item_id]->set_status($this->default_status);
  321. }
  322. }
  323. } else {
  324. if (is_object($this->items[$item_id])) {
  325. $this->items[$item_id]->set_status($this->default_status);
  326. }
  327. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  328. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  329. VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')";
  330. if ($this->debug > 2) {
  331. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql_ins, 0);
  332. }
  333. $res_ins = Database::query($sql_ins);
  334. }
  335. }
  336. }
  337. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  338. $this->max_ordered_items = 0;
  339. foreach ($this->ordered_items as $index => $dummy) {
  340. if ($index > $this->max_ordered_items && !empty($dummy)) {
  341. $this->max_ordered_items = $index;
  342. }
  343. }
  344. // TODO: Define the current item better.
  345. $this->first();
  346. if ($this->debug > 2) {
  347. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  348. }
  349. }
  350. /**
  351. * 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
  352. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  353. *
  354. * @param int $parent
  355. * @param int $previous
  356. * @param string $type
  357. * @param int resource ID (ref)
  358. * @param string $title
  359. * @param string $description
  360. * @return int
  361. */
  362. public function add_item($parent, $previous, $type = 'dokeos_chapter', $id, $title, $description, $prerequisites = 0, $max_time_allowed = 0) {
  363. $course_id = api_get_course_int_id();
  364. if ($this->debug > 0) {
  365. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  366. }
  367. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  368. $parent = intval($parent);
  369. $previous = intval($previous);
  370. $type = Database::escape_string($type);
  371. $id = intval($id);
  372. $max_time_allowed = Database::escape_string(htmlentities($max_time_allowed));
  373. if (empty ($max_time_allowed)) {
  374. $max_time_allowed = 0;
  375. }
  376. $title = Database::escape_string($title);
  377. $description = Database::escape_string($description);
  378. $sql_count = " SELECT COUNT(id) AS num
  379. FROM $tbl_lp_item
  380. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND parent_item_id = " . $parent;
  381. $res_count = Database::query($sql_count);
  382. $row = Database :: fetch_array($res_count);
  383. $num = $row['num'];
  384. if ($num > 0) {
  385. if ($previous == 0) {
  386. $sql = "SELECT id, next_item_id, display_order
  387. FROM " . $tbl_lp_item . "
  388. WHERE c_id = $course_id AND
  389. lp_id = " . $this->get_id() . " AND
  390. parent_item_id = " . $parent . " AND
  391. previous_item_id = 0 OR previous_item_id=" . $parent;
  392. $result = Database::query($sql);
  393. $row = Database :: fetch_array($result);
  394. $tmp_previous = 0;
  395. $next = $row['id'];
  396. $display_order = 0;
  397. } else {
  398. $previous = (int) $previous;
  399. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  400. FROM $tbl_lp_item
  401. WHERE c_id = $course_id AND lp_id = " . $this->get_id() . " AND id = " . $previous;
  402. $result = Database::query($sql);
  403. $row = Database :: fetch_array($result);
  404. $tmp_previous = $row['id'];
  405. $next = $row['next_item_id'];
  406. $display_order = $row['display_order'];
  407. }
  408. } else {
  409. $tmp_previous = 0;
  410. $next = 0;
  411. $display_order = 0;
  412. }
  413. $new_item_id = -1;
  414. $id = Database::escape_string($id);
  415. if ($type == 'quiz') {
  416. $sql = 'SELECT SUM(ponderation)
  417. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  418. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  419. ON quiz_question.id = quiz_rel_question.question_id
  420. WHERE quiz_rel_question.exercice_id = '.$id." AND
  421. quiz_question.c_id = $course_id AND
  422. quiz_rel_question.c_id = $course_id ";
  423. $rsQuiz = Database::query($sql);
  424. $max_score = Database :: result($rsQuiz, 0, 0);
  425. //Disabling the exercise if we add it inside a LP
  426. $exercise = new Exercise();
  427. $exercise->read($id);
  428. $exercise->disable();
  429. $exercise->save();
  430. } else {
  431. $max_score = 100;
  432. }
  433. if ($prerequisites != 0) {
  434. $sql_ins = "INSERT INTO " . $tbl_lp_item . " (
  435. c_id,
  436. lp_id, ".
  437. "item_type, ".
  438. "ref, ".
  439. "title, ".
  440. "description, ".
  441. "path, ".
  442. "max_score, ".
  443. "parent_item_id, ".
  444. "previous_item_id, ".
  445. "next_item_id, ".
  446. "display_order, ".
  447. "prerequisite, ".
  448. "max_time_allowed ".
  449. ") VALUES (
  450. $course_id ,
  451. ".$this->get_id() . ", ".
  452. "'" . $type . "', ".
  453. "'', ".
  454. "'" . $title . "', ".
  455. "'" . $description . "', ".
  456. "'" . $id . "', ".
  457. "'" . $max_score . "', ".
  458. $parent . ", ".
  459. $previous . ", ".
  460. $next . ", ".
  461. ($display_order +1) . ", ".
  462. $prerequisites . ", ".
  463. $max_time_allowed .
  464. ")";
  465. } else {
  466. // Insert new item.
  467. $sql_ins = "
  468. INSERT INTO " . $tbl_lp_item . " ( ".
  469. "c_id, ".
  470. "lp_id, ".
  471. "item_type, ".
  472. "ref, ".
  473. "title, ".
  474. "description, ".
  475. "path, ".
  476. "max_score, ".
  477. "parent_item_id, ".
  478. "previous_item_id, ".
  479. "next_item_id, ".
  480. "display_order, ".
  481. "max_time_allowed ".
  482. ") VALUES (".
  483. $course_id. ",".
  484. $this->get_id() . ",".
  485. "'" . $type . "',".
  486. "'',".
  487. "'" . $title . "',".
  488. "'" . $description . "',".
  489. "'" . $id . "',".
  490. "'" . $max_score . "',".
  491. $parent . ",".
  492. $previous . ",".
  493. $next . ",".
  494. ($display_order +1) . ",".
  495. $max_time_allowed .
  496. ")";
  497. }
  498. if ($this->debug > 2) {
  499. error_log('New LP - Inserting dokeos_chapter: ' . $sql_ins, 0);
  500. }
  501. $res_ins = Database::query($sql_ins);
  502. if ($res_ins > 0) {
  503. $new_item_id = Database :: insert_id($res_ins);
  504. // Update the item that should come after the new item.
  505. $sql_update_next = "
  506. UPDATE " . $tbl_lp_item . "
  507. SET previous_item_id = " . $new_item_id . "
  508. WHERE c_id = $course_id AND id = " . $next;
  509. Database::query($sql_update_next);
  510. // Update the item that should be before the new item.
  511. $sql_update_previous = "
  512. UPDATE " . $tbl_lp_item . "
  513. SET next_item_id = " . $new_item_id . "
  514. WHERE c_id = $course_id AND id = " . $tmp_previous;
  515. Database::query($sql_update_previous);
  516. // Update all the items after the new item.
  517. $sql_update_order = "
  518. UPDATE " . $tbl_lp_item . "
  519. SET display_order = display_order + 1
  520. WHERE
  521. c_id = $course_id AND
  522. lp_id = " . $this->get_id() . " AND
  523. id <> " . $new_item_id . " AND
  524. parent_item_id = " . $parent . " AND
  525. display_order > " . $display_order;
  526. Database::query($sql_update_order);
  527. // Update the item that should come after the new item.
  528. $sql_update_ref = "UPDATE " . $tbl_lp_item . "
  529. SET ref = " . $new_item_id . "
  530. WHERE c_id = $course_id AND id = " . $new_item_id;
  531. Database::query($sql_update_ref);
  532. }
  533. // Upload audio.
  534. if (!empty ($_FILES['mp3']['name'])) {
  535. // Create the audio folder if it does not exist yet.
  536. global $_course;
  537. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  538. if (!is_dir($filepath . 'audio')) {
  539. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  540. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  541. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  542. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  543. }
  544. // Upload the file in the documents tool.
  545. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  546. $file_path = handle_uploaded_document($_course, $_FILES['mp3'], api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document', '/audio', api_get_user_id(), '', '', '', '', false);
  547. // Getting the filename only.
  548. $file_components = explode('/', $file_path);
  549. $file = $file_components[count($file_components) - 1];
  550. // Store the mp3 file in the lp_item table.
  551. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '" . Database::escape_string($file) . "' WHERE id = '" . Database::escape_string($new_item_id) . "'";
  552. Database::query($sql_insert_audio);
  553. }
  554. return $new_item_id;
  555. }
  556. /**
  557. * Static admin function allowing addition of a learnpath to a course.
  558. * @param string Course code
  559. * @param string Learnpath name
  560. * @param string Learnpath description string, if provided
  561. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  562. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  563. * @param string Zip file containing the learnpath or directory containing the learnpath
  564. * @return integer The new learnpath ID on success, 0 on failure
  565. */
  566. public static function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '', $publicated_on = '', $expired_on = '') {
  567. global $charset;
  568. $course_id = api_get_course_int_id();
  569. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  570. // Check course code exists.
  571. // Check lp_name doesn't exist, otherwise append something.
  572. $i = 0;
  573. $name = Database::escape_string($name);
  574. // Session id.
  575. $session_id = api_get_session_id();
  576. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  577. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  578. $res_name = Database::query($check_name);
  579. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  580. //by default the publication date is the same that the creation date
  581. //The behaviour above was changed due BT#2800
  582. global $_custom;
  583. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  584. $publicated_on = '';
  585. } else {
  586. $publicated_on = api_get_utc_datetime();
  587. }
  588. } else {
  589. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  590. }
  591. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  592. $expired_on = '';
  593. } else {
  594. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  595. }
  596. while (Database :: num_rows($res_name)) {
  597. // There is already one such name, update the current one a bit.
  598. $i++;
  599. $name = $name . ' - ' . $i;
  600. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  601. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  602. $res_name = Database::query($check_name);
  603. }
  604. // New name does not exist yet; keep it.
  605. // Escape description.
  606. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset)); // Kevin: added htmlentities().
  607. $type = 1;
  608. switch ($learnpath) {
  609. case 'guess':
  610. break;
  611. case 'dokeos':
  612. case 'chamilo':
  613. $type = 1;
  614. break;
  615. case 'aicc':
  616. break;
  617. }
  618. switch ($origin) {
  619. case 'zip':
  620. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  621. break;
  622. case 'manual':
  623. default:
  624. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  625. $res_max = Database::query($get_max);
  626. if (Database :: num_rows($res_max) < 1) {
  627. $dsp = 1;
  628. } else {
  629. $row = Database :: fetch_array($res_max);
  630. $dsp = $row[0] + 1;
  631. }
  632. $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) " .
  633. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
  634. Database::query($sql_insert);
  635. $id = Database :: insert_id();
  636. if ($id > 0) {
  637. $course_info = api_get_course_info();
  638. // Insert into item_property.
  639. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  640. api_set_default_visibility($id, TOOL_LEARNPATH);
  641. return $id;
  642. }
  643. break;
  644. }
  645. }
  646. /**
  647. * Appends a message to the message attribute
  648. * @param string Message to append.
  649. */
  650. public function append_message($string) {
  651. if ($this->debug > 0) {
  652. error_log('New LP - In learnpath::append_message()', 0);
  653. }
  654. $this->message .= $string;
  655. }
  656. /**
  657. * Autocompletes the parents of an item in case it's been completed or passed
  658. * @param integer Optional ID of the item from which to look for parents
  659. */
  660. public function autocomplete_parents($item) {
  661. $debug = $this->debug;
  662. if ($debug) {
  663. error_log('Learnpath::autocomplete_parents()', 0);
  664. }
  665. if (empty($item)) {
  666. $item = $this->current;
  667. }
  668. $parent_id = $this->items[$item]->get_parent();
  669. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  670. // if $item points to an object and there is a parent.
  671. if ($debug) {
  672. error_log('Autocompleting parent of item ' . $item . ' (item ' . $parent_id . ')', 0);
  673. }
  674. $current_item = $this->items[$item];
  675. $parent = $this->items[$parent_id]; // Get the parent.
  676. // New experiment including failed and browsed in completed status.
  677. $current_status = $current_item->get_status();
  678. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  679. // If the current item is completed or passes or succeeded.
  680. $completed = true;
  681. if ($debug) {
  682. error_log('Status of current item is alright', 0);
  683. }
  684. foreach ($parent->get_children() as $child) {
  685. // Check all his brothers (parent's children) for completion status.
  686. if ($child != $item) {
  687. if ($debug) {
  688. error_log('Looking at brother with ID ' . $child . ', status is ' . $this->items[$child]->get_status(), 0);
  689. }
  690. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  691. // Trying completing parents of failed and browsed items as well.
  692. if ($this->items[$child]->status_is(array(
  693. 'completed',
  694. 'passed',
  695. 'succeeded',
  696. 'browsed',
  697. 'failed'
  698. ))) {
  699. // Keep completion status to true.
  700. } else {
  701. if ($this->debug > 2) {
  702. error_log('Found one incomplete child of ' . $parent_id . ': ' . $child . ' is ' . $this->items[$child]->get_status(), 0);
  703. }
  704. $completed = false;
  705. }
  706. }
  707. }
  708. if ($completed) { // If all the children were completed:
  709. $parent->set_status('completed');
  710. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  711. $this->update_queue[$parent->get_id()] = $parent->get_status();
  712. if ($debug) {
  713. error_log('Added parent to update queue ' . print_r($this->update_queue, true), 0);
  714. }
  715. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  716. }
  717. } else {
  718. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  719. }
  720. } else {
  721. if ($debug) {
  722. error_log("#$item is an item doesn't have parents");
  723. }
  724. }
  725. }
  726. /**
  727. * Autosaves the current results into the database for the whole learnpath
  728. */
  729. public function autosave() {
  730. if ($this->debug > 0) {
  731. error_log('New LP - In learnpath::autosave()', 0);
  732. }
  733. // TODO: Add save operations for the learnpath itself.
  734. }
  735. /**
  736. * Clears the message attribute
  737. */
  738. public function clear_message() {
  739. if ($this->debug > 0) {
  740. error_log('New LP - In learnpath::clear_message()', 0);
  741. }
  742. $this->message = '';
  743. }
  744. /**
  745. * Closes the current resource
  746. *
  747. * Stops the timer
  748. * Saves into the database if required
  749. * Clears the current resource data from this object
  750. * @return boolean True on success, false on failure
  751. */
  752. public function close() {
  753. $course_id = api_get_course_int_id();
  754. if ($this->debug > 0) {
  755. error_log('New LP - In learnpath::close()', 0);
  756. }
  757. if (empty ($this->lp_id)) {
  758. $this->error = 'Trying to close this learnpath but no ID is set';
  759. return false;
  760. }
  761. $this->current_time_stop = time();
  762. if ($this->save) {
  763. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  764. /*
  765. $sql = "UPDATE $learnpath_view_table " .
  766. "SET " .
  767. "stop_time = ".$this->current_time_stop.", " .
  768. "score = ".$this->current_score.", ".
  769. "WHERE learnpath_id = '".$this->lp_id."'";
  770. //$res = Database::query($sql);
  771. $res = Database::query($res);
  772. if (Database::affected_rows($res) < 1) {
  773. $this->error = 'Could not update learnpath_view table while closing learnpath';
  774. return false;
  775. }
  776. */
  777. }
  778. $this->ordered_items = array ();
  779. $this->index = 0;
  780. unset ($this->lp_id);
  781. //unset other stuff
  782. return true;
  783. }
  784. /**
  785. * Static admin function allowing removal of a learnpath
  786. * @param string Course code
  787. * @param integer Learnpath ID
  788. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  789. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  790. */
  791. public function delete($course = null, $id = null, $delete = 'keep') {
  792. $course_id = api_get_course_int_id();
  793. // TODO: Implement a way of getting this to work when the current object is not set.
  794. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  795. //if (empty($course)) { $course = api_get_course_id(); }
  796. //if (empty($id)) { $id = $this->get_id(); }
  797. // If an ID is specifically given and the current LP is not the same, prevent delete.
  798. if (!empty ($id) && ($id != $this->lp_id)) {
  799. return false;
  800. }
  801. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  802. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  803. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  804. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  805. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  806. // Delete lp item id.
  807. foreach ($this->items as $id => $dummy) {
  808. //$this->items[$id]->delete();
  809. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  810. $res_del_item_view = Database::query($sql_del_view);
  811. }
  812. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  813. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  814. $res_del_item = Database::query($sql_del_item);
  815. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  816. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  817. $res_del_view = Database::query($sql_del_view);
  818. self::toggle_publish($this->lp_id, 'i');
  819. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  820. if ($this->type == 2 || $this->type == 3) {
  821. // This is a scorm learning path, delete the files as well.
  822. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  823. $res = Database::query($sql);
  824. if (Database :: num_rows($res) > 0) {
  825. $row = Database :: fetch_array($res);
  826. $path = $row['path'];
  827. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  828. $res = Database::query($sql);
  829. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  830. if ($this->debug > 2) {
  831. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  832. }
  833. } else {
  834. // No other LP uses that directory, delete it.
  835. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  836. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  837. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  838. if ($this->debug > 2) {
  839. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  840. }
  841. // Proposed by Christophe (clefevre).
  842. if (strcmp(substr($path, -2), "/.") == 0) {
  843. $path = substr($path, 0, -1); // Remove "." at the end.
  844. }
  845. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  846. rmdirr($course_scorm_dir . $path);
  847. }
  848. }
  849. }
  850. }
  851. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  852. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  853. $res_del_lp = Database::query($sql_del_lp);
  854. $this->update_display_order(); // Updates the display order of all lps.
  855. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  856. require_once '../gradebook/lib/be.inc.php';
  857. // Delete link of gradebook tool
  858. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  859. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  860. $result = Database::query($sql);
  861. $row = Database :: fetch_array($result, 'ASSOC');*/
  862. // Fixing gradebook link deleted see #5229.
  863. /*
  864. if (!empty($row['id'])) {
  865. $link = LinkFactory :: load($row['id']);
  866. if ($link[0] != null) {
  867. $link[0]->delete();
  868. }
  869. }*/
  870. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  871. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  872. if ($link_info !== false) {
  873. remove_resource_from_course_gradebook($link_info['id']);
  874. }
  875. if (api_get_setting('search_enabled') == 'true') {
  876. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  877. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  878. }
  879. }
  880. /**
  881. * Removes all the children of one item - dangerous!
  882. * @param integer Element ID of which children have to be removed
  883. * @return integer Total number of children removed
  884. */
  885. public function delete_children_items($id) {
  886. $course_id = api_get_course_int_id();
  887. if ($this->debug > 0) {
  888. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  889. }
  890. $num = 0;
  891. if (empty ($id) || $id != strval(intval($id))) {
  892. return false;
  893. }
  894. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  895. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  896. $res = Database::query($sql);
  897. while ($row = Database :: fetch_array($res)) {
  898. $num += $this->delete_children_items($row['id']);
  899. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  900. $res_del = Database::query($sql_del);
  901. $num++;
  902. }
  903. return $num;
  904. }
  905. /**
  906. * Removes an item from the current learnpath
  907. * @param integer Elem ID (0 if first)
  908. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  909. * @return integer Number of elements moved
  910. * @todo implement resource removal
  911. */
  912. public function delete_item($id, $remove = 'keep') {
  913. $course_id = api_get_course_int_id();
  914. if ($this->debug > 0) {
  915. error_log('New LP - In learnpath::delete_item()', 0);
  916. }
  917. // TODO: Implement the resource removal.
  918. if (empty ($id) || $id != strval(intval($id))) {
  919. return false;
  920. }
  921. // First select item to get previous, next, and display order.
  922. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  923. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  924. $res_sel = Database::query($sql_sel);
  925. if (Database :: num_rows($res_sel) < 1) {
  926. return false;
  927. }
  928. $row = Database :: fetch_array($res_sel);
  929. $previous = $row['previous_item_id'];
  930. $next = $row['next_item_id'];
  931. $display = $row['display_order'];
  932. $parent = $row['parent_item_id'];
  933. $lp = $row['lp_id'];
  934. // Delete children items.
  935. $num = $this->delete_children_items($id);
  936. if ($this->debug > 2) {
  937. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  938. }
  939. // Now delete the item.
  940. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  941. if ($this->debug > 2) {
  942. error_log('New LP - Deleting item: ' . $sql_del, 0);
  943. }
  944. $res_del = Database::query($sql_del);
  945. // Now update surrounding items.
  946. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  947. $res_upd = Database::query($sql_upd);
  948. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  949. $res_upd = Database::query($sql_upd);
  950. // Now update all following items with new display order.
  951. $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";
  952. $res_all = Database::query($sql_all);
  953. //Removing prerequisites since the item will not longer exist
  954. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  955. $res_all = Database::query($sql_all);
  956. // Remove from search engine if enabled.
  957. if (api_get_setting('search_enabled') == 'true') {
  958. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  959. $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';
  960. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  961. $res = Database::query($sql);
  962. if (Database :: num_rows($res) > 0) {
  963. $row2 = Database :: fetch_array($res);
  964. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  965. $di = new ChamiloIndexer();
  966. $di->remove_document((int) $row2['search_did']);
  967. }
  968. $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';
  969. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  970. Database::query($sql);
  971. }
  972. }
  973. /**
  974. * Updates an item's content in place
  975. * @param integer Element ID
  976. * @param integer Parent item ID
  977. * @param integer Previous item ID
  978. * @param string Item title
  979. * @param string Item description
  980. * @param string Prerequisites (optional)
  981. * @param string Indexing terms (optional)
  982. * @param array The array resulting of the $_FILES[mp3] element
  983. * @return boolean True on success, false on error
  984. */
  985. public function edit_item($id, $parent, $previous, $title, $description, $prerequisites = 0, $audio = null, $max_time_allowed = 0) {
  986. $course_id = api_get_course_int_id();
  987. if ($this->debug > 0) {
  988. error_log('New LP - In learnpath::edit_item()', 0);
  989. }
  990. if (empty ($max_time_allowed)) {
  991. $max_time_allowed = 0;
  992. }
  993. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  994. return false;
  995. }
  996. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  997. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  998. $res_select = Database::query($sql_select);
  999. $row_select = Database :: fetch_array($res_select);
  1000. $audio_update_sql = '';
  1001. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1002. // Create the audio folder if it does not exist yet.
  1003. global $_course;
  1004. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1005. if (!is_dir($filepath . 'audio')) {
  1006. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1007. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  1008. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  1009. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
  1010. }
  1011. // Upload file in documents.
  1012. $pi = pathinfo($audio['name']);
  1013. if ($pi['extension'] == 'mp3') {
  1014. $c_det = api_get_course_info($this->cc);
  1015. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1016. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1017. $path = substr($path, 7);
  1018. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1019. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1020. }
  1021. }
  1022. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1023. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1024. // TODO: htmlspecialchars to be checked for encoding related problems.
  1025. if ($same_parent && $same_previous) {
  1026. // Only update title and description.
  1027. $sql_update = " UPDATE " . $tbl_lp_item . "
  1028. SET title = '" . Database::escape_string($title) . "',
  1029. prerequisite = '" . $prerequisites . "',
  1030. description = '" . Database::escape_string($description) . "'
  1031. " . $audio_update_sql . ",
  1032. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1033. WHERE c_id = ".$course_id." AND id = " . $id;
  1034. $res_update = Database::query($sql_update);
  1035. } else {
  1036. $old_parent = $row_select['parent_item_id'];
  1037. $old_previous = $row_select['previous_item_id'];
  1038. $old_next = $row_select['next_item_id'];
  1039. $old_order = $row_select['display_order'];
  1040. $old_prerequisite = $row_select['prerequisite'];
  1041. $old_max_time_allowed = $row_select['max_time_allowed'];
  1042. /* BEGIN -- virtually remove the current item id */
  1043. /* for the next and previous item it is like the current item doesn't exist anymore */
  1044. if ($old_previous != 0) {
  1045. $sql_update_next = "
  1046. UPDATE " . $tbl_lp_item . "
  1047. SET next_item_id = " . $old_next . "
  1048. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1049. $res_update_next = Database::query($sql_update_next);
  1050. //echo '<p>' . $sql_update_next . '</p>';
  1051. }
  1052. if ($old_next != 0) {
  1053. $sql_update_previous = "
  1054. UPDATE " . $tbl_lp_item . "
  1055. SET previous_item_id = " . $old_previous . "
  1056. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1057. $res_update_previous = Database::query($sql_update_previous);
  1058. //echo '<p>' . $sql_update_previous . '</p>';
  1059. }
  1060. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1061. $sql_update_order = "
  1062. UPDATE " . $tbl_lp_item . "
  1063. SET display_order = display_order - 1
  1064. WHERE
  1065. c_id = ".$course_id." AND
  1066. display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
  1067. parent_item_id = " . $old_parent;
  1068. $res_update_order = Database::query($sql_update_order);
  1069. //echo '<p>' . $sql_update_order . '</p>';
  1070. /* END -- virtually remove the current item id */
  1071. /* BEGIN -- update the current item id to his new location */
  1072. if ($previous == 0) {
  1073. // Select the data of the item that should come after the current item.
  1074. $sql_select_old = "SELECT id, display_order
  1075. FROM " . $tbl_lp_item . "
  1076. WHERE
  1077. c_id = ".$course_id." AND
  1078. lp_id = " . $this->lp_id . " AND
  1079. parent_item_id = " . $parent . " AND
  1080. previous_item_id = " . $previous;
  1081. $res_select_old = Database::query($sql_select_old);
  1082. $row_select_old = Database :: fetch_array($res_select_old);
  1083. //echo '<p>' . $sql_select_old . '</p>';
  1084. // If the new parent didn't have children before.
  1085. if (Database :: num_rows($res_select_old) == 0) {
  1086. $new_next = 0;
  1087. $new_order = 1;
  1088. } else {
  1089. $new_next = $row_select_old['id'];
  1090. $new_order = $row_select_old['display_order'];
  1091. }
  1092. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1093. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1094. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1095. } else {
  1096. // Select the data of the item that should come before the current item.
  1097. $sql_select_old = " SELECT next_item_id, display_order
  1098. FROM " . $tbl_lp_item . "
  1099. WHERE c_id = ".$course_id." AND id = " . $previous;
  1100. $res_select_old = Database::query($sql_select_old);
  1101. $row_select_old = Database :: fetch_array($res_select_old);
  1102. //echo '<p>' . $sql_select_old . '</p>';
  1103. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1104. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1105. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1106. $new_next = $row_select_old['next_item_id'];
  1107. $new_order = $row_select_old['display_order'] + 1;
  1108. }
  1109. // TODO: htmlspecialchars to be checked for encoding related problems.
  1110. // Update the current item with the new data.
  1111. $sql_update = "UPDATE " . $tbl_lp_item . "
  1112. SET
  1113. title = '" . Database::escape_string($title) . "',
  1114. description = '" . Database::escape_string($description) . "',
  1115. parent_item_id = " . $parent . ",
  1116. previous_item_id = " . $previous . ",
  1117. next_item_id = " . $new_next . ",
  1118. display_order = " . $new_order . "
  1119. " . $audio_update_sql . "
  1120. WHERE c_id = ".$course_id." AND id = " . $id;
  1121. $res_update_next = Database::query($sql_update);
  1122. //echo '<p>' . $sql_update . '</p>';
  1123. if ($previous != 0) {
  1124. // Update the previous item's next_item_id.
  1125. $sql_update_previous = "
  1126. UPDATE " . $tbl_lp_item . "
  1127. SET next_item_id = " . $id . "
  1128. WHERE c_id = ".$course_id." AND id = " . $previous;
  1129. $res_update_next = Database::query($sql_update_previous);
  1130. //echo '<p>' . $sql_update_previous . '</p>';
  1131. }
  1132. if ($new_next != 0) {
  1133. // Update the next item's previous_item_id.
  1134. $sql_update_next = "
  1135. UPDATE " . $tbl_lp_item . "
  1136. SET previous_item_id = " . $id . "
  1137. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1138. $res_update_next = Database::query($sql_update_next);
  1139. //echo '<p>' . $sql_update_next . '</p>';
  1140. }
  1141. if ($old_prerequisite != $prerequisites) {
  1142. $sql_update_next = "
  1143. UPDATE " . $tbl_lp_item . "
  1144. SET prerequisite = " . $prerequisites . "
  1145. WHERE c_id = ".$course_id." AND id = " . $id;
  1146. $res_update_next = Database::query($sql_update_next);
  1147. }
  1148. if ($old_max_time_allowed != $max_time_allowed) {
  1149. $sql_update_max_time_allowed = "
  1150. UPDATE " . $tbl_lp_item . "
  1151. SET max_time_allowed = " . $max_time_allowed . "
  1152. WHERE c_id = ".$course_id." AND id = " . $id;
  1153. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1154. }
  1155. // Update all the items with the same or a bigger display_order than the current item.
  1156. $sql_update_order = "
  1157. UPDATE " . $tbl_lp_item . "
  1158. SET display_order = display_order + 1
  1159. WHERE
  1160. c_id = ".$course_id." AND
  1161. lp_id = " . $this->get_id() . " AND
  1162. id <> " . $id . " AND
  1163. parent_item_id = " . $parent . " AND
  1164. display_order >= " . $new_order;
  1165. $res_update_next = Database::query($sql_update_order);
  1166. }
  1167. }
  1168. /**
  1169. * Updates an item's prereq in place
  1170. * @param integer Element ID
  1171. * @param string Prerequisite Element ID
  1172. * @param string Prerequisite item type
  1173. * @param string Prerequisite min score
  1174. * @param string Prerequisite max score
  1175. * @return boolean True on success, false on error
  1176. */
  1177. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100) {
  1178. $course_id = api_get_course_int_id();
  1179. if ($this->debug > 0) {
  1180. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1181. }
  1182. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1183. return false;
  1184. }
  1185. $prerequisite_id = Database::escape_string($prerequisite_id);
  1186. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1187. if (!is_numeric($mastery_score) || $mastery_score < 0)
  1188. $mastery_score = 0;
  1189. if (!is_numeric($max_score) || $max_score < 0)
  1190. $max_score = 100;
  1191. if ($mastery_score > $max_score)
  1192. $max_score = $mastery_score;
  1193. if (!is_numeric($prerequisite_id))
  1194. $prerequisite_id = 'NULL';
  1195. $sql_upd = " UPDATE " . $tbl_lp_item . "
  1196. SET prerequisite = " . $prerequisite_id . " WHERE c_id = ".$course_id." AND id = " . $id;
  1197. $res_upd = Database::query($sql_upd);
  1198. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1199. $sql_upd = " UPDATE " . $tbl_lp_item . " SET
  1200. mastery_score = " . $mastery_score .
  1201. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1202. " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
  1203. $res_upd = Database::query($sql_upd);
  1204. }
  1205. // TODO: Update the item object (can be ignored for now because refreshed).
  1206. return true;
  1207. }
  1208. /**
  1209. * Escapes a string with the available database escape function
  1210. * @param string String to escape
  1211. * @return string String escaped
  1212. * @deprecated use Database::escape_string
  1213. */
  1214. public function escape_string($string) {
  1215. //if ($this->debug > 0) { error_log('New LP - In learnpath::escape_string('.$string.')', 0); }
  1216. return Database::escape_string($string);
  1217. }
  1218. /**
  1219. * Static admin function exporting a learnpath into a zip file
  1220. * @param string Export type (scorm, zip, cd)
  1221. * @param string Course code
  1222. * @param integer Learnpath ID
  1223. * @param string Zip file name
  1224. * @return string Zip file path (or false on error)
  1225. */
  1226. public function export_lp($type, $course, $id, $zipname) {
  1227. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1228. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1229. return false;
  1230. }
  1231. $url = '';
  1232. switch ($type) {
  1233. case 'scorm':
  1234. break;
  1235. case 'zip':
  1236. break;
  1237. case 'cdrom':
  1238. break;
  1239. }
  1240. return $url;
  1241. }
  1242. /**
  1243. * Gets all the chapters belonging to the same parent as the item/chapter given
  1244. * Can also be called as abstract method
  1245. * @param integer Item ID
  1246. * @return array A list of all the "brother items" (or an empty array on failure)
  1247. */
  1248. public function get_brother_chapters($id) {
  1249. $course_id = api_get_course_int_id();
  1250. if ($this->debug > 0) {
  1251. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1252. }
  1253. if (empty ($id) OR $id != strval(intval($id))) {
  1254. return array ();
  1255. }
  1256. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1257. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1258. $res_parent = Database::query($sql_parent);
  1259. if (Database :: num_rows($res_parent) > 0) {
  1260. $row_parent = Database :: fetch_array($res_parent);
  1261. $parent = $row_parent['parent_item_id'];
  1262. $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";
  1263. $res_bros = Database::query($sql_bros);
  1264. $list = array ();
  1265. while ($row_bro = Database :: fetch_array($res_bros)) {
  1266. $list[] = $row_bro;
  1267. }
  1268. return $list;
  1269. }
  1270. return array ();
  1271. }
  1272. /**
  1273. * Gets all the items belonging to the same parent as the item given
  1274. * Can also be called as abstract method
  1275. * @param integer Item ID
  1276. * @return array A list of all the "brother items" (or an empty array on failure)
  1277. */
  1278. public function get_brother_items($id) {
  1279. $course_id = api_get_course_int_id();
  1280. if ($this->debug > 0) {
  1281. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1282. }
  1283. if (empty ($id) OR $id != strval(intval($id))) {
  1284. return array ();
  1285. }
  1286. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1287. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1288. $res_parent = Database::query($sql_parent);
  1289. if (Database :: num_rows($res_parent) > 0) {
  1290. $row_parent = Database :: fetch_array($res_parent);
  1291. $parent = $row_parent['parent_item_id'];
  1292. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1293. $res_bros = Database::query($sql_bros);
  1294. $list = array ();
  1295. while ($row_bro = Database :: fetch_array($res_bros)) {
  1296. $list[] = $row_bro;
  1297. }
  1298. return $list;
  1299. }
  1300. return array ();
  1301. }
  1302. /**
  1303. * Get the specific prefix index terms of this learning path
  1304. * @return array Array of terms
  1305. */
  1306. public function get_common_index_terms_by_prefix($prefix) {
  1307. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1308. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1309. $prefix_terms = array();
  1310. if (!empty($terms)) {
  1311. foreach ($terms as $term) {
  1312. $prefix_terms[] = $term['value'];
  1313. }
  1314. }
  1315. return $prefix_terms;
  1316. }
  1317. /**
  1318. * Gets the number of items currently completed
  1319. * @return integer The number of items currently completed
  1320. */
  1321. public function get_complete_items_count() {
  1322. if ($this->debug > 0) {
  1323. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1324. }
  1325. $i = 0;
  1326. foreach ($this->items as $id => $dummy) {
  1327. // Trying failed and browsed considered "progressed" as well.
  1328. if ($this->items[$id]->status_is(array(
  1329. 'completed',
  1330. 'passed',
  1331. 'succeeded',
  1332. 'browsed',
  1333. 'failed'
  1334. )) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir') {
  1335. $i++;
  1336. }
  1337. }
  1338. return $i;
  1339. }
  1340. /**
  1341. * Gets the current item ID
  1342. * @return integer The current learnpath item id
  1343. */
  1344. public function get_current_item_id() {
  1345. $current = 0;
  1346. if ($this->debug > 0) {
  1347. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1348. }
  1349. if (!empty ($this->current)) {
  1350. $current = $this->current;
  1351. }
  1352. if ($this->debug > 2) {
  1353. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1354. }
  1355. return $current;
  1356. }
  1357. /**
  1358. * Force to get the first learnpath item id
  1359. * @return integer The current learnpath item id
  1360. */
  1361. public function get_first_item_id() {
  1362. $current = 0;
  1363. if (is_array($this->ordered_items)) {
  1364. $current = $this->ordered_items[0];
  1365. }
  1366. return $current;
  1367. }
  1368. /**
  1369. * Gets the total number of items available for viewing in this SCORM
  1370. * @return integer The total number of items
  1371. */
  1372. public function get_total_items_count() {
  1373. if ($this->debug > 0) {
  1374. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1375. }
  1376. return count($this->items);
  1377. }
  1378. /**
  1379. * Gets the total number of items available for viewing in this SCORM but without chapters
  1380. * @return integer The total no-chapters number of items
  1381. */
  1382. public function get_total_items_count_without_chapters() {
  1383. if ($this->debug > 0) {
  1384. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1385. }
  1386. $total = 0;
  1387. foreach ($this->items as $temp2) {
  1388. if (!in_array($temp2->get_type(), array(
  1389. 'dokeos_chapter',
  1390. 'chapter',
  1391. 'dir'
  1392. )))
  1393. $total++;
  1394. }
  1395. return $total;
  1396. }
  1397. /**
  1398. * Gets the first element URL.
  1399. * @return string URL to load into the viewer
  1400. */
  1401. public function first() {
  1402. if ($this->debug > 0) {
  1403. error_log('New LP - In learnpath::first()', 0);
  1404. error_log('$this->last_item_seen '.$this->last_item_seen);
  1405. //error_log('$this->items '.print_r($this->items, 1));
  1406. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1407. }
  1408. // Test if the last_item_seen exists and is not a dir.
  1409. if (count($this->ordered_items) == 0) {
  1410. $this->index = 0;
  1411. }
  1412. if ($this->debug > 0) {
  1413. if (isset($this->items[$this->last_item_seen])) {
  1414. $status = $this->items[$this->last_item_seen]->get_status();
  1415. }
  1416. error_log('status '.$status);
  1417. }
  1418. if (!empty($this->last_item_seen) &&
  1419. !empty($this->items[$this->last_item_seen]) &&
  1420. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1421. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1422. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1423. //&& !$this->items[$this->last_item_seen]->is_done()
  1424. ) {
  1425. if ($this->debug > 2) {
  1426. 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);
  1427. }
  1428. $index = -1;
  1429. foreach ($this->ordered_items as $myindex => $item_id) {
  1430. if ($item_id == $this->last_item_seen) {
  1431. $index = $myindex;
  1432. break;
  1433. }
  1434. }
  1435. if ($index == -1) {
  1436. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1437. if ($this->debug > 2) {
  1438. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1439. }
  1440. return false;
  1441. } else {
  1442. $this->last = $this->last_item_seen;
  1443. $this->current = $this->last_item_seen;
  1444. $this->index = $index;
  1445. }
  1446. } else {
  1447. if ($this->debug > 2) {
  1448. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1449. }
  1450. $index = 0;
  1451. // Loop through all ordered items and stop at the first item that is
  1452. // not a directory *and* that has not been completed yet.
  1453. while ( !empty($this->ordered_items[$index]) AND
  1454. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1455. (
  1456. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1457. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1458. $this->items[$this->ordered_items[$index]]->is_done() === true
  1459. ) AND $index < $this->max_ordered_items) {
  1460. $index++;
  1461. }
  1462. $this->last = $this->current;
  1463. // current is
  1464. $this->current = $this->ordered_items[$index];
  1465. $this->index = $index;
  1466. if ($this->debug > 2) {
  1467. error_log('$index ' . $index);
  1468. }
  1469. if ($this->debug > 2) {
  1470. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1471. }
  1472. }
  1473. if ($this->debug > 2) {
  1474. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1475. }
  1476. }
  1477. /**
  1478. * Gets the information about an item in a format usable as JavaScript to update
  1479. * the JS API by just printing this content into the <head> section of the message frame
  1480. * @param integer Item ID
  1481. * @return string
  1482. */
  1483. public function get_js_info($item_id = '') {
  1484. if ($this->debug > 0) {
  1485. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1486. }
  1487. $info = '';
  1488. $item_id = Database::escape_string($item_id);
  1489. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1490. //if item is defined, return values from DB
  1491. $oItem = $this->items[$item_id];
  1492. $info .= '<script language="javascript">';
  1493. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1494. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1495. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1496. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1497. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1498. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1499. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1500. $info .= "top.set_flag_synchronized();";
  1501. $info .= '</script>';
  1502. if ($this->debug > 2) {
  1503. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1504. }
  1505. return $info;
  1506. } else {
  1507. // If item_id is empty, just update to default SCORM data.
  1508. $info .= '<script language="javascript">';
  1509. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1510. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1511. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1512. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1513. $info .= "top.set_session_time('" . learnpathItem :: get_scorm_time('js') . "');";
  1514. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1515. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1516. $info .= "top.set_flag_synchronized();";
  1517. $info .= '</script>';
  1518. if ($this->debug > 2) {
  1519. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1520. }
  1521. return $info;
  1522. }
  1523. }
  1524. /**
  1525. * Gets the js library from the database
  1526. * @return string The name of the javascript library to be used
  1527. */
  1528. public function get_js_lib() {
  1529. $lib = '';
  1530. if (!empty ($this->js_lib)) {
  1531. $lib = $this->js_lib;
  1532. }
  1533. return $lib;
  1534. }
  1535. /**
  1536. * Gets the learnpath database ID
  1537. * @return integer Learnpath ID in the lp table
  1538. */
  1539. public function get_id() {
  1540. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1541. if (!empty ($this->lp_id)) {
  1542. return $this->lp_id;
  1543. } else {
  1544. return 0;
  1545. }
  1546. }
  1547. /**
  1548. * Gets the last element URL.
  1549. * @return string URL to load into the viewer
  1550. */
  1551. public function get_last() {
  1552. if ($this->debug > 0) {
  1553. error_log('New LP - In learnpath::get_last()', 0);
  1554. }
  1555. $this->index = count($this->ordered_items) - 1;
  1556. return $this->ordered_items[$this->index];
  1557. }
  1558. /**
  1559. * Gets the navigation bar for the learnpath display screen
  1560. * @return string The HTML string to use as a navigation bar
  1561. */
  1562. public function get_navigation_bar() {
  1563. if ($this->debug > 0) {
  1564. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1565. }
  1566. $navbar = null;
  1567. $lp_id = $this->lp_id;
  1568. $mycurrentitemid = $this->get_current_item_id();
  1569. if ($this->mode == 'fullscreen') {
  1570. $navbar = '
  1571. <div class="buttons">
  1572. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
  1573. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1574. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>.
  1575. <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>
  1576. </div>';
  1577. } else {
  1578. $navbar = '
  1579. <div class="buttons">
  1580. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="' . get_lang('Reporting') . '"></a>
  1581. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="' . get_lang('ScormPrevious') . '"></a>
  1582. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="' . get_lang('ScormNext') . '"></a>
  1583. </div>';
  1584. }
  1585. return $navbar;
  1586. }
  1587. /**
  1588. * Gets the next resource in queue (url).
  1589. * @return string URL to load into the viewer
  1590. */
  1591. public function get_next_index() {
  1592. if ($this->debug > 0) {
  1593. error_log('New LP - In learnpath::get_next_index()', 0);
  1594. }
  1595. // TODO
  1596. $index = $this->index;
  1597. $index++;
  1598. if ($this->debug > 2) {
  1599. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1600. }
  1601. 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) {
  1602. $index++;
  1603. if ($index == $this->max_ordered_items){
  1604. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1605. return $this->index;
  1606. } else {
  1607. return $index;
  1608. }
  1609. }
  1610. }
  1611. if (empty ($this->ordered_items[$index])) {
  1612. return $this->index;
  1613. }
  1614. if ($this->debug > 2) {
  1615. error_log('New LP - index is now ' . $index, 0);
  1616. }
  1617. return $index;
  1618. }
  1619. /**
  1620. * Gets item_id for the next element
  1621. * @return integer Next item (DB) ID
  1622. */
  1623. public function get_next_item_id() {
  1624. if ($this->debug > 0) {
  1625. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1626. }
  1627. $new_index = $this->get_next_index();
  1628. if (!empty ($new_index)) {
  1629. if (isset ($this->ordered_items[$new_index])) {
  1630. if ($this->debug > 2) {
  1631. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1632. }
  1633. return $this->ordered_items[$new_index];
  1634. }
  1635. }
  1636. if ($this->debug > 2) {
  1637. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1638. }
  1639. return 0;
  1640. }
  1641. /**
  1642. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1643. *
  1644. * Generally, the package provided is in the form of a zip file, so the function
  1645. * has been written to test a zip file. If not a zip, the function will return the
  1646. * default return value: ''
  1647. * @param string the path to the file
  1648. * @param string the original name of the file
  1649. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1650. */
  1651. public static function get_package_type($file_path, $file_name) {
  1652. // Get name of the zip file without the extension.
  1653. $file_info = pathinfo($file_name);
  1654. $filename = $file_info['basename']; // Name including extension.
  1655. $extension = $file_info['extension']; // Extension only.
  1656. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1657. 'dll',
  1658. 'exe'
  1659. ))) {
  1660. return 'oogie';
  1661. }
  1662. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1663. 'dll',
  1664. 'exe'
  1665. ))) {
  1666. return 'woogie';
  1667. }
  1668. $file_base_name = str_replace('.' . $extension, '', $filename); // Filename without its extension.
  1669. $zipFile = new PclZip($file_path);
  1670. // Check the zip content (real size and file extension).
  1671. $zipContentArray = $zipFile->listContent();
  1672. $package_type = '';
  1673. $at_root = false;
  1674. $manifest = '';
  1675. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1676. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1677. foreach ($zipContentArray as $thisContent) {
  1678. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1679. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1680. }
  1681. elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1682. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1683. $package_type = 'scorm';
  1684. break; // Exit the foreach loop.
  1685. }
  1686. elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1687. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1688. $package_type = 'aicc';
  1689. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1690. } else {
  1691. $package_type = '';
  1692. }
  1693. }
  1694. }
  1695. return $package_type;
  1696. }
  1697. /**
  1698. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1699. * @return string URL to load into the viewer
  1700. */
  1701. public function get_previous_index() {
  1702. if ($this->debug > 0) {
  1703. error_log('New LP - In learnpath::get_previous_index()', 0);
  1704. }
  1705. $index = $this->index;
  1706. if (isset ($this->ordered_items[$index -1])) {
  1707. $index--;
  1708. 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')) {
  1709. $index--;
  1710. if ($index < 0) {
  1711. return $this->index;
  1712. }
  1713. }
  1714. } else {
  1715. if ($this->debug > 2) {
  1716. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1717. }
  1718. // There is no previous item.
  1719. }
  1720. return $index;
  1721. }
  1722. /**
  1723. * Gets item_id for the next element
  1724. * @return integer Previous item (DB) ID
  1725. */
  1726. public function get_previous_item_id() {
  1727. if ($this->debug > 0) {
  1728. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1729. }
  1730. $new_index = $this->get_previous_index();
  1731. return $this->ordered_items[$new_index];
  1732. }
  1733. /**
  1734. * Gets the progress value from the progress_db attribute
  1735. * @return integer Current progress value
  1736. */
  1737. public function get_progress() {
  1738. if ($this->debug > 0) {
  1739. error_log('New LP - In learnpath::get_progress()', 0);
  1740. }
  1741. if (!empty ($this->progress_db)) {
  1742. return $this->progress_db;
  1743. }
  1744. return 0;
  1745. }
  1746. /**
  1747. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1748. * @param integer Learnpath ID
  1749. * @param integer User ID
  1750. * @param string Mode of display ('%','abs' or 'both')
  1751. * @param string Course database name (optional, defaults to '')
  1752. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1753. * @return integer Current progress value as found in the database
  1754. */
  1755. public static function get_db_progress($lp_id, $user_id, $mode = '%', $course_code = '', $sincere = false,$session_id = 0) {
  1756. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1757. $session_id = intval($session_id);
  1758. $course_info = api_get_course_info($course_code);
  1759. $session_condition = api_get_session_condition($session_id);
  1760. $course_id = $course_info['real_id'];
  1761. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1762. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1763. $res = Database::query($sql);
  1764. $view_id = 0;
  1765. if (Database :: num_rows($res) > 0) {
  1766. $row = Database :: fetch_array($res);
  1767. $progress = $row['progress'];
  1768. $view_id = $row['id'];
  1769. } else {
  1770. if ($sincere) {
  1771. return null;
  1772. }
  1773. }
  1774. if (empty ($progress)) {
  1775. $progress = '0';
  1776. }
  1777. if ($mode == '%') {
  1778. return $progress . '%';
  1779. } else {
  1780. // Get the number of items completed and the number of items total.
  1781. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  1782. $sql = "SELECT count(*) FROM $tbl
  1783. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = " . $lp_id . " AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  1784. $res = Database::query($sql);
  1785. $row = Database :: fetch_array($res);
  1786. $total = $row[0];
  1787. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1788. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  1789. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  1790. // Trying as also counting browsed and failed items.
  1791. $sql = "SELECT count(distinct(lp_item_id))
  1792. FROM $tbl_item_view as item_view
  1793. INNER JOIN $tbl_item as item
  1794. ON item.id = item_view.lp_item_id
  1795. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  1796. WHERE
  1797. item_view.c_id = $course_id AND
  1798. item.c_id = $course_id AND
  1799. lp_view_id = " . $view_id . " AND
  1800. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  1801. $res = Database::query($sql);
  1802. $row = Database :: fetch_array($res);
  1803. $completed = $row[0];
  1804. if ($mode == 'abs') {
  1805. return $completed . '/' . $total;
  1806. } elseif ($mode == 'both') {
  1807. if ($progress < ($completed / ($total ? $total : 1))) {
  1808. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  1809. }
  1810. return $progress . '% (' . $completed . '/' . $total . ')';
  1811. }
  1812. }
  1813. return $progress;
  1814. }
  1815. /**
  1816. * Returns the HTML necessary to print a mediaplayer block inside a page
  1817. * @return string The mediaplayer HTML
  1818. */
  1819. public function get_mediaplayer($autostart='true') {
  1820. $course_id = api_get_course_int_id();
  1821. global $_course;
  1822. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1823. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1824. // Getting all the information about the item.
  1825. $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 " .
  1826. "WHERE lp.id = '" . $_SESSION['oLP']->current . "' AND
  1827. lp.c_id = $course_id AND
  1828. lp_view.c_id = $course_id";
  1829. $result = Database::query($sql);
  1830. $row = Database::fetch_assoc($result);
  1831. $output = '';
  1832. if (!empty ($row['audio'])) {
  1833. $list = $_SESSION['oLP']->get_toc();
  1834. $type_quiz = false;
  1835. foreach($list as $toc) {
  1836. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  1837. $type_quiz = true;
  1838. }
  1839. }
  1840. if ($type_quiz) {
  1841. if ($_SESSION['oLP']->prevent_reinit == 1) {
  1842. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  1843. } else {
  1844. $autostart_audio = $autostart;
  1845. }
  1846. } else {
  1847. $autostart_audio = 'true';
  1848. }
  1849. // The mp3 player.
  1850. $output = '<div id="container">';
  1851. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  1852. $output .= '<script type="text/javascript">
  1853. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  1854. s1.addParam("allowscriptaccess","always");
  1855. s1.addParam("flashvars","file=' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=' . $autostart_audio.'");
  1856. s1.write("container");
  1857. </script>
  1858. </div>';
  1859. }
  1860. return $output;
  1861. }
  1862. /**
  1863. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  1864. * @param int Learnpath id
  1865. * @param int Student id
  1866. * @param string Course code (optional)
  1867. * @return bool True if
  1868. */
  1869. public static function is_lp_visible_for_student($lp_id, $student_id, $course = null) {
  1870. $lp_id = (int)$lp_id;
  1871. $course = api_get_course_info($course);
  1872. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  1873. // Get current prerequisite
  1874. $sql = "SELECT id, prerequisite, publicated_on, expired_on FROM $tbl_learnpath WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  1875. $rs = Database::query($sql);
  1876. $now = time();
  1877. if (Database::num_rows($rs)>0) {
  1878. $row = Database::fetch_array($rs, 'ASSOC');
  1879. $prerequisite = $row['prerequisite'];
  1880. $is_visible = true;
  1881. $progress = 0;
  1882. if (!empty($prerequisite)) {
  1883. $progress = self::get_db_progress($prerequisite,$student_id,'%', '', false, api_get_session_id());
  1884. $progress = intval($progress);
  1885. if ($progress < 100) {
  1886. $is_visible = false;
  1887. }
  1888. }
  1889. // Also check the time availability of the LP
  1890. if ($is_visible) {
  1891. //Adding visibility reestrinctions
  1892. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  1893. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  1894. //api_not_allowed();
  1895. $is_visible = false;
  1896. }
  1897. }
  1898. //Blocking empty start times see BT#2800
  1899. global $_custom;
  1900. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  1901. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  1902. //api_not_allowed();
  1903. $is_visible = false;
  1904. }
  1905. }
  1906. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  1907. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  1908. //api_not_allowed();
  1909. $is_visible = false;
  1910. }
  1911. }
  1912. }
  1913. return $is_visible;
  1914. }
  1915. return false;
  1916. }
  1917. /**
  1918. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  1919. * completed so far.
  1920. * @param string Mode in which we want the values
  1921. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  1922. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  1923. * @param boolean true if it comes from a Diplay LP view
  1924. * @return string HTML string containing the progress bar
  1925. */
  1926. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false) {
  1927. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  1928. global $lp_theme_css;
  1929. // Setting up the CSS path of the current style if exists.
  1930. if (!empty ($lp_theme_css)) {
  1931. $css_path = api_get_path(WEB_CODE_PATH) . 'css/' . $lp_theme_css . '/images/';
  1932. } else {
  1933. $css_path = '../img/';
  1934. }
  1935. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  1936. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  1937. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  1938. }
  1939. $text = $percentage . $text_add;
  1940. //@todo use Display::display_progress();
  1941. $output .= '<div class="progress progress-striped">
  1942. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  1943. </div>
  1944. <div class="progresstext" id="progress_text">' . $text . '</div>';
  1945. return $output;
  1946. }
  1947. /**
  1948. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  1949. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  1950. * @param integer Additional steps to fake as completed
  1951. * @return list Percentage or number and symbol (% or /xx)
  1952. */
  1953. public function get_progress_bar_text($mode = '', $add = 0) {
  1954. if ($this->debug > 0) {
  1955. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  1956. }
  1957. if (empty ($mode)) {
  1958. $mode = $this->progress_bar_mode;
  1959. }
  1960. $total_items = $this->get_total_items_count_without_chapters();
  1961. if ($this->debug > 2) {
  1962. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  1963. }
  1964. $i = $this->get_complete_items_count();
  1965. if ($this->debug > 2) {
  1966. error_log('New LP - Items completed so far: ' . $i, 0);
  1967. }
  1968. if ($add != 0) {
  1969. $i += $add;
  1970. if ($this->debug > 2) {
  1971. error_log('New LP - Items completed so far (+modifier): ' . $i, 0);
  1972. }
  1973. }
  1974. $text = '';
  1975. if ($i > $total_items) {
  1976. $i = $total_items;
  1977. }
  1978. if ($mode == '%') {
  1979. if ($total_items > 0) {
  1980. $percentage = ((float) $i / (float) $total_items) * 100;
  1981. } else {
  1982. $percentage = 0;
  1983. }
  1984. $percentage = number_format($percentage, 0);
  1985. $text = '%';
  1986. }
  1987. elseif ($mode == 'abs') {
  1988. $percentage = $i;
  1989. $text = '/' . $total_items;
  1990. }
  1991. return array (
  1992. $percentage,
  1993. $text
  1994. );
  1995. }
  1996. /**
  1997. * Gets the progress bar mode
  1998. * @return string The progress bar mode attribute
  1999. */
  2000. public function get_progress_bar_mode() {
  2001. if ($this->debug > 0) {
  2002. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2003. }
  2004. if (!empty ($this->progress_bar_mode)) {
  2005. return $this->progress_bar_mode;
  2006. } else {
  2007. return '%';
  2008. }
  2009. }
  2010. /**
  2011. * Gets the learnpath proximity (remote or local)
  2012. * @return string Learnpath proximity
  2013. */
  2014. public function get_proximity() {
  2015. if ($this->debug > 0) {
  2016. error_log('New LP - In learnpath::get_proximity()', 0);
  2017. }
  2018. if (!empty ($this->proximity)) {
  2019. return $this->proximity;
  2020. } else {
  2021. return '';
  2022. }
  2023. }
  2024. /**
  2025. * Gets the learnpath theme (remote or local)
  2026. * @return string Learnpath theme
  2027. */
  2028. public function get_theme() {
  2029. if ($this->debug > 0) {
  2030. error_log('New LP - In learnpath::get_theme()', 0);
  2031. }
  2032. if (!empty ($this->theme)) {
  2033. return $this->theme;
  2034. } else {
  2035. return '';
  2036. }
  2037. }
  2038. /**
  2039. * Gets the learnpath session id
  2040. * @return string Learnpath theme
  2041. */
  2042. public function get_lp_session_id() {
  2043. if ($this->debug > 0) {
  2044. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2045. }
  2046. if (!empty ($this->lp_session_id)) {
  2047. return $this->lp_session_id;
  2048. } else {
  2049. return 0;
  2050. }
  2051. }
  2052. /**
  2053. * Gets the learnpath image
  2054. * @return string Web URL of the LP image
  2055. */
  2056. public function get_preview_image() {
  2057. if ($this->debug > 0) {
  2058. error_log('New LP - In learnpath::get_preview_image()', 0);
  2059. }
  2060. if (!empty ($this->preview_image)) {
  2061. return $this->preview_image;
  2062. } else {
  2063. return '';
  2064. }
  2065. }
  2066. /**
  2067. * Gets the learnpath author
  2068. * @return string LP's author
  2069. */
  2070. public function get_author() {
  2071. if ($this->debug > 0) {
  2072. error_log('New LP - In learnpath::get_author()', 0);
  2073. }
  2074. if (!empty ($this->author)) {
  2075. return $this->author;
  2076. } else {
  2077. return '';
  2078. }
  2079. }
  2080. /**
  2081. * Gets the learnpath author
  2082. * @return string LP's author
  2083. */
  2084. public function get_hide_toc_frame() {
  2085. if ($this->debug > 0) {
  2086. error_log('New LP - In learnpath::get_author()', 0);
  2087. }
  2088. if (!empty ($this->hide_toc_frame)) {
  2089. return $this->hide_toc_frame;
  2090. } else {
  2091. return '';
  2092. }
  2093. }
  2094. /**
  2095. * Generate a new prerequisites string for a given item. If this item was a sco and
  2096. * its prerequisites were strings (instead of IDs), then transform those strings into
  2097. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2098. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2099. * same rule as the scorm_export() method
  2100. * @param integer Item ID
  2101. * @return string Prerequisites string ready for the export as SCORM
  2102. */
  2103. public function get_scorm_prereq_string($item_id) {
  2104. if ($this->debug > 0) {
  2105. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2106. }
  2107. if (!is_object($this->items[$item_id])) {
  2108. return false;
  2109. }
  2110. $oItem = $this->items[$item_id];
  2111. $prereq = $oItem->get_prereq_string();
  2112. if (empty ($prereq)) {
  2113. return '';
  2114. }
  2115. 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,
  2116. // then simply return it (with the ITEM_ prefix).
  2117. return 'ITEM_' . $prereq;
  2118. } else {
  2119. if (isset ($this->refs_list[$prereq])) {
  2120. // It's a simple string item from which the ID can be found in the refs list,
  2121. // so we can transform it directly to an ID for export.
  2122. return 'ITEM_' . $this->refs_list[$prereq];
  2123. } else {
  2124. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2125. // and replace them, one by one, by the internal IDs (chamilo db)
  2126. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2127. // by a space as well.
  2128. $find = array (
  2129. '&',
  2130. '|',
  2131. '~',
  2132. '=',
  2133. '<>',
  2134. '{',
  2135. '}',
  2136. '*',
  2137. '(',
  2138. ')'
  2139. );
  2140. $replace = array (
  2141. ' ',
  2142. ' ',
  2143. ' ',
  2144. ' ',
  2145. ' ',
  2146. ' ',
  2147. ' ',
  2148. ' ',
  2149. ' ',
  2150. ' '
  2151. );
  2152. $prereq_mod = str_replace($find, $replace, $prereq);
  2153. $ids = split(' ', $prereq_mod);
  2154. foreach ($ids as $id) {
  2155. $id = trim($id);
  2156. if (isset ($this->refs_list[$id])) {
  2157. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2158. }
  2159. }
  2160. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2161. return $prereq;
  2162. }
  2163. }
  2164. }
  2165. /**
  2166. * Returns the XML DOM document's node
  2167. * @param resource Reference to a list of objects to search for the given ITEM_*
  2168. * @param string The identifier to look for
  2169. * @return mixed The reference to the element found with that identifier. False if not found
  2170. */
  2171. public function get_scorm_xml_node(& $children, $id) {
  2172. for ($i = 0; $i < $children->length; $i++) {
  2173. $item_temp = $children->item($i);
  2174. if ($item_temp->nodeName == 'item') {
  2175. if ($item_temp->getAttribute('identifier') == $id) {
  2176. return $item_temp;
  2177. }
  2178. }
  2179. $subchildren = $item_temp->childNodes;
  2180. if ($subchildren->length > 0) {
  2181. $val = $this->get_scorm_xml_node($subchildren, $id);
  2182. if (is_object($val)) {
  2183. return $val;
  2184. }
  2185. }
  2186. }
  2187. return false;
  2188. }
  2189. /**
  2190. * Returns a usable array of stats related to the current learnpath and user
  2191. * @return array Well-formatted array containing status for the current learnpath
  2192. */
  2193. public function get_stats() {
  2194. if ($this->debug > 0) {
  2195. error_log('New LP - In learnpath::get_stats()', 0);
  2196. }
  2197. // TODO
  2198. }
  2199. /**
  2200. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2201. * the given course (for all learnpaths and all users)
  2202. * @param string Course code
  2203. * @return array Well-formatted array containing status for the course's learnpaths
  2204. */
  2205. public function get_stats_course($course) {
  2206. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2207. // TODO
  2208. }
  2209. /**
  2210. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2211. * the given course and learnpath (for all users)
  2212. * @param string Course code
  2213. * @param integer Learnpath ID
  2214. * @return array Well-formatted array containing status for the specified learnpath
  2215. */
  2216. public function get_stats_lp($course, $lp) {
  2217. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2218. // TODO
  2219. }
  2220. /**
  2221. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2222. * the given course, learnpath and user.
  2223. * @param string Course code
  2224. * @param integer Learnpath ID
  2225. * @param integer User ID
  2226. * @return array Well-formatted array containing status for the specified learnpath and user
  2227. */
  2228. public function get_stats_lp_user($course, $lp, $user) {
  2229. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2230. // TODO
  2231. }
  2232. /**
  2233. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2234. * the given course and learnpath (for all users)
  2235. * @param string Course code
  2236. * @param integer User ID
  2237. * @return array Well-formatted array containing status for the user's learnpaths
  2238. */
  2239. public function get_stats_user($course, $user) {
  2240. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2241. // TODO
  2242. }
  2243. /**
  2244. * Gets the status list for all LP's items
  2245. * @return array Array of [index] => [item ID => current status]
  2246. */
  2247. public function get_items_status_list() {
  2248. if ($this->debug > 0) {
  2249. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2250. }
  2251. $list = array ();
  2252. foreach ($this->ordered_items as $item_id) {
  2253. $list[] = array (
  2254. $item_id => $this->items[$item_id]->get_status()
  2255. );
  2256. }
  2257. return $list;
  2258. }
  2259. /**
  2260. * Return the number of interactions for the given learnpath Item View ID.
  2261. * This method can be used as static.
  2262. * @param integer Item View ID
  2263. * @param integer course id
  2264. * @return integer Number of interactions
  2265. */
  2266. public static function get_interactions_count_from_db($lp_iv_id, $course_id) {
  2267. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2268. $lp_iv_id = intval($lp_iv_id);
  2269. $course_id = intval($course_id);
  2270. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2271. $res = Database::query($sql);
  2272. $res = 0;
  2273. if (Database::num_rows($res)) {
  2274. $row = Database::fetch_array($res);
  2275. $num = $row[0];
  2276. }
  2277. return $num;
  2278. }
  2279. /**
  2280. * Return the interactions as an array for the given lp_iv_id.
  2281. * This method can be used as static.
  2282. * @param integer Learnpath Item View ID
  2283. * @return array
  2284. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2285. */
  2286. public static function get_iv_interactions_array($lp_iv_id = 0) {
  2287. $course_id = api_get_course_int_id();
  2288. $list = array ();
  2289. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2290. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2291. $res = Database::query($sql);
  2292. $num = Database :: num_rows($res);
  2293. if ($num > 0) {
  2294. $list[] = array (
  2295. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2296. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2297. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2298. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2299. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2300. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2301. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2302. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2303. );
  2304. while ($row = Database :: fetch_array($res)) {
  2305. $list[] = array (
  2306. 'order_id' => ($row['order_id'] + 1),
  2307. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2308. 'type' => $row['interaction_type'],
  2309. 'time' => $row['completion_time'],
  2310. //'correct_responses' => $row['correct_responses'],
  2311. 'correct_responses' => '', // Hide correct responses from students.
  2312. 'student_response' => $row['student_response'],
  2313. 'result' => $row['result'],
  2314. 'latency' => $row['latency']
  2315. );
  2316. }
  2317. }
  2318. return $list;
  2319. }
  2320. /**
  2321. * Return the number of objectives for the given learnpath Item View ID.
  2322. * This method can be used as static.
  2323. * @param integer Item View ID
  2324. * @return integer Number of objectives
  2325. */
  2326. public static function get_objectives_count_from_db($lp_iv_id, $course_id) {
  2327. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2328. $course_id = intval($course_id);
  2329. $lp_iv_id = intval($lp_iv_id);
  2330. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2331. //@todo seems that this always returns 0
  2332. $res = Database::query($sql);
  2333. $res = 0;
  2334. if (Database::num_rows($res)) {
  2335. $row = Database :: fetch_array($res);
  2336. $num = $row[0];
  2337. }
  2338. return $num;
  2339. }
  2340. /**
  2341. * Return the objectives as an array for the given lp_iv_id.
  2342. * This method can be used as static.
  2343. * @param integer Learnpath Item View ID
  2344. * @return array
  2345. * @todo Translate labels
  2346. */
  2347. public function get_iv_objectives_array($lp_iv_id = 0) {
  2348. $course_id = api_get_course_int_id();
  2349. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2350. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2351. $res = Database::query($sql);
  2352. $num = Database :: num_rows($res);
  2353. $list = array();
  2354. if ($num > 0) {
  2355. $list[] = array (
  2356. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2357. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2358. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2359. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2360. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2361. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2362. );
  2363. while ($row = Database :: fetch_array($res)) {
  2364. $list[] = array (
  2365. 'order_id' => ($row['order_id'] + 1),
  2366. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2367. 'score_raw' => $row['score_raw'],
  2368. 'score_max' => $row['score_max'],
  2369. 'score_min' => $row['score_min'],
  2370. 'status' => $row['status']
  2371. );
  2372. }
  2373. }
  2374. return $list;
  2375. }
  2376. /**
  2377. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2378. * used by get_html_toc() to be ready to display
  2379. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2380. */
  2381. public function get_toc() {
  2382. if ($this->debug > 0) {
  2383. error_log('New LP - In learnpath::get_toc()', 0);
  2384. }
  2385. $toc = array();
  2386. //echo "<pre>".print_r($this->items,true)."</pre>";
  2387. foreach ($this->ordered_items as $item_id) {
  2388. if ($this->debug > 2) {
  2389. error_log('New LP - learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2390. }
  2391. // TODO: Change this link generation and use new function instead.
  2392. $toc[] = array (
  2393. 'id' => $item_id,
  2394. 'title' => $this->items[$item_id]->get_title(),
  2395. //'link' => get_addedresource_link_in_learnpath('document', $item_id, 1),
  2396. 'status' => $this->items[$item_id]->get_status(),
  2397. 'level' => $this->items[$item_id]->get_level(),
  2398. 'type' => $this->items[$item_id]->get_type(),
  2399. 'description' => $this->items[$item_id]->get_description(),
  2400. 'path' => $this->items[$item_id]->get_path(),
  2401. );
  2402. }
  2403. if ($this->debug > 2) {
  2404. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2405. }
  2406. return $toc;
  2407. }
  2408. /**
  2409. * Generate and return the table of contents for this learnpath. The JS
  2410. * table returned is used inside of scorm_api.php
  2411. * @return string A JS array vairiable construction
  2412. */
  2413. public function get_items_details_as_js($varname = 'olms.lms_item_types') {
  2414. if ($this->debug > 0) {
  2415. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2416. }
  2417. $toc = $varname.' = new Array();';
  2418. //echo "<pre>".print_r($this->items,true)."</pre>";
  2419. foreach ($this->ordered_items as $item_id) {
  2420. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2421. }
  2422. if ($this->debug > 2) {
  2423. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2424. }
  2425. return $toc;
  2426. }
  2427. /**
  2428. * Gets the learning path type
  2429. * @param boolean Return the name? If false, return the ID. Default is false.
  2430. * @return mixed Type ID or name, depending on the parameter
  2431. */
  2432. public function get_type($get_name = false) {
  2433. $res = false;
  2434. if ($this->debug > 0) {
  2435. error_log('New LP - In learnpath::get_type()', 0);
  2436. }
  2437. if (!empty ($this->type)) {
  2438. if ($get_name) {
  2439. // Get it from the lp_type table in main db.
  2440. } else {
  2441. $res = $this->type;
  2442. }
  2443. }
  2444. if ($this->debug > 2) {
  2445. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2446. }
  2447. return $res;
  2448. }
  2449. /**
  2450. * Gets the learning path type as static method
  2451. * @param boolean Return the name? If false, return the ID. Default is false.
  2452. * @return mixed Type ID or name, depending on the parameter
  2453. */
  2454. public static function get_type_static($lp_id = 0) {
  2455. $course_id = api_get_course_int_id();
  2456. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2457. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2458. $res = Database::query($sql);
  2459. if ($res === false) {
  2460. return null;
  2461. }
  2462. if (Database :: num_rows($res) <= 0) {
  2463. return null;
  2464. }
  2465. $row = Database :: fetch_array($res);
  2466. return $row['lp_type'];
  2467. }
  2468. /**
  2469. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2470. * This method can be used as abstract and is recursive
  2471. * @param integer Learnpath ID
  2472. * @param integer Parent ID of the items to look for
  2473. * @return mixed Ordered list of item IDs or false on error
  2474. */
  2475. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null) {
  2476. if (empty($course_id)) {
  2477. $course_id = api_get_course_int_id();
  2478. } else {
  2479. $course_id = intval($course_id);
  2480. }
  2481. $list = array();
  2482. if (empty ($lp)) {
  2483. return false;
  2484. }
  2485. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2486. $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";
  2487. $res = Database::query($sql);
  2488. while ($row = Database :: fetch_array($res)) {
  2489. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2490. $list[] = $row['id'];
  2491. foreach ($sublist as $item) {
  2492. $list[] = $item;
  2493. }
  2494. }
  2495. return $list;
  2496. }
  2497. /**
  2498. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2499. * @return string HTML TOC ready to display
  2500. */
  2501. public function get_html_toc($toc_list = null) {
  2502. global $_configuration;
  2503. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2504. if ($this->debug > 0) {
  2505. error_log('New LP - In learnpath::get_html_toc()', 0);
  2506. }
  2507. if (empty($toc_list)) {
  2508. $toc_list = $this->get_toc();
  2509. }
  2510. $html = '<div id="scorm_title" class="scorm_title">' . Security::remove_XSS($this->get_name()) . '</div>';
  2511. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : false;
  2512. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2513. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2514. if ($this->get_lp_session_id() == api_get_session_id()) {
  2515. $html .= '<div id="actions_lp" class="actions_lp">';
  2516. $html .= '<div class="btn-group">';
  2517. $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>";
  2518. $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>";
  2519. $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>';
  2520. $html .= '</div>';
  2521. $html .= '</div>';
  2522. }
  2523. }
  2524. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2525. require_once 'resourcelinker.inc.php';
  2526. // Temporary variables.
  2527. $mycurrentitemid = $this->get_current_item_id();
  2528. $color_counter = 0;
  2529. $i = 0;
  2530. foreach ($toc_list as $item) {
  2531. if ($this->debug > 2) {
  2532. //error_log('New LP - learnpath::get_html_toc(): using item ' . $item['id'], 0);
  2533. }
  2534. // TODO: Complete this.
  2535. $icon_name = array (
  2536. 'not attempted' => '../img/notattempted.gif',
  2537. 'incomplete' => '../img/incomplete.png',
  2538. 'failed' => '../img/delete.png',
  2539. 'completed' => '../img/completed.png',
  2540. 'passed' => '../img/passed.png',
  2541. 'succeeded' => '../img/succeeded.png',
  2542. 'browsed' => '../img/completed.png',
  2543. );
  2544. $style = 'scorm_item';
  2545. $scorm_color_background = 'scorm_item';
  2546. $style_item = 'scorm_item';
  2547. $current = false;
  2548. if ($item['id'] == $this->current) {
  2549. $style = 'scorm_item_highlight';
  2550. $scorm_color_background = 'scorm_item_highlight';
  2551. } else {
  2552. if ($color_counter % 2 == 0) {
  2553. $scorm_color_background = 'scorm_item_1';
  2554. } else {
  2555. $scorm_color_background = 'scorm_item_2';
  2556. }
  2557. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2558. $scorm_color_background =' scorm_item_section ';
  2559. }
  2560. }
  2561. if ($scorm_color_background != '') {
  2562. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . '">';
  2563. }
  2564. // The anchor will let us center the TOC on the currently viewed item &^D
  2565. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2566. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 1.5) . 'em; padding-right:' . ($item['level'] / 2) . 'em" title="' . $item['description'] . '" >';
  2567. $html .= '<a name="atoc_' . $item['id'] . '" />';
  2568. } else {
  2569. $html .= '<div class="' . $style_item . '" style="padding-left: ' . ($item['level'] * 2) . 'em; padding-right:' . ($item['level'] * 1.5) . 'em" title="' . $item['description'] . '" >';
  2570. }
  2571. $title = $item['title'];
  2572. if (empty ($title)) {
  2573. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2574. }
  2575. $title = Security::remove_XSS($title);
  2576. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2577. //$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>" ;
  2578. $url = $this->get_link('http', $item['id'], $toc_list);
  2579. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2580. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2581. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2582. $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2583. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2584. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;" . stripslashes($title);
  2585. } elseif ($item['type'] == 'dir') {
  2586. $html .= stripslashes($title);
  2587. }
  2588. /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2589. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2590. $user_id = api_get_user_id();
  2591. $sql = "SELECT path FROM $tbl_track_e_exercises, $tbl_lp_item
  2592. WHERE c_id = $course_id AND
  2593. path = '" . $item['path'] . "' AND
  2594. exe_user_id = '$user_id' AND
  2595. exe_cours_id = '$course_code' AND
  2596. path = exe_exo_id AND
  2597. status <> 'incomplete'";
  2598. $result = Database::query($sql);
  2599. $count = Database :: num_rows($result);*/
  2600. if ($item['type'] == 'quiz') {
  2601. if ($item['status'] == 'completed') {
  2602. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2603. }
  2604. } else {
  2605. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2606. $html .= "&nbsp;<img id='toc_img_" . $item['id'] . "' src='" . $icon_name[$item['status']] . "' alt='" . substr($item['status'], 0, 1) . "' width='14' />";
  2607. }
  2608. }
  2609. $html .= "</div>";
  2610. if ($scorm_color_background != '') {
  2611. $html .= '</div>';
  2612. }
  2613. $color_counter++;
  2614. }
  2615. $html .= "</div>";
  2616. return $html;
  2617. }
  2618. /**
  2619. * Gets the learnpath maker name - generally the editor's name
  2620. * @return string Learnpath maker name
  2621. */
  2622. public function get_maker() {
  2623. if ($this->debug > 0) {
  2624. error_log('New LP - In learnpath::get_maker()', 0);
  2625. }
  2626. if (!empty ($this->maker)) {
  2627. return $this->maker;
  2628. } else {
  2629. return '';
  2630. }
  2631. }
  2632. /**
  2633. * Gets the user-friendly message stored in $this->message
  2634. * @return string Message
  2635. */
  2636. public function get_message() {
  2637. if ($this->debug > 0) {
  2638. error_log('New LP - In learnpath::get_message()', 0);
  2639. }
  2640. return $this->message;
  2641. }
  2642. /**
  2643. * Gets the learnpath name/title
  2644. * @return string Learnpath name/title
  2645. */
  2646. public function get_name() {
  2647. if ($this->debug > 0) {
  2648. error_log('New LP - In learnpath::get_name()', 0);
  2649. }
  2650. if (!empty ($this->name)) {
  2651. return $this->name;
  2652. } else {
  2653. return 'N/A';
  2654. }
  2655. }
  2656. /**
  2657. * Gets a link to the resource from the present location, depending on item ID.
  2658. * @param string Type of link expected
  2659. * @param integer Learnpath item ID
  2660. * @return string Link to the lp_item resource
  2661. */
  2662. public function get_link($type = 'http', $item_id = null, $provided_toc = false) {
  2663. $course_id = $this->get_course_int_id();
  2664. if ($this->debug > 0) {
  2665. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  2666. }
  2667. if (empty($item_id)) {
  2668. if ($this->debug > 2) {
  2669. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  2670. }
  2671. $item_id = $this->get_current_item_id();
  2672. }
  2673. if (empty($item_id)) {
  2674. if ($this->debug > 2) {
  2675. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  2676. }
  2677. //still empty, this means there was no item_id given and we are not in an object context or
  2678. //the object property is empty, return empty link
  2679. $item_id = $this->first();
  2680. return '';
  2681. }
  2682. $file = '';
  2683. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  2684. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  2685. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2686. $item_id = Database::escape_string($item_id);
  2687. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  2688. FROM $lp_table l
  2689. INNER JOIN $lp_item_table li
  2690. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  2691. WHERE li.id = $item_id ";
  2692. if ($this->debug > 2) {
  2693. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  2694. }
  2695. $res = Database::query($sql);
  2696. if (Database :: num_rows($res) > 0) {
  2697. $row = Database :: fetch_array($res);
  2698. $lp_type = $row['ltype'];
  2699. $lp_path = $row['lpath'];
  2700. $lp_item_type = $row['litype'];
  2701. $lp_item_path = $row['lipath'];
  2702. $lp_item_params = $row['liparams'];
  2703. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  2704. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  2705. }
  2706. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  2707. if ($type == 'http') {
  2708. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  2709. } else {
  2710. $course_path = $sys_course_path; //system path
  2711. }
  2712. // 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.
  2713. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  2714. $lp_type = 1;
  2715. }
  2716. if ($this->debug > 2) {
  2717. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  2718. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  2719. }
  2720. // Now go through the specific cases to get the end of the path
  2721. // @todo Use constants instead of int values.
  2722. switch ($lp_type) {
  2723. case 1 :
  2724. if ($lp_item_type == 'dokeos_chapter') {
  2725. $file = 'lp_content.php?type=dir';
  2726. } else {
  2727. require_once 'resourcelinker.inc.php';
  2728. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  2729. if ($this->debug > 0) {
  2730. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  2731. }
  2732. if ($lp_item_type == 'link') {
  2733. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  2734. if (is_youtube_link($file)) {
  2735. $src = get_youtube_video_id($file);
  2736. $file = 'embed.php?type=youtube&src='.$src;
  2737. }
  2738. } else {
  2739. // check how much attempts of a exercise exits in lp
  2740. $lp_item_id = $this->get_current_item_id();
  2741. $lp_view_id = $this->get_view_id();
  2742. $prevent_reinit = null;
  2743. if (isset($this->items[$this->current])) {
  2744. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  2745. }
  2746. if (empty($provided_toc)) {
  2747. if ($this->debug > 0) {
  2748. error_log('In learnpath::get_link() Loading get_toc ', 0);
  2749. }
  2750. $list = $this->get_toc();
  2751. } else {
  2752. if ($this->debug > 0) {
  2753. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  2754. }
  2755. $list = $provided_toc;
  2756. }
  2757. $type_quiz = false;
  2758. foreach ($list as $toc) {
  2759. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  2760. $type_quiz = true;
  2761. }
  2762. }
  2763. if ($type_quiz) {
  2764. $lp_item_id = Database::escape_string($lp_item_id);
  2765. $lp_view_id = Database::escape_string($lp_view_id);
  2766. $sql = "SELECT count(*) FROM $lp_item_view_table
  2767. WHERE c_id = $course_id AND lp_item_id='" . (int) $lp_item_id . "' AND lp_view_id ='" . (int) $lp_view_id . "' AND status='completed'";
  2768. $result = Database::query($sql);
  2769. $row_count = Database :: fetch_row($result);
  2770. $count_item_view = (int) $row_count[0];
  2771. $not_multiple_attempt = 0;
  2772. if ($prevent_reinit === 1 && $count_item_view > 0) {
  2773. $not_multiple_attempt = 1;
  2774. }
  2775. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  2776. }
  2777. $tmp_array = explode('/', $file);
  2778. $document_name = $tmp_array[count($tmp_array) - 1];
  2779. if (strpos($document_name, '_DELETED_')) {
  2780. $file = 'blank.php?error=document_deleted';
  2781. }
  2782. }
  2783. }
  2784. break;
  2785. case 2 :
  2786. if ($this->debug > 2) {
  2787. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2788. }
  2789. if ($lp_item_type != 'dir') {
  2790. // Quite complex here:
  2791. // We want to make sure 'http://' (and similar) links can
  2792. // be loaded as is (withouth the Chamilo path in front) but
  2793. // some contents use this form: resource.htm?resource=http://blablabla
  2794. // which means we have to find a protocol at the path's start, otherwise
  2795. // it should not be considered as an external URL.
  2796. //if ($this->prerequisites_match($item_id)) {
  2797. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2798. if ($this->debug > 2) {
  2799. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2800. }
  2801. // Distant url, return as is.
  2802. $file = $lp_item_path;
  2803. } else {
  2804. if ($this->debug > 2) {
  2805. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2806. }
  2807. // Prevent getting untranslatable urls.
  2808. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2809. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2810. // Prepare the path.
  2811. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2812. // TODO: Fix this for urls with protocol header.
  2813. $file = str_replace('//', '/', $file);
  2814. $file = str_replace(':/', '://', $file);
  2815. if (substr($lp_path, -1) == '/') {
  2816. $lp_path = substr($lp_path, 0, -1);
  2817. }
  2818. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  2819. // if file not found.
  2820. $decoded = html_entity_decode($lp_item_path);
  2821. list ($decoded) = explode('?', $decoded);
  2822. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  2823. require_once 'resourcelinker.inc.php';
  2824. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  2825. if (empty($file)) {
  2826. $file = 'blank.php?error=document_not_found';
  2827. } else {
  2828. $tmp_array = explode('/', $file);
  2829. $document_name = $tmp_array[count($tmp_array) - 1];
  2830. if (strpos($document_name, '_DELETED_')) {
  2831. $file = 'blank.php?error=document_deleted';
  2832. } else {
  2833. $file = 'blank.php?error=document_not_found';
  2834. }
  2835. }
  2836. } else {
  2837. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  2838. }
  2839. }
  2840. }
  2841. //}else{
  2842. //prerequisites did not match
  2843. //$file = 'blank.php';
  2844. //}
  2845. // We want to use parameters if they were defined in the imsmanifest
  2846. if (strpos($file, 'blank.php') === false) {
  2847. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  2848. }
  2849. } else {
  2850. $file = 'lp_content.php?type=dir';
  2851. }
  2852. break;
  2853. case 3 :
  2854. if ($this->debug > 2) {
  2855. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  2856. }
  2857. // Formatting AICC HACP append URL.
  2858. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  2859. if ($lp_item_type != 'dir') {
  2860. // Quite complex here:
  2861. // We want to make sure 'http://' (and similar) links can
  2862. // be loaded as is (withouth the Chamilo path in front) but
  2863. // some contents use this form: resource.htm?resource=http://blablabla
  2864. // which means we have to find a protocol at the path's start, otherwise
  2865. // it should not be considered as an external URL.
  2866. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  2867. if ($this->debug > 2) {
  2868. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  2869. }
  2870. // Distant url, return as is.
  2871. $file = $lp_item_path;
  2872. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  2873. /*
  2874. if (stristr($file,'<servername>') !== false) {
  2875. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  2876. }
  2877. */
  2878. if (stripos($file, '<servername>') !== false) {
  2879. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  2880. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  2881. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  2882. }
  2883. //
  2884. $file .= $aicc_append;
  2885. } else {
  2886. if ($this->debug > 2) {
  2887. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  2888. }
  2889. // Prevent getting untranslatable urls.
  2890. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  2891. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  2892. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  2893. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  2894. // TODO: Fix this for urls with protocol header.
  2895. $file = str_replace('//', '/', $file);
  2896. $file = str_replace(':/', '://', $file);
  2897. $file .= $aicc_append;
  2898. }
  2899. } else {
  2900. $file = 'lp_content.php?type=dir';
  2901. }
  2902. break;
  2903. case 4 :
  2904. break;
  2905. default :
  2906. break;
  2907. }
  2908. }
  2909. if ($this->debug > 2) {
  2910. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  2911. }
  2912. return $file;
  2913. }
  2914. /**
  2915. * Gets the latest usable view or generate a new one
  2916. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  2917. * @return integer DB lp_view id
  2918. */
  2919. public function get_view($attempt_num = 0) {
  2920. if ($this->debug > 0) {
  2921. error_log('New LP - In learnpath::get_view()', 0);
  2922. }
  2923. $search = '';
  2924. // Use $attempt_num to enable multi-views management (disabled so far).
  2925. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  2926. $search = 'AND view_count = ' . $attempt_num;
  2927. }
  2928. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  2929. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  2930. $course_id = api_get_course_int_id();
  2931. $sql = "SELECT id, view_count FROM $lp_view_table
  2932. WHERE c_id = ".$course_id." AND lp_id = " . $this->get_id() ." AND user_id = " . $this->get_user_id() . " " .$search .
  2933. " ORDER BY view_count DESC";
  2934. $res = Database::query($sql);
  2935. if (Database :: num_rows($res) > 0) {
  2936. $row = Database :: fetch_array($res);
  2937. $this->lp_view_id = $row['id'];
  2938. } else {
  2939. // There is no database record, create one.
  2940. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  2941. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ",1)";
  2942. $res = Database::query($sql);
  2943. $id = Database :: insert_id();
  2944. $this->lp_view_id = $id;
  2945. }
  2946. return $this->lp_view_id;
  2947. }
  2948. /**
  2949. * Gets the current view id
  2950. * @return integer View ID (from lp_view)
  2951. */
  2952. public function get_view_id() {
  2953. if ($this->debug > 0) {
  2954. error_log('New LP - In learnpath::get_view_id()', 0);
  2955. }
  2956. if (!empty ($this->lp_view_id)) {
  2957. return $this->lp_view_id;
  2958. } else {
  2959. return 0;
  2960. }
  2961. }
  2962. /**
  2963. * Gets the update queue
  2964. * @return array Array containing IDs of items to be updated by JavaScript
  2965. */
  2966. public function get_update_queue() {
  2967. if ($this->debug > 0) {
  2968. error_log('New LP - In learnpath::get_update_queue()', 0);
  2969. }
  2970. return $this->update_queue;
  2971. }
  2972. /**
  2973. * Gets the user ID
  2974. * @return integer User ID
  2975. */
  2976. public function get_user_id() {
  2977. if ($this->debug > 0) {
  2978. error_log('New LP - In learnpath::get_user_id()', 0);
  2979. }
  2980. if (!empty ($this->user_id)) {
  2981. return $this->user_id;
  2982. } else {
  2983. return false;
  2984. }
  2985. }
  2986. /**
  2987. * Checks if any of the items has an audio element attached
  2988. * @return bool True or false
  2989. */
  2990. public function has_audio() {
  2991. if ($this->debug > 1) {
  2992. error_log('New LP - In learnpath::has_audio()', 0);
  2993. }
  2994. $has = false;
  2995. foreach ($this->items as $i => $item) {
  2996. if (!empty ($this->items[$i]->audio)) {
  2997. $has = true;
  2998. break;
  2999. }
  3000. }
  3001. return $has;
  3002. }
  3003. /**
  3004. * Logs a message into a file
  3005. * @param string Message to log
  3006. * @return boolean True on success, false on error or if msg empty
  3007. */
  3008. public function log($msg) {
  3009. if ($this->debug > 0) {
  3010. error_log('New LP - In learnpath::log()', 0);
  3011. }
  3012. // TODO
  3013. $this->error .= $msg;
  3014. return true;
  3015. }
  3016. /**
  3017. * Moves an item up and down at its level
  3018. * @param integer Item to move up and down
  3019. * @param string Direction 'up' or 'down'
  3020. * @return integer New display order, or false on error
  3021. */
  3022. public function move_item($id, $direction) {
  3023. $course_id = api_get_course_int_id();
  3024. if ($this->debug > 0) {
  3025. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3026. }
  3027. if (empty ($id) or empty ($direction)) {
  3028. return false;
  3029. }
  3030. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3031. $sql_sel = "SELECT *
  3032. FROM " . $tbl_lp_item . "
  3033. WHERE c_id = ".$course_id." AND id = " . $id;
  3034. $res_sel = Database::query($sql_sel);
  3035. // Check if elem exists.
  3036. if (Database :: num_rows($res_sel) < 1) {
  3037. return false;
  3038. }
  3039. // Gather data.
  3040. $row = Database :: fetch_array($res_sel);
  3041. $previous = $row['previous_item_id'];
  3042. $next = $row['next_item_id'];
  3043. $display = $row['display_order'];
  3044. $parent = $row['parent_item_id'];
  3045. $lp = $row['lp_id'];
  3046. // Update the item (switch with previous/next one).
  3047. switch ($direction) {
  3048. case 'up' :
  3049. if ($this->debug > 2) {
  3050. error_log('Movement up detected', 0);
  3051. }
  3052. if ($display <= 1) { /*do nothing*/
  3053. } else {
  3054. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3055. WHERE c_id = ".$course_id." AND id = $previous";
  3056. if ($this->debug > 2) {
  3057. error_log('Selecting previous: ' . $sql_sel2, 0);
  3058. }
  3059. $res_sel2 = Database::query($sql_sel2);
  3060. if (Database :: num_rows($res_sel2) < 1) {
  3061. $previous_previous = 0;
  3062. }
  3063. // Gather data.
  3064. $row2 = Database :: fetch_array($res_sel2);
  3065. $previous_previous = $row2['previous_item_id'];
  3066. // Update previous_previous item (switch "next" with current).
  3067. if ($previous_previous != 0) {
  3068. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3069. if ($this->debug > 2) {
  3070. error_log($sql_upd2, 0);
  3071. }
  3072. $res_upd2 = Database::query($sql_upd2);
  3073. }
  3074. // Update previous item (switch with current).
  3075. if ($previous != 0) {
  3076. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3077. WHERE c_id = ".$course_id." AND id = $previous";
  3078. if ($this->debug > 2) {
  3079. error_log($sql_upd2, 0);
  3080. }
  3081. $res_upd2 = Database::query($sql_upd2);
  3082. }
  3083. // Update current item (switch with previous).
  3084. if ($id != 0) {
  3085. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3086. WHERE c_id = ".$course_id." AND id = $id";
  3087. if ($this->debug > 2) {
  3088. error_log($sql_upd2, 0);
  3089. }
  3090. $res_upd2 = Database::query($sql_upd2);
  3091. }
  3092. // Update next item (new previous item).
  3093. if ($next != 0) {
  3094. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3095. WHERE c_id = ".$course_id." AND id = $next";
  3096. if ($this->debug > 2) {
  3097. error_log($sql_upd2, 0);
  3098. }
  3099. $res_upd2 = Database::query($sql_upd2);
  3100. }
  3101. $display = $display -1;
  3102. }
  3103. break;
  3104. case 'down' :
  3105. if ($this->debug > 2) {
  3106. error_log('Movement down detected', 0);
  3107. }
  3108. if ($next == 0) { /* Do nothing. */
  3109. } else {
  3110. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3111. if ($this->debug > 2) {
  3112. error_log('Selecting next: ' . $sql_sel2, 0);
  3113. }
  3114. $res_sel2 = Database::query($sql_sel2);
  3115. if (Database :: num_rows($res_sel2) < 1) {
  3116. $next_next = 0;
  3117. }
  3118. // Gather data.
  3119. $row2 = Database :: fetch_array($res_sel2);
  3120. $next_next = $row2['next_item_id'];
  3121. // Update previous item (switch with current).
  3122. if ($previous != 0) {
  3123. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3124. WHERE c_id = ".$course_id." AND id = $previous";
  3125. $res_upd2 = Database::query($sql_upd2);
  3126. }
  3127. // Update current item (switch with previous).
  3128. if ($id != 0) {
  3129. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3130. WHERE c_id = ".$course_id." AND id = $id";
  3131. $res_upd2 = Database::query($sql_upd2);
  3132. }
  3133. // Update next item (new previous item).
  3134. if ($next != 0) {
  3135. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3136. WHERE c_id = ".$course_id." AND id = $next";
  3137. $res_upd2 = Database::query($sql_upd2);
  3138. }
  3139. // Update next_next item (switch "previous" with current).
  3140. if ($next_next != 0) {
  3141. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3142. WHERE c_id = ".$course_id." AND id = $next_next";
  3143. $res_upd2 = Database::query($sql_upd2);
  3144. }
  3145. $display = $display +1;
  3146. }
  3147. break;
  3148. default :
  3149. return false;
  3150. }
  3151. return $display;
  3152. }
  3153. /**
  3154. * Move a learnpath up (display_order)
  3155. * @param integer Learnpath ID
  3156. */
  3157. public function move_up($lp_id) {
  3158. $course_id = api_get_course_int_id();
  3159. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3160. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3161. $res = Database::query($sql);
  3162. if ($res === false)
  3163. return false;
  3164. $lps = array ();
  3165. $lp_order = array ();
  3166. $num = Database :: num_rows($res);
  3167. // First check the order is correct, globally (might be wrong because
  3168. // of versions < 1.8.4)
  3169. if ($num > 0) {
  3170. $i = 1;
  3171. while ($row = Database :: fetch_array($res)) {
  3172. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3173. $need_fix = true;
  3174. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3175. $res_u = Database::query($sql_u);
  3176. }
  3177. $row['display_order'] = $i;
  3178. $lps[$row['id']] = $row;
  3179. $lp_order[$i] = $row['id'];
  3180. $i++;
  3181. }
  3182. }
  3183. if ($num > 1) { // If there's only one element, no need to sort.
  3184. $order = $lps[$lp_id]['display_order'];
  3185. if ($order > 1) { // If it's the first element, no need to move up.
  3186. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3187. $res_u1 = Database::query($sql_u1);
  3188. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . " WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3189. $res_u2 = Database::query($sql_u2);
  3190. }
  3191. }
  3192. }
  3193. /**
  3194. * Move a learnpath down (display_order)
  3195. * @param integer Learnpath ID
  3196. */
  3197. public function move_down($lp_id) {
  3198. $course_id = api_get_course_int_id();
  3199. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3200. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3201. $res = Database::query($sql);
  3202. if ($res === false)
  3203. return false;
  3204. $lps = array ();
  3205. $lp_order = array ();
  3206. $num = Database :: num_rows($res);
  3207. $max = 0;
  3208. // First check the order is correct, globally (might be wrong because
  3209. // of versions < 1.8.4).
  3210. if ($num > 0) {
  3211. $i = 1;
  3212. while ($row = Database :: fetch_array($res)) {
  3213. $max = $i;
  3214. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3215. $need_fix = true;
  3216. $sql_u = "UPDATE $lp_table SET display_order = $i
  3217. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3218. $res_u = Database::query($sql_u);
  3219. }
  3220. $row['display_order'] = $i;
  3221. $lps[$row['id']] = $row;
  3222. $lp_order[$i] = $row['id'];
  3223. $i++;
  3224. }
  3225. }
  3226. if ($num > 1) { // If there's only one element, no need to sort.
  3227. $order = $lps[$lp_id]['display_order'];
  3228. if ($order < $max) { // If it's the first element, no need to move up.
  3229. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3230. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3231. $res_u1 = Database::query($sql_u1);
  3232. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3233. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3234. $res_u2 = Database::query($sql_u2);
  3235. }
  3236. }
  3237. }
  3238. /**
  3239. * Updates learnpath attributes to point to the next element
  3240. * The last part is similar to set_current_item but processing the other way around
  3241. */
  3242. public function next() {
  3243. if ($this->debug > 0) {
  3244. error_log('New LP - In learnpath::next()', 0);
  3245. }
  3246. $this->last = $this->get_current_item_id();
  3247. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3248. $this->autocomplete_parents($this->last);
  3249. $new_index = $this->get_next_index();
  3250. if ($this->debug > 2) {
  3251. error_log('New LP - New index: ' . $new_index, 0);
  3252. }
  3253. $this->index = $new_index;
  3254. if ($this->debug > 2) {
  3255. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3256. }
  3257. $this->current = $this->ordered_items[$new_index];
  3258. if ($this->debug > 2) {
  3259. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3260. }
  3261. }
  3262. /**
  3263. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3264. * class, this might be redefined to allow several behaviours depending on the document type.
  3265. * @param integer Resource ID
  3266. * @return boolean True on success, false otherwise
  3267. */
  3268. public function open($id) {
  3269. if ($this->debug > 0) {
  3270. error_log('New LP - In learnpath::open()', 0);
  3271. }
  3272. // TODO:
  3273. // set the current resource attribute to this resource
  3274. // switch on element type (redefine in child class?)
  3275. // set status for this item to "opened"
  3276. // start timer
  3277. // initialise score
  3278. $this->index = 0; //or = the last item seen (see $this->last)
  3279. }
  3280. /**
  3281. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3282. * and the prerequisite string on error.
  3283. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3284. * @param integer Optional item ID. If none given, uses the current open item.
  3285. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3286. */
  3287. public function prerequisites_match($item = null) {
  3288. if ($this->debug > 0) {
  3289. error_log('New LP - In learnpath::prerequisites_match()', 0);
  3290. }
  3291. if (empty ($item)) {
  3292. $item = $this->current;
  3293. }
  3294. if (is_object($this->items[$item])) {
  3295. $prereq_string = $this->items[$item]->get_prereq_string();
  3296. if (empty($prereq_string)) {
  3297. return true;
  3298. }
  3299. // Clean spaces.
  3300. $prereq_string = str_replace(' ', '', $prereq_string);
  3301. if ($this->debug > 0) {
  3302. error_log('Found prereq_string: ' . $prereq_string, 0);
  3303. }
  3304. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3305. $result = $this->items[$item]->parse_prereq($prereq_string, $this->items, $this->refs_list, $this->get_user_id());
  3306. if ($result === false) {
  3307. $this->set_error_msg($this->items[$item]->prereq_alert);
  3308. }
  3309. } else {
  3310. $result = true;
  3311. if ($this->debug > 1) {
  3312. error_log('New LP - $this->items[' . $item . '] was not an object', 0);
  3313. }
  3314. }
  3315. if ($this->debug > 1) {
  3316. error_log('New LP - End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3317. }
  3318. return $result;
  3319. }
  3320. /**
  3321. * Updates learnpath attributes to point to the previous element
  3322. * The last part is similar to set_current_item but processing the other way around
  3323. */
  3324. public function previous() {
  3325. if ($this->debug > 0) {
  3326. error_log('New LP - In learnpath::previous()', 0);
  3327. }
  3328. $this->last = $this->get_current_item_id();
  3329. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3330. $this->autocomplete_parents($this->last);
  3331. $new_index = $this->get_previous_index();
  3332. $this->index = $new_index;
  3333. $this->current = $this->ordered_items[$new_index];
  3334. }
  3335. /**
  3336. * Publishes a learnpath. This basically means show or hide the learnpath
  3337. * to normal users.
  3338. * Can be used as abstract
  3339. * @param integer Learnpath ID
  3340. * @param string New visibility
  3341. */
  3342. public function toggle_visibility($lp_id, $set_visibility = 1) {
  3343. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3344. $action = 'visible';
  3345. if ($set_visibility != 1) {
  3346. $action = 'invisible';
  3347. }
  3348. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3349. }
  3350. /**
  3351. * Publishes a learnpath. This basically means show or hide the learnpath
  3352. * on the course homepage
  3353. * Can be used as abstract
  3354. * @param integer Learnpath id
  3355. * @param string New visibility (v/s - visible/invisible)
  3356. */
  3357. public static function toggle_publish($lp_id, $set_visibility = 'v') {
  3358. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_publish()', 0); }
  3359. $course_id = api_get_course_int_id();
  3360. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3361. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3362. $result = Database::query($sql);
  3363. $row = Database :: fetch_array($result);
  3364. $name = domesticate($row['name']);
  3365. if ($set_visibility == 'i') {
  3366. $s = $name . " " . get_lang('LearnpathNotPublished');
  3367. $dialogBox = $s;
  3368. $v = 0;
  3369. }
  3370. if ($set_visibility == 'v') {
  3371. $s = $name . " " . get_lang('LearnpathPublished');
  3372. $dialogBox = $s;
  3373. $v = 1;
  3374. }
  3375. $session_id = api_get_session_id();
  3376. $session_condition = api_get_session_condition($session_id);
  3377. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3378. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  3379. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3380. $result = Database::query($sql);
  3381. $num = Database :: num_rows($result);
  3382. //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
  3383. if (($set_visibility == 'i') && ($num > 0)) {
  3384. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3385. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3386. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3387. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3388. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3389. $sql = "UPDATE $tbl_tool SET c_id = $course_id, name = '$name', link = '$link', image = 'scormbuilder.gif', visibility = '$v', admin = '0', address = 'pastillegris.gif', added_tool = 0, session_id = $session_id
  3390. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3391. } else {
  3392. // Parameter and database incompatible, do nothing, exit.
  3393. return false;
  3394. }
  3395. $result = Database::query($sql);
  3396. //if ($this->debug > 2) { error_log('New LP - Leaving learnpath::toggle_visibility: '.$sql, 0); }
  3397. }
  3398. /**
  3399. * Restart the whole learnpath. Return the URL of the first element.
  3400. * Make sure the results are saved with anoter method. This method should probably be
  3401. * redefined in children classes.
  3402. * To use a similar method statically, use the create_new_attempt() method
  3403. * @return string URL to load in the viewer
  3404. */
  3405. public function restart() {
  3406. if ($this->debug > 0) {
  3407. error_log('New LP - In learnpath::restart()', 0);
  3408. }
  3409. // TODO
  3410. // Call autosave method to save the current progress.
  3411. //$this->index = 0;
  3412. $session_id = api_get_session_id();
  3413. $course_id = api_get_course_int_id();
  3414. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3415. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
  3416. "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3417. if ($this->debug > 2) {
  3418. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3419. }
  3420. $res = Database::query($sql);
  3421. if ($view_id = Database :: insert_id($res)) {
  3422. $this->lp_view_id = $view_id;
  3423. $this->attempt = $this->attempt + 1;
  3424. } else {
  3425. $this->error = 'Could not insert into item_view table...';
  3426. return false;
  3427. }
  3428. $this->autocomplete_parents($this->current);
  3429. foreach ($this->items as $index => $dummy) {
  3430. $this->items[$index]->restart();
  3431. $this->items[$index]->set_lp_view($this->lp_view_id);
  3432. }
  3433. $this->first();
  3434. return true;
  3435. }
  3436. /**
  3437. * Saves the current item
  3438. * @return boolean
  3439. */
  3440. public function save_current() {
  3441. if ($this->debug > 0) {
  3442. error_log('learnpath::save_current()', 0);
  3443. }
  3444. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3445. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3446. if ($this->debug > 2) {
  3447. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3448. }
  3449. if ($this->debug > 2) {
  3450. error_log('' . print_r($this->items, true), 0);
  3451. }
  3452. if (is_object($this->items[$this->current])) {
  3453. //$res = $this->items[$this->current]->save(false);
  3454. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3455. $this->autocomplete_parents($this->current);
  3456. $status = $this->items[$this->current]->get_status();
  3457. $this->append_message('new_item_status: ' . $status);
  3458. $this->update_queue[$this->current] = $status;
  3459. return $res;
  3460. }
  3461. return false;
  3462. }
  3463. /**
  3464. * Saves the given item
  3465. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3466. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3467. * @return boolean
  3468. */
  3469. public function save_item($item_id = null, $from_outside = true) {
  3470. $debug = $this->debug;
  3471. if ($debug) {
  3472. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3473. }
  3474. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3475. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3476. if (empty($item_id)) {
  3477. $item_id = intval($_REQUEST['id']);
  3478. }
  3479. if (empty($item_id)) {
  3480. $item_id = $this->get_current_item_id();
  3481. }
  3482. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3483. if ($debug) { error_log('Object exists'); }
  3484. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3485. $this->autocomplete_parents($item_id);
  3486. if ($debug) {
  3487. error_log('update_queue before:');
  3488. error_log(print_r($this->update_queue,1));
  3489. }
  3490. $status = $this->items[$item_id]->get_status();
  3491. $this->update_queue[$item_id] = $status;
  3492. if ($debug) {
  3493. error_log('get_status(): ' . $status);
  3494. error_log('update_queue after:');
  3495. error_log(print_r($this->update_queue,1));
  3496. }
  3497. return $res;
  3498. }
  3499. return false;
  3500. }
  3501. /**
  3502. * Saves the last item seen's ID only in case
  3503. */
  3504. public function save_last() {
  3505. $course_id = api_get_course_int_id();
  3506. if ($this->debug > 0) {
  3507. error_log('New LP - In learnpath::save_last()', 0);
  3508. }
  3509. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3510. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3511. if (isset($this->current)) {
  3512. if ($this->debug > 2) {
  3513. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3514. }
  3515. $sql = "UPDATE $table SET last_item = " . Database::escape_string($this->get_current_item_id()). "
  3516. WHERE c_id = $course_id AND
  3517. lp_id = " . $this->get_id() . " AND
  3518. user_id = " . $this->get_user_id()." ".$session_condition;
  3519. if ($this->debug > 2) {
  3520. error_log('New LP - Saving last item seen : ' . $sql, 0);
  3521. }
  3522. $res = Database::query($sql);
  3523. }
  3524. // Save progress.
  3525. list($progress, $text) = $this->get_progress_bar_text('%');
  3526. if ($progress >= 0 && $progress <= 100) {
  3527. $progress = (int) $progress;
  3528. $sql = "UPDATE $table SET progress = $progress
  3529. WHERE c_id = ".$course_id." AND
  3530. lp_id = " . $this->get_id() . " AND
  3531. user_id = " . $this->get_user_id()." ".$session_condition;
  3532. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3533. $this->progress_db = $progress;
  3534. }
  3535. }
  3536. /**
  3537. * Sets the current item ID (checks if valid and authorized first)
  3538. * @param integer New item ID. If not given or not authorized, defaults to current
  3539. */
  3540. public function set_current_item($item_id = null) {
  3541. if ($this->debug > 0) {
  3542. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  3543. }
  3544. if (empty ($item_id)) {
  3545. if ($this->debug > 2) {
  3546. error_log('New LP - No new current item given, ignore...', 0);
  3547. }
  3548. // Do nothing.
  3549. } else {
  3550. if ($this->debug > 2) {
  3551. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  3552. }
  3553. if (is_numeric($item_id)) {
  3554. $item_id = Database::escape_string($item_id);
  3555. // TODO: Check in database here.
  3556. $this->last = $this->current;
  3557. $this->current = $item_id;
  3558. // TODO: Update $this->index as well.
  3559. foreach ($this->ordered_items as $index => $item) {
  3560. if ($item == $this->current) {
  3561. $this->index = $index;
  3562. break;
  3563. }
  3564. }
  3565. if ($this->debug > 2) {
  3566. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  3567. }
  3568. } else {
  3569. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  3570. }
  3571. }
  3572. }
  3573. /**
  3574. * Sets the encoding
  3575. * @param string New encoding
  3576. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3577. */
  3578. public function set_encoding($enc = 'UTF-8') {
  3579. if ($this->debug > 0) {
  3580. error_log('New LP - In learnpath::set_encoding()', 0);
  3581. }
  3582. $course_id = api_get_course_int_id();
  3583. /* // Deprecated code (Chamilo 1.8.8).
  3584. $enc = strtoupper($enc);
  3585. $encodings = array (
  3586. 'UTF-8',
  3587. 'ISO-8859-1',
  3588. 'ISO-8859-15',
  3589. 'cp1251',
  3590. 'cp1252',
  3591. 'KOI8-R',
  3592. 'BIG5',
  3593. 'GB2312',
  3594. 'Shift_JIS',
  3595. 'EUC-JP',
  3596. ''
  3597. );
  3598. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  3599. $lp = $this->get_id();
  3600. if ($lp != 0) {
  3601. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3602. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  3603. $res = Database::query($sql);
  3604. return $res;
  3605. }
  3606. }
  3607. return false;
  3608. */
  3609. $enc = api_refine_encoding_id($enc);
  3610. if (empty($enc)) {
  3611. $enc = api_get_system_encoding();
  3612. }
  3613. if (api_is_encoding_supported($enc)) {
  3614. $lp = $this->get_id();
  3615. if ($lp != 0) {
  3616. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3617. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  3618. $res = Database::query($sql);
  3619. return $res;
  3620. }
  3621. }
  3622. return false;
  3623. }
  3624. /**
  3625. * Sets the JS lib setting in the database directly.
  3626. * This is the JavaScript library file this lp needs to load on startup
  3627. * @param string Proximity setting
  3628. * @return boolean True on update success. False otherwise.
  3629. */
  3630. public function set_jslib($lib = '') {
  3631. if ($this->debug > 0) {
  3632. error_log('New LP - In learnpath::set_jslib()', 0);
  3633. }
  3634. $lp = $this->get_id();
  3635. $course_id = api_get_course_int_id();
  3636. if ($lp != 0) {
  3637. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3638. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  3639. $res = Database::query($sql);
  3640. return $res;
  3641. } else {
  3642. return false;
  3643. }
  3644. }
  3645. /**
  3646. * Sets the name of the LP maker (publisher) (and save)
  3647. * @param string Optional string giving the new content_maker of this learnpath
  3648. * @return boolean True
  3649. */
  3650. public function set_maker($name = '') {
  3651. if ($this->debug > 0) {
  3652. error_log('New LP - In learnpath::set_maker()', 0);
  3653. }
  3654. if (empty ($name))
  3655. return false;
  3656. $this->maker = Database::escape_string($name);
  3657. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3658. $course_id = api_get_course_int_id();
  3659. $lp_id = $this->get_id();
  3660. $sql = "UPDATE $lp_table SET content_maker = '" . $this->maker . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3661. if ($this->debug > 2) {
  3662. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  3663. }
  3664. Database::query($sql);
  3665. return true;
  3666. }
  3667. /**
  3668. * Sets the name of the current learnpath (and save)
  3669. * @param string Optional string giving the new name of this learnpath
  3670. * @return boolean True/False
  3671. */
  3672. public function set_name($name = null) {
  3673. if ($this->debug > 0) {
  3674. error_log('New LP - In learnpath::set_name()', 0);
  3675. }
  3676. if (empty($name)) {
  3677. return false;
  3678. }
  3679. $this->name = Database::escape_string($name);
  3680. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3681. $lp_id = $this->get_id();
  3682. $course_id = api_get_course_int_id();
  3683. $sql = "UPDATE $lp_table SET name = '" . $this->name . "' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  3684. if ($this->debug > 2) {
  3685. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  3686. }
  3687. Database::query($sql);
  3688. // If the lp is visible on the homepage, change his name there.
  3689. if (Database::affected_rows()) {
  3690. $session_id = api_get_session_id();
  3691. $session_condition = api_get_session_condition($session_id);
  3692. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3693. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  3694. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  3695. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3696. Database::query($sql);
  3697. return true;
  3698. } else {
  3699. return false;
  3700. }
  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. //Link for the documents
  4645. if ($arrLP[$i]['item_type'] == 'document') {
  4646. $url = api_get_self() . '?'.api_get_cidreq().'&amp;action=view_item&amp;mode=preview_document&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id;
  4647. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  4648. }
  4649. if (($i % 2) == 0) {
  4650. $oddclass = 'row_odd';
  4651. } else {
  4652. $oddclass = 'row_even';
  4653. }
  4654. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddclass . '">';
  4655. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4656. $icon = '';
  4657. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4658. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  4659. } else {
  4660. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4661. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  4662. } else {
  4663. $icon = '<img src="../img/folder_document.gif" />';
  4664. }
  4665. }
  4666. // The audio column.
  4667. $return_audio .= '<td align="center">';
  4668. $audio = '';
  4669. if (!$update_audio OR $update_audio <> 'true') {
  4670. if (!empty($arrLP[$i]['audio'])) {
  4671. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  4672. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  4673. $audio .= '<script type="text/javascript">
  4674. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  4675. s1.addParam("allowscriptaccess","always");
  4676. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  4677. s1.write("container' . $i . '");
  4678. </script>';*/
  4679. } else {
  4680. $audio .= '';
  4681. }
  4682. } else {
  4683. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  4684. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  4685. if (!empty ($arrLP[$i]['audio'])) {
  4686. $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');
  4687. }
  4688. }
  4689. }
  4690. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  4691. $return_audio .= '</td>';
  4692. $move_icon = '';
  4693. $move_item_icon = '';
  4694. $edit_icon = '';
  4695. $delete_icon = '';
  4696. $audio_icon = '';
  4697. $prerequisities_icon = '';
  4698. if ($is_allowed_to_edit) {
  4699. if (!$update_audio OR $update_audio <> 'true') {
  4700. $move_icon .= '<a class="moved" href="#">';
  4701. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  4702. $move_icon .= '</a>';
  4703. }
  4704. // No edit for this item types
  4705. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  4706. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  4707. $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'] . '">';
  4708. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4709. $edit_icon .= '</a>';
  4710. } else {
  4711. $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'] . '">';
  4712. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  4713. $edit_icon .= '</a>';
  4714. }
  4715. }
  4716. $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) . '\');">';
  4717. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  4718. $delete_icon .= '</a>';
  4719. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  4720. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  4721. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  4722. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  4723. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  4724. }
  4725. }
  4726. if ($update_audio != 'true') {
  4727. $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'));
  4728. } else {
  4729. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  4730. }
  4731. $parent_id = $arrLP[$i]['parent_item_id'];
  4732. $default_data[$arrLP[$i]['id']] = $row;
  4733. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  4734. if (empty($parent_id)) {
  4735. $elements[$arrLP[$i]['id']]['data'] = $row;
  4736. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4737. } else {
  4738. $parent_arrays = array();
  4739. if ($arrLP[$i]['depth'] > 1) {
  4740. //Getting list of parents
  4741. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  4742. foreach($arrLP as $item) {
  4743. if ($item['id'] == $parent_id) {
  4744. if ($item['parent_item_id'] == 0) {
  4745. $parent_id = $item['id'];
  4746. break;
  4747. } else {
  4748. $parent_id = $item['parent_item_id'];
  4749. if (empty($parent_arrays)) {
  4750. $parent_arrays[] = intval($item['id']);
  4751. }
  4752. $parent_arrays[] = $parent_id;
  4753. break;
  4754. }
  4755. }
  4756. }
  4757. }
  4758. }
  4759. if (!empty($parent_arrays)) {
  4760. $parent_arrays = array_reverse($parent_arrays);
  4761. $val = '$elements';
  4762. $x = 0;
  4763. foreach($parent_arrays as $item) {
  4764. if ($x != count($parent_arrays) -1) {
  4765. $val .= '["'.$item.'"]["children"]';
  4766. } else {
  4767. $val .= '["'.$item.'"]["children"]';
  4768. }
  4769. $x++;
  4770. }
  4771. $val .= "";
  4772. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  4773. eval($code_str);
  4774. } else {
  4775. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  4776. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  4777. }
  4778. }
  4779. }
  4780. $return = '<div class="lp_tree well">';
  4781. $return .= '<ul id="lp_item_list">';
  4782. $return .='<h4>'.$this->name.'</h4><br>';
  4783. $tree = self::print_recursive($elements, $default_data, $default_content);
  4784. if (!empty($tree)) {
  4785. $return .= $tree;
  4786. } else {
  4787. if ($drop_element_here) {
  4788. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  4789. }
  4790. }
  4791. $return .= '</ul>';
  4792. if ($update_audio == 'true') {
  4793. $return = $return_audio;
  4794. } else {
  4795. $return .= '</div>';
  4796. }
  4797. return $return;
  4798. }
  4799. function print_recursive($elements, $default_data, $default_content) {
  4800. $return = '';
  4801. foreach ($elements as $key => $item) {
  4802. if (isset($item['load_data']) || empty($item['data'])) {
  4803. $item['data'] = $default_data[$item['load_data']];
  4804. $item['type'] = $default_content[$item['load_data']]['item_type'];
  4805. }
  4806. $sub_list = '';
  4807. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  4808. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  4809. }
  4810. if (empty($item['children'])) {
  4811. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4812. $active = null;
  4813. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  4814. $active = 'active';
  4815. }
  4816. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4817. } else {
  4818. //sections
  4819. if (isset($item['children'])) {
  4820. $data = self::print_recursive($item['children'], $default_data, $default_content);
  4821. }
  4822. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  4823. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  4824. }
  4825. }
  4826. return $return;
  4827. }
  4828. /**
  4829. * This function builds the action menu
  4830. * @return void
  4831. */
  4832. public function build_action_menu() {
  4833. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4834. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4835. $return = '<div class="actions">';
  4836. //$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>';
  4837. //$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>';
  4838. $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> ';
  4839. $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>';
  4840. // 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>';
  4841. $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>';
  4842. $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>';
  4843. $return .= '</div>';
  4844. echo $return;
  4845. }
  4846. /**
  4847. * This functions builds the LP tree based on data from the database.
  4848. * @return string
  4849. * @deprecated use the return_new_tree() function
  4850. * @uses dtree.js :: necessary javascript for building this tree
  4851. */
  4852. public function build_tree() {
  4853. $course_id = api_get_course_int_id();
  4854. $return = "<script type=\"text/javascript\">\n";
  4855. $return .= "\tm = new dTree('m');\n\n";
  4856. $return .= "\tm.config.folderLinks = true;\n";
  4857. $return .= "\tm.config.useCookies = true;\n";
  4858. $return .= "\tm.config.useIcons = true;\n";
  4859. $return .= "\tm.config.useLines = true;\n";
  4860. $return .= "\tm.config.useSelection = true;\n";
  4861. $return .= "\tm.config.useStatustext = false;\n\n";
  4862. $menu = 0;
  4863. $parent = '';
  4864. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  4865. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  4866. $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
  4867. FROM $tbl_lp_item
  4868. WHERE c_id = ".$course_id." AND lp_id = " . Database::escape_string($this->lp_id);
  4869. $result = Database::query($sql);
  4870. $arrLP = array ();
  4871. while ($row = Database :: fetch_array($result)) {
  4872. $row['title'] = Security :: remove_XSS($row['title']);
  4873. $row['description'] = Security :: remove_XSS($row['description']);
  4874. $arrLP[] = array (
  4875. 'id' => $row['id'],
  4876. 'item_type' => $row['item_type'],
  4877. 'title' => $row['title'],
  4878. 'path' => $row['path'],
  4879. 'description' => $row['description'],
  4880. 'parent_item_id' => $row['parent_item_id'],
  4881. 'previous_item_id' => $row['previous_item_id'],
  4882. 'next_item_id' => $row['next_item_id'],
  4883. 'max_score' => $row['max_score'],
  4884. 'min_score' => $row['min_score'],
  4885. 'mastery_score' => $row['mastery_score'],
  4886. 'display_order' => $row['display_order']
  4887. );
  4888. }
  4889. $this->tree_array($arrLP);
  4890. $arrLP = $this->arrMenu;
  4891. unset ($this->arrMenu);
  4892. $title = '';
  4893. for ($i = 0; $i < count($arrLP); $i++) {
  4894. $title = addslashes($arrLP[$i]['title']);
  4895. $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;
  4896. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  4897. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  4898. $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";
  4899. } else
  4900. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  4901. $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";
  4902. } else {
  4903. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  4904. }
  4905. if ($menu < $arrLP[$i]['id'])
  4906. $menu = $arrLP[$i]['id'];
  4907. }
  4908. $return .= "\n\tdocument.write(m);\n";
  4909. $return .= "\t if(!m.selectedNode) m.s(1);";
  4910. $return .= "</script>\n";
  4911. return $return;
  4912. }
  4913. /**
  4914. * Creates the default learning path folder
  4915. */
  4916. public static function generate_learning_path_folder($course) {
  4917. //Creating learning_path folder
  4918. $dir = '/learning_path';
  4919. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4920. $folder = false;
  4921. if (!is_dir($filepath.'/'.$dir)) {
  4922. $folder = create_unexisting_directory($course, api_get_user_id(), api_get_session_id(), 0, 0, $filepath, $dir , get_lang('LearningPaths'), 0);
  4923. } else {
  4924. $folder = true;
  4925. }
  4926. return $folder;
  4927. }
  4928. public function generate_lp_folder($course, $lp_name = null) {
  4929. $filepath = '';
  4930. $dir = '/learning_path/';
  4931. if (empty($lp_name)) {
  4932. $lp_name = $this->name;
  4933. }
  4934. $folder = self::generate_learning_path_folder($course);
  4935. //Creating LP folder
  4936. if ($folder) {
  4937. //Limits title size
  4938. $title = api_substr(replace_dangerous_char($lp_name), 0 , 80);
  4939. $dir = $dir.$title;
  4940. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  4941. if (!is_dir($filepath.'/'.$dir)) {
  4942. $folder = create_unexisting_directory($course, api_get_user_id(), 0, 0, 0, $filepath, $dir , $lp_name);
  4943. } else {
  4944. $folder = true;
  4945. }
  4946. $dir = $dir.'/';
  4947. if ($folder) {
  4948. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  4949. }
  4950. }
  4951. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  4952. return $array;
  4953. }
  4954. /**
  4955. * Create a new document //still needs some finetuning
  4956. * @param array $_course
  4957. * @return string
  4958. */
  4959. public function create_document($_course) {
  4960. $course_id = api_get_course_int_id();
  4961. global $charset;
  4962. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  4963. if (strstr($dir, '..'))
  4964. $dir = '/';
  4965. if ($dir[0] == '.')
  4966. $dir = substr($dir, 1);
  4967. if ($dir[0] != '/')
  4968. $dir = '/' . $dir;
  4969. if ($dir[strlen($dir) - 1] != '/')
  4970. $dir .= '/';
  4971. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  4972. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  4973. //Generates folder
  4974. $result = $this->generate_lp_folder($_course);
  4975. $dir = $result['dir'];
  4976. $filepath = $result['filepath'];
  4977. }
  4978. if (!is_dir($filepath)) {
  4979. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  4980. $dir = '/';
  4981. }
  4982. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  4983. // is already escaped twice when it gets here.
  4984. $title = replace_dangerous_char(stripslashes($_POST['title']));
  4985. $title = disable_dangerous_file($title);
  4986. $filename = $title;
  4987. $content = $_POST['content_lp'];
  4988. $tmp_filename = $filename;
  4989. $i = 0;
  4990. while (file_exists($filepath . $tmp_filename . '.html'))
  4991. $tmp_filename = $filename . '_' . ++ $i;
  4992. $filename = $tmp_filename . '.html';
  4993. $content = stripslashes($content);
  4994. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  4995. // Change the path of mp3 to absolute.
  4996. // The first regexp deals with ../../../ urls.
  4997. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  4998. // The second regexp deals with audio/ urls.
  4999. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5000. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5001. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5002. if (!file_exists($filepath . $filename)) {
  5003. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5004. fputs($fp, $content);
  5005. fclose($fp);
  5006. $file_size = filesize($filepath . $filename);
  5007. $save_file_path = $dir.$filename;
  5008. $document_id = add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5009. if ($document_id) {
  5010. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id(), null, null, null, null, api_get_session_id());
  5011. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5012. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5013. if ($new_comment || $new_title) {
  5014. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5015. $ct = '';
  5016. if ($new_comment)
  5017. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5018. if ($new_title)
  5019. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5020. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5021. Database::query($sql_update);
  5022. }
  5023. }
  5024. return $document_id;
  5025. }
  5026. }
  5027. }
  5028. /**
  5029. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5030. * @param array $_course array
  5031. * @return void
  5032. */
  5033. public function edit_document($_course) {
  5034. $course_id = api_get_course_int_id();
  5035. global $_configuration;
  5036. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5037. if (strstr($dir, '..'))
  5038. $dir = '/';
  5039. if ($dir[0] == '.')
  5040. $dir = substr($dir, 1);
  5041. if ($dir[0] != '/')
  5042. $dir = '/' . $dir;
  5043. if ($dir[strlen($dir) - 1] != '/')
  5044. $dir .= '/';
  5045. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5046. if (!is_dir($filepath)) {
  5047. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5048. $dir = '/';
  5049. }
  5050. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5051. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5052. $document_id = intval($_POST['path']);
  5053. $sql = "SELECT path FROM " . $table_doc . " WHERE c_id = $course_id AND id = " . $document_id;
  5054. $res = Database::query($sql);
  5055. $row = Database :: fetch_array($res);
  5056. $content = stripslashes($_POST['content_lp']);
  5057. $file = $filepath . $row['path'];
  5058. if ($fp = @ fopen($file, 'w')) {
  5059. $content = text_filter($content);
  5060. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5061. // Change the path of mp3 to absolute.
  5062. // The first regexp deals with ../../../ urls.
  5063. $content = preg_replace("|(flashvars=\"file=)(\.+/)+|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5064. // The second regexp deals with audio/ urls.
  5065. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5066. fputs($fp, $content);
  5067. fclose($fp);
  5068. $sql_update = "UPDATE " . $table_doc ." SET title='".Database::escape_string($_POST['title'])."' WHERE c_id = ".$course_id." AND id = " . $document_id;
  5069. Database::query($sql_update);
  5070. }
  5071. }
  5072. }
  5073. /**
  5074. * Displays the selected item, with a panel for manipulating the item
  5075. * @param int $item_id
  5076. * @param string $msg
  5077. * @return string
  5078. */
  5079. public function display_item($item_id, $msg = null, $show_actions = true) {
  5080. $course_id = api_get_course_int_id();
  5081. $return = '';
  5082. if (is_numeric($item_id)) {
  5083. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5084. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5085. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5086. WHERE c_id = ".$course_id." AND lp.id = " . Database::escape_string($item_id);
  5087. $result = Database::query($sql);
  5088. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5089. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5090. // Prevents wrong parent selection for document, see Bug#1251.
  5091. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5092. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5093. }
  5094. if ($show_actions) {
  5095. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5096. }
  5097. $return .= '<div style="padding:10px;">';
  5098. if ($msg != '')
  5099. $return .= $msg;
  5100. $return .= '<h3>'.$row['title'].'</h3>';
  5101. switch ($row['item_type']) {
  5102. case TOOL_QUIZ:
  5103. if (!empty($row['path'])) {
  5104. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5105. $exercise = new Exercise();
  5106. $exercise->read($row['path']);
  5107. $return .= $exercise->description.'<br />';
  5108. }
  5109. break;
  5110. case TOOL_DOCUMENT:
  5111. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5112. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($row['path']);
  5113. $result = Database::query($sql_doc);
  5114. $path_file = Database::result($result, 0, 0);
  5115. $path_parts = pathinfo($path_file);
  5116. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5117. if (in_array($path_parts['extension'], array (
  5118. 'html',
  5119. 'txt',
  5120. 'png',
  5121. 'jpg',
  5122. 'JPG',
  5123. 'jpeg',
  5124. 'JPEG',
  5125. 'gif',
  5126. 'swf'
  5127. ))) {
  5128. $return .= $this->display_document($row['path'], true, true);
  5129. }
  5130. break;
  5131. }
  5132. $return .= '</div>';
  5133. }
  5134. }
  5135. return $return;
  5136. }
  5137. /**
  5138. * Shows the needed forms for editing a specific item
  5139. * @param int $item_id
  5140. * @return string
  5141. */
  5142. public function display_edit_item($item_id) {
  5143. global $_course; // It will disappear.
  5144. $course_id = api_get_course_int_id();
  5145. $return = '';
  5146. if (is_numeric($item_id)) {
  5147. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5148. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = " . Database::escape_string($item_id);
  5149. $res = Database::query($sql);
  5150. $row = Database::fetch_array($res);
  5151. switch ($row['item_type']) {
  5152. case 'dokeos_chapter' :
  5153. case 'dir' :
  5154. case 'asset' :
  5155. case 'sco' :
  5156. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5157. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5158. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5159. } else {
  5160. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5161. }
  5162. break;
  5163. case TOOL_DOCUMENT :
  5164. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5165. $sql_step = " SELECT lp.*, doc.path as dir
  5166. FROM " . $tbl_lp_item . " as lp
  5167. LEFT JOIN " . $tbl_doc . " as doc ON doc.id = lp.path
  5168. WHERE lp.c_id = $course_id AND
  5169. doc.c_id = $course_id AND
  5170. lp.id = " . Database::escape_string($item_id);
  5171. $res_step = Database::query($sql_step);
  5172. $row_step = Database :: fetch_array($res_step);
  5173. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5174. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5175. break;
  5176. case TOOL_LINK :
  5177. $link_id = (string) $row['path'];
  5178. if (ctype_digit($link_id)) {
  5179. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5180. $sql_select = 'SELECT url FROM ' . $tbl_link . ' WHERE c_id = '.$course_id.' AND id = ' . Database::escape_string($link_id);
  5181. $res_link = Database::query($sql_select);
  5182. $row_link = Database :: fetch_array($res_link);
  5183. if (is_array($row_link)) {
  5184. $row['url'] = $row_link['url'];
  5185. }
  5186. }
  5187. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5188. $return .= $this->display_link_form('edit', $item_id, $row);
  5189. break;
  5190. case 'dokeos_module' :
  5191. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5192. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5193. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5194. } else {
  5195. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5196. }
  5197. break;
  5198. case TOOL_QUIZ :
  5199. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5200. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5201. break;
  5202. case TOOL_HOTPOTATOES :
  5203. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5204. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5205. break;
  5206. case TOOL_STUDENTPUBLICATION :
  5207. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5208. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5209. break;
  5210. case TOOL_FORUM :
  5211. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5212. $return .= $this->display_forum_form('edit', $item_id, $row);
  5213. break;
  5214. case TOOL_THREAD :
  5215. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5216. $return .= $this->display_thread_form('edit', $item_id, $row);
  5217. break;
  5218. }
  5219. }
  5220. return $return;
  5221. }
  5222. /**
  5223. * Function that displays a list with al the resources that could be added to the learning path
  5224. * @return string
  5225. */
  5226. public function display_resources() {
  5227. global $_course; // TODO: Don't use globals.
  5228. $course_code = api_get_course_id();
  5229. //Get all the docs
  5230. $documents = $this->get_documents();
  5231. //Get all the exercises
  5232. $exercises = $this->get_exercises();
  5233. // Get all the links
  5234. $links = $this->get_links();
  5235. //Get al the student publications
  5236. $works = $this->get_student_publications();
  5237. //Get al the forums
  5238. $forums = $this->get_forums(null, $course_code);
  5239. $headers = array( Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  5240. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  5241. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  5242. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  5243. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  5244. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  5245. );
  5246. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5247. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5248. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  5249. return true;
  5250. }
  5251. /**
  5252. * Returns the extension of a document
  5253. * @param string filename
  5254. * @return string Extension (part after the last dot)
  5255. */
  5256. public function get_extension($filename) {
  5257. $explode = explode('.', $filename);
  5258. return $explode[count($explode) - 1];
  5259. }
  5260. /**
  5261. * Displays a document by id
  5262. *
  5263. * @param unknown_type $id
  5264. * @return unknown
  5265. */
  5266. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false) {
  5267. global $_course; // It is temporary.
  5268. $course_id = api_get_course_int_id();
  5269. $return = '';
  5270. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5271. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5272. WHERE c_id = ".$course_id." AND id = " . $id;
  5273. $res_doc = Database::query($sql_doc);
  5274. $row_doc = Database :: fetch_array($res_doc);
  5275. // TODO: Add a path filter.
  5276. if ($iframe) {
  5277. $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>';
  5278. } else {
  5279. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5280. }
  5281. return $return;
  5282. }
  5283. /**
  5284. * Return HTML form to add/edit a quiz
  5285. * @param string Action (add/edit)
  5286. * @param integer Item ID if already exists
  5287. * @param mixed Extra information (quiz ID if integer)
  5288. * @return string HTML form
  5289. */
  5290. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '') {
  5291. $course_id = api_get_course_int_id();
  5292. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5293. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5294. if ($id != 0 && is_array($extra_info)) {
  5295. $item_title = $extra_info['title'];
  5296. $item_description = $extra_info['description'];
  5297. } elseif (is_numeric($extra_info)) {
  5298. $sql_quiz = "SELECT title, description FROM " . $tbl_quiz . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5299. $result = Database::query($sql_quiz);
  5300. $row = Database :: fetch_array($result);
  5301. $item_title = $row['title'];
  5302. $item_description = $row['description'];
  5303. } else {
  5304. $item_title = '';
  5305. $item_description = '';
  5306. }
  5307. $item_title = Security::remove_XSS($item_title);
  5308. $item_description = Security::remove_XSS($item_description);
  5309. $legend = '<legend>';
  5310. if ($id != 0 && is_array($extra_info))
  5311. $parent = $extra_info['parent_item_id'];
  5312. else
  5313. $parent = 0;
  5314. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5315. $result = Database::query($sql);
  5316. $arrLP = array ();
  5317. while ($row = Database :: fetch_array($result)) {
  5318. $arrLP[] = array (
  5319. 'id' => $row['id'],
  5320. 'item_type' => $row['item_type'],
  5321. 'title' => $row['title'],
  5322. 'path' => $row['path'],
  5323. 'description' => $row['description'],
  5324. 'parent_item_id' => $row['parent_item_id'],
  5325. 'previous_item_id' => $row['previous_item_id'],
  5326. 'next_item_id' => $row['next_item_id'],
  5327. 'display_order' => $row['display_order'],
  5328. 'max_score' => $row['max_score'],
  5329. 'min_score' => $row['min_score'],
  5330. 'mastery_score' => $row['mastery_score'],
  5331. 'prerequisite' => $row['prerequisite'],
  5332. 'max_time_allowed' => $row['max_time_allowed']
  5333. );
  5334. }
  5335. $this->tree_array($arrLP);
  5336. $arrLP = $this->arrMenu;
  5337. unset ($this->arrMenu);
  5338. if ($action == 'add')
  5339. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5340. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5341. else
  5342. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5343. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5344. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5345. }
  5346. $legend .= '</legend>';
  5347. $return .= '<div class="sectioncomment">';
  5348. $return .= '<form method="POST">';
  5349. $return .= $legend;
  5350. $return .= '<table class="lp_form">';
  5351. if ($action != 'move') {
  5352. $return .= '<tr>';
  5353. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5354. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5355. $return .= '</tr>';
  5356. }
  5357. $return .= '<tr>';
  5358. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5359. $return .= '<td class="input">';
  5360. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5361. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5362. $arrHide = array (
  5363. $id
  5364. );
  5365. //$parent_item_id = $_SESSION['parent_item_id'];
  5366. for ($i = 0; $i < count($arrLP); $i++) {
  5367. if ($action != 'add') {
  5368. 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)) {
  5369. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5370. } else {
  5371. $arrHide[] = $arrLP[$i]['id'];
  5372. }
  5373. } else {
  5374. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5375. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5376. }
  5377. }
  5378. if (is_array($arrLP)) {
  5379. reset($arrLP);
  5380. }
  5381. $return .= '</select>';
  5382. $return .= '</td>';
  5383. $return .= '</tr>';
  5384. $return .= '<tr>';
  5385. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5386. $return .= '<td class="input">';
  5387. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5388. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5389. for ($i = 0; $i < count($arrLP); $i++) {
  5390. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5391. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5392. $selected = 'selected="selected" ';
  5393. elseif ($action == 'add') $selected = 'selected="selected" ';
  5394. else
  5395. $selected = '';
  5396. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5397. }
  5398. }
  5399. $return .= '</select>';
  5400. $return .= '</td>';
  5401. $return .= '</tr>';
  5402. if ($action != 'move') {
  5403. $id_prerequisite = 0;
  5404. if (is_array($arrLP)) {
  5405. foreach ($arrLP as $key => $value) {
  5406. if ($value['id'] == $id) {
  5407. $id_prerequisite = $value['prerequisite'];
  5408. break;
  5409. }
  5410. }
  5411. }
  5412. $arrHide = array ();
  5413. for ($i = 0; $i < count($arrLP); $i++) {
  5414. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5415. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5416. $s_selected_position = $arrLP[$i]['id'];
  5417. elseif ($action == 'add') $s_selected_position = 0;
  5418. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5419. }
  5420. }
  5421. /*// Commented the prerequisites, only visible in edit (exercise).
  5422. $return .= '<tr>';
  5423. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5424. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5425. foreach($arrHide as $key => $value){
  5426. if($key==$s_selected_position && $action == 'add'){
  5427. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5428. }
  5429. elseif($key==$id_prerequisite && $action == 'edit'){
  5430. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5431. }
  5432. else{
  5433. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5434. }
  5435. }
  5436. $return .= "</select></td>";
  5437. */
  5438. $return .= '</tr>';
  5439. /*$return .= '<tr>';
  5440. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5441. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5442. // Remove temporarily the test description.
  5443. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5444. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5445. $return .= '</tr>'; */
  5446. }
  5447. $return .= '<tr>';
  5448. if ($action == 'add') {
  5449. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5450. } else {
  5451. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5452. }
  5453. $return .= '</tr>';
  5454. $return .= '</table>';
  5455. if ($action == 'move') {
  5456. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5457. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5458. }
  5459. if (is_numeric($extra_info)) {
  5460. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5461. }
  5462. elseif (is_array($extra_info)) {
  5463. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5464. }
  5465. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5466. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5467. $return .= '</form>';
  5468. $return .= '</div>';
  5469. return $return;
  5470. }
  5471. /**
  5472. * Addition of Hotpotatoes tests
  5473. * @param string Action
  5474. * @param integer Internal ID of the item
  5475. * @param mixed Extra information - can be an array with title and description indexes
  5476. * @return string HTML structure to display the hotpotatoes addition formular
  5477. */
  5478. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') {
  5479. $course_id = api_get_course_int_id();
  5480. global $charset;
  5481. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5482. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5483. if ($id != 0 && is_array($extra_info)) {
  5484. $item_title = stripslashes($extra_info['title']);
  5485. $item_description = stripslashes($extra_info['description']);
  5486. } elseif (is_numeric($extra_info)) {
  5487. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5488. $sql_hot = "SELECT * FROM " . $TBL_DOCUMENT . "
  5489. WHERE c_id = ".$course_id." AND
  5490. path LIKE '" . $uploadPath . "/%/%htm%' AND
  5491. id = " . (int) $extra_info . "
  5492. ORDER BY id ASC";
  5493. $res_hot = Database::query($sql_hot);
  5494. $row = Database::fetch_array($res_hot);
  5495. $item_title = $row['title'];
  5496. $item_description = $row['description'];
  5497. if (!empty ($row['comment'])) {
  5498. $item_title = $row['comment'];
  5499. }
  5500. } else {
  5501. $item_title = '';
  5502. $item_description = '';
  5503. }
  5504. if ($id != 0 && is_array($extra_info))
  5505. $parent = $extra_info['parent_item_id'];
  5506. else
  5507. $parent = 0;
  5508. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5509. $result = Database::query($sql);
  5510. $arrLP = array ();
  5511. while ($row = Database :: fetch_array($result)) {
  5512. $arrLP[] = array (
  5513. 'id' => $row['id'],
  5514. 'item_type' => $row['item_type'],
  5515. 'title' => $row['title'],
  5516. 'path' => $row['path'],
  5517. 'description' => $row['description'],
  5518. 'parent_item_id' => $row['parent_item_id'],
  5519. 'previous_item_id' => $row['previous_item_id'],
  5520. 'next_item_id' => $row['next_item_id'],
  5521. 'display_order' => $row['display_order'],
  5522. 'max_score' => $row['max_score'],
  5523. 'min_score' => $row['min_score'],
  5524. 'mastery_score' => $row['mastery_score'],
  5525. 'prerequisite' => $row['prerequisite'],
  5526. 'max_time_allowed' => $row['max_time_allowed']
  5527. );
  5528. }
  5529. $legend = '<legend>';
  5530. if ($action == 'add')
  5531. $legend .= get_lang('CreateTheExercise');
  5532. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  5533. else
  5534. $legend .= get_lang('EditCurrentExecice');
  5535. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5536. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5537. }
  5538. $legend .= '</legend>';
  5539. $return .= '<form method="POST">';
  5540. $return .= $legend;
  5541. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5542. $return .= '<tr>';
  5543. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  5544. $return .= '<td class="input">';
  5545. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5546. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5547. $arrHide = array (
  5548. $id
  5549. );
  5550. if (count($arrLP) > 0) {
  5551. for ($i = 0; $i < count($arrLP); $i++) {
  5552. if ($action != 'add') {
  5553. 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)) {
  5554. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5555. } else {
  5556. $arrHide[] = $arrLP[$i]['id'];
  5557. }
  5558. } else {
  5559. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5560. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5561. }
  5562. }
  5563. reset($arrLP);
  5564. }
  5565. $return .= '</select>';
  5566. $return .= '</td>';
  5567. $return .= '</tr>';
  5568. $return .= '<tr>';
  5569. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  5570. $return .= '<td class="input">';
  5571. $return .= '<select id="previous" name="previous" size="1">';
  5572. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5573. for ($i = 0; $i < count($arrLP); $i++) {
  5574. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5575. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5576. $selected = 'selected="selected" ';
  5577. elseif ($action == 'add') $selected = 'selected="selected" ';
  5578. else
  5579. $selected = '';
  5580. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5581. }
  5582. }
  5583. $return .= '</select>';
  5584. $return .= '</td>';
  5585. $return .= '</tr>';
  5586. if ($action != 'move') {
  5587. $return .= '<tr>';
  5588. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  5589. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5590. $return .= '</tr>';
  5591. $id_prerequisite = 0;
  5592. if (is_array($arrLP) && count($arrLP) > 0) {
  5593. foreach ($arrLP as $key => $value) {
  5594. if ($value['id'] == $id) {
  5595. $id_prerequisite = $value['prerequisite'];
  5596. break;
  5597. }
  5598. }
  5599. $arrHide = array ();
  5600. for ($i = 0; $i < count($arrLP); $i++) {
  5601. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5602. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5603. $s_selected_position = $arrLP[$i]['id'];
  5604. elseif ($action == 'add') $s_selected_position = 0;
  5605. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5606. }
  5607. }
  5608. }
  5609. }
  5610. $return .= '<tr>';
  5611. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  5612. $return .= '</tr>';
  5613. $return .= '</table>';
  5614. if ($action == 'move') {
  5615. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5616. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5617. }
  5618. if (is_numeric($extra_info)) {
  5619. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5620. } elseif (is_array($extra_info)) {
  5621. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5622. }
  5623. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  5624. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5625. $return .= '</form>';
  5626. return $return;
  5627. }
  5628. /**
  5629. * Return the form to display the forum edit/add option
  5630. * @param string Action (add/edit)
  5631. * @param integer ID of the lp_item if already exists
  5632. * @param mixed Forum ID or title
  5633. * @return string HTML form
  5634. */
  5635. public function display_forum_form($action = 'add', $id = 0, $extra_info = '') {
  5636. $course_id = api_get_course_int_id();
  5637. global $charset;
  5638. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5639. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  5640. if ($id != 0 && is_array($extra_info)) {
  5641. $item_title = stripslashes($extra_info['title']);
  5642. }
  5643. elseif (is_numeric($extra_info)) {
  5644. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  5645. FROM " . $tbl_forum . "
  5646. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  5647. $result = Database::query($sql_forum);
  5648. $row = Database :: fetch_array($result);
  5649. $item_title = $row['title'];
  5650. $item_description = $row['comment'];
  5651. } else {
  5652. $item_title = '';
  5653. $item_description = '';
  5654. }
  5655. $legend = '<legend>';
  5656. if ($id != 0 && is_array($extra_info))
  5657. $parent = $extra_info['parent_item_id'];
  5658. else
  5659. $parent = 0;
  5660. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5661. WHERE c_id = ".$course_id." AND
  5662. lp_id = " . $this->lp_id;
  5663. $result = Database::query($sql);
  5664. $arrLP = array ();
  5665. while ($row = Database :: fetch_array($result)) {
  5666. $arrLP[] = array (
  5667. 'id' => $row['id'],
  5668. 'item_type' => $row['item_type'],
  5669. 'title' => $row['title'],
  5670. 'path' => $row['path'],
  5671. 'description' => $row['description'],
  5672. 'parent_item_id' => $row['parent_item_id'],
  5673. 'previous_item_id' => $row['previous_item_id'],
  5674. 'next_item_id' => $row['next_item_id'],
  5675. 'display_order' => $row['display_order'],
  5676. 'max_score' => $row['max_score'],
  5677. 'min_score' => $row['min_score'],
  5678. 'mastery_score' => $row['mastery_score'],
  5679. 'prerequisite' => $row['prerequisite']
  5680. );
  5681. }
  5682. $this->tree_array($arrLP);
  5683. $arrLP = $this->arrMenu;
  5684. unset ($this->arrMenu);
  5685. if ($action == 'add')
  5686. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  5687. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  5688. else
  5689. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  5690. $legend .= '</legend>';
  5691. $return .= '<div class="sectioncomment">';
  5692. $return .= '<form method="POST">';
  5693. $return .= $legend;
  5694. $return .= '<table class="lp_form">';
  5695. if ($action != 'move') {
  5696. $return .= '<tr>';
  5697. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5698. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  5699. $return .= '</tr>';
  5700. }
  5701. $return .= '<tr>';
  5702. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5703. $return .= '<td class="input">';
  5704. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  5705. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5706. $arrHide = array (
  5707. $id
  5708. );
  5709. //$parent_item_id = $_SESSION['parent_item_id'];
  5710. for ($i = 0; $i < count($arrLP); $i++) {
  5711. if ($action != 'add') {
  5712. 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)) {
  5713. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5714. } else {
  5715. $arrHide[] = $arrLP[$i]['id'];
  5716. }
  5717. } else {
  5718. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5719. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5720. }
  5721. }
  5722. if (is_array($arrLP)) {
  5723. reset($arrLP);
  5724. }
  5725. $return .= "\t\t\t\t" . '</select>';
  5726. $return .= '</td>';
  5727. $return .= '</tr>';
  5728. $return .= '<tr>';
  5729. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5730. $return .= '<td class="input">';
  5731. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  5732. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5733. for ($i = 0; $i < count($arrLP); $i++) {
  5734. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5735. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5736. $selected = 'selected="selected" ';
  5737. elseif ($action == 'add') $selected = 'selected="selected" ';
  5738. else
  5739. $selected = '';
  5740. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5741. }
  5742. }
  5743. $return .= "\t\t\t\t" . '</select>';
  5744. $return .= '</td>';
  5745. $return .= '</tr>';
  5746. if ($action != 'move') {
  5747. $return .= '<tr>';
  5748. $return .= '</tr>';
  5749. $id_prerequisite = 0;
  5750. if (is_array($arrLP)) {
  5751. foreach ($arrLP as $key => $value) {
  5752. if ($value['id'] == $id) {
  5753. $id_prerequisite = $value['prerequisite'];
  5754. break;
  5755. }
  5756. }
  5757. }
  5758. $arrHide = array ();
  5759. for ($i = 0; $i < count($arrLP); $i++) {
  5760. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5761. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5762. $s_selected_position = $arrLP[$i]['id'];
  5763. elseif ($action == 'add') $s_selected_position = 0;
  5764. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5765. }
  5766. }
  5767. $return .= '</tr>';
  5768. }
  5769. $return .= '<tr>';
  5770. if ($action == 'add') {
  5771. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  5772. } else {
  5773. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  5774. }
  5775. $return .= '</tr>';
  5776. $return .= '</table>';
  5777. if ($action == 'move') {
  5778. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5779. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5780. }
  5781. if (is_numeric($extra_info)) {
  5782. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5783. }
  5784. elseif (is_array($extra_info)) {
  5785. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5786. }
  5787. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  5788. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5789. $return .= '</form>';
  5790. $return .= '</div>';
  5791. return $return;
  5792. }
  5793. /**
  5794. * Return HTML form to add/edit forum threads
  5795. * @param string Action (add/edit)
  5796. * @param integer Item ID if already exists in learning path
  5797. * @param mixed Extra information (thread ID if integer)
  5798. * @return string HTML form
  5799. */
  5800. public function display_thread_form($action = 'add', $id = 0, $extra_info = '') {
  5801. $course_id = api_get_course_int_id();
  5802. if (empty($course_id)) {
  5803. return null;
  5804. }
  5805. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5806. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  5807. if ($id != 0 && is_array($extra_info)) {
  5808. $item_title = stripslashes($extra_info['title']);
  5809. } elseif (is_numeric($extra_info)) {
  5810. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  5811. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  5812. $result = Database::query($sql_forum);
  5813. $row = Database :: fetch_array($result);
  5814. $item_title = $row['title'];
  5815. $item_description = '';
  5816. } else {
  5817. $item_title = '';
  5818. $item_description = '';
  5819. }
  5820. $return = null;
  5821. if ($id != 0 && is_array($extra_info))
  5822. $parent = $extra_info['parent_item_id'];
  5823. else
  5824. $parent = 0;
  5825. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5826. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5827. $result = Database::query($sql);
  5828. $arrLP = array ();
  5829. while ($row = Database :: fetch_array($result)) {
  5830. $arrLP[] = array (
  5831. 'id' => $row['id'],
  5832. 'item_type' => $row['item_type'],
  5833. 'title' => $row['title'],
  5834. 'path' => $row['path'],
  5835. 'description' => $row['description'],
  5836. 'parent_item_id' => $row['parent_item_id'],
  5837. 'previous_item_id' => $row['previous_item_id'],
  5838. 'next_item_id' => $row['next_item_id'],
  5839. 'display_order' => $row['display_order'],
  5840. 'max_score' => $row['max_score'],
  5841. 'min_score' => $row['min_score'],
  5842. 'mastery_score' => $row['mastery_score'],
  5843. 'prerequisite' => $row['prerequisite']
  5844. );
  5845. }
  5846. $this->tree_array($arrLP);
  5847. $arrLP = $this->arrMenu;
  5848. unset ($this->arrMenu);
  5849. $return .= '<form method="POST">';
  5850. if ($action == 'add')
  5851. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  5852. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  5853. else
  5854. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  5855. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  5856. $return .= '<tr>';
  5857. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5858. $return .= '<td class="input">';
  5859. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5860. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5861. $arrHide = array (
  5862. $id
  5863. );
  5864. for ($i = 0; $i < count($arrLP); $i++) {
  5865. if ($action != 'add') {
  5866. 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)) {
  5867. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5868. } else {
  5869. $arrHide[] = $arrLP[$i]['id'];
  5870. }
  5871. } else {
  5872. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5873. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5874. }
  5875. }
  5876. if ($arrLP != null) {
  5877. reset($arrLP);
  5878. }
  5879. $return .= '</select>';
  5880. $return .= '</td>';
  5881. $return .= '</tr>';
  5882. $return .= '<tr>';
  5883. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5884. $return .= '<td class="input">';
  5885. $return .= "\t\t\t\t" . '<select id="previous" name="previous" size="1">';
  5886. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5887. for ($i = 0; $i < count($arrLP); $i++) {
  5888. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5889. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5890. $selected = 'selected="selected" ';
  5891. elseif ($action == 'add') $selected = 'selected="selected" ';
  5892. else
  5893. $selected = '';
  5894. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5895. }
  5896. }
  5897. $return .= "\t\t\t\t" . '</select>';
  5898. $return .= '</td>';
  5899. $return .= '</tr>';
  5900. if ($action != 'move') {
  5901. $return .= '<tr>';
  5902. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5903. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  5904. $return .= '</tr>';
  5905. $return .= '<tr>';
  5906. $return .= '</tr>';
  5907. $id_prerequisite = 0;
  5908. if ($arrLP != null) {
  5909. foreach ($arrLP as $key => $value) {
  5910. if ($value['id'] == $id) {
  5911. $id_prerequisite = $value['prerequisite'];
  5912. break;
  5913. }
  5914. }
  5915. }
  5916. $arrHide = array();
  5917. for ($i = 0; $i < count($arrLP); $i++) {
  5918. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5919. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5920. $s_selected_position = $arrLP[$i]['id'];
  5921. elseif ($action == 'add') $s_selected_position = 0;
  5922. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5923. }
  5924. }
  5925. $return .= '<tr>';
  5926. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  5927. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  5928. foreach ($arrHide as $key => $value) {
  5929. if ($key == $s_selected_position && $action == 'add') {
  5930. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5931. }
  5932. elseif ($key == $id_prerequisite && $action == 'edit') {
  5933. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  5934. } else {
  5935. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  5936. }
  5937. }
  5938. $return .= "</select></td>";
  5939. $return .= '</tr>';
  5940. }
  5941. $return .= '<tr>';
  5942. $return .= '<td></td><td>
  5943. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  5944. $return .= '</tr>';
  5945. $return .= '</table>';
  5946. if ($action == 'move') {
  5947. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5948. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5949. }
  5950. if (is_numeric($extra_info)) {
  5951. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5952. }
  5953. elseif (is_array($extra_info)) {
  5954. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5955. }
  5956. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  5957. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5958. $return .= '</form>';
  5959. $return .= '</div>';
  5960. return $return;
  5961. }
  5962. /**
  5963. * Return the HTML form to display an item (generally a section/module item)
  5964. * @param string Item type (module/dokeos_module)
  5965. * @param string Title (optional, only when creating)
  5966. * @param string Action ('add'/'edit')
  5967. * @param integer lp_item ID
  5968. * @param mixed Extra info
  5969. * @return string HTML form
  5970. */
  5971. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new') {
  5972. $course_id = api_get_course_int_id();
  5973. global $_course;
  5974. global $charset;
  5975. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5976. if ($id != 0 && is_array($extra_info)) {
  5977. $item_title = $extra_info['title'];
  5978. $item_description = $extra_info['description'];
  5979. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  5980. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  5981. } else {
  5982. $item_title = '';
  5983. $item_description = '';
  5984. $item_path_fck = '';
  5985. }
  5986. if ($id != 0 && is_array($extra_info))
  5987. $parent = $extra_info['parent_item_id'];
  5988. else
  5989. $parent = 0;
  5990. $id = intval($id);
  5991. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5992. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND id != $id";
  5993. if ($item_type == 'module')
  5994. $sql .= " AND parent_item_id = 0";
  5995. $result = Database::query($sql);
  5996. $arrLP = array ();
  5997. while ($row = Database :: fetch_array($result)) {
  5998. $arrLP[] = array (
  5999. 'id' => $row['id'],
  6000. 'item_type' => $row['item_type'],
  6001. 'title' => $row['title'],
  6002. 'path' => $row['path'],
  6003. 'description' => $row['description'],
  6004. 'parent_item_id' => $row['parent_item_id'],
  6005. 'previous_item_id' => $row['previous_item_id'],
  6006. 'next_item_id' => $row['next_item_id'],
  6007. 'max_score' => $row['max_score'],
  6008. 'min_score' => $row['min_score'],
  6009. 'mastery_score' => $row['mastery_score'],
  6010. 'prerequisite' => $row['prerequisite'],
  6011. 'display_order' => $row['display_order']
  6012. );
  6013. }
  6014. $this->tree_array($arrLP);
  6015. $arrLP = $this->arrMenu;
  6016. unset ($this->arrMenu);
  6017. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6018. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6019. $form = new FormValidator('form', 'POST', $url);
  6020. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6021. $defaults['description'] = $item_description;
  6022. $form->addElement('header', $title);
  6023. //$arrHide = array($id);
  6024. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6025. $arrHide[0]['padding'] = 3;
  6026. $charset = api_get_system_encoding();
  6027. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6028. for ($i = 0; $i < count($arrLP); $i++) {
  6029. if ($action != 'add') {
  6030. 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)) {
  6031. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6032. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6033. if ($parent == $arrLP[$i]['id']) {
  6034. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6035. }
  6036. }
  6037. } else {
  6038. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6039. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6040. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6041. if ($parent == $arrLP[$i]['id']) {
  6042. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6043. }
  6044. }
  6045. }
  6046. }
  6047. if ($action != 'move') {
  6048. $form->addElement('text', 'title', get_lang('Title'), 'id="idTitle" class="learnpath_chapter_form" size="40%"');
  6049. $form->applyFilter('title', 'html_filter');
  6050. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6051. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6052. } else {
  6053. $form->addElement('hidden', 'title');
  6054. }
  6055. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"');
  6056. foreach ($arrHide as $key => $value) {
  6057. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6058. }
  6059. if (!empty($s_selected_parent)) {
  6060. $parent_select->setSelected($s_selected_parent);
  6061. }
  6062. }
  6063. if (is_array($arrLP)) {
  6064. reset($arrLP);
  6065. }
  6066. $arrHide = array();
  6067. // POSITION
  6068. for ($i = 0; $i < count($arrLP); $i++) {
  6069. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6070. //this is the same!
  6071. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6072. $s_selected_position = $arrLP[$i]['id'];
  6073. } elseif ($action == 'add') {
  6074. $s_selected_position = $arrLP[$i]['id'];
  6075. }
  6076. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6077. }
  6078. }
  6079. $position = & $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_chapter_form" style="width:37%;"');
  6080. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6081. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6082. foreach ($arrHide as $key => $value) {
  6083. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6084. }
  6085. if (!empty ($s_selected_position)) {
  6086. $position->setSelected($s_selected_position);
  6087. }
  6088. if (is_array($arrLP)) {
  6089. reset($arrLP);
  6090. }
  6091. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6092. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6093. $form->addElement('hidden', 'parent', '0');
  6094. }
  6095. //fix in order to use the tab
  6096. if ($item_type == 'chapter') {
  6097. $form->addElement('hidden', 'type', 'chapter');
  6098. }
  6099. $extension = null;
  6100. if (!empty($item_path)) {
  6101. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6102. }
  6103. //assets can't be modified
  6104. //$item_type == 'asset' ||
  6105. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6106. if ($item_type == 'sco') {
  6107. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6108. }
  6109. $renderer = $form->defaultRenderer();
  6110. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6111. $relative_prefix = '';
  6112. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6113. 'Width' => '100%',
  6114. 'Height' => '500',
  6115. 'FullPage' => true,
  6116. 'CreateDocumentDir' => $relative_prefix,
  6117. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6118. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6119. );
  6120. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6121. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6122. //$defaults['content_lp'] = file_get_contents($item_path);
  6123. $defaults['content_lp'] = file_get_contents($content_path);
  6124. }
  6125. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6126. $form->addElement('hidden', 'post_time', time());
  6127. $form->setDefaults($defaults);
  6128. return $form->return_form();
  6129. }
  6130. /**
  6131. * Returns the form to update or create a document
  6132. * @param string Action (add/edit)
  6133. * @param integer ID of the lp_item (if already exists)
  6134. * @param mixed Integer if document ID, string if info ('new')
  6135. * @return string HTML form
  6136. */
  6137. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new') {
  6138. $course_id = api_get_course_int_id();
  6139. global $charset;
  6140. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6141. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6142. $no_display_edit_textarea = false;
  6143. //If action==edit document
  6144. //We don't display the document form if it's not an editable document (html or txt file)
  6145. if ($action == "edit") {
  6146. if (is_array($extra_info)) {
  6147. $path_parts = pathinfo($extra_info['dir']);
  6148. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6149. $no_display_edit_textarea = true;
  6150. }
  6151. }
  6152. }
  6153. $no_display_add = false;
  6154. // If action==add an existing document
  6155. // We don't display the document form if it's not an editable document (html or txt file).
  6156. if ($action == "add") {
  6157. if (is_numeric($extra_info)) {
  6158. $sql_doc = "SELECT path FROM " . $tbl_doc . " WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6159. $result = Database::query($sql_doc);
  6160. $path_file = Database :: result($result, 0, 0);
  6161. $path_parts = pathinfo($path_file);
  6162. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6163. $no_display_add = true;
  6164. }
  6165. }
  6166. }
  6167. if ($id != 0 && is_array($extra_info)) {
  6168. $item_title = stripslashes($extra_info['title']);
  6169. $item_description = stripslashes($extra_info['description']);
  6170. $item_terms = stripslashes($extra_info['terms']);
  6171. if (empty ($item_title)) {
  6172. $path_parts = pathinfo($extra_info['path']);
  6173. $item_title = stripslashes($path_parts['filename']);
  6174. }
  6175. } elseif (is_numeric($extra_info)) {
  6176. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6177. WHERE c_id = ".$course_id." AND id = " . Database::escape_string($extra_info);
  6178. $result = Database::query($sql_doc);
  6179. $row = Database::fetch_array($result);
  6180. $explode = explode('.', $row['title']);
  6181. if (count($explode) > 1) {
  6182. for ($i = 0; $i < count($explode) - 1; $i++)
  6183. $item_title .= $explode[$i];
  6184. } else {
  6185. $item_title = $row['title'];
  6186. }
  6187. $item_title = str_replace('_', ' ', $item_title);
  6188. if (empty ($item_title)) {
  6189. $path_parts = pathinfo($row['path']);
  6190. $item_title = stripslashes($path_parts['filename']);
  6191. }
  6192. } else {
  6193. $item_title = '';
  6194. $item_description = '';
  6195. }
  6196. $return = '<legend>';
  6197. if ($id != 0 && is_array($extra_info))
  6198. $parent = $extra_info['parent_item_id'];
  6199. else
  6200. $parent = 0;
  6201. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6202. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6203. $result = Database::query($sql);
  6204. $arrLP = array ();
  6205. while ($row = Database :: fetch_array($result)) {
  6206. $arrLP[] = array (
  6207. 'id' => $row['id'],
  6208. 'item_type' => $row['item_type'],
  6209. 'title' => $row['title'],
  6210. 'path' => $row['path'],
  6211. 'description' => $row['description'],
  6212. 'parent_item_id' => $row['parent_item_id'],
  6213. 'previous_item_id' => $row['previous_item_id'],
  6214. 'next_item_id' => $row['next_item_id'],
  6215. 'display_order' => $row['display_order'],
  6216. 'max_score' => $row['max_score'],
  6217. 'min_score' => $row['min_score'],
  6218. 'mastery_score' => $row['mastery_score'],
  6219. 'prerequisite' => $row['prerequisite']
  6220. );
  6221. }
  6222. $this->tree_array($arrLP);
  6223. $arrLP = $this->arrMenu;
  6224. unset ($this->arrMenu);
  6225. if ($action == 'add') {
  6226. $return .= get_lang('CreateTheDocument');
  6227. } elseif ($action == 'move') {
  6228. $return .= get_lang('MoveTheCurrentDocument');
  6229. } else {
  6230. $return .= get_lang('EditTheCurrentDocument');
  6231. }
  6232. $return .= '</legend>';
  6233. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6234. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6235. }
  6236. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6237. $defaults['title'] = Security :: remove_XSS($item_title);
  6238. if (empty($item_title)) {
  6239. $defaults['title'] = Security::remove_XSS($item_title);
  6240. }
  6241. $defaults['description'] = $item_description;
  6242. $form->addElement('html', $return);
  6243. if ($action != 'move') {
  6244. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6245. $form->applyFilter('title', 'html_filter');
  6246. }
  6247. //$arrHide = array($id);
  6248. $arrHide[0]['value'] = $this->name;
  6249. $arrHide[0]['padding'] = 3;
  6250. for ($i = 0; $i < count($arrLP); $i++) {
  6251. if ($action != 'add') {
  6252. 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)) {
  6253. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6254. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6255. if ($parent == $arrLP[$i]['id']) {
  6256. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6257. }
  6258. }
  6259. } else {
  6260. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6261. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6262. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6263. if ($parent == $arrLP[$i]['id']) {
  6264. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6265. }
  6266. }
  6267. }
  6268. }
  6269. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6270. $my_count=0;
  6271. foreach ($arrHide as $key => $value) {
  6272. if ($my_count!=0) {
  6273. // The LP name is also the first section and is not in the same charset like the other sections.
  6274. $value['value'] = Security :: remove_XSS($value['value']);
  6275. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6276. } else {
  6277. $value['value'] = Security :: remove_XSS($value['value']);
  6278. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6279. }
  6280. $my_count++;
  6281. }
  6282. if (!empty($id)) {
  6283. $parent_select->setSelected($parent);
  6284. } else {
  6285. $parent_item_id = $_SESSION['parent_item_id'];
  6286. $parent_select->setSelected($parent_item_id);
  6287. }
  6288. if (is_array($arrLP)) {
  6289. reset($arrLP);
  6290. }
  6291. $arrHide = array ();
  6292. //POSITION
  6293. for ($i = 0; $i < count($arrLP); $i++) {
  6294. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6295. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6296. $s_selected_position = $arrLP[$i]['id'];
  6297. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6298. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6299. }
  6300. }
  6301. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6302. $position->addOption(get_lang('FirstPosition'), 0);
  6303. foreach ($arrHide as $key => $value) {
  6304. $padding = isset($value['padding']) ? $value['padding']: 0;
  6305. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6306. }
  6307. $position->setSelected($s_selected_position);
  6308. if (is_array($arrLP)) {
  6309. reset($arrLP);
  6310. }
  6311. if ($action != 'move') {
  6312. $id_prerequisite = 0;
  6313. if (is_array($arrLP)) {
  6314. foreach ($arrLP as $key => $value) {
  6315. if ($value['id'] == $id) {
  6316. $id_prerequisite = $value['prerequisite'];
  6317. break;
  6318. }
  6319. }
  6320. }
  6321. $arrHide = array ();
  6322. for ($i = 0; $i < count($arrLP); $i++) {
  6323. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6324. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6325. $s_selected_position = $arrLP[$i]['id'];
  6326. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6327. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6328. }
  6329. }
  6330. if (!$no_display_add) {
  6331. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6332. if (isset ($_POST['content']))
  6333. $content = stripslashes($_POST['content']);
  6334. elseif (is_array($extra_info)) {
  6335. //If it's an html document or a text file
  6336. if (!$no_display_edit_textarea) {
  6337. $content = $this->display_document($extra_info['path'], false, false);
  6338. }
  6339. } elseif (is_numeric($extra_info))
  6340. $content = $this->display_document($extra_info, false, false);
  6341. else
  6342. $content = '';
  6343. if (!$no_display_edit_textarea) {
  6344. // We need to calculate here some specific settings for the online editor.
  6345. // The calculated settings work for documents in the Documents tool
  6346. // (on the root or in subfolders).
  6347. // For documents in native scorm packages it is unclear whether the
  6348. // online editor should be activated or not.
  6349. // A new document, it is in the root of the repository.
  6350. $relative_path = '';
  6351. $relative_prefix = '';
  6352. if (is_array($extra_info) && $extra_info != 'new') {
  6353. // The document already exists. Whe have to determine its relative path towards the repository root.
  6354. $relative_path = explode('/', $extra_info['dir']);
  6355. $cnt = count($relative_path) - 2;
  6356. if ($cnt < 0) {
  6357. $cnt = 0;
  6358. }
  6359. $relative_prefix = str_repeat('../', $cnt);
  6360. $relative_path = array_slice($relative_path, 1, $cnt);
  6361. $relative_path = implode('/', $relative_path);
  6362. if (strlen($relative_path) > 0) {
  6363. $relative_path = $relative_path . '/';
  6364. }
  6365. } else {
  6366. global $_course;
  6367. $result = $this->generate_lp_folder($_course);
  6368. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6369. $relative_prefix = '../../';
  6370. }
  6371. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6372. 'Width' => '100%',
  6373. 'Height' => '500',
  6374. 'FullPage' => true,
  6375. 'CreateDocumentDir' => $relative_prefix,
  6376. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6377. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6378. );
  6379. if ($_GET['action'] == 'add_item') {
  6380. $class = 'add';
  6381. $text = get_lang('LPCreateDocument');
  6382. } else
  6383. if ($_GET['action'] == 'edit_item') {
  6384. $class = 'save';
  6385. $text = get_lang('SaveDocument');
  6386. }
  6387. $form->addElement('style_submit_button', 'submit_button', $text, 'class="' . $class . '"');
  6388. $renderer = $form->defaultRenderer();
  6389. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6390. $form->addElement('html', '<div>');
  6391. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6392. $form->addElement('html', '</div>');
  6393. $defaults['content_lp'] = $content;
  6394. }
  6395. } elseif (is_numeric($extra_info)) {
  6396. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6397. $return = $this->display_document($extra_info, true, true, true);
  6398. $form->addElement('html', $return);
  6399. }
  6400. }
  6401. }
  6402. if ($action == 'move') {
  6403. $form->addElement('hidden', 'title', $item_title);
  6404. $form->addElement('hidden', 'description', $item_description);
  6405. }
  6406. if (is_numeric($extra_info)) {
  6407. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'value="submit_button", class="save"');
  6408. $form->addElement('hidden', 'path', $extra_info);
  6409. } elseif (is_array($extra_info)) {
  6410. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6411. $form->addElement('hidden', 'path', $extra_info['path']);
  6412. }
  6413. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6414. $form->addElement('hidden', 'post_time', time());
  6415. $form->setDefaults($defaults);
  6416. return $form->return_form();
  6417. }
  6418. /**
  6419. * Return HTML form to add/edit a link item
  6420. * @param string Action (add/edit)
  6421. * @param integer Item ID if exists
  6422. * @param mixed Extra info
  6423. * @return string HTML form
  6424. */
  6425. public function display_link_form($action = 'add', $id = 0, $extra_info = '') {
  6426. $course_id = api_get_course_int_id();
  6427. global $charset;
  6428. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6429. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6430. if ($id != 0 && is_array($extra_info)) {
  6431. $item_title = stripslashes($extra_info['title']);
  6432. $item_description = stripslashes($extra_info['description']);
  6433. $item_url = stripslashes($extra_info['url']);
  6434. } elseif (is_numeric($extra_info)) {
  6435. $sql_link = "SELECT title, description, url FROM " . $tbl_link . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6436. $result = Database::query($sql_link);
  6437. $row = Database :: fetch_array($result);
  6438. $item_title = $row['title'];
  6439. $item_description = $row['description'];
  6440. $item_url = $row['url'];
  6441. } else {
  6442. $item_title = '';
  6443. $item_description = '';
  6444. $item_url = '';
  6445. }
  6446. $legend = '<legend>';
  6447. if ($id != 0 && is_array($extra_info))
  6448. $parent = $extra_info['parent_item_id'];
  6449. else
  6450. $parent = 0;
  6451. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6452. $result = Database::query($sql);
  6453. $arrLP = array ();
  6454. while ($row = Database :: fetch_array($result)) {
  6455. $arrLP[] = array (
  6456. 'id' => $row['id'],
  6457. 'item_type' => $row['item_type'],
  6458. 'title' => $row['title'],
  6459. 'path' => $row['path'],
  6460. 'description' => $row['description'],
  6461. 'parent_item_id' => $row['parent_item_id'],
  6462. 'previous_item_id' => $row['previous_item_id'],
  6463. 'next_item_id' => $row['next_item_id'],
  6464. 'display_order' => $row['display_order'],
  6465. 'max_score' => $row['max_score'],
  6466. 'min_score' => $row['min_score'],
  6467. 'mastery_score' => $row['mastery_score'],
  6468. 'prerequisite' => $row['prerequisite']
  6469. );
  6470. }
  6471. $this->tree_array($arrLP);
  6472. $arrLP = $this->arrMenu;
  6473. unset ($this->arrMenu);
  6474. if ($action == 'add')
  6475. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6476. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6477. else
  6478. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  6479. $legend .= '</legend>';
  6480. $return .= '<div class="sectioncomment">';
  6481. $return .= '<form method="POST">';
  6482. $return .= $legend;
  6483. $return .= '<table>';
  6484. if ($action != 'move') {
  6485. $return .= '<tr>';
  6486. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6487. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  6488. $return .= '</tr>';
  6489. }
  6490. $return .= '<tr>';
  6491. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6492. $return .= '<td class="input">';
  6493. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6494. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6495. $arrHide = array (
  6496. $id
  6497. );
  6498. $parent_item_id = $_SESSION['parent_item_id'];
  6499. for ($i = 0; $i < count($arrLP); $i++) {
  6500. if ($action != 'add') {
  6501. 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)) {
  6502. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6503. } else {
  6504. $arrHide[] = $arrLP[$i]['id'];
  6505. }
  6506. } else {
  6507. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6508. $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>';
  6509. }
  6510. }
  6511. if (is_array($arrLP)) {
  6512. reset($arrLP);
  6513. }
  6514. $return .= '</select>';
  6515. $return .= '</td>';
  6516. $return .= '</tr>';
  6517. $return .= '<tr>';
  6518. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6519. $return .= '<td class="input">';
  6520. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6521. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6522. for ($i = 0; $i < count($arrLP); $i++) {
  6523. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  6524. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6525. $selected = 'selected="selected" ';
  6526. elseif ($action == 'add')
  6527. $selected = 'selected="selected" ';
  6528. else
  6529. $selected = '';
  6530. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6531. }
  6532. }
  6533. $return .= '</select>';
  6534. $return .= '</td>';
  6535. $return .= '</tr>';
  6536. if ($action != 'move') {
  6537. $return .= '<tr>';
  6538. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  6539. $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>';
  6540. $return .= '</tr>';
  6541. $id_prerequisite = 0;
  6542. if (is_array($arrLP)) {
  6543. foreach ($arrLP as $key => $value) {
  6544. if ($value['id'] == $id) {
  6545. $id_prerequisite = $value['prerequisite'];
  6546. break;
  6547. }
  6548. }
  6549. }
  6550. $arrHide = array();
  6551. for ($i = 0; $i < count($arrLP); $i++) {
  6552. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6553. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6554. $s_selected_position = $arrLP[$i]['id'];
  6555. elseif ($action == 'add') $s_selected_position = 0;
  6556. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6557. }
  6558. }
  6559. $return .= '</tr>';
  6560. }
  6561. $return .= '<tr>';
  6562. if ($action == 'add') {
  6563. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  6564. } else {
  6565. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  6566. }
  6567. $return .= '</tr>';
  6568. $return .= '</table>';
  6569. if ($action == 'move') {
  6570. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6571. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6572. }
  6573. if (is_numeric($extra_info)) {
  6574. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6575. } elseif (is_array($extra_info)) {
  6576. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6577. }
  6578. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  6579. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6580. $return .= '</form>';
  6581. $return .= '</div>';
  6582. return $return;
  6583. }
  6584. /**
  6585. * Return HTML form to add/edit a student publication (work)
  6586. * @param string Action (add/edit)
  6587. * @param integer Item ID if already exists
  6588. * @param mixed Extra info (work ID if integer)
  6589. * @return string HTML form
  6590. */
  6591. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '') {
  6592. $course_id = api_get_course_int_id();
  6593. global $charset;
  6594. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6595. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  6596. if ($id != 0 && is_array($extra_info)) {
  6597. $item_title = stripslashes($extra_info['title']);
  6598. $item_description = stripslashes($extra_info['description']);
  6599. }
  6600. elseif (is_numeric($extra_info)) {
  6601. $sql_publication = "SELECT title, description FROM " . $tbl_publication . "
  6602. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6603. $result = Database::query($sql_publication);
  6604. $row = Database :: fetch_array($result);
  6605. $item_title = $row['title'];
  6606. } else {
  6607. $item_title = get_lang('Student_publication');
  6608. }
  6609. $legend = '<legend>';
  6610. if ($id != 0 && is_array($extra_info))
  6611. $parent = $extra_info['parent_item_id'];
  6612. else
  6613. $parent = 0;
  6614. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6615. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6616. $result = Database::query($sql);
  6617. $arrLP = array ();
  6618. while ($row = Database :: fetch_array($result)) {
  6619. $arrLP[] = array (
  6620. 'id' => $row['id'],
  6621. 'item_type' => $row['item_type'],
  6622. 'title' => $row['title'],
  6623. 'path' => $row['path'],
  6624. 'description' => $row['description'],
  6625. 'parent_item_id' => $row['parent_item_id'],
  6626. 'previous_item_id' => $row['previous_item_id'],
  6627. 'next_item_id' => $row['next_item_id'],
  6628. 'display_order' => $row['display_order'],
  6629. 'max_score' => $row['max_score'],
  6630. 'min_score' => $row['min_score'],
  6631. 'mastery_score' => $row['mastery_score'],
  6632. 'prerequisite' => $row['prerequisite']
  6633. );
  6634. }
  6635. $this->tree_array($arrLP);
  6636. $arrLP = $this->arrMenu;
  6637. unset ($this->arrMenu);
  6638. if ($action == 'add')
  6639. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  6640. elseif ($action == 'move') $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  6641. else
  6642. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  6643. $legend .= '</legend>';
  6644. $return .= '<div class="sectioncomment">';
  6645. $return .= '<form method="POST">';
  6646. $return .= $legend;
  6647. $return .= '<table class="lp_form">';
  6648. if ($action != 'move') {
  6649. $return .= '<tr>';
  6650. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6651. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6652. $return .= '</tr>';
  6653. }
  6654. $return .= '<tr>';
  6655. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6656. $return .= '<td class="input">';
  6657. $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">';
  6658. //$parent_item_id = $_SESSION['parent_item_id'];
  6659. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6660. $arrHide = array (
  6661. $id
  6662. );
  6663. for ($i = 0; $i < count($arrLP); $i++) {
  6664. if ($action != 'add') {
  6665. 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)) {
  6666. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6667. } else {
  6668. $arrHide[] = $arrLP[$i]['id'];
  6669. }
  6670. } else {
  6671. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6672. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6673. }
  6674. }
  6675. if (is_array($arrLP)) {
  6676. reset($arrLP);
  6677. }
  6678. $return .= "\t\t\t\t" . '</select>';
  6679. $return .= '</td>';
  6680. $return .= '</tr>';
  6681. $return .= '<tr>';
  6682. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6683. $return .= '<td class="input">';
  6684. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6685. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6686. for ($i = 0; $i < count($arrLP); $i++) {
  6687. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6688. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6689. $selected = 'selected="selected" ';
  6690. elseif ($action == 'add') $selected = 'selected="selected" ';
  6691. else
  6692. $selected = '';
  6693. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6694. }
  6695. }
  6696. $return .= "\t\t\t\t" . '</select>';
  6697. $return .= '</td>';
  6698. $return .= '</tr>';
  6699. if ($action != 'move') {
  6700. $id_prerequisite = 0;
  6701. if (is_array($arrLP)) {
  6702. foreach ($arrLP as $key => $value) {
  6703. if ($value['id'] == $id) {
  6704. $id_prerequisite = $value['prerequisite'];
  6705. break;
  6706. }
  6707. }
  6708. }
  6709. $arrHide = array ();
  6710. for ($i = 0; $i < count($arrLP); $i++) {
  6711. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6712. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6713. $s_selected_position = $arrLP[$i]['id'];
  6714. elseif ($action == 'add') $s_selected_position = 0;
  6715. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6716. }
  6717. }
  6718. // Commented the prerequisites, only visible in edit (work).
  6719. /*
  6720. $return .= '<tr>';
  6721. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6722. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6723. foreach($arrHide as $key => $value) {
  6724. if ($key == $s_selected_position && $action == 'add') {
  6725. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6726. }
  6727. elseif ($key == $id_prerequisite && $action == 'edit') {
  6728. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6729. }
  6730. else {
  6731. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6732. }
  6733. }
  6734. $return .= "</select></td>";
  6735. */
  6736. $return .= '</tr>';
  6737. }
  6738. $return .= '<tr>';
  6739. if ($action == 'add') {
  6740. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  6741. } else {
  6742. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  6743. }
  6744. $return .= '</tr>';
  6745. $return .= '</table>';
  6746. if ($action == 'move') {
  6747. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6748. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6749. }
  6750. if (is_numeric($extra_info)) {
  6751. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6752. } elseif (is_array($extra_info)) {
  6753. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6754. }
  6755. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  6756. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6757. $return .= '</form>';
  6758. $return .= '</div>';
  6759. return $return;
  6760. }
  6761. /**
  6762. * Displays the menu for manipulating a step
  6763. * @return string html
  6764. */
  6765. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT) {
  6766. $course_id = api_get_course_int_id();
  6767. $course_code = api_get_course_id();
  6768. global $charset, $_course;
  6769. $return = '<div class="actions">';
  6770. switch ($item_type) {
  6771. case 'dokeos_chapter' :
  6772. case 'chapter' :
  6773. // Commented the message cause should not show it.
  6774. //$lang = get_lang('TitleManipulateChapter');
  6775. break;
  6776. case 'dokeos_module' :
  6777. case 'module' :
  6778. // Commented the message cause should not show it.
  6779. //$lang = get_lang('TitleManipulateModule');
  6780. break;
  6781. case TOOL_DOCUMENT :
  6782. // Commented the message cause should not show it.
  6783. //$lang = get_lang('TitleManipulateDocument');
  6784. break;
  6785. case TOOL_LINK :
  6786. case 'link' :
  6787. // Commented the message cause should not show it.
  6788. //$lang = get_lang('TitleManipulateLink');
  6789. break;
  6790. case TOOL_QUIZ :
  6791. // Commented the message cause should not show it.
  6792. //$lang = get_lang('TitleManipulateQuiz');
  6793. break;
  6794. case TOOL_STUDENTPUBLICATION :
  6795. // Commented the message cause should not show it.
  6796. //$lang = get_lang('TitleManipulateStudentPublication');
  6797. break;
  6798. }
  6799. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6800. $item_id = intval($item_id);
  6801. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  6802. $result = Database::query($sql);
  6803. $row = Database::fetch_assoc($result);
  6804. $audio_player = null;
  6805. // We display an audio player if needed.
  6806. if (!empty($row['audio'])) {
  6807. $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>';
  6808. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  6809. $audio_player .= '<script>
  6810. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  6811. s1.addParam("allowscriptaccess","always");
  6812. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  6813. s1.write("container");
  6814. </script>';
  6815. }
  6816. $url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  6817. $return .= Display::url(Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item&path_item=' . $row['path']);
  6818. $return .= Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL), $url.'&action=move_item');
  6819. // Commented for now as prerequisites cannot be added to chapters.
  6820. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  6821. $return .= Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL), $url.'&action=edit_item_prereq');
  6822. }
  6823. $return .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), $url.'&action=delete_item');
  6824. if ($item_type == TOOL_HOTPOTATOES ) {
  6825. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6826. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6827. }
  6828. if ($item_type == TOOL_DOCUMENT ) {
  6829. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  6830. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  6831. }
  6832. $return .= '</div>';
  6833. if (!empty($audio_player)) {
  6834. $return .= '<br />'.$audio_player;
  6835. }
  6836. return $return;
  6837. }
  6838. /**
  6839. * Creates the javascript needed for filling up the checkboxes without page reload
  6840. *
  6841. * @return string
  6842. */
  6843. public function get_js_dropdown_array() {
  6844. $course_id = api_get_course_int_id();
  6845. $return = 'var child_name = new Array();' . "\n";
  6846. $return .= 'var child_value = new Array();' . "\n\n";
  6847. $return .= 'child_name[0] = new Array();' . "\n";
  6848. $return .= 'child_value[0] = new Array();' . "\n\n";
  6849. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6850. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  6851. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  6852. ORDER BY display_order ASC";
  6853. $res_zero = Database::query($sql_zero);
  6854. global $charset;
  6855. $i = 0;
  6856. while ($row_zero = Database :: fetch_array($res_zero)) {
  6857. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  6858. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  6859. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  6860. }
  6861. $return .= "\n";
  6862. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6863. $res = Database::query($sql);
  6864. while ($row = Database :: fetch_array($res)) {
  6865. $sql_parent = "
  6866. SELECT * FROM " . $tbl_lp_item . "
  6867. WHERE c_id = ".$course_id." AND parent_item_id = " . $row['id'] . "
  6868. ORDER BY display_order ASC";
  6869. $res_parent = Database::query($sql_parent);
  6870. $i = 0;
  6871. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  6872. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  6873. while ($row_parent = Database :: fetch_array($res_parent)) {
  6874. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  6875. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  6876. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  6877. }
  6878. $return .= "\n";
  6879. }
  6880. return $return;
  6881. }
  6882. /**
  6883. * Display the form to allow moving an item
  6884. * @param integer Item ID
  6885. * @return string HTML form
  6886. */
  6887. public function display_move_item($item_id) {
  6888. $course_id = api_get_course_int_id();
  6889. global $_course; //will disappear
  6890. global $charset;
  6891. $return = '';
  6892. if (is_numeric($item_id)) {
  6893. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6894. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6895. WHERE c_id = ".$course_id." AND id = " . $item_id;
  6896. $res = Database::query($sql);
  6897. $row = Database :: fetch_array($res);
  6898. switch ($row['item_type']) {
  6899. case 'dokeos_chapter' :
  6900. case 'dir' :
  6901. case 'asset' :
  6902. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6903. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  6904. break;
  6905. case 'dokeos_module' :
  6906. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6907. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  6908. break;
  6909. case TOOL_DOCUMENT :
  6910. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6911. $return .= $this->display_document_form('move', $item_id, $row);
  6912. break;
  6913. case TOOL_LINK :
  6914. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6915. $return .= $this->display_link_form('move', $item_id, $row);
  6916. break;
  6917. case TOOL_HOTPOTATOES :
  6918. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6919. $return .= $this->display_link_form('move', $item_id, $row);
  6920. break;
  6921. case TOOL_QUIZ :
  6922. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6923. $return .= $this->display_quiz_form('move', $item_id, $row);
  6924. break;
  6925. case TOOL_STUDENTPUBLICATION :
  6926. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6927. $return .= $this->display_student_publication_form('move', $item_id, $row);
  6928. break;
  6929. case TOOL_FORUM :
  6930. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6931. $return .= $this->display_forum_form('move', $item_id, $row);
  6932. break;
  6933. case TOOL_THREAD :
  6934. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6935. $return .= $this->display_forum_form('move', $item_id, $row);
  6936. break;
  6937. }
  6938. }
  6939. return $return;
  6940. }
  6941. /**
  6942. * Displays a basic form on the overview page for changing the item title and the item description.
  6943. * @param string $item_type
  6944. * @param string $title
  6945. * @param array $data
  6946. * @return string
  6947. */
  6948. public function display_item_small_form($item_type, $title = '', $data = array()) {
  6949. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  6950. $form = new FormValidator('small_form', 'post', $url);
  6951. $form->addElement('header', $title);
  6952. $form->addElement('text', 'title', get_lang('Title'));
  6953. $form->addElement('button', 'submit_button', get_lang('Save'));
  6954. $form->addElement('hidden', 'id', $data['id']);
  6955. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  6956. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  6957. $form->setDefaults(array('title' => $data['title']));
  6958. return $form->toHtml();
  6959. }
  6960. /**
  6961. * Return HTML form to allow prerequisites selection
  6962. * @param integer Item ID
  6963. * @return string HTML form
  6964. */
  6965. public function display_item_prerequisites_form($item_id) {
  6966. $course_id = api_get_course_int_id();
  6967. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6968. $item_id = intval($item_id);
  6969. /* Current prerequisite */
  6970. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = " . $item_id;
  6971. $result = Database::query($sql);
  6972. $row = Database::fetch_array($result);
  6973. $preq_id = $row['prerequisite'];
  6974. //$preq_mastery = $row['mastery_score'];
  6975. //$preq_max = $row['max_score'];
  6976. $return = $this->display_manipulate($item_id, TOOL_DOCUMENT);
  6977. $return = '<legend>';
  6978. $return .= get_lang('AddEditPrerequisites');
  6979. $return .= '</legend>';
  6980. $return .= '<div class="sectioncomment">';
  6981. $return .= '<form method="POST">';
  6982. $return .= '<table class="data_table" style="width:650px">';
  6983. $return .= '<tr>';
  6984. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  6985. $return .= '<th width="70" height="24">' . get_lang('Minimum') . '</th>';
  6986. $return .= '<th width="70" height="24">' . get_lang('Maximum') . '</th>';
  6987. $return .= '</tr>';
  6988. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  6989. $return .= '<tr >';
  6990. $return .= '<td colspan="3" class="radio">';
  6991. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  6992. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  6993. $return .= '</tr>';
  6994. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  6995. $result = Database::query($sql);
  6996. $arrLP = array ();
  6997. while ($row = Database :: fetch_array($result)) {
  6998. $arrLP[] = array (
  6999. 'id' => $row['id'],
  7000. 'item_type' => $row['item_type'],
  7001. 'title' => $row['title'],
  7002. 'ref' => $row['ref'],
  7003. 'description' => $row['description'],
  7004. 'parent_item_id' => $row['parent_item_id'],
  7005. 'previous_item_id' => $row['previous_item_id'],
  7006. 'next_item_id' => $row['next_item_id'],
  7007. 'max_score' => $row['max_score'],
  7008. 'min_score' => $row['min_score'],
  7009. 'mastery_score' => $row['mastery_score'],
  7010. 'prerequisite' => $row['prerequisite'],
  7011. 'next_item_id' => $row['next_item_id'],
  7012. 'display_order' => $row['display_order']
  7013. );
  7014. if ($row['ref'] == $preq_id) {
  7015. $preq_mastery = $row['mastery_score'];
  7016. $preq_max = $row['max_score'];
  7017. }
  7018. }
  7019. $this->tree_array($arrLP);
  7020. $arrLP = $this->arrMenu;
  7021. unset ($this->arrMenu);
  7022. for ($i = 0; $i < count($arrLP); $i++) {
  7023. if ($arrLP[$i]['id'] == $item_id)
  7024. break;
  7025. $return .= '<tr>';
  7026. $return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7027. $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'] . '" />';
  7028. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  7029. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7030. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7031. } else
  7032. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7033. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7034. } else {
  7035. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7036. }
  7037. $return .= '<label for="id' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</label>';
  7038. $return .= '</td>';
  7039. //$return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . ' />';
  7040. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  7041. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7042. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7043. $return .= '</td>';
  7044. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7045. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7046. $return .= '</td>';
  7047. }
  7048. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  7049. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7050. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7051. $return .= '</td>';
  7052. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7053. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7054. $return .= '</td>';
  7055. }
  7056. $return .= '</tr>';
  7057. }
  7058. $return .= '<tr>';
  7059. $return .= '</tr>';
  7060. $return .= '</table>';
  7061. $return .= '<div style="padding-top:3px;">';
  7062. $return .= '<button class="save" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7063. $return .= '</div>';
  7064. $return .= '</form>';
  7065. // $return .= '</div>';
  7066. return $return;
  7067. }
  7068. /**
  7069. * Return HTML list to allow prerequisites selection for lp
  7070. * @param integer Item ID
  7071. * @return string HTML form
  7072. */
  7073. public function display_lp_prerequisites_list() {
  7074. $course_id = api_get_course_int_id();
  7075. $lp_id = $this->lp_id;
  7076. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7077. // get current prerequisite
  7078. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7079. $result = Database::query($sql);
  7080. $row = Database :: fetch_array($result);
  7081. $preq_id = $row['prerequisite'];
  7082. $session_id = api_get_session_id();
  7083. $session_condition = api_get_session_condition($session_id);
  7084. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  7085. $rs = Database::query($sql);
  7086. $return = '';
  7087. $return .= '<select name="prerequisites" >';
  7088. $return .= '<option value="0">'.get_lang('None').'</option>';
  7089. if (Database::num_rows($rs) > 0) {
  7090. while ($row = Database::fetch_array($rs)) {
  7091. if ($row['id'] == $lp_id) {
  7092. continue;
  7093. }
  7094. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$preq_id)?' selected ' : '').'>'.$row['name'].'</option>';
  7095. }
  7096. }
  7097. $return .= '</select>';
  7098. return $return;
  7099. }
  7100. /**
  7101. * Creates a list with all the documents in it
  7102. * @return string
  7103. */
  7104. public function get_documents() {
  7105. $course_info = api_get_course_info();
  7106. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  7107. return $document_tree;
  7108. }
  7109. /**
  7110. * Creates a list with all the exercises (quiz) in it
  7111. * @return string
  7112. */
  7113. public function get_exercises() {
  7114. $course_id = api_get_course_int_id();
  7115. // New for hotpotatoes.
  7116. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7117. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7118. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7119. $session_id = api_get_session_id();
  7120. $condition_session = api_get_session_condition($session_id);
  7121. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  7122. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session ORDER BY id ASC";
  7123. $res_quiz = Database::query($sql_quiz);
  7124. $res_hot = Database::query($sql_hot);
  7125. $return = '<ul class="lp_resource">';
  7126. $return .= '<li class="lp_resource_element">';
  7127. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7128. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?lp_id=' . $this->lp_id . '">' . get_lang('NewExercise') . '</a>';
  7129. $return .= '</li>';
  7130. // Display hotpotatoes
  7131. while ($row_hot = Database :: fetch_array($res_hot)) {
  7132. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7133. $return .= '<a class="moved" href="#">';
  7134. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7135. $return .= '</a> ';
  7136. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7137. $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 . '">'.
  7138. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7139. $return .= '</li>';
  7140. }
  7141. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7142. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7143. $return .= '<a class="moved" href="#">';
  7144. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7145. $return .= '</a> ';
  7146. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7147. $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 . '">' .
  7148. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7149. '</a>';
  7150. $return .= '</li>';
  7151. }
  7152. $return .= '</ul>';
  7153. return $return;
  7154. }
  7155. /**
  7156. * Creates a list with all the links in it
  7157. * @return string
  7158. */
  7159. public function get_links() {
  7160. $course_id = api_get_course_int_id();
  7161. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7162. $session_id = api_get_session_id();
  7163. $condition_session = api_get_session_condition($session_id);
  7164. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  7165. $res_link = Database::query($sql_link);
  7166. $return = '<ul class="lp_resource">';
  7167. $return .= '<li class="lp_resource_element">';
  7168. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  7169. $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>';
  7170. $return .= '</li>';
  7171. $course_info = api_get_course_info();
  7172. while ($row_link = Database :: fetch_array($res_link)) {
  7173. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  7174. if ($item_visibility != 2) {
  7175. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  7176. $return .= '<a class="moved" href="#">';
  7177. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7178. $return .= '</a> ';
  7179. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  7180. $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 . '">'.
  7181. $row_link['title'].
  7182. '</a>';
  7183. $return .= '</li>';
  7184. }
  7185. }
  7186. $return .= '</ul>';
  7187. return $return;
  7188. }
  7189. /**
  7190. * Creates a list with all the student publications in it
  7191. * @return unknown
  7192. */
  7193. public function get_student_publications() {
  7194. $return = '<div class="lp_resource" >';
  7195. $return .= '<div class="lp_resource_element">';
  7196. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7197. $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>';
  7198. $return .= '</div>';
  7199. $return .= '</div>';
  7200. return $return;
  7201. }
  7202. /**
  7203. * Creates a list with all the forums in it
  7204. * @return string
  7205. */
  7206. public function get_forums() {
  7207. require_once '../forum/forumfunction.inc.php';
  7208. require_once '../forum/forumconfig.inc.php';
  7209. $a_forums = get_forums();
  7210. $return = '<ul class="lp_resource">';
  7211. //First add link
  7212. $return .= '<li class="lp_resource_element">';
  7213. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7214. $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>';
  7215. $return .= '</li>';
  7216. $return .= '<script>
  7217. function toggle_forum(forum_id){
  7218. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7219. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7220. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7221. } else {
  7222. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7223. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7224. }
  7225. }
  7226. </script>';
  7227. foreach ($a_forums as $forum) {
  7228. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7229. if (!empty($forum['forum_id'])) {
  7230. $return .= '<a class="moved" href="#">';
  7231. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7232. $return .= ' </a>';
  7233. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7234. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7235. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7236. </a>
  7237. <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>';
  7238. }
  7239. $return .= '</li>';
  7240. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7241. $a_threads = get_threads($forum['forum_id']);
  7242. if (is_array($a_threads)) {
  7243. foreach ($a_threads as $thread) {
  7244. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7245. $return .= '&nbsp;<a class="moved" href="#">';
  7246. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7247. $return .= ' </a>';
  7248. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7249. $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>';
  7250. $return .= '</li>';
  7251. }
  7252. }
  7253. $return .= '</div>';
  7254. }
  7255. $return .= '</ul>';
  7256. return $return;
  7257. }
  7258. /**
  7259. * // TODO: The output encoding should be equal to the system encoding.
  7260. *
  7261. * Exports the learning path as a SCORM package. This is the main function that
  7262. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7263. * whole thing and returns the zip.
  7264. *
  7265. * This method needs to be called in PHP5, as it will fail with non-adequate
  7266. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7267. * you need to call it on a learnpath object.
  7268. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7269. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7270. * path has been modified, it should use the generic method here below.
  7271. * @TODO link this function with the export_lp() function in the same class
  7272. * @param string Optional name of zip file. If none, title of learnpath is
  7273. * domesticated and trailed with ".zip"
  7274. * @return string Returns the zip package string, or null if error
  7275. */
  7276. public function scorm_export() {
  7277. global $_course;
  7278. $course_id = api_get_course_int_id();
  7279. // Remove memory and time limits as much as possible as this might be a long process...
  7280. if (function_exists('ini_set')) {
  7281. $mem = ini_get('memory_limit');
  7282. if (substr($mem, -1, 1) == 'M') {
  7283. $mem_num = substr($mem, 0, -1);
  7284. if ($mem_num < 128) {
  7285. ini_set('memory_limit', '128M');
  7286. }
  7287. } else {
  7288. ini_set('memory_limit', '128M');
  7289. }
  7290. ini_set('max_execution_time', 600);
  7291. }
  7292. // Create the zip handler (this will remain available throughout the method).
  7293. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7294. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7295. $temp_dir_short = uniqid();
  7296. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7297. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7298. $zip_folder = new PclZip($temp_zip_file);
  7299. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7300. $root_path = $main_path = api_get_path(SYS_PATH);
  7301. $files_cleanup = array();
  7302. // Place to temporarily stash the zipfiles.
  7303. // create the temp dir if it doesn't exist
  7304. // or do a cleanup befor creating the zipfile.
  7305. if (!is_dir($temp_zip_dir)) {
  7306. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7307. } else {
  7308. // Cleanup: Check the temp dir for old files and delete them.
  7309. $handle = opendir($temp_zip_dir);
  7310. while (false !== ($file = readdir($handle))) {
  7311. if ($file != '.' && $file != '..') {
  7312. unlink("$temp_zip_dir/$file");
  7313. }
  7314. }
  7315. closedir($handle);
  7316. }
  7317. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7318. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7319. // Remove the possible . at the end of the path.
  7320. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7321. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7322. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7323. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7324. }
  7325. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  7326. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7327. // Aggregation Model official document, secion "2.3 Content Packaging".
  7328. $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.
  7329. $root = $xmldoc->createElement('manifest');
  7330. $root->setAttribute('identifier', 'SingleCourseManifest');
  7331. $root->setAttribute('version', '1.1');
  7332. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7333. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7334. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7335. $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');
  7336. // Build mandatory sub-root container elements.
  7337. $metadata = $xmldoc->createElement('metadata');
  7338. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7339. $metadata->appendChild($md_schema);
  7340. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7341. $metadata->appendChild($md_schemaversion);
  7342. $root->appendChild($metadata);
  7343. $organizations = $xmldoc->createElement('organizations');
  7344. $resources = $xmldoc->createElement('resources');
  7345. // Build the only organization we will use in building our learnpaths.
  7346. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7347. $organization = $xmldoc->createElement('organization');
  7348. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7349. // To set the title of the SCORM entity (=organization), we take the name given
  7350. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7351. // learning path charset) as it is the encoding that defines how it is stored
  7352. // in the database. Then we convert it to HTML entities again as the "&" character
  7353. // alone is not authorized in XML (must be &amp;).
  7354. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7355. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7356. $organization->appendChild($org_title);
  7357. $folder_name = 'document';
  7358. // Removes the learning_path/scorm_folder path when exporting see #4841
  7359. $path_to_remove = null;
  7360. $result = $this->generate_lp_folder($_course);
  7361. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7362. $path_to_remove = 'document'.$result['dir'];
  7363. $path_to_replace = $folder_name.'/';
  7364. }
  7365. //Fixes chamilo scorm exports
  7366. if ($this->ref == 'chamilo_scorm_export') {
  7367. $path_to_remove = 'scorm/'.$this->path.'/document/';
  7368. }
  7369. // For each element, add it to the imsmanifest structure, then add it to the zip.
  7370. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  7371. $link_updates = array();
  7372. foreach ($this->items as $index => $item) {
  7373. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  7374. // Get included documents from this item.
  7375. if ($item->type == 'sco')
  7376. $inc_docs = $item->get_resources_from_source(null, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.'scorm/'.$this->path.'/'.$item->get_path());
  7377. else
  7378. $inc_docs = $item->get_resources_from_source();
  7379. // Give a child element <item> to the <organization> element.
  7380. $my_item_id = $item->get_id();
  7381. $my_item = $xmldoc->createElement('item');
  7382. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  7383. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  7384. $my_item->setAttribute('isvisible', 'true');
  7385. // Give a child element <title> to the <item> element.
  7386. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7387. $my_item->appendChild($my_title);
  7388. // Give a child element <adlcp:prerequisites> to the <item> element.
  7389. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  7390. $my_prereqs->setAttribute('type', 'aicc_script');
  7391. $my_item->appendChild($my_prereqs);
  7392. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7393. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  7394. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7395. //$xmldoc->createElement('adlcp:timelimitaction','');
  7396. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7397. //$xmldoc->createElement('adlcp:datafromlms','');
  7398. // Give a child element <adlcp:masteryscore> to the <item> element.
  7399. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7400. $my_item->appendChild($my_masteryscore);
  7401. // Attach this item to the organization element or hits parent if there is one.
  7402. if (!empty($item->parent) && $item->parent != 0) {
  7403. $children = $organization->childNodes;
  7404. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  7405. if (is_object($possible_parent)) {
  7406. $possible_parent->appendChild($my_item);
  7407. } else {
  7408. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  7409. }
  7410. } else {
  7411. if ($this->debug > 0) { error_log('No parent'); }
  7412. $organization->appendChild($my_item);
  7413. }
  7414. // Get the path of the file(s) from the course directory root.
  7415. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7416. if (!empty($path_to_remove)) {
  7417. //From docs
  7418. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  7419. //From quiz
  7420. if ($this->ref == 'chamilo_scorm_export') {
  7421. $path_to_remove = 'scorm/'.$this->path.'/';
  7422. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  7423. }
  7424. } else {
  7425. $my_xml_file_path = $my_file_path;
  7426. }
  7427. $my_sub_dir = dirname($my_file_path);
  7428. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7429. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7430. $my_xml_sub_dir = $my_sub_dir;
  7431. // Give a <resource> child to the <resources> element
  7432. $my_resource = $xmldoc->createElement('resource');
  7433. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7434. $my_resource->setAttribute('type', 'webcontent');
  7435. $my_resource->setAttribute('href', $my_xml_file_path);
  7436. // adlcp:scormtype can be either 'sco' or 'asset'.
  7437. if ($item->type == 'sco') {
  7438. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7439. } else {
  7440. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7441. }
  7442. // xml:base is the base directory to find the files declared in this resource.
  7443. $my_resource->setAttribute('xml:base', '');
  7444. // Give a <file> child to the <resource> element.
  7445. $my_file = $xmldoc->createElement('file');
  7446. $my_file->setAttribute('href', $my_xml_file_path);
  7447. $my_resource->appendChild($my_file);
  7448. // Dependency to other files - not yet supported.
  7449. $i = 1;
  7450. foreach ($inc_docs as $doc_info) {
  7451. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7452. $my_dep = $xmldoc->createElement('resource');
  7453. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7454. $my_dep->setAttribute('identifier', $res_id);
  7455. $my_dep->setAttribute('type', 'webcontent');
  7456. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7457. $my_dep_file = $xmldoc->createElement('file');
  7458. // Check type of URL.
  7459. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7460. if ($doc_info[1] == 'remote') {
  7461. // Remote file. Save url as is.
  7462. $my_dep_file->setAttribute('href', $doc_info[0]);
  7463. $my_dep->setAttribute('xml:base', '');
  7464. } elseif ($doc_info[1] == 'local') {
  7465. switch ($doc_info[2]) {
  7466. case 'url': // Local URL - save path as url for now, don't zip file.
  7467. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7468. $current_dir = dirname($abs_path);
  7469. $current_dir = str_replace('\\', '/', $current_dir);
  7470. $file_path = realpath($abs_path);
  7471. $file_path = str_replace('\\', '/', $file_path);
  7472. $my_dep_file->setAttribute('href', $file_path);
  7473. $my_dep->setAttribute('xml:base', '');
  7474. if (strstr($file_path, $main_path) !== false) {
  7475. // The calculated real path is really inside Chamilo's root path.
  7476. // Reduce file path to what's under the DocumentRoot.
  7477. $file_path = substr($file_path, strlen($root_path) - 1);
  7478. //echo $file_path;echo '<br /><br />';
  7479. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  7480. $zip_files_abs[] = $file_path;
  7481. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7482. $my_dep_file->setAttribute('href', $file_path);
  7483. $my_dep->setAttribute('xml:base', '');
  7484. } elseif (empty($file_path)) {
  7485. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7486. if (strpos($document_root, -1) == '/') {
  7487. $document_root = substr(0, -1, $document_root);
  7488. }*/
  7489. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7490. $file_path = str_replace('//', '/', $file_path);
  7491. if (file_exists($file_path)) {
  7492. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7493. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7494. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7495. $my_dep_file->setAttribute('href', $file_path);
  7496. $my_dep->setAttribute('xml:base', '');
  7497. }
  7498. }
  7499. break;
  7500. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7501. $my_dep_file->setAttribute('href', $doc_info[0]);
  7502. $my_dep->setAttribute('xml:base', '');
  7503. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  7504. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  7505. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  7506. $abs_img_path_without_subdir = $doc_info[0];
  7507. $relp = api_get_path(REL_PATH); // The url-append config param.
  7508. $pos = strpos($abs_img_path_without_subdir, $relp);
  7509. if ($pos === 0) {
  7510. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  7511. }
  7512. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  7513. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  7514. $file_path = str_replace('\\', '/', $file_path);
  7515. $file_path = str_replace('//', '/', $file_path);
  7516. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  7517. // Prepare the current directory path (until just under 'document') with a trailing slash.
  7518. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  7519. // Check if the current document is in that path.
  7520. if (strstr($file_path, $cur_path) !== false) {
  7521. // The document is in that path, now get the relative path
  7522. // to the containing document.
  7523. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  7524. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  7525. $relative_path = '';
  7526. if (strstr($file_path, $cur_path) !== false) {
  7527. $relative_path = substr($file_path, strlen($orig_file_path));
  7528. $file_path = substr($file_path, strlen($cur_path));
  7529. } else {
  7530. // This case is still a problem as it's difficult to calculate a relative path easily
  7531. // might still generate wrong links.
  7532. //$file_path = substr($file_path,strlen($cur_path));
  7533. // Calculate the directory path to the current file (without trailing slash).
  7534. $my_relative_path = dirname($file_path);
  7535. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  7536. $my_relative_file = basename($file_path);
  7537. // Calculate the directory path to the containing file (without trailing slash).
  7538. $my_orig_file_path = substr($orig_file_path, 0, -1);
  7539. $dotdot = '';
  7540. $subdir = '';
  7541. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  7542. $my_relative_path2 = dirname($my_relative_path);
  7543. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  7544. $my_orig_file_path = dirname($my_orig_file_path);
  7545. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  7546. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  7547. $dotdot += '../';
  7548. $my_relative_path = $my_relative_path2;
  7549. }
  7550. $relative_path = $dotdot.$subdir.$my_relative_file;
  7551. }
  7552. // Put the current document in the zip (this array is the array
  7553. // that will manage documents already in the course folder - relative).
  7554. $zip_files[] = $file_path;
  7555. // Update the links to the current document in the containing document (make them relative).
  7556. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  7557. $my_dep_file->setAttribute('href', $file_path);
  7558. $my_dep->setAttribute('xml:base', '');
  7559. } elseif (strstr($file_path, $main_path) !== false) {
  7560. // The calculated real path is really inside Chamilo's root path.
  7561. // Reduce file path to what's under the DocumentRoot.
  7562. $file_path = substr($file_path, strlen($root_path));
  7563. //echo $file_path;echo '<br /><br />';
  7564. //error_log('Reduced path: '.$file_path, 0);
  7565. $zip_files_abs[] = $file_path;
  7566. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7567. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7568. $my_dep->setAttribute('xml:base', '');
  7569. } elseif (empty($file_path)) {
  7570. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7571. if(strpos($document_root,-1) == '/') {
  7572. $document_root = substr(0, -1, $document_root);
  7573. }*/
  7574. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7575. $file_path = str_replace('//', '/', $file_path);
  7576. if (file_exists($file_path)) {
  7577. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7578. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7579. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7580. $my_dep_file->setAttribute('href','document/'.$file_path);
  7581. $my_dep->setAttribute('xml:base', '');
  7582. }
  7583. }
  7584. break;
  7585. 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
  7586. if (substr($doc_info[0], 0, 2) == '..') {
  7587. // Relative path going up.
  7588. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7589. $current_dir = str_replace('\\', '/', $current_dir);
  7590. $file_path = realpath($current_dir.$doc_info[0]);
  7591. $file_path = str_replace('\\', '/', $file_path);
  7592. //error_log($file_path.' <-> '.$main_path,0);
  7593. if (strstr($file_path, $main_path) !== false) {
  7594. // The calculated real path is really inside Chamilo's root path.
  7595. // Reduce file path to what's under the DocumentRoot.
  7596. $file_path = substr($file_path, strlen($root_path));
  7597. //error_log('Reduced path: '.$file_path, 0);
  7598. $zip_files_abs[] = $file_path;
  7599. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7600. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7601. $my_dep->setAttribute('xml:base', '');
  7602. }
  7603. } else {
  7604. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7605. $my_dep_file->setAttribute('href', $doc_info[0]);
  7606. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7607. }
  7608. break;
  7609. default:
  7610. $my_dep_file->setAttribute('href', $doc_info[0]);
  7611. $my_dep->setAttribute('xml:base', '');
  7612. break;
  7613. }
  7614. }
  7615. $my_dep->appendChild($my_dep_file);
  7616. $resources->appendChild($my_dep);
  7617. $dependency = $xmldoc->createElement('dependency');
  7618. $dependency->setAttribute('identifierref', $res_id);
  7619. $my_resource->appendChild($dependency);
  7620. $i++;
  7621. }
  7622. //$my_dependency = $xmldoc->createElement('dependency');
  7623. //$my_dependency->setAttribute('identifierref', '');
  7624. $resources->appendChild($my_resource);
  7625. $zip_files[] = $my_file_path;
  7626. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  7627. } else {
  7628. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  7629. switch ($item->type) {
  7630. case TOOL_LINK:
  7631. $my_item = $xmldoc->createElement('item');
  7632. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7633. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7634. $my_item->setAttribute('isvisible', 'true');
  7635. // Give a child element <title> to the <item> element.
  7636. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7637. $my_item->appendChild($my_title);
  7638. // Give a child element <adlcp:prerequisites> to the <item> element.
  7639. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7640. $my_prereqs->setAttribute('type', 'aicc_script');
  7641. $my_item->appendChild($my_prereqs);
  7642. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7643. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  7644. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7645. //$xmldoc->createElement('adlcp:timelimitaction', '');
  7646. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7647. //$xmldoc->createElement('adlcp:datafromlms', '');
  7648. // Give a child element <adlcp:masteryscore> to the <item> element.
  7649. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7650. $my_item->appendChild($my_masteryscore);
  7651. // Attach this item to the organization element or its parent if there is one.
  7652. if (!empty($item->parent) && $item->parent != 0) {
  7653. $children = $organization->childNodes;
  7654. for ($i = 0; $i < $children->length; $i++) {
  7655. $item_temp = $children->item($i);
  7656. if ($item_temp -> nodeName == 'item') {
  7657. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7658. $item_temp -> appendChild($my_item);
  7659. }
  7660. }
  7661. }
  7662. } else {
  7663. $organization->appendChild($my_item);
  7664. }
  7665. $my_file_path = 'link_'.$item->get_id().'.html';
  7666. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  7667. $rs = Database::query($sql);
  7668. if ($link = Database :: fetch_array($rs)) {
  7669. $url = $link['url'];
  7670. $title = stripslashes($link['title']);
  7671. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  7672. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7673. $my_xml_file_path = $my_file_path;
  7674. $my_sub_dir = dirname($my_file_path);
  7675. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7676. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7677. $my_xml_sub_dir = $my_sub_dir;
  7678. // Give a <resource> child to the <resources> element.
  7679. $my_resource = $xmldoc->createElement('resource');
  7680. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7681. $my_resource->setAttribute('type', 'webcontent');
  7682. $my_resource->setAttribute('href', $my_xml_file_path);
  7683. // adlcp:scormtype can be either 'sco' or 'asset'.
  7684. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7685. // xml:base is the base directory to find the files declared in this resource.
  7686. $my_resource->setAttribute('xml:base', '');
  7687. // give a <file> child to the <resource> element.
  7688. $my_file = $xmldoc->createElement('file');
  7689. $my_file->setAttribute('href', $my_xml_file_path);
  7690. $my_resource->appendChild($my_file);
  7691. $resources->appendChild($my_resource);
  7692. }
  7693. break;
  7694. case TOOL_QUIZ:
  7695. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  7696. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  7697. $exe = new Exercise();
  7698. $exe->read($exe_id);
  7699. $my_item = $xmldoc->createElement('item');
  7700. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  7701. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  7702. $my_item->setAttribute('isvisible', 'true');
  7703. // Give a child element <title> to the <item> element.
  7704. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7705. $my_item->appendChild($my_title);
  7706. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  7707. //$my_item->appendChild($my_max_score);
  7708. // Give a child element <adlcp:prerequisites> to the <item> element.
  7709. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  7710. $my_prereqs->setAttribute('type','aicc_script');
  7711. $my_item->appendChild($my_prereqs);
  7712. // Give a child element <adlcp:masteryscore> to the <item> element.
  7713. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7714. $my_item->appendChild($my_masteryscore);
  7715. // Attach this item to the organization element or hits parent if there is one.
  7716. if (!empty($item->parent) && $item->parent != 0) {
  7717. $children = $organization->childNodes;
  7718. for ($i = 0; $i < $children->length; $i++) {
  7719. $item_temp = $children->item($i);
  7720. if ($item_temp -> nodeName == 'item') {
  7721. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  7722. $item_temp -> appendChild($my_item);
  7723. }
  7724. }
  7725. }
  7726. } else {
  7727. $organization->appendChild($my_item);
  7728. }
  7729. // Include export scripts.
  7730. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  7731. // Get the path of the file(s) from the course directory root
  7732. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  7733. $my_file_path = 'quiz_'.$item->get_id().'.html';
  7734. // Write the contents of the exported exercise into a (big) html file
  7735. // to later pack it into the exported SCORM. The file will be removed afterwards.
  7736. $contents = export_exercise($exe_id, true);
  7737. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  7738. $res = file_put_contents($tmp_file_path, $contents);
  7739. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  7740. $files_cleanup[] = $tmp_file_path;
  7741. //error_log($tmp_path); die();
  7742. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  7743. $my_xml_file_path = $my_file_path;
  7744. $my_sub_dir = dirname($my_file_path);
  7745. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7746. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  7747. $my_xml_sub_dir = $my_sub_dir;
  7748. // Give a <resource> child to the <resources> element.
  7749. $my_resource = $xmldoc->createElement('resource');
  7750. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7751. $my_resource->setAttribute('type', 'webcontent');
  7752. $my_resource->setAttribute('href', $my_xml_file_path);
  7753. // adlcp:scormtype can be either 'sco' or 'asset'.
  7754. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  7755. // xml:base is the base directory to find the files declared in this resource.
  7756. $my_resource->setAttribute('xml:base', '');
  7757. // Give a <file> child to the <resource> element.
  7758. $my_file = $xmldoc->createElement('file');
  7759. $my_file->setAttribute('href', $my_xml_file_path);
  7760. $my_resource->appendChild($my_file);
  7761. // Get included docs.
  7762. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  7763. // Dependency to other files - not yet supported.
  7764. $i = 1;
  7765. foreach ($inc_docs as $doc_info) {
  7766. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  7767. $my_dep = $xmldoc->createElement('resource');
  7768. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  7769. $my_dep->setAttribute('identifier', $res_id);
  7770. $my_dep->setAttribute('type', 'webcontent');
  7771. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  7772. $my_dep_file = $xmldoc->createElement('file');
  7773. // Check type of URL.
  7774. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  7775. if ($doc_info[1] == 'remote') {
  7776. // Remote file. Save url as is.
  7777. $my_dep_file->setAttribute('href', $doc_info[0]);
  7778. $my_dep->setAttribute('xml:base', '');
  7779. } elseif ($doc_info[1] == 'local') {
  7780. switch ($doc_info[2]) {
  7781. case 'url': // Local URL - save path as url for now, don't zip file.
  7782. // Save file but as local file (retrieve from URL).
  7783. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  7784. $current_dir = dirname($abs_path);
  7785. $current_dir = str_replace('\\', '/', $current_dir);
  7786. $file_path = realpath($abs_path);
  7787. $file_path = str_replace('\\', '/', $file_path);
  7788. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7789. $my_dep->setAttribute('xml:base', '');
  7790. if (strstr($file_path, $main_path) !== false) {
  7791. // The calculated real path is really inside the chamilo root path.
  7792. // Reduce file path to what's under the DocumentRoot.
  7793. $file_path = substr($file_path, strlen($root_path));
  7794. //echo $file_path;echo '<br /><br />';
  7795. //error_log('Reduced path: '.$file_path, 0);
  7796. $zip_files_abs[] = $file_path;
  7797. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7798. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7799. $my_dep->setAttribute('xml:base', '');
  7800. } elseif (empty($file_path)) {
  7801. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  7802. if (strpos($document_root,-1) == '/') {
  7803. $document_root = substr(0, -1, $document_root);
  7804. }*/
  7805. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  7806. $file_path = str_replace('//', '/', $file_path);
  7807. if (file_exists($file_path)) {
  7808. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  7809. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7810. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  7811. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7812. $my_dep->setAttribute('xml:base', '');
  7813. }
  7814. }
  7815. break;
  7816. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  7817. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7818. $current_dir = str_replace('\\', '/', $current_dir);
  7819. $file_path = realpath($doc_info[0]);
  7820. $file_path = str_replace('\\', '/', $file_path);
  7821. $my_dep_file->setAttribute('href', $file_path);
  7822. $my_dep->setAttribute('xml:base', '');
  7823. if (strstr($file_path,$main_path) !== false) {
  7824. // The calculated real path is really inside the chamilo root path.
  7825. // Reduce file path to what's under the DocumentRoot.
  7826. $file_path = substr($file_path, strlen($root_path));
  7827. //echo $file_path;echo '<br /><br />';
  7828. //error_log('Reduced path: '.$file_path, 0);
  7829. $zip_files_abs[] = $file_path;
  7830. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7831. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7832. $my_dep->setAttribute('xml:base', '');
  7833. } elseif (empty($file_path)) {
  7834. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  7835. if (strpos($document_root,-1) == '/') {
  7836. $document_root = substr(0, -1, $document_root);
  7837. }*/
  7838. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  7839. $file_path = str_replace('//', '/', $file_path);
  7840. if (file_exists($file_path)) {
  7841. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  7842. $zip_files[] = $my_sub_dir.'/'.$file_path;
  7843. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  7844. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7845. $my_dep->setAttribute('xml:base', '');
  7846. }
  7847. }
  7848. break;
  7849. 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
  7850. if (substr($doc_info[0], 0, 2) == '..') {
  7851. // Relative path going up.
  7852. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  7853. $current_dir = str_replace('\\', '/', $current_dir);
  7854. $file_path = realpath($current_dir.$doc_info[0]);
  7855. $file_path = str_replace('\\', '/', $file_path);
  7856. //error_log($file_path.' <-> '.$main_path, 0);
  7857. if (strstr($file_path, $main_path) !== false) {
  7858. // The calculated real path is really inside Chamilo's root path.
  7859. // Reduce file path to what's under the DocumentRoot.
  7860. $file_path = substr($file_path, strlen($root_path));
  7861. $file_path_dest = $file_path;
  7862. // File path is courses/CHAMILO/document/....
  7863. $info_file_path = explode('/', $file_path);
  7864. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  7865. $file_path_dest = 'document/'.$file_path;
  7866. }
  7867. //error_log('Reduced path: '.$file_path, 0);
  7868. $zip_files_abs[] = $file_path;
  7869. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  7870. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  7871. $my_dep->setAttribute('xml:base', '');
  7872. }
  7873. } else {
  7874. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  7875. $my_dep_file->setAttribute('href', $doc_info[0]);
  7876. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  7877. }
  7878. break;
  7879. default:
  7880. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  7881. $my_dep->setAttribute('xml:base', '');
  7882. break;
  7883. }
  7884. }
  7885. $my_dep->appendChild($my_dep_file);
  7886. $resources->appendChild($my_dep);
  7887. $dependency = $xmldoc->createElement('dependency');
  7888. $dependency->setAttribute('identifierref', $res_id);
  7889. $my_resource->appendChild($dependency);
  7890. $i++;
  7891. }
  7892. $resources->appendChild($my_resource);
  7893. $zip_files[] = $my_file_path;
  7894. break;
  7895. default:
  7896. // Get the path of the file(s) from the course directory root
  7897. $my_file_path = 'non_exportable.html';
  7898. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  7899. $my_xml_file_path = $my_file_path;
  7900. $my_sub_dir = dirname($my_file_path);
  7901. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  7902. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  7903. $my_xml_sub_dir = $my_sub_dir;
  7904. // Give a <resource> child to the <resources> element.
  7905. $my_resource = $xmldoc->createElement('resource');
  7906. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  7907. $my_resource->setAttribute('type', 'webcontent');
  7908. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  7909. // adlcp:scormtype can be either 'sco' or 'asset'.
  7910. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  7911. // xml:base is the base directory to find the files declared in this resource.
  7912. $my_resource->setAttribute('xml:base', '');
  7913. // Give a <file> child to the <resource> element.
  7914. $my_file = $xmldoc->createElement('file');
  7915. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  7916. $my_resource->appendChild($my_file);
  7917. $resources->appendChild($my_resource);
  7918. break;
  7919. }
  7920. }
  7921. }
  7922. $organizations->appendChild($organization);
  7923. $root->appendChild($organizations);
  7924. $root->appendChild($resources);
  7925. $xmldoc->appendChild($root);
  7926. // TODO: Add a readme file here, with a short description and a link to the Reload player
  7927. // then add the file to the zip, then destroy the file (this is done automatically).
  7928. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  7929. //error_log(print_r($zip_files,true), 0);
  7930. foreach ($zip_files as $file_path) {
  7931. if (empty($file_path)) { continue; }
  7932. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  7933. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  7934. $this->create_path($dest_file);
  7935. //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);
  7936. //echo $main_path.$file_path.'<br />';
  7937. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  7938. // Check if the file needs a link update.
  7939. if (in_array($file_path, array_keys($link_updates))) {
  7940. $string = file_get_contents($dest_file);
  7941. unlink($dest_file);
  7942. foreach ($link_updates[$file_path] as $old_new) {
  7943. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  7944. // This is an ugly hack that allows .flv files to be found by the flv player that
  7945. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  7946. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  7947. // ../../.. to return from inc/lib/flv_player to the document/main path.
  7948. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  7949. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  7950. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  7951. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  7952. }
  7953. //Fix to avoid problems with default_course_document
  7954. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  7955. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  7956. } else {
  7957. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  7958. $new_dest = $old_new['dest'];
  7959. }
  7960. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  7961. $string = str_replace($old_new['orig'], $new_dest, $string);
  7962. //Add files inside the HTMLs
  7963. $new_path = str_replace('/courses/', '', $old_new['orig']);
  7964. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  7965. if (file_exists($sys_course_path.$new_path)) {
  7966. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  7967. }
  7968. }
  7969. file_put_contents($dest_file, $string);
  7970. }
  7971. }
  7972. foreach ($zip_files_abs as $file_path) {
  7973. if (empty($file_path)) { continue; }
  7974. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  7975. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) { continue; }
  7976. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  7977. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  7978. $this->create_path($dest_file);
  7979. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  7980. //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);
  7981. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  7982. copy($main_path.$file_path, $dest_file);
  7983. // Check if the file needs a link update.
  7984. if (in_array($file_path, array_keys($link_updates))) {
  7985. $string = file_get_contents($dest_file);
  7986. unlink($dest_file);
  7987. foreach ($link_updates[$file_path] as $old_new) {
  7988. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  7989. // This is an ugly hack that allows .flv files to be found by the flv player that
  7990. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  7991. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  7992. // ../../.. to return from inc/lib/flv_player to the document/main path.
  7993. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  7994. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  7995. }
  7996. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  7997. }
  7998. file_put_contents($dest_file, $string);
  7999. }
  8000. }
  8001. if (is_array($links_to_create)) {
  8002. foreach ($links_to_create as $file => $link) {
  8003. $file_content = '<!DOCTYPE html>
  8004. <head>
  8005. <meta charset="'.api_get_language_isocode().'" />
  8006. <title>'.$link['title'].'</title>
  8007. </head>
  8008. <body dir="'.api_get_text_direction().'">
  8009. <div style="text-align:center">
  8010. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8011. </body>
  8012. </html>';
  8013. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8014. }
  8015. }
  8016. // Add non exportable message explanation.
  8017. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8018. $file_content = '<!DOCTYPE html>
  8019. <head>
  8020. <meta charset="'.api_get_language_isocode().'" />
  8021. <title>'.$lang_not_exportable.'</title>
  8022. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8023. </head>
  8024. <body dir="'.api_get_text_direction().'">';
  8025. $file_content .=
  8026. <<<EOD
  8027. <style>
  8028. .error-message {
  8029. font-family: arial, verdana, helvetica, sans-serif;
  8030. border-width: 1px;
  8031. border-style: solid;
  8032. left: 50%;
  8033. margin: 10px auto;
  8034. min-height: 30px;
  8035. padding: 5px;
  8036. right: 50%;
  8037. width: 500px;
  8038. background-color: #FFD1D1;
  8039. border-color: #FF0000;
  8040. color: #000;
  8041. }
  8042. </style>
  8043. <body>
  8044. <div class="error-message">
  8045. $lang_not_exportable
  8046. </div>
  8047. </body>
  8048. </html>
  8049. EOD;
  8050. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8051. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8052. }
  8053. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8054. // Add the extra files that go along with a SCORM package.
  8055. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8056. $extra_files = scandir($main_code_path);
  8057. foreach ($extra_files as $extra_file) {
  8058. if (strpos($extra_file, '.') === 0)
  8059. continue;
  8060. else {
  8061. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8062. $this->create_path($dest_file);
  8063. copy($main_code_path.$extra_file, $dest_file);
  8064. }
  8065. }
  8066. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8067. $manifest = @$xmldoc->saveXML();
  8068. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8069. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8070. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8071. // Clean possible temporary files.
  8072. foreach ($files_cleanup as $file) {
  8073. $res = unlink($file);
  8074. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8075. }
  8076. // Send file to client.
  8077. //$name = 'scorm_export_'.$this->lp_id.'.zip';
  8078. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  8079. $name = replace_dangerous_char($this->get_name()).'.zip';
  8080. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8081. }
  8082. public function scorm_export_to_pdf($lp_id) {
  8083. $lp_id = intval($lp_id);
  8084. $files_to_export = array();
  8085. $course_data = api_get_course_info($this->cc);
  8086. if (!empty($course_data)) {
  8087. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8088. $list = self::get_flat_ordered_items_list($lp_id);
  8089. if (!empty($list)) {
  8090. foreach ($list as $item_id) {
  8091. $item = $this->items[$item_id];
  8092. switch ($item->type) {
  8093. case 'document':
  8094. //Getting documents from a LP with chamilo documents
  8095. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8096. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8097. if (file_exists($file_path)) {
  8098. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8099. }
  8100. break;
  8101. case 'asset': //commes from a scorm package generated by chamilo
  8102. case 'sco':
  8103. $file_path = $scorm_path.'/'.$item->path;
  8104. if (file_exists($file_path)) {
  8105. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8106. }
  8107. break;
  8108. case 'dokeos_chapter':
  8109. case 'dir':
  8110. case 'chapter':
  8111. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8112. break;
  8113. }
  8114. }
  8115. }
  8116. $pdf = new PDF();
  8117. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8118. return $result;
  8119. }
  8120. return false;
  8121. }
  8122. /**
  8123. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  8124. * if it doesn't exist
  8125. */
  8126. public function create_path($path) {
  8127. $path_bits = split('/', dirname($path));
  8128. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8129. $path_built = IS_WINDOWS_OS ? '' : '/';
  8130. foreach ($path_bits as $bit) {
  8131. if (!empty ($bit)) {
  8132. $new_path = $path_built . $bit;
  8133. if (is_dir($new_path)) {
  8134. $path_built = $new_path . '/';
  8135. } else {
  8136. mkdir($new_path, api_get_permissions_for_new_directories());
  8137. $path_built = $new_path . '/';
  8138. }
  8139. }
  8140. }
  8141. }
  8142. /**
  8143. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8144. * @return boolean The results of the unlink function, or false if there was no image to start with
  8145. */
  8146. public function delete_lp_image() {
  8147. $img = $this->get_preview_image();
  8148. if ($img != '') {
  8149. $del_file = api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/upload/learning_path/images/' . $img;
  8150. $this->set_preview_image('');
  8151. return @ unlink($del_file);
  8152. } else {
  8153. return false;
  8154. }
  8155. }
  8156. /**
  8157. * Uploads an author image to the upload/learning_path/images directory
  8158. * @param array The image array, coming from the $_FILES superglobal
  8159. * @return boolean True on success, false on error
  8160. */
  8161. public function upload_image($image_array) {
  8162. $image_moved = false;
  8163. if (!empty ($image_array['name'])) {
  8164. $upload_ok = process_uploaded_file($image_array);
  8165. $has_attachment = true;
  8166. } else {
  8167. $image_moved = true;
  8168. }
  8169. if ($upload_ok) {
  8170. if ($has_attachment) {
  8171. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8172. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8173. $updir = $sys_course_path . $courseDir;
  8174. // Try to add an extension to the file if it hasn't one.
  8175. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8176. if (!filter_extension($new_file_name)) {
  8177. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8178. $image_moved = false;
  8179. } else {
  8180. $file_extension = explode('.', $image_array['name']);
  8181. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8182. $new_file_name = uniqid('') . '.' . $file_extension;
  8183. $new_path = $updir . '/' . $new_file_name;
  8184. // Resize the image.
  8185. $temp = new Image($image_array['tmp_name']);
  8186. $picture_infos = $temp->get_image_info();
  8187. if ($picture_infos['width'] > 104) {
  8188. $thumbwidth = 104;
  8189. } else {
  8190. $thumbwidth = $picture_infos['width'];
  8191. }
  8192. if ($picture_infos['height'] > 96) {
  8193. $new_height = 96;
  8194. } else {
  8195. $new_height = $picture_infos['height'];
  8196. }
  8197. $temp->resize($thumbwidth, $new_height, 0);
  8198. $result = $temp->send_image($new_path);
  8199. // Storing the image filename.
  8200. if ($result) {
  8201. $image_moved = true;
  8202. $this->set_preview_image($new_file_name);
  8203. return true;
  8204. }
  8205. }
  8206. }
  8207. }
  8208. return false;
  8209. }
  8210. public function set_autolunch($lp_id, $status) {
  8211. $course_id = api_get_course_int_id();
  8212. $lp_id = intval($lp_id);
  8213. $status = intval($status);
  8214. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8215. //Setting everything to autolunch = 0
  8216. $attributes['autolunch'] = 0;
  8217. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8218. Database::update($lp_table, $attributes, $where);
  8219. if ($status == 1) {
  8220. //Setting my lp_id to autolunch = 1
  8221. $attributes['autolunch'] = 1;
  8222. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8223. Database::update($lp_table, $attributes, $where );
  8224. }
  8225. }
  8226. /**
  8227. * Gets previous_item_id for the next element of the lp_item table
  8228. * @author Isaac flores paz
  8229. * @return integer Previous item ID
  8230. */
  8231. function select_previous_item_id() {
  8232. $course_id = api_get_course_int_id();
  8233. if ($this->debug > 0) {
  8234. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8235. }
  8236. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8237. // Get the max order of the items
  8238. $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 . "'";
  8239. $rs_max_order = Database::query($sql_max_order);
  8240. $row_max_order = Database::fetch_object($rs_max_order);
  8241. $max_order = $row_max_order->display_order;
  8242. // Get the previous item ID
  8243. $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."' ";
  8244. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  8245. $row_max = Database::fetch_object($rs_max);
  8246. // Return the previous item ID
  8247. return $row_max->previous;
  8248. }
  8249. function copy() {
  8250. $main_path = api_get_path(SYS_CODE_PATH);
  8251. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8252. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8253. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8254. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8255. //Course builder
  8256. $cb = new CourseBuilder();
  8257. //Setting tools that will be copied
  8258. $cb->set_tools_to_build(array('learnpaths'));
  8259. //Setting elements that will be copied
  8260. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  8261. $course = $cb->build();
  8262. //Course restorer
  8263. $course_restorer = new CourseRestorer($course);
  8264. $course_restorer->set_add_text_in_items(true);
  8265. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8266. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8267. }
  8268. function verify_document_size($s) {
  8269. $post_max = ini_get('post_max_size');
  8270. $upl_max = ini_get('upload_max_filesize');
  8271. $documents_total_space = DocumentManager::documents_total_space();
  8272. $course_max_space = DocumentManager::get_course_quota();
  8273. $total_size = filesize($s) + $documents_total_space;
  8274. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8275. return true;
  8276. } else{
  8277. return false;
  8278. }
  8279. }
  8280. }
  8281. if (!function_exists('trim_value')) {
  8282. function trim_value(& $value) {
  8283. $value = trim($value);
  8284. }
  8285. }