learnpath.class.php 454 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
  5. * learnpaths. It is used by the scorm class.
  6. *
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  10. * @todo this file is too big, need a refactor more than 10000 lines!
  11. */
  12. /**
  13. * Defines the learnpath parent class
  14. * @package chamilo.learnpath
  15. */
  16. use Chamilo\CoreBundle\Entity\CLpCategory;
  17. use \ChamiloSession as Session;
  18. class learnpath
  19. {
  20. public $attempt = 0; // The number for the current ID view.
  21. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  22. public $current; // Id of the current item the user is viewing.
  23. public $current_score; // The score of the current item.
  24. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  25. public $current_time_stop; // The time the user closed this resource.
  26. public $default_status = 'not attempted';
  27. public $encoding = 'UTF-8';
  28. public $error = '';
  29. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  30. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  31. public $index; // The index of the active learnpath_item in $ordered_items array.
  32. public $items = array();
  33. public $last; // item_id of last item viewed in the learning path.
  34. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  35. public $license; // Which license this course has been given - not used yet on 20060522.
  36. public $lp_id; // DB ID for this learnpath.
  37. public $lp_view_id; // DB ID for lp_view
  38. public $log_file; // File where to log learnpath API msg.
  39. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  40. public $message = '';
  41. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  42. public $name; // Learnpath name (they generally have one).
  43. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  44. public $path = ''; // Path inside the scorm directory (if scorm).
  45. public $theme; // The current theme of the learning path.
  46. public $preview_image; // The current image of the learning path.
  47. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  48. public $prevent_reinit = 1;
  49. // Describes the mode of progress bar display.
  50. public $seriousgame_mode = 0;
  51. public $progress_bar_mode = '%';
  52. // Percentage progress as saved in the db.
  53. public $progress_db = '0';
  54. public $proximity; // Wether the content is distant or local or unknown.
  55. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  56. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  57. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  58. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  59. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  60. public $user_id; //ID of the user that is viewing/using the course
  61. public $update_queue = array();
  62. public $scorm_debug = 0;
  63. public $arrMenu = array(); // Array for the menu items.
  64. public $debug = 0; // Logging level.
  65. public $lp_session_id = 0;
  66. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  67. public $prerequisite = 0;
  68. public $use_max_score = 1; // 1 or 0
  69. public $subscribe_users = 0;
  70. public $created_on = '';
  71. public $modified_on = '';
  72. public $publicated_on = '';
  73. public $expired_on = '';
  74. public $ref = null;
  75. public $course_int_id;
  76. public $course_info = array();
  77. public function get_course_int_id()
  78. {
  79. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  80. }
  81. public function set_course_int_id($course_id)
  82. {
  83. return $this->course_int_id = intval($course_id);
  84. }
  85. /**
  86. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  87. * Also builds the list of items into $this->items.
  88. * @param string Course code
  89. * @param integer Learnpath ID
  90. * @param integer User ID
  91. * @return boolean True on success, false on error
  92. */
  93. public function __construct($course, $lp_id, $user_id)
  94. {
  95. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  96. // Check course code.
  97. $course_id = api_get_course_int_id();
  98. if ($this->debug > 0) {
  99. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  100. }
  101. if (empty($course)) {
  102. $this->error = 'Course code is empty';
  103. return false;
  104. } else {
  105. $course_info = api_get_course_info($course);
  106. if (!empty($course_info)) {
  107. $this->cc = $course_info['code'];
  108. $this->course_info = $course_info;
  109. $course_id = $course_info['real_id'];
  110. } else {
  111. $this->error = 'Course code does not exist in database';
  112. return false;
  113. }
  114. }
  115. $this->set_course_int_id($course_id);
  116. // Check learnpath ID.
  117. if (empty($lp_id)) {
  118. $this->error = 'Learnpath ID is empty';
  119. return false;
  120. } else {
  121. // TODO: Make it flexible to use any course_code (still using env course code here).
  122. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  123. $lp_id = intval($lp_id);
  124. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  125. if ($this->debug > 2) {
  126. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  127. }
  128. $res = Database::query($sql);
  129. if (Database::num_rows($res) > 0) {
  130. $this->lp_id = $lp_id;
  131. $row = Database::fetch_array($res);
  132. $this->type = $row['lp_type'];
  133. $this->name = stripslashes($row['name']);
  134. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  135. $this->proximity = $row['content_local'];
  136. $this->theme = $row['theme'];
  137. $this->maker = $row['content_maker'];
  138. $this->prevent_reinit = $row['prevent_reinit'];
  139. $this->seriousgame_mode = $row['seriousgame_mode'];
  140. $this->license = $row['content_license'];
  141. $this->scorm_debug = $row['debug'];
  142. $this->js_lib = $row['js_lib'];
  143. $this->path = $row['path'];
  144. $this->preview_image = $row['preview_image'];
  145. $this->author = $row['author'];
  146. $this->hide_toc_frame = $row['hide_toc_frame'];
  147. $this->lp_session_id = $row['session_id'];
  148. $this->use_max_score = $row['use_max_score'];
  149. $this->subscribe_users = $row['subscribe_users'];
  150. $this->created_on = $row['created_on'];
  151. $this->modified_on = $row['modified_on'];
  152. $this->ref = $row['ref'];
  153. $this->category_id = $row['category_id'];
  154. $this->max_attempts = $row['max_attempts'];
  155. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  156. $this->publicated_on = $row['publicated_on'];
  157. }
  158. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  159. $this->expired_on = $row['expired_on'];
  160. }
  161. if ($this->type == 2) {
  162. if ($row['force_commit'] == 1) {
  163. $this->force_commit = true;
  164. }
  165. }
  166. $this->mode = $row['default_view_mod'];
  167. } else {
  168. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // Check user ID.
  173. if (empty($user_id)) {
  174. $this->error = 'User ID is empty';
  175. return false;
  176. } else {
  177. $user_info = api_get_user_info($user_id);
  178. if (!empty($user_info)) {
  179. $this->user_id = $user_info['user_id'];
  180. } else {
  181. $this->error = 'User ID does not exist in database ('.$sql.')';
  182. return false;
  183. }
  184. }
  185. // End of variables checking.
  186. $session_id = api_get_session_id();
  187. // Get the session condition for learning paths of the base + session.
  188. $session = api_get_session_condition($session_id);
  189. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  190. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  191. // Selecting by view_count descending allows to get the highest view_count first.
  192. $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";
  193. $res = Database::query($sql);
  194. if ($this->debug > 2) {
  195. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  196. }
  197. if (Database :: num_rows($res) > 0) {
  198. if ($this->debug > 2) {
  199. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  200. }
  201. $row = Database :: fetch_array($res);
  202. $this->attempt = $row['view_count'];
  203. $this->lp_view_id = $row['id'];
  204. $this->last_item_seen = $row['last_item'];
  205. $this->progress_db = $row['progress'];
  206. $this->lp_view_session_id = $row['session_id'];
  207. } else {
  208. if ($this->debug > 2) {
  209. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  210. }
  211. $this->attempt = 1;
  212. $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)";
  213. $res_ins = Database::query($sql_ins);
  214. $this->lp_view_id = Database :: insert_id();
  215. if ($this->debug > 2) {
  216. error_log('New LP - learnpath::__construct() '.__LINE__.' - inserting new lp_view: '.$sql_ins, 0);
  217. }
  218. }
  219. // Initialise items.
  220. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  221. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  222. $res = Database::query($sql);
  223. if ($this->debug > 2) {
  224. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  225. error_log('-- Start while--', 0);
  226. }
  227. $lp_item_id_list = array();
  228. while ($row = Database::fetch_array($res)) {
  229. $oItem = '';
  230. $lp_item_id_list[] = $row['id'];
  231. switch ($this->type) {
  232. case 3: //aicc
  233. $oItem = new aiccItem('db', $row['id'], $course_id);
  234. if (is_object($oItem)) {
  235. $my_item_id = $oItem->get_id();
  236. $oItem->set_lp_view($this->lp_view_id, $course_id);
  237. $oItem->set_prevent_reinit($this->prevent_reinit);
  238. // Don't use reference here as the next loop will make the pointed object change.
  239. $this->items[$my_item_id] = $oItem;
  240. $this->refs_list[$oItem->ref] = $my_item_id;
  241. if ($this->debug > 2) {
  242. error_log(
  243. 'New LP - learnpath::__construct() - aicc object with id '.$my_item_id.' set in items[]',
  244. 0
  245. );
  246. }
  247. }
  248. break;
  249. case 2:
  250. require_once 'scorm.class.php';
  251. require_once 'scormItem.class.php';
  252. $oItem = new scormItem('db', $row['id'], $course_id);
  253. if (is_object($oItem)) {
  254. $my_item_id = $oItem->get_id();
  255. $oItem->set_lp_view($this->lp_view_id, $course_id);
  256. $oItem->set_prevent_reinit($this->prevent_reinit);
  257. // Don't use reference here as the next loop will make the pointed object change.
  258. $this->items[$my_item_id] = $oItem;
  259. $this->refs_list[$oItem->ref] = $my_item_id;
  260. if ($this->debug > 2) {
  261. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  262. }
  263. }
  264. break;
  265. case 1:
  266. default:
  267. if ($this->debug > 2) {
  268. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  269. }
  270. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  271. if ($this->debug > 2) {
  272. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  273. }
  274. if (is_object($oItem)) {
  275. $my_item_id = $oItem->get_id();
  276. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  277. $oItem->set_prevent_reinit($this->prevent_reinit);
  278. // Don't use reference here as the next loop will make the pointed object change.
  279. $this->items[$my_item_id] = $oItem;
  280. $this->refs_list[$my_item_id] = $my_item_id;
  281. if ($this->debug > 2) {
  282. error_log(
  283. 'New LP - learnpath::__construct() '.__LINE__.' - object with id '.$my_item_id.' set in items[]',
  284. 0
  285. );
  286. }
  287. }
  288. break;
  289. }
  290. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  291. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  292. if (is_object($this->items[$row['id']])) {
  293. $this->items[$row['id']]->set_level(0);
  294. }
  295. } else {
  296. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  297. $this->items[$row['id']]->set_level($level);
  298. if (is_object($this->items[$row['parent_item_id']])) {
  299. // Items is a list of pointers from item DB ids to item objects.
  300. $this->items[$row['parent_item_id']]->add_child($row['id']);
  301. } else {
  302. if ($this->debug > 2) {
  303. error_log(
  304. 'New LP - learnpath::__construct() '.__LINE__.' - The parent item ('.$row['parent_item_id'].') of item '.$row['id'].' could not be found',
  305. 0
  306. );
  307. }
  308. }
  309. }
  310. // Setting the view in the item object.
  311. if (is_object($this->items[$row['id']])) {
  312. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  313. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  314. $this->items[$row['id']]->current_start_time = 0;
  315. $this->items[$row['id']]->current_stop_time = 0;
  316. }
  317. }
  318. }
  319. if ($this->debug > 2) {
  320. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  321. }
  322. if (!empty($lp_item_id_list)) {
  323. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  324. // Get last viewing vars.
  325. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  326. // This query should only return one or zero result.
  327. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  328. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  329. ORDER BY view_count DESC ";
  330. if ($this->debug > 2) {
  331. error_log('New LP - learnpath::__construct() - Selecting item_views: '.$sql, 0);
  332. }
  333. $status_list = array();
  334. $res = Database::query($sql);
  335. while ($row = Database :: fetch_array($res)) {
  336. $status_list[$row['lp_item_id']] = $row['status'];
  337. }
  338. foreach ($lp_item_id_list as $item_id) {
  339. if (isset($status_list[$item_id])) {
  340. $status = $status_list[$item_id];
  341. if (is_object($this->items[$item_id])) {
  342. $this->items[$item_id]->set_status($status);
  343. if (empty($status)) {
  344. $this->items[$item_id]->set_status($this->default_status);
  345. }
  346. }
  347. } else {
  348. if (is_object($this->items[$item_id])) {
  349. $this->items[$item_id]->set_status($this->default_status);
  350. }
  351. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  352. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  353. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  354. if ($this->debug > 2) {
  355. error_log(
  356. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  357. 0
  358. );
  359. }
  360. Database::query($sql_ins);
  361. }
  362. }
  363. }
  364. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  365. $this->max_ordered_items = 0;
  366. foreach ($this->ordered_items as $index => $dummy) {
  367. if ($index > $this->max_ordered_items && !empty($dummy)) {
  368. $this->max_ordered_items = $index;
  369. }
  370. }
  371. // TODO: Define the current item better.
  372. $this->first();
  373. if ($this->debug > 2) {
  374. error_log(
  375. 'New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(
  376. ),
  377. 0
  378. );
  379. }
  380. }
  381. /**
  382. * 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
  383. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  384. *
  385. * @param int $parent
  386. * @param int $previous
  387. * @param string $type
  388. * @param int resource ID (ref)
  389. * @param string $title
  390. * @param string $description
  391. * @return int
  392. */
  393. public function add_item(
  394. $parent,
  395. $previous,
  396. $type = 'dokeos_chapter',
  397. $id,
  398. $title,
  399. $description,
  400. $prerequisites = 0,
  401. $max_time_allowed = 0
  402. ) {
  403. $course_id = api_get_course_int_id();
  404. if ($this->debug > 0) {
  405. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  406. }
  407. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  408. $parent = intval($parent);
  409. $previous = intval($previous);
  410. $type = Database::escape_string($type);
  411. $id = intval($id);
  412. $max_time_allowed = Database::escape_string($max_time_allowed);
  413. if (empty ($max_time_allowed)) {
  414. $max_time_allowed = 0;
  415. }
  416. $title = Database::escape_string($title);
  417. $description = Database::escape_string($description);
  418. $sql_count = " SELECT COUNT(id) AS num
  419. FROM $tbl_lp_item
  420. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND parent_item_id = ".$parent;
  421. $res_count = Database::query($sql_count);
  422. $row = Database :: fetch_array($res_count);
  423. $num = $row['num'];
  424. if ($num > 0) {
  425. if ($previous == 0) {
  426. $sql = "SELECT id, next_item_id, display_order
  427. FROM ".$tbl_lp_item."
  428. WHERE c_id = $course_id AND
  429. lp_id = ".$this->get_id()." AND
  430. parent_item_id = ".$parent." AND
  431. previous_item_id = 0 OR previous_item_id=".$parent;
  432. $result = Database::query($sql);
  433. $row = Database :: fetch_array($result);
  434. $tmp_previous = 0;
  435. $next = $row['id'];
  436. $display_order = 0;
  437. } else {
  438. $previous = (int)$previous;
  439. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  440. FROM $tbl_lp_item
  441. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND id = ".$previous;
  442. $result = Database::query($sql);
  443. $row = Database :: fetch_array($result);
  444. $tmp_previous = $row['id'];
  445. $next = $row['next_item_id'];
  446. $display_order = $row['display_order'];
  447. }
  448. } else {
  449. $tmp_previous = 0;
  450. $next = 0;
  451. $display_order = 0;
  452. }
  453. $new_item_id = -1;
  454. $id = Database::escape_string($id);
  455. if ($type == 'quiz') {
  456. $sql = 'SELECT SUM(ponderation)
  457. FROM '.Database :: get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  458. INNER JOIN '.Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  459. ON quiz_question.iid = quiz_rel_question.question_id
  460. WHERE quiz_rel_question.exercice_id = '.$id." AND
  461. quiz_question.c_id = $course_id AND
  462. quiz_rel_question.c_id = $course_id ";
  463. $rsQuiz = Database::query($sql);
  464. $max_score = Database :: result($rsQuiz, 0, 0);
  465. //Disabling the exercise if we add it inside a LP
  466. $exercise = new Exercise();
  467. $exercise->read($id);
  468. $exercise->disable();
  469. $exercise->save();
  470. } else {
  471. $max_score = 100;
  472. }
  473. if ($prerequisites != 0) {
  474. $sql_ins = "INSERT INTO ".$tbl_lp_item." (
  475. c_id,
  476. lp_id, ".
  477. "item_type, ".
  478. "ref, ".
  479. "title, ".
  480. "description, ".
  481. "path, ".
  482. "max_score, ".
  483. "parent_item_id, ".
  484. "previous_item_id, ".
  485. "next_item_id, ".
  486. "display_order, ".
  487. "prerequisite, ".
  488. "max_time_allowed ".
  489. ") VALUES (
  490. $course_id ,
  491. ".$this->get_id().", ".
  492. "'".$type."', ".
  493. "'', ".
  494. "'".$title."', ".
  495. "'".$description."', ".
  496. "'".$id."', ".
  497. "'".$max_score."', ".
  498. $parent.", ".
  499. $previous.", ".
  500. $next.", ".
  501. ($display_order + 1).", ".
  502. $prerequisites.", ".
  503. $max_time_allowed.
  504. ")";
  505. } else {
  506. // Insert new item.
  507. $sql_ins = "
  508. INSERT INTO ".$tbl_lp_item." ( ".
  509. "c_id, ".
  510. "lp_id, ".
  511. "item_type, ".
  512. "ref, ".
  513. "title, ".
  514. "description, ".
  515. "path, ".
  516. "max_score, ".
  517. "parent_item_id, ".
  518. "previous_item_id, ".
  519. "next_item_id, ".
  520. "display_order, ".
  521. "max_time_allowed ".
  522. ") VALUES (".
  523. $course_id.",".
  524. $this->get_id().",".
  525. "'".$type."',".
  526. "'',".
  527. "'".$title."',".
  528. "'".$description."',".
  529. "'".$id."',".
  530. "'".$max_score."',".
  531. $parent.",".
  532. $previous.",".
  533. $next.",".
  534. ($display_order + 1).",".
  535. $max_time_allowed.
  536. ")";
  537. }
  538. if ($this->debug > 2) {
  539. error_log('New LP - Inserting dokeos_chapter: '.$sql_ins, 0);
  540. }
  541. $res_ins = Database::query($sql_ins);
  542. if ($res_ins) {
  543. $new_item_id = Database::insert_id();
  544. // Update the item that should come after the new item.
  545. $sql_update_next = "
  546. UPDATE ".$tbl_lp_item."
  547. SET previous_item_id = ".$new_item_id."
  548. WHERE c_id = $course_id AND id = ".$next;
  549. Database::query($sql_update_next);
  550. // Update the item that should be before the new item.
  551. $sql_update_previous = "
  552. UPDATE ".$tbl_lp_item."
  553. SET next_item_id = ".$new_item_id."
  554. WHERE c_id = $course_id AND id = ".$tmp_previous;
  555. Database::query($sql_update_previous);
  556. // Update all the items after the new item.
  557. $sql_update_order = "
  558. UPDATE ".$tbl_lp_item."
  559. SET display_order = display_order + 1
  560. WHERE
  561. c_id = $course_id AND
  562. lp_id = ".$this->get_id()." AND
  563. id <> ".$new_item_id." AND
  564. parent_item_id = ".$parent." AND
  565. display_order > ".$display_order;
  566. Database::query($sql_update_order);
  567. // Update the item that should come after the new item.
  568. $sql_update_ref = "UPDATE ".$tbl_lp_item."
  569. SET ref = ".$new_item_id."
  570. WHERE c_id = $course_id AND id = ".$new_item_id;
  571. Database::query($sql_update_ref);
  572. }
  573. // Upload audio.
  574. if (!empty ($_FILES['mp3']['name'])) {
  575. // Create the audio folder if it does not exist yet.
  576. $_course = api_get_course_info();
  577. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  578. if (!is_dir($filepath.'audio')) {
  579. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  580. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  581. api_item_property_update(
  582. $_course,
  583. TOOL_DOCUMENT,
  584. $audio_id,
  585. 'FolderCreated',
  586. api_get_user_id(),
  587. null,
  588. null,
  589. null,
  590. null,
  591. api_get_session_id()
  592. );
  593. api_item_property_update(
  594. $_course,
  595. TOOL_DOCUMENT,
  596. $audio_id,
  597. 'invisible',
  598. api_get_user_id(),
  599. null,
  600. null,
  601. null,
  602. null,
  603. api_get_session_id()
  604. );
  605. }
  606. // Upload the file in the documents tool.
  607. $file_path = FileManager::handle_uploaded_document(
  608. $_course,
  609. $_FILES['mp3'],
  610. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  611. '/audio',
  612. api_get_user_id(),
  613. '',
  614. '',
  615. '',
  616. '',
  617. false
  618. );
  619. // Getting the filename only.
  620. $file_components = explode('/', $file_path);
  621. $file = $file_components[count($file_components) - 1];
  622. // Store the mp3 file in the lp_item table.
  623. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string(
  624. $file
  625. )."' WHERE id = '".Database::escape_string($new_item_id)."'";
  626. Database::query($sql_insert_audio);
  627. }
  628. return $new_item_id;
  629. }
  630. /**
  631. * Static admin function allowing addition of a learnpath to a course.
  632. * @param string Course code
  633. * @param string Learnpath name
  634. * @param string Learnpath description string, if provided
  635. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  636. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  637. * @param string Zip file containing the learnpath or directory containing the learnpath
  638. * @return integer The new learnpath ID on success, 0 on failure
  639. */
  640. public static function add_lp(
  641. $course,
  642. $name,
  643. $description = '',
  644. $learnpath = 'guess',
  645. $origin = 'zip',
  646. $zipname = '',
  647. $publicated_on = '',
  648. $expired_on = '',
  649. $category_id = 0
  650. ) {
  651. global $charset;
  652. $course_id = api_get_course_int_id();
  653. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  654. // Check course code exists.
  655. // Check lp_name doesn't exist, otherwise append something.
  656. $i = 0;
  657. $name = Database::escape_string($name);
  658. $category_id = intval($category_id);
  659. // Session id.
  660. $session_id = api_get_session_id();
  661. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  662. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  663. $res_name = Database::query($check_name);
  664. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  665. //by default the publication date is the same that the creation date
  666. //The behaviour above was changed due BT#2800
  667. global $_custom;
  668. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  669. $publicated_on = '';
  670. } else {
  671. $publicated_on = api_get_utc_datetime();
  672. }
  673. } else {
  674. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  675. }
  676. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  677. $expired_on = '';
  678. } else {
  679. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  680. }
  681. while (Database :: num_rows($res_name)) {
  682. // There is already one such name, update the current one a bit.
  683. $i++;
  684. $name = $name.' - '.$i;
  685. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  686. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  687. $res_name = Database::query($check_name);
  688. }
  689. // New name does not exist yet; keep it.
  690. // Escape description.
  691. $description = Database::escape_string(
  692. api_htmlentities($description, ENT_QUOTES, $charset)
  693. ); // Kevin: added htmlentities().
  694. $type = 1;
  695. switch ($learnpath) {
  696. case 'guess':
  697. break;
  698. case 'dokeos':
  699. case 'chamilo':
  700. $type = 1;
  701. break;
  702. case 'aicc':
  703. break;
  704. }
  705. switch ($origin) {
  706. case 'zip':
  707. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  708. break;
  709. case 'manual':
  710. default:
  711. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  712. $res_max = Database::query($get_max);
  713. if (Database :: num_rows($res_max) < 1) {
  714. $dsp = 1;
  715. } else {
  716. $row = Database :: fetch_array($res_max);
  717. $dsp = $row[0] + 1;
  718. }
  719. $sql_insert = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id) ".
  720. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime(
  721. )."' , '".$publicated_on."' , '".$expired_on."', $category_id)";
  722. Database::query($sql_insert);
  723. $id = Database :: insert_id();
  724. if ($id > 0) {
  725. $course_info = api_get_course_info();
  726. // Insert into item_property.
  727. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  728. api_set_default_visibility($course_info, $id, TOOL_LEARNPATH);
  729. return $id;
  730. }
  731. break;
  732. }
  733. }
  734. /**
  735. * Appends a message to the message attribute
  736. * @param string Message to append.
  737. */
  738. public function append_message($string)
  739. {
  740. if ($this->debug > 0) {
  741. error_log('New LP - In learnpath::append_message()', 0);
  742. }
  743. $this->message .= $string;
  744. }
  745. /**
  746. * Autocompletes the parents of an item in case it's been completed or passed
  747. * @param integer Optional ID of the item from which to look for parents
  748. */
  749. public function autocomplete_parents($item)
  750. {
  751. $debug = $this->debug;
  752. if ($debug) {
  753. error_log('Learnpath::autocomplete_parents()', 0);
  754. }
  755. if (empty($item)) {
  756. $item = $this->current;
  757. }
  758. $parent_id = $this->items[$item]->get_parent();
  759. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  760. // if $item points to an object and there is a parent.
  761. if ($debug) {
  762. error_log('Autocompleting parent of item '.$item.' (item '.$parent_id.')', 0);
  763. }
  764. $current_item = $this->items[$item];
  765. $parent = $this->items[$parent_id]; // Get the parent.
  766. // New experiment including failed and browsed in completed status.
  767. $current_status = $current_item->get_status();
  768. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  769. // If the current item is completed or passes or succeeded.
  770. $completed = true;
  771. if ($debug) {
  772. error_log('Status of current item is alright', 0);
  773. }
  774. foreach ($parent->get_children() as $child) {
  775. // Check all his brothers (parent's children) for completion status.
  776. if ($child != $item) {
  777. if ($debug) {
  778. error_log(
  779. 'Looking at brother with ID '.$child.', status is '.$this->items[$child]->get_status(),
  780. 0
  781. );
  782. }
  783. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  784. // Trying completing parents of failed and browsed items as well.
  785. if ($this->items[$child]->status_is(
  786. array(
  787. 'completed',
  788. 'passed',
  789. 'succeeded',
  790. 'browsed',
  791. 'failed'
  792. )
  793. )
  794. ) {
  795. // Keep completion status to true.
  796. } else {
  797. if ($this->debug > 2) {
  798. error_log(
  799. 'Found one incomplete child of '.$parent_id.': '.$child.' is '.$this->items[$child]->get_status(
  800. ),
  801. 0
  802. );
  803. }
  804. $completed = false;
  805. }
  806. }
  807. }
  808. if ($completed) { // If all the children were completed:
  809. $parent->set_status('completed');
  810. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  811. $this->update_queue[$parent->get_id()] = $parent->get_status();
  812. if ($debug) {
  813. error_log('Added parent to update queue '.print_r($this->update_queue, true), 0);
  814. }
  815. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  816. }
  817. } else {
  818. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  819. }
  820. } else {
  821. if ($debug) {
  822. error_log("#$item is an item doesn't have parents");
  823. }
  824. }
  825. }
  826. /**
  827. * Autosaves the current results into the database for the whole learnpath
  828. */
  829. public function autosave()
  830. {
  831. if ($this->debug > 0) {
  832. error_log('New LP - In learnpath::autosave()', 0);
  833. }
  834. // TODO: Add save operations for the learnpath itself.
  835. }
  836. /**
  837. * Clears the message attribute
  838. */
  839. public function clear_message()
  840. {
  841. if ($this->debug > 0) {
  842. error_log('New LP - In learnpath::clear_message()', 0);
  843. }
  844. $this->message = '';
  845. }
  846. /**
  847. * Closes the current resource
  848. *
  849. * Stops the timer
  850. * Saves into the database if required
  851. * Clears the current resource data from this object
  852. * @return boolean True on success, false on failure
  853. */
  854. public function close()
  855. {
  856. $course_id = api_get_course_int_id();
  857. if ($this->debug > 0) {
  858. error_log('New LP - In learnpath::close()', 0);
  859. }
  860. if (empty ($this->lp_id)) {
  861. $this->error = 'Trying to close this learnpath but no ID is set';
  862. return false;
  863. }
  864. $this->current_time_stop = time();
  865. if ($this->save) {
  866. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  867. /*
  868. $sql = "UPDATE $learnpath_view_table " .
  869. "SET " .
  870. "stop_time = ".$this->current_time_stop.", " .
  871. "score = ".$this->current_score.", ".
  872. "WHERE learnpath_id = '".$this->lp_id."'";
  873. //$res = Database::query($sql);
  874. $res = Database::query($res);
  875. if (Database::affected_rows($res) < 1) {
  876. $this->error = 'Could not update learnpath_view table while closing learnpath';
  877. return false;
  878. }
  879. */
  880. }
  881. $this->ordered_items = array();
  882. $this->index = 0;
  883. unset ($this->lp_id);
  884. //unset other stuff
  885. return true;
  886. }
  887. /**
  888. * Static admin function allowing removal of a learnpath
  889. * @param string Course code
  890. * @param integer Learnpath ID
  891. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  892. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  893. */
  894. public function delete($course = null, $id = null, $delete = 'keep')
  895. {
  896. $course_id = api_get_course_int_id();
  897. // TODO: Implement a way of getting this to work when the current object is not set.
  898. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  899. //if (empty($course)) { $course = api_get_course_id(); }
  900. //if (empty($id)) { $id = $this->get_id(); }
  901. // If an ID is specifically given and the current LP is not the same, prevent delete.
  902. if (!empty ($id) && ($id != $this->lp_id)) {
  903. return false;
  904. }
  905. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  906. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  907. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  908. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  909. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  910. // Delete lp item id.
  911. foreach ($this->items as $id => $dummy) {
  912. //$this->items[$id]->delete();
  913. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  914. $res_del_item_view = Database::query($sql_del_view);
  915. }
  916. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  917. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  918. $res_del_item = Database::query($sql_del_item);
  919. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  920. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  921. $res_del_view = Database::query($sql_del_view);
  922. self::toggle_publish($this->lp_id, 'i');
  923. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  924. if ($this->type == 2 || $this->type == 3) {
  925. // This is a scorm learning path, delete the files as well.
  926. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  927. $res = Database::query($sql);
  928. if (Database :: num_rows($res) > 0) {
  929. $row = Database :: fetch_array($res);
  930. $path = $row['path'];
  931. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  932. $res = Database::query($sql);
  933. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  934. if ($this->debug > 2) {
  935. error_log(
  936. 'New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory',
  937. 0
  938. );
  939. }
  940. } else {
  941. // No other LP uses that directory, delete it.
  942. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  943. $course_scorm_dir = api_get_path(
  944. SYS_COURSE_PATH
  945. ).$course_rel_dir; // The absolute system path for this course.
  946. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) and !empty ($course_scorm_dir)) {
  947. if ($this->debug > 2) {
  948. error_log(
  949. 'New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path,
  950. 0
  951. );
  952. }
  953. // Proposed by Christophe (clefevre).
  954. if (strcmp(substr($path, -2), "/.") == 0) {
  955. $path = substr($path, 0, -1); // Remove "." at the end.
  956. }
  957. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  958. api_rmdirr($course_scorm_dir.$path);
  959. }
  960. }
  961. }
  962. }
  963. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  964. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  965. $res_del_lp = Database::query($sql_del_lp);
  966. $this->update_display_order(); // Updates the display order of all lps.
  967. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  968. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  969. // Delete link of gradebook tool
  970. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  971. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  972. $result = Database::query($sql);
  973. $row = Database :: fetch_array($result, 'ASSOC');*/
  974. // Fixing gradebook link deleted see #5229.
  975. /*
  976. if (!empty($row['id'])) {
  977. $link = LinkFactory :: load($row['id']);
  978. if ($link[0] != null) {
  979. $link[0]->delete();
  980. }
  981. }*/
  982. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  983. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4, $id, api_get_session_id());
  984. if ($link_info !== false) {
  985. remove_resource_from_course_gradebook($link_info['id']);
  986. }
  987. if (api_get_setting('search_enabled') == 'true') {
  988. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  989. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  990. }
  991. }
  992. /**
  993. * Removes all the children of one item - dangerous!
  994. * @param integer Element ID of which children have to be removed
  995. * @return integer Total number of children removed
  996. */
  997. public function delete_children_items($id)
  998. {
  999. $course_id = api_get_course_int_id();
  1000. if ($this->debug > 0) {
  1001. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  1002. }
  1003. $num = 0;
  1004. if (empty ($id) || $id != strval(intval($id))) {
  1005. return false;
  1006. }
  1007. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1008. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1009. $res = Database::query($sql);
  1010. while ($row = Database :: fetch_array($res)) {
  1011. $num += $this->delete_children_items($row['id']);
  1012. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1013. $res_del = Database::query($sql_del);
  1014. $num++;
  1015. }
  1016. return $num;
  1017. }
  1018. /**
  1019. * Removes an item from the current learnpath
  1020. * @param integer Elem ID (0 if first)
  1021. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  1022. * @return integer Number of elements moved
  1023. * @todo implement resource removal
  1024. */
  1025. public function delete_item($id, $remove = 'keep')
  1026. {
  1027. $course_id = api_get_course_int_id();
  1028. if ($this->debug > 0) {
  1029. error_log('New LP - In learnpath::delete_item()', 0);
  1030. }
  1031. // TODO: Implement the resource removal.
  1032. if (empty ($id) || $id != strval(intval($id))) {
  1033. return false;
  1034. }
  1035. // First select item to get previous, next, and display order.
  1036. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1037. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1038. $res_sel = Database::query($sql_sel);
  1039. if (Database :: num_rows($res_sel) < 1) {
  1040. return false;
  1041. }
  1042. $row = Database :: fetch_array($res_sel);
  1043. $previous = $row['previous_item_id'];
  1044. $next = $row['next_item_id'];
  1045. $display = $row['display_order'];
  1046. $parent = $row['parent_item_id'];
  1047. $lp = $row['lp_id'];
  1048. // Delete children items.
  1049. $num = $this->delete_children_items($id);
  1050. if ($this->debug > 2) {
  1051. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1052. }
  1053. // Now delete the item.
  1054. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1055. if ($this->debug > 2) {
  1056. error_log('New LP - Deleting item: '.$sql_del, 0);
  1057. }
  1058. $res_del = Database::query($sql_del);
  1059. // Now update surrounding items.
  1060. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  1061. $res_upd = Database::query($sql_upd);
  1062. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  1063. $res_upd = Database::query($sql_upd);
  1064. // Now update all following items with new display order.
  1065. $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";
  1066. $res_all = Database::query($sql_all);
  1067. //Removing prerequisites since the item will not longer exist
  1068. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1069. $res_all = Database::query($sql_all);
  1070. // Remove from search engine if enabled.
  1071. if (api_get_setting('search_enabled') == 'true') {
  1072. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1073. $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';
  1074. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1075. $res = Database::query($sql);
  1076. if (Database :: num_rows($res) > 0) {
  1077. $row2 = Database :: fetch_array($res);
  1078. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1079. $di = new ChamiloIndexer();
  1080. $di->remove_document((int)$row2['search_did']);
  1081. }
  1082. $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';
  1083. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1084. Database::query($sql);
  1085. }
  1086. }
  1087. /**
  1088. * Updates an item's content in place
  1089. * @param integer Element ID
  1090. * @param integer Parent item ID
  1091. * @param integer Previous item ID
  1092. * @param string Item title
  1093. * @param string Item description
  1094. * @param string Prerequisites (optional)
  1095. * @param string Indexing terms (optional)
  1096. * @param array The array resulting of the $_FILES[mp3] element
  1097. * @return boolean True on success, false on error
  1098. */
  1099. public function edit_item(
  1100. $id,
  1101. $parent,
  1102. $previous,
  1103. $title,
  1104. $description,
  1105. $prerequisites = 0,
  1106. $audio = null,
  1107. $max_time_allowed = 0
  1108. ) {
  1109. $course_id = api_get_course_int_id();
  1110. if ($this->debug > 0) {
  1111. error_log('New LP - In learnpath::edit_item()', 0);
  1112. }
  1113. if (empty ($max_time_allowed)) {
  1114. $max_time_allowed = 0;
  1115. }
  1116. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1117. return false;
  1118. }
  1119. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1120. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1121. $res_select = Database::query($sql_select);
  1122. $row_select = Database :: fetch_array($res_select);
  1123. $audio_update_sql = '';
  1124. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1125. // Create the audio folder if it does not exist yet.
  1126. $_course = api_get_course_info();
  1127. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1128. if (!is_dir($filepath.'audio')) {
  1129. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1130. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  1131. api_item_property_update(
  1132. $_course,
  1133. TOOL_DOCUMENT,
  1134. $audio_id,
  1135. 'FolderCreated',
  1136. api_get_user_id(),
  1137. null,
  1138. null,
  1139. null,
  1140. null,
  1141. api_get_session_id()
  1142. );
  1143. api_item_property_update(
  1144. $_course,
  1145. TOOL_DOCUMENT,
  1146. $audio_id,
  1147. 'invisible',
  1148. api_get_user_id(),
  1149. null,
  1150. null,
  1151. null,
  1152. null,
  1153. api_get_session_id()
  1154. );
  1155. }
  1156. // Upload file in documents.
  1157. $pi = pathinfo($audio['name']);
  1158. if ($pi['extension'] == 'mp3') {
  1159. $c_det = api_get_course_info($this->cc);
  1160. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1161. $path = FileManager::handle_uploaded_document(
  1162. $c_det,
  1163. $audio,
  1164. $bp,
  1165. '/audio',
  1166. api_get_user_id(),
  1167. 0,
  1168. null,
  1169. 0,
  1170. 'rename',
  1171. false,
  1172. 0
  1173. );
  1174. $path = substr($path, 7);
  1175. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1176. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1177. }
  1178. }
  1179. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1180. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1181. // TODO: htmlspecialchars to be checked for encoding related problems.
  1182. if ($same_parent && $same_previous) {
  1183. // Only update title and description.
  1184. $sql_update = " UPDATE ".$tbl_lp_item."
  1185. SET title = '".Database::escape_string($title)."',
  1186. prerequisite = '".$prerequisites."',
  1187. description = '".Database::escape_string($description)."'
  1188. ".$audio_update_sql.",
  1189. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1190. WHERE c_id = ".$course_id." AND id = ".$id;
  1191. $res_update = Database::query($sql_update);
  1192. } else {
  1193. $old_parent = $row_select['parent_item_id'];
  1194. $old_previous = $row_select['previous_item_id'];
  1195. $old_next = $row_select['next_item_id'];
  1196. $old_order = $row_select['display_order'];
  1197. $old_prerequisite = $row_select['prerequisite'];
  1198. $old_max_time_allowed = $row_select['max_time_allowed'];
  1199. /* BEGIN -- virtually remove the current item id */
  1200. /* for the next and previous item it is like the current item doesn't exist anymore */
  1201. if ($old_previous != 0) {
  1202. $sql_update_next = "
  1203. UPDATE ".$tbl_lp_item."
  1204. SET next_item_id = ".$old_next."
  1205. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1206. $res_update_next = Database::query($sql_update_next);
  1207. //echo '<p>' . $sql_update_next . '</p>';
  1208. }
  1209. if ($old_next != 0) {
  1210. $sql_update_previous = "
  1211. UPDATE ".$tbl_lp_item."
  1212. SET previous_item_id = ".$old_previous."
  1213. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1214. $res_update_previous = Database::query($sql_update_previous);
  1215. //echo '<p>' . $sql_update_previous . '</p>';
  1216. }
  1217. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1218. $sql_update_order = "
  1219. UPDATE ".$tbl_lp_item."
  1220. SET display_order = display_order - 1
  1221. WHERE
  1222. c_id = ".$course_id." AND
  1223. display_order > ".$old_order." AND lp_id = ".$this->lp_id." AND
  1224. parent_item_id = ".$old_parent;
  1225. $res_update_order = Database::query($sql_update_order);
  1226. //echo '<p>' . $sql_update_order . '</p>';
  1227. /* END -- virtually remove the current item id */
  1228. /* BEGIN -- update the current item id to his new location */
  1229. if ($previous == 0) {
  1230. // Select the data of the item that should come after the current item.
  1231. $sql_select_old = "SELECT id, display_order
  1232. FROM ".$tbl_lp_item."
  1233. WHERE
  1234. c_id = ".$course_id." AND
  1235. lp_id = ".$this->lp_id." AND
  1236. parent_item_id = ".$parent." AND
  1237. previous_item_id = ".$previous;
  1238. $res_select_old = Database::query($sql_select_old);
  1239. $row_select_old = Database :: fetch_array($res_select_old);
  1240. //echo '<p>' . $sql_select_old . '</p>';
  1241. // If the new parent didn't have children before.
  1242. if (Database :: num_rows($res_select_old) == 0) {
  1243. $new_next = 0;
  1244. $new_order = 1;
  1245. } else {
  1246. $new_next = $row_select_old['id'];
  1247. $new_order = $row_select_old['display_order'];
  1248. }
  1249. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1250. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1251. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1252. } else {
  1253. // Select the data of the item that should come before the current item.
  1254. $sql_select_old = " SELECT next_item_id, display_order
  1255. FROM ".$tbl_lp_item."
  1256. WHERE c_id = ".$course_id." AND id = ".$previous;
  1257. $res_select_old = Database::query($sql_select_old);
  1258. $row_select_old = Database :: fetch_array($res_select_old);
  1259. //echo '<p>' . $sql_select_old . '</p>';
  1260. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1261. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1262. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1263. $new_next = $row_select_old['next_item_id'];
  1264. $new_order = $row_select_old['display_order'] + 1;
  1265. }
  1266. // TODO: htmlspecialchars to be checked for encoding related problems.
  1267. // Update the current item with the new data.
  1268. $sql_update = "UPDATE ".$tbl_lp_item."
  1269. SET
  1270. title = '".Database::escape_string($title)."',
  1271. description = '".Database::escape_string($description)."',
  1272. parent_item_id = ".$parent.",
  1273. previous_item_id = ".$previous.",
  1274. next_item_id = ".$new_next.",
  1275. display_order = ".$new_order."
  1276. ".$audio_update_sql."
  1277. WHERE c_id = ".$course_id." AND id = ".$id;
  1278. $res_update_next = Database::query($sql_update);
  1279. //echo '<p>' . $sql_update . '</p>';
  1280. if ($previous != 0) {
  1281. // Update the previous item's next_item_id.
  1282. $sql_update_previous = "
  1283. UPDATE ".$tbl_lp_item."
  1284. SET next_item_id = ".$id."
  1285. WHERE c_id = ".$course_id." AND id = ".$previous;
  1286. $res_update_next = Database::query($sql_update_previous);
  1287. //echo '<p>' . $sql_update_previous . '</p>';
  1288. }
  1289. if ($new_next != 0) {
  1290. // Update the next item's previous_item_id.
  1291. $sql_update_next = "
  1292. UPDATE ".$tbl_lp_item."
  1293. SET previous_item_id = ".$id."
  1294. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1295. $res_update_next = Database::query($sql_update_next);
  1296. //echo '<p>' . $sql_update_next . '</p>';
  1297. }
  1298. if ($old_prerequisite != $prerequisites) {
  1299. $sql_update_next = "
  1300. UPDATE ".$tbl_lp_item."
  1301. SET prerequisite = ".$prerequisites."
  1302. WHERE c_id = ".$course_id." AND id = ".$id;
  1303. $res_update_next = Database::query($sql_update_next);
  1304. }
  1305. if ($old_max_time_allowed != $max_time_allowed) {
  1306. $sql_update_max_time_allowed = "
  1307. UPDATE ".$tbl_lp_item."
  1308. SET max_time_allowed = ".$max_time_allowed."
  1309. WHERE c_id = ".$course_id." AND id = ".$id;
  1310. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1311. }
  1312. // Update all the items with the same or a bigger display_order than the current item.
  1313. $sql_update_order = "
  1314. UPDATE ".$tbl_lp_item."
  1315. SET display_order = display_order + 1
  1316. WHERE
  1317. c_id = ".$course_id." AND
  1318. lp_id = ".$this->get_id()." AND
  1319. id <> ".$id." AND
  1320. parent_item_id = ".$parent." AND
  1321. display_order >= ".$new_order;
  1322. $res_update_next = Database::query($sql_update_order);
  1323. }
  1324. }
  1325. /**
  1326. * Updates an item's prereq in place
  1327. * @param integer Element ID
  1328. * @param string Prerequisite Element ID
  1329. * @param string Prerequisite item type
  1330. * @param string Prerequisite min score
  1331. * @param string Prerequisite max score
  1332. * @return boolean True on success, false on error
  1333. */
  1334. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1335. {
  1336. $course_id = api_get_course_int_id();
  1337. if ($this->debug > 0) {
  1338. error_log(
  1339. 'New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')',
  1340. 0
  1341. );
  1342. }
  1343. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1344. return false;
  1345. }
  1346. $prerequisite_id = Database::escape_string($prerequisite_id);
  1347. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1348. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1349. $mastery_score = 0;
  1350. }
  1351. if (!is_numeric($max_score) || $max_score < 0) {
  1352. $max_score = 100;
  1353. }
  1354. if ($mastery_score > $max_score) {
  1355. $max_score = $mastery_score;
  1356. }
  1357. if (!is_numeric($prerequisite_id)) {
  1358. $prerequisite_id = 'NULL';
  1359. }
  1360. $sql_upd = " UPDATE ".$tbl_lp_item."
  1361. SET prerequisite = ".$prerequisite_id." WHERE c_id = ".$course_id." AND id = ".$id;
  1362. $res_upd = Database::query($sql_upd);
  1363. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1364. $sql_upd = " UPDATE ".$tbl_lp_item." SET
  1365. mastery_score = ".$mastery_score.
  1366. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1367. " WHERE c_id = ".$course_id." AND ref = '".$prerequisite_id."'"; // Will this be enough to ensure unicity?
  1368. Database::query($sql_upd);
  1369. }
  1370. // TODO: Update the item object (can be ignored for now because refreshed).
  1371. return true;
  1372. }
  1373. /**
  1374. * Escapes a string with the available database escape function
  1375. * @param string String to escape
  1376. * @return string String escaped
  1377. * @deprecated use Database::escape_string
  1378. */
  1379. public function escape_string($string)
  1380. {
  1381. return Database::escape_string($string);
  1382. }
  1383. /**
  1384. * Static admin function exporting a learnpath into a zip file
  1385. * @param string Export type (scorm, zip, cd)
  1386. * @param string Course code
  1387. * @param integer Learnpath ID
  1388. * @param string Zip file name
  1389. * @return string Zip file path (or false on error)
  1390. */
  1391. public function export_lp($type, $course, $id, $zipname)
  1392. {
  1393. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1394. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1395. return false;
  1396. }
  1397. $url = '';
  1398. switch ($type) {
  1399. case 'scorm':
  1400. break;
  1401. case 'zip':
  1402. break;
  1403. case 'cdrom':
  1404. break;
  1405. }
  1406. return $url;
  1407. }
  1408. /**
  1409. * Gets all the chapters belonging to the same parent as the item/chapter given
  1410. * Can also be called as abstract method
  1411. * @param integer Item ID
  1412. * @return array A list of all the "brother items" (or an empty array on failure)
  1413. */
  1414. public function get_brother_chapters($id)
  1415. {
  1416. $course_id = api_get_course_int_id();
  1417. if ($this->debug > 0) {
  1418. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1419. }
  1420. if (empty ($id) OR $id != strval(intval($id))) {
  1421. return array();
  1422. }
  1423. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1424. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1425. $res_parent = Database::query($sql_parent);
  1426. if (Database :: num_rows($res_parent) > 0) {
  1427. $row_parent = Database :: fetch_array($res_parent);
  1428. $parent = $row_parent['parent_item_id'];
  1429. $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";
  1430. $res_bros = Database::query($sql_bros);
  1431. $list = array();
  1432. while ($row_bro = Database :: fetch_array($res_bros)) {
  1433. $list[] = $row_bro;
  1434. }
  1435. return $list;
  1436. }
  1437. return array();
  1438. }
  1439. /**
  1440. * Gets all the items belonging to the same parent as the item given
  1441. * Can also be called as abstract method
  1442. * @param integer Item ID
  1443. * @return array A list of all the "brother items" (or an empty array on failure)
  1444. */
  1445. public function get_brother_items($id)
  1446. {
  1447. $course_id = api_get_course_int_id();
  1448. if ($this->debug > 0) {
  1449. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1450. }
  1451. if (empty ($id) OR $id != strval(intval($id))) {
  1452. return array();
  1453. }
  1454. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1455. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1456. $res_parent = Database::query($sql_parent);
  1457. if (Database :: num_rows($res_parent) > 0) {
  1458. $row_parent = Database :: fetch_array($res_parent);
  1459. $parent = $row_parent['parent_item_id'];
  1460. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1461. $res_bros = Database::query($sql_bros);
  1462. $list = array();
  1463. while ($row_bro = Database :: fetch_array($res_bros)) {
  1464. $list[] = $row_bro;
  1465. }
  1466. return $list;
  1467. }
  1468. return array();
  1469. }
  1470. /**
  1471. * Get the specific prefix index terms of this learning path
  1472. * @return array Array of terms
  1473. */
  1474. public function get_common_index_terms_by_prefix($prefix)
  1475. {
  1476. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1477. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1478. $prefix_terms = array();
  1479. if (!empty($terms)) {
  1480. foreach ($terms as $term) {
  1481. $prefix_terms[] = $term['value'];
  1482. }
  1483. }
  1484. return $prefix_terms;
  1485. }
  1486. /**
  1487. * Gets the number of items currently completed
  1488. * @return integer The number of items currently completed
  1489. */
  1490. public function get_complete_items_count()
  1491. {
  1492. if ($this->debug > 0) {
  1493. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1494. }
  1495. $i = 0;
  1496. foreach ($this->items as $id => $dummy) {
  1497. // Trying failed and browsed considered "progressed" as well.
  1498. if ($this->items[$id]->status_is(
  1499. array(
  1500. 'completed',
  1501. 'passed',
  1502. 'succeeded',
  1503. 'browsed',
  1504. 'failed'
  1505. )
  1506. ) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir'
  1507. ) {
  1508. $i++;
  1509. }
  1510. }
  1511. return $i;
  1512. }
  1513. /**
  1514. * Gets the current item ID
  1515. * @return integer The current learnpath item id
  1516. */
  1517. public function get_current_item_id()
  1518. {
  1519. $current = 0;
  1520. if ($this->debug > 0) {
  1521. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1522. }
  1523. if (!empty ($this->current)) {
  1524. $current = $this->current;
  1525. }
  1526. if ($this->debug > 2) {
  1527. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1528. }
  1529. return $current;
  1530. }
  1531. /**
  1532. * Force to get the first learnpath item id
  1533. * @return integer The current learnpath item id
  1534. */
  1535. public function get_first_item_id()
  1536. {
  1537. $current = 0;
  1538. if (is_array($this->ordered_items)) {
  1539. $current = $this->ordered_items[0];
  1540. }
  1541. return $current;
  1542. }
  1543. /**
  1544. * Gets the total number of items available for viewing in this SCORM
  1545. * @return integer The total number of items
  1546. */
  1547. public function get_total_items_count()
  1548. {
  1549. if ($this->debug > 0) {
  1550. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1551. }
  1552. return count($this->items);
  1553. }
  1554. /**
  1555. * Gets the total number of items available for viewing in this SCORM but without chapters
  1556. * @return integer The total no-chapters number of items
  1557. */
  1558. public function get_total_items_count_without_chapters()
  1559. {
  1560. if ($this->debug > 0) {
  1561. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1562. }
  1563. $total = 0;
  1564. foreach ($this->items as $temp2) {
  1565. if (!in_array(
  1566. $temp2->get_type(),
  1567. array(
  1568. 'dokeos_chapter',
  1569. 'chapter',
  1570. 'dir'
  1571. )
  1572. )
  1573. ) {
  1574. $total++;
  1575. }
  1576. }
  1577. return $total;
  1578. }
  1579. /**
  1580. * Gets the first element URL.
  1581. * @return string URL to load into the viewer
  1582. */
  1583. public function first()
  1584. {
  1585. if ($this->debug > 0) {
  1586. error_log('New LP - In learnpath::first()', 0);
  1587. error_log('$this->last_item_seen '.$this->last_item_seen);
  1588. //error_log('$this->items '.print_r($this->items, 1));
  1589. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1590. }
  1591. // Test if the last_item_seen exists and is not a dir.
  1592. if (count($this->ordered_items) == 0) {
  1593. $this->index = 0;
  1594. }
  1595. if ($this->debug > 0) {
  1596. if (isset($this->items[$this->last_item_seen])) {
  1597. $status = $this->items[$this->last_item_seen]->get_status();
  1598. }
  1599. error_log('status '.$status);
  1600. }
  1601. if (!empty($this->last_item_seen) &&
  1602. !empty($this->items[$this->last_item_seen]) &&
  1603. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1604. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1605. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1606. //&& !$this->items[$this->last_item_seen]->is_done()
  1607. ) {
  1608. if ($this->debug > 2) {
  1609. error_log(
  1610. 'New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(
  1611. ),
  1612. 0
  1613. );
  1614. }
  1615. $index = -1;
  1616. foreach ($this->ordered_items as $myindex => $item_id) {
  1617. if ($item_id == $this->last_item_seen) {
  1618. $index = $myindex;
  1619. break;
  1620. }
  1621. }
  1622. if ($index == -1) {
  1623. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1624. if ($this->debug > 2) {
  1625. error_log(
  1626. 'New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!',
  1627. 0
  1628. );
  1629. }
  1630. return false;
  1631. } else {
  1632. $this->last = $this->last_item_seen;
  1633. $this->current = $this->last_item_seen;
  1634. $this->index = $index;
  1635. }
  1636. } else {
  1637. if ($this->debug > 2) {
  1638. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1639. }
  1640. $index = 0;
  1641. // Loop through all ordered items and stop at the first item that is
  1642. // not a directory *and* that has not been completed yet.
  1643. while (!empty($this->ordered_items[$index]) AND
  1644. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1645. (
  1646. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1647. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1648. $this->items[$this->ordered_items[$index]]->is_done() === true
  1649. ) AND $index < $this->max_ordered_items) {
  1650. $index++;
  1651. }
  1652. $this->last = $this->current;
  1653. // current is
  1654. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1655. $this->index = $index;
  1656. if ($this->debug > 2) {
  1657. error_log('$index '.$index);
  1658. }
  1659. if ($this->debug > 2) {
  1660. error_log(
  1661. 'New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')',
  1662. 0
  1663. );
  1664. }
  1665. }
  1666. if ($this->debug > 2) {
  1667. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1668. }
  1669. }
  1670. /**
  1671. * Gets the information about an item in a format usable as JavaScript to update
  1672. * the JS API by just printing this content into the <head> section of the message frame
  1673. * @param integer Item ID
  1674. * @return string
  1675. */
  1676. public function get_js_info($item_id = '')
  1677. {
  1678. if ($this->debug > 0) {
  1679. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1680. }
  1681. $info = '';
  1682. $item_id = Database::escape_string($item_id);
  1683. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1684. //if item is defined, return values from DB
  1685. $oItem = $this->items[$item_id];
  1686. $info .= '<script language="javascript">';
  1687. $info .= "top.set_score(".$oItem->get_score().");\n";
  1688. $info .= "top.set_max(".$oItem->get_max().");\n";
  1689. $info .= "top.set_min(".$oItem->get_min().");\n";
  1690. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1691. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1692. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1693. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1694. $info .= "top.set_flag_synchronized();";
  1695. $info .= '</script>';
  1696. if ($this->debug > 2) {
  1697. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1698. }
  1699. return $info;
  1700. } else {
  1701. // If item_id is empty, just update to default SCORM data.
  1702. $info .= '<script language="javascript">';
  1703. $info .= "top.set_score(".learnpathItem :: get_score().");\n";
  1704. $info .= "top.set_max(".learnpathItem :: get_max().");\n";
  1705. $info .= "top.set_min(".learnpathItem :: get_min().");\n";
  1706. $info .= "top.set_lesson_status('".learnpathItem :: get_status()."');";
  1707. $info .= "top.set_session_time('".learnpathItem :: get_scorm_time('js')."');";
  1708. $info .= "top.set_suspend_data('".learnpathItem :: get_suspend_data()."');";
  1709. $info .= "top.set_saved_lesson_status('".learnpathItem :: get_status()."');";
  1710. $info .= "top.set_flag_synchronized();";
  1711. $info .= '</script>';
  1712. if ($this->debug > 2) {
  1713. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1714. }
  1715. return $info;
  1716. }
  1717. }
  1718. /**
  1719. * Gets the js library from the database
  1720. * @return string The name of the javascript library to be used
  1721. */
  1722. public function get_js_lib()
  1723. {
  1724. $lib = '';
  1725. if (!empty ($this->js_lib)) {
  1726. $lib = $this->js_lib;
  1727. }
  1728. return $lib;
  1729. }
  1730. /**
  1731. * Gets the learnpath database ID
  1732. * @return integer Learnpath ID in the lp table
  1733. */
  1734. public function get_id()
  1735. {
  1736. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1737. if (!empty ($this->lp_id)) {
  1738. return $this->lp_id;
  1739. } else {
  1740. return 0;
  1741. }
  1742. }
  1743. /**
  1744. * Gets the last element URL.
  1745. * @return string URL to load into the viewer
  1746. */
  1747. public function get_last()
  1748. {
  1749. if ($this->debug > 0) {
  1750. error_log('New LP - In learnpath::get_last()', 0);
  1751. }
  1752. $this->index = count($this->ordered_items) - 1;
  1753. return $this->ordered_items[$this->index];
  1754. }
  1755. /**
  1756. * Gets the navigation bar for the learnpath display screen
  1757. * @return string The HTML string to use as a navigation bar
  1758. */
  1759. public function get_navigation_bar()
  1760. {
  1761. if ($this->debug > 0) {
  1762. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1763. }
  1764. $navbar = null;
  1765. $lp_id = $this->lp_id;
  1766. $mycurrentitemid = $this->get_current_item_id();
  1767. if ($this->mode == 'fullscreen') {
  1768. $navbar = '
  1769. <div class="buttons">
  1770. <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">
  1771. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'</a>
  1772. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1773. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1774. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next">
  1775. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).' </a>
  1776. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1777. '.Display::return_icon('view_choose.gif', get_lang('ScormExitFullScreen')).'</a>
  1778. </div>';
  1779. } else {
  1780. $navbar = '
  1781. <div class="buttons">
  1782. <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">
  1783. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'
  1784. </a>
  1785. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1786. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1787. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" >
  1788. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).'
  1789. </a>
  1790. </div>';
  1791. }
  1792. return $navbar;
  1793. }
  1794. /**
  1795. * Gets the next resource in queue (url).
  1796. * @return string URL to load into the viewer
  1797. */
  1798. public function get_next_index()
  1799. {
  1800. if ($this->debug > 0) {
  1801. error_log('New LP - In learnpath::get_next_index()', 0);
  1802. }
  1803. // TODO
  1804. $index = $this->index;
  1805. $index++;
  1806. if ($this->debug > 2) {
  1807. error_log(
  1808. 'New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type,
  1809. 0
  1810. );
  1811. }
  1812. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1813. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type(
  1814. ) == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1815. $index++;
  1816. if ($index == $this->max_ordered_items) {
  1817. if ($this->items[$this->ordered_items[$index]]->get_type(
  1818. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter'
  1819. ) {
  1820. return $this->index;
  1821. } else {
  1822. return $index;
  1823. }
  1824. }
  1825. }
  1826. if (empty ($this->ordered_items[$index])) {
  1827. return $this->index;
  1828. }
  1829. if ($this->debug > 2) {
  1830. error_log('New LP - index is now '.$index, 0);
  1831. }
  1832. return $index;
  1833. }
  1834. /**
  1835. * Gets item_id for the next element
  1836. * @return integer Next item (DB) ID
  1837. */
  1838. public function get_next_item_id()
  1839. {
  1840. if ($this->debug > 0) {
  1841. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1842. }
  1843. $new_index = $this->get_next_index();
  1844. if (!empty ($new_index)) {
  1845. if (isset ($this->ordered_items[$new_index])) {
  1846. if ($this->debug > 2) {
  1847. error_log(
  1848. 'New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index],
  1849. 0
  1850. );
  1851. }
  1852. return $this->ordered_items[$new_index];
  1853. }
  1854. }
  1855. if ($this->debug > 2) {
  1856. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1857. }
  1858. return 0;
  1859. }
  1860. /**
  1861. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1862. *
  1863. * Generally, the package provided is in the form of a zip file, so the function
  1864. * has been written to test a zip file. If not a zip, the function will return the
  1865. * default return value: ''
  1866. * @param string the path to the file
  1867. * @param string the original name of the file
  1868. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1869. */
  1870. public static function get_package_type($file_path, $file_name)
  1871. {
  1872. // Get name of the zip file without the extension.
  1873. $file_info = pathinfo($file_name);
  1874. $filename = $file_info['basename']; // Name including extension.
  1875. $extension = $file_info['extension']; // Extension only.
  1876. if (!empty($_POST['ppt2lp']) && !in_array(
  1877. strtolower($extension),
  1878. array(
  1879. 'dll',
  1880. 'exe'
  1881. )
  1882. )
  1883. ) {
  1884. return 'oogie';
  1885. }
  1886. if (!empty($_POST['woogie']) && !in_array(
  1887. strtolower($extension),
  1888. array(
  1889. 'dll',
  1890. 'exe'
  1891. )
  1892. )
  1893. ) {
  1894. return 'woogie';
  1895. }
  1896. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  1897. $zipFile = new PclZip($file_path);
  1898. // Check the zip content (real size and file extension).
  1899. $zipContentArray = $zipFile->listContent();
  1900. $package_type = '';
  1901. $at_root = false;
  1902. $manifest = '';
  1903. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1904. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1905. foreach ($zipContentArray as $thisContent) {
  1906. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1907. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1908. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1909. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1910. $package_type = 'scorm';
  1911. break; // Exit the foreach loop.
  1912. } elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1913. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1914. $package_type = 'aicc';
  1915. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1916. } else {
  1917. $package_type = '';
  1918. }
  1919. }
  1920. }
  1921. return $package_type;
  1922. }
  1923. /**
  1924. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1925. * @return string URL to load into the viewer
  1926. */
  1927. public function get_previous_index()
  1928. {
  1929. if ($this->debug > 0) {
  1930. error_log('New LP - In learnpath::get_previous_index()', 0);
  1931. }
  1932. $index = $this->index;
  1933. if (isset ($this->ordered_items[$index - 1])) {
  1934. $index--;
  1935. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1936. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1937. $index--;
  1938. if ($index < 0) {
  1939. return $this->index;
  1940. }
  1941. }
  1942. } else {
  1943. if ($this->debug > 2) {
  1944. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1945. }
  1946. // There is no previous item.
  1947. }
  1948. return $index;
  1949. }
  1950. /**
  1951. * Gets item_id for the next element
  1952. * @return integer Previous item (DB) ID
  1953. */
  1954. public function get_previous_item_id()
  1955. {
  1956. if ($this->debug > 0) {
  1957. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1958. }
  1959. $new_index = $this->get_previous_index();
  1960. return $this->ordered_items[$new_index];
  1961. }
  1962. /**
  1963. * Gets the progress value from the progress_db attribute
  1964. * @return integer Current progress value
  1965. */
  1966. public function get_progress()
  1967. {
  1968. if ($this->debug > 0) {
  1969. error_log('New LP - In learnpath::get_progress()', 0);
  1970. }
  1971. if (!empty ($this->progress_db)) {
  1972. return $this->progress_db;
  1973. }
  1974. return 0;
  1975. }
  1976. /**
  1977. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1978. * @param integer Learnpath ID
  1979. * @param integer User ID
  1980. * @param string Mode of display ('%','abs' or 'both')
  1981. * @param string Course database name (optional, defaults to '')
  1982. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1983. * @return integer Current progress value as found in the database
  1984. */
  1985. public static function get_db_progress(
  1986. $lp_id,
  1987. $user_id,
  1988. $mode = '%',
  1989. $course_code = '',
  1990. $sincere = false,
  1991. $session_id = 0
  1992. ) {
  1993. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1994. $session_id = intval($session_id);
  1995. $course_info = api_get_course_info($course_code);
  1996. $session_condition = api_get_session_condition($session_id);
  1997. $course_id = $course_info['real_id'];
  1998. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1999. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  2000. $res = Database::query($sql);
  2001. $view_id = 0;
  2002. if (Database :: num_rows($res) > 0) {
  2003. $row = Database :: fetch_array($res);
  2004. $progress = $row['progress'];
  2005. $view_id = $row['id'];
  2006. } else {
  2007. if ($sincere) {
  2008. return null;
  2009. }
  2010. }
  2011. if (empty ($progress)) {
  2012. $progress = '0';
  2013. }
  2014. if ($mode == '%') {
  2015. return $progress.'%';
  2016. } else {
  2017. // Get the number of items completed and the number of items total.
  2018. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2019. $sql = "SELECT count(*) FROM $tbl
  2020. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = ".$lp_id." AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  2021. $res = Database::query($sql);
  2022. $row = Database :: fetch_array($res);
  2023. $total = $row[0];
  2024. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2025. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2026. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  2027. // Trying as also counting browsed and failed items.
  2028. $sql = "SELECT count(distinct(lp_item_id))
  2029. FROM $tbl_item_view as item_view
  2030. INNER JOIN $tbl_item as item
  2031. ON item.id = item_view.lp_item_id
  2032. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  2033. WHERE
  2034. item_view.c_id = $course_id AND
  2035. item.c_id = $course_id AND
  2036. lp_view_id = ".$view_id." AND
  2037. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  2038. $res = Database::query($sql);
  2039. $row = Database :: fetch_array($res);
  2040. $completed = $row[0];
  2041. if ($mode == 'abs') {
  2042. return $completed.'/'.$total;
  2043. } elseif ($mode == 'both') {
  2044. if ($progress < ($completed / ($total ? $total : 1))) {
  2045. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2046. }
  2047. return $progress.'% ('.$completed.'/'.$total.')';
  2048. }
  2049. }
  2050. return $progress;
  2051. }
  2052. /**
  2053. * Returns the HTML necessary to print a mediaplayer block inside a page
  2054. * @return string The mediaplayer HTML
  2055. */
  2056. public function get_mediaplayer($autostart = 'true')
  2057. {
  2058. $course_id = api_get_course_int_id();
  2059. $_course = api_get_course_info();
  2060. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2061. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2062. // Getting all the information about the item.
  2063. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2064. INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id
  2065. WHERE lp.id = '".$_SESSION['oLP']->current."' AND
  2066. lp.c_id = $course_id AND
  2067. lp_view.c_id = $course_id";
  2068. $result = Database::query($sql);
  2069. $row = Database::fetch_assoc($result);
  2070. $output = '';
  2071. if (!empty ($row['audio'])) {
  2072. $list = $_SESSION['oLP']->get_toc();
  2073. $type_quiz = false;
  2074. foreach ($list as $toc) {
  2075. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2076. $type_quiz = true;
  2077. }
  2078. }
  2079. if ($type_quiz) {
  2080. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2081. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2082. } else {
  2083. $autostart_audio = $autostart;
  2084. }
  2085. } else {
  2086. $autostart_audio = 'true';
  2087. }
  2088. $courseInfo = api_get_course_info();
  2089. $audio = $row['audio'];
  2090. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2091. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2092. if (!file_exists($file)) {
  2093. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2094. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2095. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2096. }
  2097. $player = Display::getMediaPlayer(
  2098. $file,
  2099. array(
  2100. 'id' => 'lp_audio_media_player',
  2101. 'url' => $url,
  2102. 'autoplay' => $autostart_audio,
  2103. 'width' => '100%'
  2104. )
  2105. );
  2106. // The mp3 player.
  2107. $output = '<div id="container">';
  2108. $output .= $player;
  2109. $output .= '</div>';
  2110. }
  2111. return $output;
  2112. }
  2113. /**
  2114. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  2115. * @param int Learnpath id
  2116. * @param int Student id
  2117. * @param string Course code (optional)
  2118. * @return bool True if
  2119. */
  2120. public static function is_lp_visible_for_student($lp_id, $student_id, $course_code = null)
  2121. {
  2122. $lp_id = (int)$lp_id;
  2123. $course_info = api_get_course_info($course_code);
  2124. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  2125. // Get current prerequisite
  2126. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2127. FROM $tbl_learnpath WHERE c_id = ".$course_info['real_id']." AND id = $lp_id";
  2128. $rs = Database::query($sql);
  2129. $now = time();
  2130. $session_id = api_get_session_id();
  2131. if (Database::num_rows($rs) > 0) {
  2132. $row = Database::fetch_array($rs, 'ASSOC');
  2133. $prerequisite = $row['prerequisite'];
  2134. $is_visible = true;
  2135. $progress = 0;
  2136. //Checking LP prerequisites
  2137. if (!empty($prerequisite)) {
  2138. $progress = intval(self::get_db_progress($prerequisite, $student_id, '%', '', false, $session_id));
  2139. if ($progress < 100) {
  2140. $is_visible = false;
  2141. }
  2142. }
  2143. // Also check the time availability of the LP
  2144. if ($is_visible) {
  2145. //Adding visibility restrictions
  2146. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  2147. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2148. //api_not_allowed();
  2149. $is_visible = false;
  2150. }
  2151. }
  2152. //Blocking empty start times see BT#2800
  2153. global $_custom;
  2154. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  2155. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2156. //api_not_allowed();
  2157. $is_visible = false;
  2158. }
  2159. }
  2160. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2161. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2162. //api_not_allowed();
  2163. $is_visible = false;
  2164. }
  2165. }
  2166. }
  2167. //Check if the subscription users/group to a LP is ON
  2168. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2169. //Checking only the user visibility
  2170. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2171. if ($userVisibility == 1) {
  2172. $is_visible = true;
  2173. } else {
  2174. $is_visible = false;
  2175. }
  2176. //the group visibility is not going to be checked because the user *must* be related to the LP
  2177. /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2178. if (!empty($groupList)) {
  2179. foreach($groupList as $groupId) {
  2180. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2181. if ($groupVisibility == 1) {
  2182. $is_visible = true;
  2183. break;
  2184. }
  2185. }
  2186. }*/
  2187. }
  2188. return $is_visible;
  2189. }
  2190. return false;
  2191. }
  2192. /**
  2193. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2194. * completed so far.
  2195. * @param string Mode in which we want the values
  2196. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2197. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2198. * @param boolean true if it comes from a Diplay LP view
  2199. * @return string HTML string containing the progress bar
  2200. */
  2201. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2202. {
  2203. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2204. global $lp_theme_css;
  2205. // Setting up the CSS path of the current style if exists.
  2206. if (!empty ($lp_theme_css)) {
  2207. //$css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2208. } else {
  2209. //$css_path = '../img/';
  2210. }
  2211. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2212. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2213. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2214. }
  2215. $text = $percentage.$text_add;
  2216. //@todo use Display::display_progress();
  2217. $output = '<div class="progress progress-striped">
  2218. <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="'.$text.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.'%">
  2219. <span class="sr-only">'.$text.'% Complete</span>
  2220. </div>
  2221. </div>
  2222. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2223. return $output;
  2224. }
  2225. /**
  2226. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2227. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2228. * @param integer Additional steps to fake as completed
  2229. * @return list Percentage or number and symbol (% or /xx)
  2230. */
  2231. public function get_progress_bar_text($mode = '', $add = 0)
  2232. {
  2233. if ($this->debug > 0) {
  2234. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2235. }
  2236. if (empty ($mode)) {
  2237. $mode = $this->progress_bar_mode;
  2238. }
  2239. $total_items = $this->get_total_items_count_without_chapters();
  2240. if ($this->debug > 2) {
  2241. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2242. }
  2243. $i = $this->get_complete_items_count();
  2244. if ($this->debug > 2) {
  2245. error_log('New LP - Items completed so far: '.$i, 0);
  2246. }
  2247. if ($add != 0) {
  2248. $i += $add;
  2249. if ($this->debug > 2) {
  2250. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2251. }
  2252. }
  2253. $text = '';
  2254. if ($i > $total_items) {
  2255. $i = $total_items;
  2256. }
  2257. if ($mode == '%') {
  2258. if ($total_items > 0) {
  2259. $percentage = ((float)$i / (float)$total_items) * 100;
  2260. } else {
  2261. $percentage = 0;
  2262. }
  2263. $percentage = number_format($percentage, 0);
  2264. $text = '%';
  2265. } elseif ($mode == 'abs') {
  2266. $percentage = $i;
  2267. $text = '/'.$total_items;
  2268. }
  2269. return array(
  2270. $percentage,
  2271. $text
  2272. );
  2273. }
  2274. /**
  2275. * Gets the progress bar mode
  2276. * @return string The progress bar mode attribute
  2277. */
  2278. public function get_progress_bar_mode()
  2279. {
  2280. if ($this->debug > 0) {
  2281. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2282. }
  2283. if (!empty ($this->progress_bar_mode)) {
  2284. return $this->progress_bar_mode;
  2285. } else {
  2286. return '%';
  2287. }
  2288. }
  2289. /**
  2290. * Gets the learnpath proximity (remote or local)
  2291. * @return string Learnpath proximity
  2292. */
  2293. public function get_proximity()
  2294. {
  2295. if ($this->debug > 0) {
  2296. error_log('New LP - In learnpath::get_proximity()', 0);
  2297. }
  2298. if (!empty ($this->proximity)) {
  2299. return $this->proximity;
  2300. } else {
  2301. return '';
  2302. }
  2303. }
  2304. /**
  2305. * Gets the learnpath theme (remote or local)
  2306. * @return string Learnpath theme
  2307. */
  2308. public function get_theme()
  2309. {
  2310. if ($this->debug > 0) {
  2311. error_log('New LP - In learnpath::get_theme()', 0);
  2312. }
  2313. if (!empty ($this->theme)) {
  2314. return $this->theme;
  2315. } else {
  2316. return '';
  2317. }
  2318. }
  2319. /**
  2320. * Gets the learnpath session id
  2321. * @return string Learnpath theme
  2322. */
  2323. public function get_lp_session_id()
  2324. {
  2325. if ($this->debug > 0) {
  2326. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2327. }
  2328. if (!empty ($this->lp_session_id)) {
  2329. return $this->lp_session_id;
  2330. } else {
  2331. return 0;
  2332. }
  2333. }
  2334. /**
  2335. * Gets the learnpath image
  2336. * @return string Web URL of the LP image
  2337. */
  2338. public function get_preview_image()
  2339. {
  2340. if ($this->debug > 0) {
  2341. error_log('New LP - In learnpath::get_preview_image()', 0);
  2342. }
  2343. if (!empty($this->preview_image)) {
  2344. return $this->preview_image;
  2345. } else {
  2346. return '';
  2347. }
  2348. }
  2349. /**
  2350. * @param string $size
  2351. * @param string $path_type
  2352. * @return bool|string
  2353. */
  2354. public function get_preview_image_path($size = null, $path_type = 'web')
  2355. {
  2356. $preview_image = $this->get_preview_image();
  2357. if (isset($preview_image) && !empty($preview_image)) {
  2358. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2359. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2360. if (isset($size)) {
  2361. $info = pathinfo($preview_image);
  2362. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2363. if (file_exists($image_sys_path.$image_custom_size)) {
  2364. if ($path_type == 'web') {
  2365. return $image_path.$image_custom_size;
  2366. } else {
  2367. return $image_sys_path.$image_custom_size;
  2368. }
  2369. }
  2370. } else {
  2371. if ($path_type == 'web') {
  2372. return $image_path.$preview_image;
  2373. } else {
  2374. return $image_sys_path.$preview_image;
  2375. }
  2376. }
  2377. }
  2378. return false;
  2379. }
  2380. public function get_category_id()
  2381. {
  2382. return $this->category_id;
  2383. }
  2384. /**
  2385. * Gets the learnpath author
  2386. * @return string LP's author
  2387. */
  2388. public function get_author()
  2389. {
  2390. if ($this->debug > 0) {
  2391. error_log('New LP - In learnpath::get_author()', 0);
  2392. }
  2393. if (!empty ($this->author)) {
  2394. return $this->author;
  2395. } else {
  2396. return '';
  2397. }
  2398. }
  2399. /**
  2400. * Gets the learnpath author
  2401. * @return string LP's author
  2402. */
  2403. public function get_hide_toc_frame()
  2404. {
  2405. if ($this->debug > 0) {
  2406. error_log('New LP - In learnpath::get_author()', 0);
  2407. }
  2408. if (!empty ($this->hide_toc_frame)) {
  2409. return $this->hide_toc_frame;
  2410. } else {
  2411. return '';
  2412. }
  2413. }
  2414. /**
  2415. * Generate a new prerequisites string for a given item. If this item was a sco and
  2416. * its prerequisites were strings (instead of IDs), then transform those strings into
  2417. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2418. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2419. * same rule as the scorm_export() method
  2420. * @param integer Item ID
  2421. * @return string Prerequisites string ready for the export as SCORM
  2422. */
  2423. public function get_scorm_prereq_string($item_id)
  2424. {
  2425. if ($this->debug > 0) {
  2426. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2427. }
  2428. if (!is_object($this->items[$item_id])) {
  2429. return false;
  2430. }
  2431. $oItem = $this->items[$item_id];
  2432. $prereq = $oItem->get_prereq_string();
  2433. if (empty($prereq)) {
  2434. return '';
  2435. }
  2436. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2437. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2438. // then simply return it (with the ITEM_ prefix).
  2439. //return 'ITEM_' . $prereq;
  2440. return $this->items[$prereq]->ref;
  2441. } else {
  2442. if (isset($this->refs_list[$prereq])) {
  2443. // It's a simple string item from which the ID can be found in the refs list,
  2444. // so we can transform it directly to an ID for export.
  2445. return $this->items[$this->refs_list[$prereq]]->ref;
  2446. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2447. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2448. } else {
  2449. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2450. // and replace them, one by one, by the internal IDs (chamilo db)
  2451. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2452. // by a space as well.
  2453. $find = array(
  2454. '&',
  2455. '|',
  2456. '~',
  2457. '=',
  2458. '<>',
  2459. '{',
  2460. '}',
  2461. '*',
  2462. '(',
  2463. ')'
  2464. );
  2465. $replace = array(
  2466. ' ',
  2467. ' ',
  2468. ' ',
  2469. ' ',
  2470. ' ',
  2471. ' ',
  2472. ' ',
  2473. ' ',
  2474. ' ',
  2475. ' '
  2476. );
  2477. $prereq_mod = str_replace($find, $replace, $prereq);
  2478. $ids = split(' ', $prereq_mod);
  2479. foreach ($ids as $id) {
  2480. $id = trim($id);
  2481. if (isset ($this->refs_list[$id])) {
  2482. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2483. }
  2484. }
  2485. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2486. return $prereq;
  2487. }
  2488. }
  2489. }
  2490. /**
  2491. * Returns the XML DOM document's node
  2492. * @param resource Reference to a list of objects to search for the given ITEM_*
  2493. * @param string The identifier to look for
  2494. * @return mixed The reference to the element found with that identifier. False if not found
  2495. */
  2496. public function get_scorm_xml_node(& $children, $id)
  2497. {
  2498. for ($i = 0; $i < $children->length; $i++) {
  2499. $item_temp = $children->item($i);
  2500. if ($item_temp->nodeName == 'item') {
  2501. if ($item_temp->getAttribute('identifier') == $id) {
  2502. return $item_temp;
  2503. }
  2504. }
  2505. $subchildren = $item_temp->childNodes;
  2506. if ($subchildren->length > 0) {
  2507. $val = $this->get_scorm_xml_node($subchildren, $id);
  2508. if (is_object($val)) {
  2509. return $val;
  2510. }
  2511. }
  2512. }
  2513. return false;
  2514. }
  2515. /**
  2516. * Returns a usable array of stats related to the current learnpath and user
  2517. * @return array Well-formatted array containing status for the current learnpath
  2518. */
  2519. public function get_stats()
  2520. {
  2521. if ($this->debug > 0) {
  2522. error_log('New LP - In learnpath::get_stats()', 0);
  2523. }
  2524. // TODO
  2525. }
  2526. /**
  2527. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2528. * the given course (for all learnpaths and all users)
  2529. * @param string Course code
  2530. * @return array Well-formatted array containing status for the course's learnpaths
  2531. */
  2532. public function get_stats_course($course)
  2533. {
  2534. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2535. // TODO
  2536. }
  2537. /**
  2538. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2539. * the given course and learnpath (for all users)
  2540. * @param string Course code
  2541. * @param integer Learnpath ID
  2542. * @return array Well-formatted array containing status for the specified learnpath
  2543. */
  2544. public function get_stats_lp($course, $lp)
  2545. {
  2546. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2547. // TODO
  2548. }
  2549. /**
  2550. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2551. * the given course, learnpath and user.
  2552. * @param string Course code
  2553. * @param integer Learnpath ID
  2554. * @param integer User ID
  2555. * @return array Well-formatted array containing status for the specified learnpath and user
  2556. */
  2557. public function get_stats_lp_user($course, $lp, $user)
  2558. {
  2559. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2560. // TODO
  2561. }
  2562. /**
  2563. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2564. * the given course and learnpath (for all users)
  2565. * @param string Course code
  2566. * @param integer User ID
  2567. * @return array Well-formatted array containing status for the user's learnpaths
  2568. */
  2569. public function get_stats_user($course, $user)
  2570. {
  2571. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2572. // TODO
  2573. }
  2574. /**
  2575. * Gets the status list for all LP's items
  2576. * @return array Array of [index] => [item ID => current status]
  2577. */
  2578. public function get_items_status_list()
  2579. {
  2580. if ($this->debug > 0) {
  2581. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2582. }
  2583. $list = array();
  2584. foreach ($this->ordered_items as $item_id) {
  2585. $list[] = array(
  2586. $item_id => $this->items[$item_id]->get_status()
  2587. );
  2588. }
  2589. return $list;
  2590. }
  2591. /**
  2592. * Return the number of interactions for the given learnpath Item View ID.
  2593. * This method can be used as static.
  2594. * @param integer Item View ID
  2595. * @param integer course id
  2596. * @return integer Number of interactions
  2597. */
  2598. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2599. {
  2600. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2601. $lp_iv_id = intval($lp_iv_id);
  2602. $course_id = intval($course_id);
  2603. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2604. $res = Database::query($sql);
  2605. $num = 0;
  2606. if (Database::num_rows($res)) {
  2607. $row = Database::fetch_array($res);
  2608. $num = $row[0];
  2609. }
  2610. return $num;
  2611. }
  2612. /**
  2613. * Return the interactions as an array for the given lp_iv_id.
  2614. * This method can be used as static.
  2615. * @param integer Learnpath Item View ID
  2616. * @return array
  2617. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2618. */
  2619. public static function get_iv_interactions_array($lp_iv_id = 0)
  2620. {
  2621. $course_id = api_get_course_int_id();
  2622. $list = array();
  2623. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2624. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2625. $res = Database::query($sql);
  2626. $num = Database :: num_rows($res);
  2627. if ($num > 0) {
  2628. $list[] = array(
  2629. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2630. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2631. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2632. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2633. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2634. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2635. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2636. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2637. );
  2638. while ($row = Database :: fetch_array($res)) {
  2639. $list[] = array(
  2640. 'order_id' => ($row['order_id'] + 1),
  2641. 'id' => urldecode($row['interaction_id']),
  2642. //urldecode because they often have %2F or stuff like that
  2643. 'type' => $row['interaction_type'],
  2644. 'time' => $row['completion_time'],
  2645. //'correct_responses' => $row['correct_responses'],
  2646. 'correct_responses' => '',
  2647. // Hide correct responses from students.
  2648. 'student_response' => $row['student_response'],
  2649. 'result' => $row['result'],
  2650. 'latency' => $row['latency']
  2651. );
  2652. }
  2653. }
  2654. return $list;
  2655. }
  2656. /**
  2657. * Return the number of objectives for the given learnpath Item View ID.
  2658. * This method can be used as static.
  2659. * @param integer Item View ID
  2660. * @return integer Number of objectives
  2661. */
  2662. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2663. {
  2664. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2665. $course_id = intval($course_id);
  2666. $lp_iv_id = intval($lp_iv_id);
  2667. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2668. //@todo seems that this always returns 0
  2669. $res = Database::query($sql);
  2670. $num = 0;
  2671. if (Database::num_rows($res)) {
  2672. $row = Database :: fetch_array($res);
  2673. $num = $row[0];
  2674. }
  2675. return $num;
  2676. }
  2677. /**
  2678. * Return the objectives as an array for the given lp_iv_id.
  2679. * This method can be used as static.
  2680. * @param integer Learnpath Item View ID
  2681. * @return array
  2682. * @todo Translate labels
  2683. */
  2684. public function get_iv_objectives_array($lp_iv_id = 0)
  2685. {
  2686. $course_id = api_get_course_int_id();
  2687. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2688. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2689. $res = Database::query($sql);
  2690. $num = Database :: num_rows($res);
  2691. $list = array();
  2692. if ($num > 0) {
  2693. $list[] = array(
  2694. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2695. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2696. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2697. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2698. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2699. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2700. );
  2701. while ($row = Database :: fetch_array($res)) {
  2702. $list[] = array(
  2703. 'order_id' => ($row['order_id'] + 1),
  2704. 'objective_id' => urldecode($row['objective_id']),
  2705. // urldecode() because they often have %2F or stuff like that.
  2706. 'score_raw' => $row['score_raw'],
  2707. 'score_max' => $row['score_max'],
  2708. 'score_min' => $row['score_min'],
  2709. 'status' => $row['status']
  2710. );
  2711. }
  2712. }
  2713. return $list;
  2714. }
  2715. /**
  2716. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2717. * used by get_html_toc() to be ready to display
  2718. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2719. */
  2720. public function get_toc()
  2721. {
  2722. if ($this->debug > 0) {
  2723. error_log('learnpath::get_toc()', 0);
  2724. }
  2725. $toc = array();
  2726. //echo "<pre>".print_r($this->items,true)."</pre>";
  2727. foreach ($this->ordered_items as $item_id) {
  2728. if ($this->debug > 2) {
  2729. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2730. }
  2731. // TODO: Change this link generation and use new function instead.
  2732. $toc[] = array(
  2733. 'id' => $item_id,
  2734. 'title' => $this->items[$item_id]->get_title(),
  2735. 'status' => $this->items[$item_id]->get_status(),
  2736. 'level' => $this->items[$item_id]->get_level(),
  2737. 'type' => $this->items[$item_id]->get_type(),
  2738. 'description' => $this->items[$item_id]->get_description(),
  2739. 'path' => $this->items[$item_id]->get_path(),
  2740. );
  2741. }
  2742. if ($this->debug > 2) {
  2743. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2744. }
  2745. return $toc;
  2746. }
  2747. /**
  2748. * Generate and return the table of contents for this learnpath. The JS
  2749. * table returned is used inside of scorm_api.php
  2750. * @return string A JS array vairiable construction
  2751. */
  2752. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2753. {
  2754. if ($this->debug > 0) {
  2755. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2756. }
  2757. $toc = $varname.' = new Array();';
  2758. //echo "<pre>".print_r($this->items,true)."</pre>";
  2759. foreach ($this->ordered_items as $item_id) {
  2760. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2761. }
  2762. if ($this->debug > 2) {
  2763. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2764. }
  2765. return $toc;
  2766. }
  2767. /**
  2768. * Gets the learning path type
  2769. * @param boolean Return the name? If false, return the ID. Default is false.
  2770. * @return mixed Type ID or name, depending on the parameter
  2771. */
  2772. public function get_type($get_name = false)
  2773. {
  2774. $res = false;
  2775. if ($this->debug > 0) {
  2776. error_log('New LP - In learnpath::get_type()', 0);
  2777. }
  2778. if (!empty ($this->type)) {
  2779. if ($get_name) {
  2780. // Get it from the lp_type table in main db.
  2781. } else {
  2782. $res = $this->type;
  2783. }
  2784. }
  2785. if ($this->debug > 2) {
  2786. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2787. }
  2788. return $res;
  2789. }
  2790. /**
  2791. * Gets the learning path type as static method
  2792. * @param boolean Return the name? If false, return the ID. Default is false.
  2793. * @return mixed Type ID or name, depending on the parameter
  2794. */
  2795. public static function get_type_static($lp_id = 0)
  2796. {
  2797. $course_id = api_get_course_int_id();
  2798. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2799. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2800. $res = Database::query($sql);
  2801. if ($res === false) {
  2802. return null;
  2803. }
  2804. if (Database :: num_rows($res) <= 0) {
  2805. return null;
  2806. }
  2807. $row = Database :: fetch_array($res);
  2808. return $row['lp_type'];
  2809. }
  2810. /**
  2811. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2812. * This method can be used as abstract and is recursive
  2813. * @param integer Learnpath ID
  2814. * @param integer Parent ID of the items to look for
  2815. * @return mixed Ordered list of item IDs or false on error
  2816. */
  2817. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2818. {
  2819. if (empty($course_id)) {
  2820. $course_id = api_get_course_int_id();
  2821. } else {
  2822. $course_id = intval($course_id);
  2823. }
  2824. $list = array();
  2825. if (empty ($lp)) {
  2826. return false;
  2827. }
  2828. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2829. $sql = "SELECT id FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent ORDER BY display_order";
  2830. $res = Database::query($sql);
  2831. while ($row = Database :: fetch_array($res)) {
  2832. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2833. $list[] = $row['id'];
  2834. foreach ($sublist as $item) {
  2835. $list[] = $item;
  2836. }
  2837. }
  2838. return $list;
  2839. }
  2840. /**
  2841. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2842. * @param array
  2843. * @return string HTML TOC ready to display
  2844. */
  2845. public function get_html_toc($toc_list = null)
  2846. {
  2847. global $_configuration;
  2848. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2849. if ($this->debug > 0) {
  2850. error_log('In learnpath::get_html_toc()', 0);
  2851. }
  2852. if (empty($toc_list)) {
  2853. $toc_list = $this->get_toc();
  2854. }
  2855. $html = '';
  2856. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2857. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2858. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2859. if ($this->get_lp_session_id() == api_get_session_id()) {
  2860. $html .= '<div id="actions_lp" class="actions_lp">';
  2861. $html .= '<div class="btn-group">';
  2862. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2863. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2864. 'Overview'
  2865. )."</a>";
  2866. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2867. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2868. 'Edit'
  2869. )."</a>";
  2870. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2871. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2872. $html .= '</div>';
  2873. $html .= '</div>';
  2874. }
  2875. }
  2876. require_once 'resourcelinker.inc.php';
  2877. // Temporary variables.
  2878. $mycurrentitemid = $this->get_current_item_id();
  2879. $color_counter = 0;
  2880. $i = 0;
  2881. foreach ($toc_list as $item) {
  2882. // TODO: Complete this
  2883. $icon_name = array(
  2884. 'not attempted' => 'notattempted.gif',
  2885. 'incomplete' => 'incomplete.png',
  2886. 'failed' => 'delete.png',
  2887. 'completed' => 'completed.png',
  2888. 'passed' => 'passed.png',
  2889. 'succeeded' => 'succeeded.png',
  2890. 'browsed' => 'completed.png',
  2891. );
  2892. $style_item = 'scorm_item';
  2893. $active = null;
  2894. if ($item['id'] == $this->current) {
  2895. $scorm_color_background = 'scorm_item_highlight';
  2896. $active = ' active';
  2897. } else {
  2898. if ($color_counter % 2 == 0) {
  2899. $scorm_color_background = 'scorm_item_1';
  2900. } else {
  2901. $scorm_color_background = 'scorm_item_2';
  2902. }
  2903. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2904. $scorm_color_background = ' scorm_item_section ';
  2905. }
  2906. }
  2907. $onClick = null;
  2908. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2909. $onClick = 'onclick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;"';
  2910. }
  2911. $html .= '<a href="" '.$onClick.' class="list-group-item">';
  2912. $imageStatus = null;
  2913. if ($item['type'] == 'quiz') {
  2914. if ($item['status'] == 'completed') {
  2915. $imageStatus = "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2916. } else {
  2917. $imageStatus = "&nbsp;".Display::return_icon($icon_name['not attempted'], substr('not attempted', 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2918. }
  2919. } else {
  2920. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2921. $imageStatus = "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2922. }
  2923. }
  2924. $html .= '<span class="badge_lp">'.$imageStatus."</span>";
  2925. if ($scorm_color_background != '') {
  2926. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2927. }
  2928. // The anchor will let us center the TOC on the currently viewed item &^D
  2929. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2930. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2931. //$html .= '<a name="atoc_'.$item['id'].'"></a>';
  2932. } else {
  2933. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2934. }
  2935. $title = $item['title'];
  2936. if (empty($title)) {
  2937. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2938. }
  2939. $title = Security::remove_XSS($title);
  2940. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2941. //$html .= '<a href="" >'.stripslashes($title).'</a>';
  2942. $html .= $title;
  2943. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2944. $html .= Display::return_icon('lp_dokeos_module.png', $title).'&nbsp;'.$title;
  2945. } elseif ($item['type'] == 'dir') {
  2946. $html .= $title;
  2947. }
  2948. $html .= "</div>";
  2949. if ($scorm_color_background != '') {
  2950. $html .= '</div>';
  2951. }
  2952. $html .= '</a>';
  2953. $color_counter++;
  2954. }
  2955. return $html;
  2956. }
  2957. /**
  2958. * Gets the learnpath maker name - generally the editor's name
  2959. * @return string Learnpath maker name
  2960. */
  2961. public function get_maker()
  2962. {
  2963. if ($this->debug > 0) {
  2964. error_log('New LP - In learnpath::get_maker()', 0);
  2965. }
  2966. if (!empty ($this->maker)) {
  2967. return $this->maker;
  2968. } else {
  2969. return '';
  2970. }
  2971. }
  2972. /**
  2973. * Gets the user-friendly message stored in $this->message
  2974. * @return string Message
  2975. */
  2976. public function get_message()
  2977. {
  2978. if ($this->debug > 0) {
  2979. error_log('New LP - In learnpath::get_message()', 0);
  2980. }
  2981. return $this->message;
  2982. }
  2983. /**
  2984. * Gets the learnpath name/title
  2985. * @return string Learnpath name/title
  2986. */
  2987. public function get_name()
  2988. {
  2989. if ($this->debug > 0) {
  2990. error_log('New LP - In learnpath::get_name()', 0);
  2991. }
  2992. if (!empty ($this->name)) {
  2993. return $this->name;
  2994. } else {
  2995. return 'N/A';
  2996. }
  2997. }
  2998. /**
  2999. * Gets a link to the resource from the present location, depending on item ID.
  3000. * @param string Type of link expected
  3001. * @param integer Learnpath item ID
  3002. * @return string Link to the lp_item resource
  3003. */
  3004. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3005. {
  3006. $course_id = $this->get_course_int_id();
  3007. if ($this->debug > 0) {
  3008. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3009. }
  3010. if (empty($item_id)) {
  3011. if ($this->debug > 2) {
  3012. error_log(
  3013. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  3014. ),
  3015. 0
  3016. );
  3017. }
  3018. $item_id = $this->get_current_item_id();
  3019. }
  3020. if (empty($item_id)) {
  3021. if ($this->debug > 2) {
  3022. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3023. }
  3024. //still empty, this means there was no item_id given and we are not in an object context or
  3025. //the object property is empty, return empty link
  3026. $item_id = $this->first();
  3027. return '';
  3028. }
  3029. $file = '';
  3030. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3031. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3032. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3033. $item_id = Database::escape_string($item_id);
  3034. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3035. FROM $lp_table l
  3036. INNER JOIN $lp_item_table li
  3037. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3038. WHERE li.id = $item_id ";
  3039. if ($this->debug > 2) {
  3040. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3041. }
  3042. $res = Database::query($sql);
  3043. if (Database :: num_rows($res) > 0) {
  3044. $row = Database :: fetch_array($res);
  3045. $lp_type = $row['ltype'];
  3046. $lp_path = $row['lpath'];
  3047. $lp_item_type = $row['litype'];
  3048. $lp_item_path = $row['lipath'];
  3049. $lp_item_params = $row['liparams'];
  3050. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3051. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3052. }
  3053. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3054. if ($type == 'http') {
  3055. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3056. } else {
  3057. $course_path = $sys_course_path; //system path
  3058. }
  3059. // 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.
  3060. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3061. $lp_type = 1;
  3062. }
  3063. if ($this->debug > 2) {
  3064. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3065. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3066. }
  3067. // Now go through the specific cases to get the end of the path
  3068. // @todo Use constants instead of int values.
  3069. switch ($lp_type) {
  3070. case 1 :
  3071. if ($lp_item_type == 'dokeos_chapter') {
  3072. $file = 'lp_content.php?type=dir';
  3073. } else {
  3074. require_once 'resourcelinker.inc.php';
  3075. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3076. if ($this->debug > 0) {
  3077. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3078. }
  3079. if ($lp_item_type == 'link') {
  3080. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3081. if (is_youtube_link($file)) {
  3082. $src = get_youtube_video_id($file);
  3083. $file = 'embed.php?type=youtube&src='.$src;
  3084. }
  3085. if (isVimeoLink($file)) {
  3086. $src = getVimeoLinkId($file);
  3087. $file = 'embed.php?type=vimeo&src='.$src;
  3088. }
  3089. } else {
  3090. // check how much attempts of a exercise exits in lp
  3091. $lp_item_id = $this->get_current_item_id();
  3092. $lp_view_id = $this->get_view_id();
  3093. $prevent_reinit = null;
  3094. if (isset($this->items[$this->current])) {
  3095. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3096. }
  3097. if (empty($provided_toc)) {
  3098. if ($this->debug > 0) {
  3099. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3100. }
  3101. $list = $this->get_toc();
  3102. } else {
  3103. if ($this->debug > 0) {
  3104. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3105. }
  3106. $list = $provided_toc;
  3107. }
  3108. $type_quiz = false;
  3109. foreach ($list as $toc) {
  3110. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3111. $type_quiz = true;
  3112. }
  3113. }
  3114. if ($type_quiz) {
  3115. $lp_item_id = Database::escape_string($lp_item_id);
  3116. $lp_view_id = Database::escape_string($lp_view_id);
  3117. $sql = "SELECT count(*) FROM $lp_item_view_table
  3118. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3119. $result = Database::query($sql);
  3120. $row_count = Database :: fetch_row($result);
  3121. $count_item_view = (int)$row_count[0];
  3122. $not_multiple_attempt = 0;
  3123. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3124. $not_multiple_attempt = 1;
  3125. }
  3126. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3127. }
  3128. $tmp_array = explode('/', $file);
  3129. $document_name = $tmp_array[count($tmp_array) - 1];
  3130. if (strpos($document_name, '_DELETED_')) {
  3131. $file = 'blank.php?error=document_deleted';
  3132. }
  3133. }
  3134. }
  3135. break;
  3136. case 2 :
  3137. if ($this->debug > 2) {
  3138. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3139. }
  3140. if ($lp_item_type != 'dir') {
  3141. // Quite complex here:
  3142. // We want to make sure 'http://' (and similar) links can
  3143. // be loaded as is (withouth the Chamilo path in front) but
  3144. // some contents use this form: resource.htm?resource=http://blablabla
  3145. // which means we have to find a protocol at the path's start, otherwise
  3146. // it should not be considered as an external URL.
  3147. //if ($this->prerequisites_match($item_id)) {
  3148. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3149. if ($this->debug > 2) {
  3150. error_log(
  3151. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3152. 0
  3153. );
  3154. }
  3155. // Distant url, return as is.
  3156. $file = $lp_item_path;
  3157. } else {
  3158. if ($this->debug > 2) {
  3159. error_log(
  3160. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3161. 0
  3162. );
  3163. }
  3164. // Prevent getting untranslatable urls.
  3165. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3166. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3167. // Prepare the path.
  3168. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3169. // TODO: Fix this for urls with protocol header.
  3170. $file = str_replace('//', '/', $file);
  3171. $file = str_replace(':/', '://', $file);
  3172. if (substr($lp_path, -1) == '/') {
  3173. $lp_path = substr($lp_path, 0, -1);
  3174. }
  3175. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3176. // if file not found.
  3177. $decoded = html_entity_decode($lp_item_path);
  3178. list ($decoded) = explode('?', $decoded);
  3179. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3180. require_once 'resourcelinker.inc.php';
  3181. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3182. if (empty($file)) {
  3183. $file = 'blank.php?error=document_not_found';
  3184. } else {
  3185. $tmp_array = explode('/', $file);
  3186. $document_name = $tmp_array[count($tmp_array) - 1];
  3187. if (strpos($document_name, '_DELETED_')) {
  3188. $file = 'blank.php?error=document_deleted';
  3189. } else {
  3190. $file = 'blank.php?error=document_not_found';
  3191. }
  3192. }
  3193. } else {
  3194. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3195. }
  3196. }
  3197. }
  3198. //}else{
  3199. //prerequisites did not match
  3200. //$file = 'blank.php';
  3201. //}
  3202. // We want to use parameters if they were defined in the imsmanifest
  3203. if (strpos($file, 'blank.php') === false) {
  3204. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3205. }
  3206. } else {
  3207. $file = 'lp_content.php?type=dir';
  3208. }
  3209. break;
  3210. case 3 :
  3211. if ($this->debug > 2) {
  3212. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3213. }
  3214. // Formatting AICC HACP append URL.
  3215. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3216. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3217. ).'&';
  3218. if ($lp_item_type != 'dir') {
  3219. // Quite complex here:
  3220. // We want to make sure 'http://' (and similar) links can
  3221. // be loaded as is (withouth the Chamilo path in front) but
  3222. // some contents use this form: resource.htm?resource=http://blablabla
  3223. // which means we have to find a protocol at the path's start, otherwise
  3224. // it should not be considered as an external URL.
  3225. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3226. if ($this->debug > 2) {
  3227. error_log(
  3228. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3229. 0
  3230. );
  3231. }
  3232. // Distant url, return as is.
  3233. $file = $lp_item_path;
  3234. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3235. /*
  3236. if (stristr($file,'<servername>') !== false) {
  3237. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3238. }
  3239. */
  3240. if (stripos($file, '<servername>') !== false) {
  3241. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3242. $web_course_path = str_replace(
  3243. 'https://',
  3244. '',
  3245. str_replace('http://', '', $course_path)
  3246. );
  3247. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3248. }
  3249. //
  3250. $file .= $aicc_append;
  3251. } else {
  3252. if ($this->debug > 2) {
  3253. error_log(
  3254. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3255. 0
  3256. );
  3257. }
  3258. // Prevent getting untranslatable urls.
  3259. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3260. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3261. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3262. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3263. // TODO: Fix this for urls with protocol header.
  3264. $file = str_replace('//', '/', $file);
  3265. $file = str_replace(':/', '://', $file);
  3266. $file .= $aicc_append;
  3267. }
  3268. } else {
  3269. $file = 'lp_content.php?type=dir';
  3270. }
  3271. break;
  3272. case 4 :
  3273. break;
  3274. default :
  3275. break;
  3276. }
  3277. }
  3278. if ($this->debug > 2) {
  3279. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3280. }
  3281. return $file;
  3282. }
  3283. /**
  3284. * Gets the latest usable view or generate a new one
  3285. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3286. * @return integer DB lp_view id
  3287. */
  3288. public function get_view($attempt_num = 0)
  3289. {
  3290. if ($this->debug > 0) {
  3291. error_log('New LP - In learnpath::get_view()', 0);
  3292. }
  3293. $search = '';
  3294. // Use $attempt_num to enable multi-views management (disabled so far).
  3295. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3296. $search = 'AND view_count = '.$attempt_num;
  3297. }
  3298. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3299. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3300. $course_id = api_get_course_int_id();
  3301. $sql = "SELECT id, view_count FROM $lp_view_table
  3302. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3303. )." ".$search.
  3304. " ORDER BY view_count DESC";
  3305. $res = Database::query($sql);
  3306. if (Database :: num_rows($res) > 0) {
  3307. $row = Database :: fetch_array($res);
  3308. $this->lp_view_id = $row['id'];
  3309. } else {
  3310. // There is no database record, create one.
  3311. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3312. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3313. $res = Database::query($sql);
  3314. $id = Database :: insert_id();
  3315. $this->lp_view_id = $id;
  3316. }
  3317. return $this->lp_view_id;
  3318. }
  3319. /**
  3320. * Gets the current view id
  3321. * @return integer View ID (from lp_view)
  3322. */
  3323. public function get_view_id()
  3324. {
  3325. if ($this->debug > 0) {
  3326. error_log('New LP - In learnpath::get_view_id()', 0);
  3327. }
  3328. if (!empty ($this->lp_view_id)) {
  3329. return $this->lp_view_id;
  3330. } else {
  3331. return 0;
  3332. }
  3333. }
  3334. /**
  3335. * Gets the update queue
  3336. * @return array Array containing IDs of items to be updated by JavaScript
  3337. */
  3338. public function get_update_queue()
  3339. {
  3340. if ($this->debug > 0) {
  3341. error_log('New LP - In learnpath::get_update_queue()', 0);
  3342. }
  3343. return $this->update_queue;
  3344. }
  3345. /**
  3346. * Gets the user ID
  3347. * @return integer User ID
  3348. */
  3349. public function get_user_id()
  3350. {
  3351. if ($this->debug > 0) {
  3352. error_log('New LP - In learnpath::get_user_id()', 0);
  3353. }
  3354. if (!empty ($this->user_id)) {
  3355. return $this->user_id;
  3356. } else {
  3357. return false;
  3358. }
  3359. }
  3360. /**
  3361. * Checks if any of the items has an audio element attached
  3362. * @return bool True or false
  3363. */
  3364. public function has_audio()
  3365. {
  3366. if ($this->debug > 1) {
  3367. error_log('New LP - In learnpath::has_audio()', 0);
  3368. }
  3369. $has = false;
  3370. foreach ($this->items as $i => $item) {
  3371. if (!empty ($this->items[$i]->audio)) {
  3372. $has = true;
  3373. break;
  3374. }
  3375. }
  3376. return $has;
  3377. }
  3378. /**
  3379. * Logs a message into a file
  3380. * @param string Message to log
  3381. * @return boolean True on success, false on error or if msg empty
  3382. */
  3383. public function log($msg)
  3384. {
  3385. if ($this->debug > 0) {
  3386. error_log('New LP - In learnpath::log()', 0);
  3387. }
  3388. // TODO
  3389. $this->error .= $msg;
  3390. return true;
  3391. }
  3392. /**
  3393. * Moves an item up and down at its level
  3394. * @param integer Item to move up and down
  3395. * @param string Direction 'up' or 'down'
  3396. * @return integer New display order, or false on error
  3397. */
  3398. public function move_item($id, $direction)
  3399. {
  3400. $course_id = api_get_course_int_id();
  3401. if ($this->debug > 0) {
  3402. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3403. }
  3404. if (empty ($id) or empty ($direction)) {
  3405. return false;
  3406. }
  3407. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3408. $sql_sel = "SELECT *
  3409. FROM ".$tbl_lp_item."
  3410. WHERE c_id = ".$course_id." AND id = ".$id;
  3411. $res_sel = Database::query($sql_sel);
  3412. // Check if elem exists.
  3413. if (Database :: num_rows($res_sel) < 1) {
  3414. return false;
  3415. }
  3416. // Gather data.
  3417. $row = Database :: fetch_array($res_sel);
  3418. $previous = $row['previous_item_id'];
  3419. $next = $row['next_item_id'];
  3420. $display = $row['display_order'];
  3421. $parent = $row['parent_item_id'];
  3422. $lp = $row['lp_id'];
  3423. // Update the item (switch with previous/next one).
  3424. switch ($direction) {
  3425. case 'up' :
  3426. if ($this->debug > 2) {
  3427. error_log('Movement up detected', 0);
  3428. }
  3429. if ($display <= 1) { /*do nothing*/
  3430. } else {
  3431. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3432. WHERE c_id = ".$course_id." AND id = $previous";
  3433. if ($this->debug > 2) {
  3434. error_log('Selecting previous: '.$sql_sel2, 0);
  3435. }
  3436. $res_sel2 = Database::query($sql_sel2);
  3437. if (Database :: num_rows($res_sel2) < 1) {
  3438. $previous_previous = 0;
  3439. }
  3440. // Gather data.
  3441. $row2 = Database :: fetch_array($res_sel2);
  3442. $previous_previous = $row2['previous_item_id'];
  3443. // Update previous_previous item (switch "next" with current).
  3444. if ($previous_previous != 0) {
  3445. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3446. if ($this->debug > 2) {
  3447. error_log($sql_upd2, 0);
  3448. }
  3449. $res_upd2 = Database::query($sql_upd2);
  3450. }
  3451. // Update previous item (switch with current).
  3452. if ($previous != 0) {
  3453. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3454. WHERE c_id = ".$course_id." AND id = $previous";
  3455. if ($this->debug > 2) {
  3456. error_log($sql_upd2, 0);
  3457. }
  3458. $res_upd2 = Database::query($sql_upd2);
  3459. }
  3460. // Update current item (switch with previous).
  3461. if ($id != 0) {
  3462. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3463. WHERE c_id = ".$course_id." AND id = $id";
  3464. if ($this->debug > 2) {
  3465. error_log($sql_upd2, 0);
  3466. }
  3467. $res_upd2 = Database::query($sql_upd2);
  3468. }
  3469. // Update next item (new previous item).
  3470. if ($next != 0) {
  3471. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3472. WHERE c_id = ".$course_id." AND id = $next";
  3473. if ($this->debug > 2) {
  3474. error_log($sql_upd2, 0);
  3475. }
  3476. $res_upd2 = Database::query($sql_upd2);
  3477. }
  3478. $display = $display - 1;
  3479. }
  3480. break;
  3481. case 'down' :
  3482. if ($this->debug > 2) {
  3483. error_log('Movement down detected', 0);
  3484. }
  3485. if ($next == 0) { /* Do nothing. */
  3486. } else {
  3487. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3488. if ($this->debug > 2) {
  3489. error_log('Selecting next: '.$sql_sel2, 0);
  3490. }
  3491. $res_sel2 = Database::query($sql_sel2);
  3492. if (Database :: num_rows($res_sel2) < 1) {
  3493. $next_next = 0;
  3494. }
  3495. // Gather data.
  3496. $row2 = Database :: fetch_array($res_sel2);
  3497. $next_next = $row2['next_item_id'];
  3498. // Update previous item (switch with current).
  3499. if ($previous != 0) {
  3500. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3501. WHERE c_id = ".$course_id." AND id = $previous";
  3502. $res_upd2 = Database::query($sql_upd2);
  3503. }
  3504. // Update current item (switch with previous).
  3505. if ($id != 0) {
  3506. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3507. WHERE c_id = ".$course_id." AND id = $id";
  3508. $res_upd2 = Database::query($sql_upd2);
  3509. }
  3510. // Update next item (new previous item).
  3511. if ($next != 0) {
  3512. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3513. WHERE c_id = ".$course_id." AND id = $next";
  3514. $res_upd2 = Database::query($sql_upd2);
  3515. }
  3516. // Update next_next item (switch "previous" with current).
  3517. if ($next_next != 0) {
  3518. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3519. WHERE c_id = ".$course_id." AND id = $next_next";
  3520. $res_upd2 = Database::query($sql_upd2);
  3521. }
  3522. $display = $display + 1;
  3523. }
  3524. break;
  3525. default :
  3526. return false;
  3527. }
  3528. return $display;
  3529. }
  3530. /**
  3531. * Move a learnpath up (display_order)
  3532. * @param integer Learnpath ID
  3533. */
  3534. public function move_up($lp_id)
  3535. {
  3536. $course_id = api_get_course_int_id();
  3537. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3538. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3539. $res = Database::query($sql);
  3540. if ($res === false) {
  3541. return false;
  3542. }
  3543. $lps = array();
  3544. $lp_order = array();
  3545. $num = Database :: num_rows($res);
  3546. // First check the order is correct, globally (might be wrong because
  3547. // of versions < 1.8.4)
  3548. if ($num > 0) {
  3549. $i = 1;
  3550. while ($row = Database :: fetch_array($res)) {
  3551. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3552. $need_fix = true;
  3553. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3554. $res_u = Database::query($sql_u);
  3555. }
  3556. $row['display_order'] = $i;
  3557. $lps[$row['id']] = $row;
  3558. $lp_order[$i] = $row['id'];
  3559. $i++;
  3560. }
  3561. }
  3562. if ($num > 1) { // If there's only one element, no need to sort.
  3563. $order = $lps[$lp_id]['display_order'];
  3564. if ($order > 1) { // If it's the first element, no need to move up.
  3565. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3566. $res_u1 = Database::query($sql_u1);
  3567. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3568. $res_u2 = Database::query($sql_u2);
  3569. }
  3570. }
  3571. }
  3572. /**
  3573. * Move a learnpath down (display_order)
  3574. * @param integer Learnpath ID
  3575. */
  3576. public function move_down($lp_id)
  3577. {
  3578. $course_id = api_get_course_int_id();
  3579. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3580. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3581. $res = Database::query($sql);
  3582. if ($res === false) {
  3583. return false;
  3584. }
  3585. $lps = array();
  3586. $lp_order = array();
  3587. $num = Database :: num_rows($res);
  3588. $max = 0;
  3589. // First check the order is correct, globally (might be wrong because
  3590. // of versions < 1.8.4).
  3591. if ($num > 0) {
  3592. $i = 1;
  3593. while ($row = Database :: fetch_array($res)) {
  3594. $max = $i;
  3595. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3596. $need_fix = true;
  3597. $sql_u = "UPDATE $lp_table SET display_order = $i
  3598. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3599. $res_u = Database::query($sql_u);
  3600. }
  3601. $row['display_order'] = $i;
  3602. $lps[$row['id']] = $row;
  3603. $lp_order[$i] = $row['id'];
  3604. $i++;
  3605. }
  3606. }
  3607. if ($num > 1) { // If there's only one element, no need to sort.
  3608. $order = $lps[$lp_id]['display_order'];
  3609. if ($order < $max) { // If it's the first element, no need to move up.
  3610. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3611. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3612. $res_u1 = Database::query($sql_u1);
  3613. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3614. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3615. $res_u2 = Database::query($sql_u2);
  3616. }
  3617. }
  3618. }
  3619. /**
  3620. * Updates learnpath attributes to point to the next element
  3621. * The last part is similar to set_current_item but processing the other way around
  3622. */
  3623. public function next()
  3624. {
  3625. if ($this->debug > 0) {
  3626. error_log('New LP - In learnpath::next()', 0);
  3627. }
  3628. $this->last = $this->get_current_item_id();
  3629. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3630. $this->autocomplete_parents($this->last);
  3631. $new_index = $this->get_next_index();
  3632. if ($this->debug > 2) {
  3633. error_log('New LP - New index: '.$new_index, 0);
  3634. }
  3635. $this->index = $new_index;
  3636. if ($this->debug > 2) {
  3637. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3638. }
  3639. $this->current = $this->ordered_items[$new_index];
  3640. if ($this->debug > 2) {
  3641. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3642. }
  3643. }
  3644. /**
  3645. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3646. * class, this might be redefined to allow several behaviours depending on the document type.
  3647. * @param integer Resource ID
  3648. * @return boolean True on success, false otherwise
  3649. */
  3650. public function open($id)
  3651. {
  3652. if ($this->debug > 0) {
  3653. error_log('New LP - In learnpath::open()', 0);
  3654. }
  3655. // TODO:
  3656. // set the current resource attribute to this resource
  3657. // switch on element type (redefine in child class?)
  3658. // set status for this item to "opened"
  3659. // start timer
  3660. // initialise score
  3661. $this->index = 0; //or = the last item seen (see $this->last)
  3662. }
  3663. /**
  3664. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3665. * and the prerequisite string on error.
  3666. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3667. * @param integer Optional item ID. If none given, uses the current open item.
  3668. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3669. */
  3670. public function prerequisites_match($item = null)
  3671. {
  3672. if ($this->debug > 0) {
  3673. error_log('In learnpath::prerequisites_match()', 0);
  3674. }
  3675. if (empty($item)) {
  3676. $item = $this->current;
  3677. }
  3678. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3679. if ($this->type == 2) {
  3680. //Getting prereq from scorm
  3681. $prereq_string = $this->get_scorm_prereq_string($item);
  3682. } else {
  3683. $prereq_string = $this->items[$item]->get_prereq_string();
  3684. }
  3685. if (empty($prereq_string)) {
  3686. return true;
  3687. }
  3688. // Clean spaces.
  3689. $prereq_string = str_replace(' ', '', $prereq_string);
  3690. if ($this->debug > 0) {
  3691. error_log('Found prereq_string: '.$prereq_string, 0);
  3692. }
  3693. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3694. $result = $this->items[$item]->parse_prereq(
  3695. $prereq_string,
  3696. $this->items,
  3697. $this->refs_list,
  3698. $this->get_user_id()
  3699. );
  3700. if ($result === false) {
  3701. $this->set_error_msg($this->items[$item]->prereq_alert);
  3702. }
  3703. } else {
  3704. $result = true;
  3705. if ($this->debug > 1) {
  3706. error_log('$this->items['.$item.'] was not an object', 0);
  3707. }
  3708. }
  3709. if ($this->debug > 1) {
  3710. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3711. }
  3712. return $result;
  3713. }
  3714. /**
  3715. * Updates learnpath attributes to point to the previous element
  3716. * The last part is similar to set_current_item but processing the other way around
  3717. */
  3718. public function previous()
  3719. {
  3720. if ($this->debug > 0) {
  3721. error_log('New LP - In learnpath::previous()', 0);
  3722. }
  3723. $this->last = $this->get_current_item_id();
  3724. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3725. $this->autocomplete_parents($this->last);
  3726. $new_index = $this->get_previous_index();
  3727. $this->index = $new_index;
  3728. $this->current = $this->ordered_items[$new_index];
  3729. }
  3730. /**
  3731. * Publishes a learnpath. This basically means show or hide the learnpath
  3732. * to normal users.
  3733. * Can be used as abstract
  3734. * @param integer Learnpath ID
  3735. * @param string New visibility
  3736. */
  3737. public function toggle_visibility($lp_id, $set_visibility = 1, $course_id = null)
  3738. {
  3739. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3740. $action = 'visible';
  3741. if ($set_visibility != 1) {
  3742. $action = 'invisible';
  3743. }
  3744. if (!empty($course_id)) {
  3745. $course = api_get_course_info_by_id($course_id);
  3746. } else {
  3747. $course = api_get_course_info();
  3748. }
  3749. return api_item_property_update($course, TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3750. }
  3751. /**
  3752. * Publishes a learnpath. This basically means show or hide the learnpath
  3753. * on the course homepage
  3754. * Can be used as abstract
  3755. * @param integer Learnpath id
  3756. * @param string New visibility (v/s - visible/invisible)
  3757. */
  3758. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3759. {
  3760. $course_id = api_get_course_int_id();
  3761. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3762. $lp_id = Database::escape_string($lp_id);
  3763. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3764. $result = Database::query($sql);
  3765. if (Database::num_rows($result)) {
  3766. $row = Database :: fetch_array($result);
  3767. $name = Text::domesticate($row['name']);
  3768. if ($set_visibility == 'i') {
  3769. $s = $name." ".get_lang('LearnpathNotPublished');
  3770. $dialogBox = $s;
  3771. $v = 0;
  3772. }
  3773. if ($set_visibility == 'v') {
  3774. $s = $name." ".get_lang('LearnpathPublished');
  3775. $dialogBox = $s;
  3776. $v = 1;
  3777. }
  3778. } else {
  3779. return false;
  3780. }
  3781. $session_id = api_get_session_id();
  3782. $session_condition = api_get_session_condition($session_id);
  3783. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3784. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3785. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3786. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3787. $result = Database::query($sql);
  3788. $num = Database :: num_rows($result);
  3789. if (($set_visibility == 'i') && ($num > 0)) {
  3790. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3791. Database::query($sql);
  3792. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3793. Database::query($sql);
  3794. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3795. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3796. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3797. Database::query($sql);
  3798. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3799. $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
  3800. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3801. Database::query($sql);
  3802. } else {
  3803. // Parameter and database incompatible, do nothing, exit.
  3804. return false;
  3805. }
  3806. }
  3807. /**
  3808. * Restart the whole learnpath. Return the URL of the first element.
  3809. * Make sure the results are saved with anoter method. This method should probably be
  3810. * redefined in children classes.
  3811. * To use a similar method statically, use the create_new_attempt() method
  3812. * @return string URL to load in the viewer
  3813. */
  3814. public function restart()
  3815. {
  3816. if ($this->debug > 0) {
  3817. error_log('New LP - In learnpath::restart()', 0);
  3818. }
  3819. // TODO
  3820. // Call autosave method to save the current progress.
  3821. //$this->index = 0;
  3822. $session_id = api_get_session_id();
  3823. $course_id = api_get_course_int_id();
  3824. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3825. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3826. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3827. if ($this->debug > 2) {
  3828. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3829. }
  3830. $res = Database::query($sql);
  3831. if ($view_id = Database :: insert_id($res)) {
  3832. $this->lp_view_id = $view_id;
  3833. $this->attempt = $this->attempt + 1;
  3834. } else {
  3835. $this->error = 'Could not insert into item_view table...';
  3836. return false;
  3837. }
  3838. $this->autocomplete_parents($this->current);
  3839. foreach ($this->items as $index => $dummy) {
  3840. $this->items[$index]->restart();
  3841. $this->items[$index]->set_lp_view($this->lp_view_id);
  3842. }
  3843. $this->first();
  3844. return true;
  3845. }
  3846. /**
  3847. * Saves the current item
  3848. * @return boolean
  3849. */
  3850. public function save_current()
  3851. {
  3852. if ($this->debug > 0) {
  3853. error_log('learnpath::save_current()', 0);
  3854. }
  3855. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3856. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3857. if ($this->debug > 2) {
  3858. error_log('New LP - save_current() saving item '.$this->current, 0);
  3859. }
  3860. if ($this->debug > 2) {
  3861. error_log(''.print_r($this->items, true), 0);
  3862. }
  3863. if (is_object($this->items[$this->current])) {
  3864. //$res = $this->items[$this->current]->save(false);
  3865. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3866. $this->autocomplete_parents($this->current);
  3867. $status = $this->items[$this->current]->get_status();
  3868. $this->append_message('new_item_status: '.$status);
  3869. $this->update_queue[$this->current] = $status;
  3870. return $res;
  3871. }
  3872. return false;
  3873. }
  3874. /**
  3875. * Saves the given item
  3876. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3877. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3878. * @return boolean
  3879. */
  3880. public function save_item($item_id = null, $from_outside = true)
  3881. {
  3882. $debug = $this->debug;
  3883. if ($debug) {
  3884. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3885. }
  3886. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3887. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3888. if (empty($item_id)) {
  3889. $item_id = intval($_REQUEST['id']);
  3890. }
  3891. if (empty($item_id)) {
  3892. $item_id = $this->get_current_item_id();
  3893. }
  3894. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3895. if ($debug) {
  3896. error_log('Object exists');
  3897. }
  3898. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3899. if ($debug) {
  3900. error_log('update_queue before:');
  3901. error_log(print_r($this->update_queue, 1));
  3902. }
  3903. $this->autocomplete_parents($item_id);
  3904. $status = $this->items[$item_id]->get_status();
  3905. $this->update_queue[$item_id] = $status;
  3906. if ($debug) {
  3907. error_log('get_status(): '.$status);
  3908. error_log('update_queue after:');
  3909. error_log(print_r($this->update_queue, 1));
  3910. }
  3911. return $res;
  3912. }
  3913. return false;
  3914. }
  3915. /**
  3916. * Saves the last item seen's ID only in case
  3917. */
  3918. public function save_last()
  3919. {
  3920. $course_id = api_get_course_int_id();
  3921. if ($this->debug > 0) {
  3922. error_log('New LP - In learnpath::save_last()', 0);
  3923. }
  3924. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3925. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3926. if (isset($this->current)) {
  3927. if ($this->debug > 2) {
  3928. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3929. }
  3930. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3931. WHERE c_id = $course_id AND
  3932. lp_id = ".$this->get_id()." AND
  3933. user_id = ".$this->get_user_id()." ".$session_condition;
  3934. if ($this->debug > 2) {
  3935. error_log('New LP - Saving last item seen : '.$sql, 0);
  3936. }
  3937. $res = Database::query($sql);
  3938. }
  3939. // Save progress.
  3940. list($progress, $text) = $this->get_progress_bar_text('%');
  3941. if ($progress >= 0 && $progress <= 100) {
  3942. $progress = (int)$progress;
  3943. $sql = "UPDATE $table SET progress = $progress
  3944. WHERE c_id = ".$course_id." AND
  3945. lp_id = ".$this->get_id()." AND
  3946. user_id = ".$this->get_user_id()." ".$session_condition;
  3947. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3948. $this->progress_db = $progress;
  3949. }
  3950. }
  3951. /**
  3952. * Sets the current item ID (checks if valid and authorized first)
  3953. * @param integer New item ID. If not given or not authorized, defaults to current
  3954. */
  3955. public function set_current_item($item_id = null)
  3956. {
  3957. if ($this->debug > 0) {
  3958. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3959. }
  3960. if (empty ($item_id)) {
  3961. if ($this->debug > 2) {
  3962. error_log('New LP - No new current item given, ignore...', 0);
  3963. }
  3964. // Do nothing.
  3965. } else {
  3966. if ($this->debug > 2) {
  3967. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3968. }
  3969. if (is_numeric($item_id)) {
  3970. $item_id = Database::escape_string($item_id);
  3971. // TODO: Check in database here.
  3972. $this->last = $this->current;
  3973. $this->current = $item_id;
  3974. // TODO: Update $this->index as well.
  3975. foreach ($this->ordered_items as $index => $item) {
  3976. if ($item == $this->current) {
  3977. $this->index = $index;
  3978. break;
  3979. }
  3980. }
  3981. if ($this->debug > 2) {
  3982. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  3983. }
  3984. } else {
  3985. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  3986. }
  3987. }
  3988. }
  3989. /**
  3990. * Sets the encoding
  3991. * @param string New encoding
  3992. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3993. */
  3994. public function set_encoding($enc = 'UTF-8')
  3995. {
  3996. if ($this->debug > 0) {
  3997. error_log('New LP - In learnpath::set_encoding()', 0);
  3998. }
  3999. $course_id = api_get_course_int_id();
  4000. /* // Deprecated code (Chamilo 1.8.8).
  4001. $enc = strtoupper($enc);
  4002. $encodings = array (
  4003. 'UTF-8',
  4004. 'ISO-8859-1',
  4005. 'ISO-8859-15',
  4006. 'cp1251',
  4007. 'cp1252',
  4008. 'KOI8-R',
  4009. 'BIG5',
  4010. 'GB2312',
  4011. 'Shift_JIS',
  4012. 'EUC-JP',
  4013. ''
  4014. );
  4015. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  4016. $lp = $this->get_id();
  4017. if ($lp != 0) {
  4018. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4019. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4020. $res = Database::query($sql);
  4021. return $res;
  4022. }
  4023. }
  4024. return false;
  4025. */
  4026. $enc = api_refine_encoding_id($enc);
  4027. if (empty($enc)) {
  4028. $enc = api_get_system_encoding();
  4029. }
  4030. if (api_is_encoding_supported($enc)) {
  4031. $lp = $this->get_id();
  4032. if ($lp != 0) {
  4033. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4034. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4035. $res = Database::query($sql);
  4036. return $res;
  4037. }
  4038. }
  4039. return false;
  4040. }
  4041. /**
  4042. * Sets the JS lib setting in the database directly.
  4043. * This is the JavaScript library file this lp needs to load on startup
  4044. * @param string Proximity setting
  4045. * @return boolean True on update success. False otherwise.
  4046. */
  4047. public function set_jslib($lib = '')
  4048. {
  4049. if ($this->debug > 0) {
  4050. error_log('New LP - In learnpath::set_jslib()', 0);
  4051. }
  4052. $lp = $this->get_id();
  4053. $course_id = api_get_course_int_id();
  4054. if ($lp != 0) {
  4055. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4056. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4057. $res = Database::query($sql);
  4058. return $res;
  4059. } else {
  4060. return false;
  4061. }
  4062. }
  4063. /**
  4064. * Sets the name of the LP maker (publisher) (and save)
  4065. * @param string Optional string giving the new content_maker of this learnpath
  4066. * @return boolean True
  4067. */
  4068. public function set_maker($name = '')
  4069. {
  4070. if ($this->debug > 0) {
  4071. error_log('New LP - In learnpath::set_maker()', 0);
  4072. }
  4073. if (empty ($name)) {
  4074. return false;
  4075. }
  4076. $this->maker = Database::escape_string($name);
  4077. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4078. $course_id = api_get_course_int_id();
  4079. $lp_id = $this->get_id();
  4080. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4081. if ($this->debug > 2) {
  4082. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4083. }
  4084. Database::query($sql);
  4085. return true;
  4086. }
  4087. /**
  4088. * Sets the name of the current learnpath (and save)
  4089. * @param string Optional string giving the new name of this learnpath
  4090. * @return boolean True/False
  4091. */
  4092. public function set_name($name = null)
  4093. {
  4094. if ($this->debug > 0) {
  4095. error_log('New LP - In learnpath::set_name()', 0);
  4096. }
  4097. if (empty($name)) {
  4098. return false;
  4099. }
  4100. $this->name = Database::escape_string($name);
  4101. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4102. $lp_id = $this->get_id();
  4103. $course_id = api_get_course_int_id();
  4104. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4105. if ($this->debug > 2) {
  4106. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4107. }
  4108. $result = Database::query($sql);
  4109. // If the lp is visible on the homepage, change his name there.
  4110. if (Database::affected_rows($result)) {
  4111. $session_id = api_get_session_id();
  4112. $session_condition = api_get_session_condition($session_id);
  4113. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4114. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4115. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4116. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4117. Database::query($sql);
  4118. return true;
  4119. } else {
  4120. return false;
  4121. }
  4122. }
  4123. /**
  4124. * Set index specified prefix terms for all items in this path
  4125. * @param string Comma-separated list of terms
  4126. * @param char Xapian term prefix
  4127. * @return boolean False on error, true otherwise
  4128. */
  4129. public function set_terms_by_prefix($terms_string, $prefix)
  4130. {
  4131. $course_id = api_get_course_int_id();
  4132. if (api_get_setting('search_enabled') !== 'true') {
  4133. return false;
  4134. }
  4135. if (!extension_loaded('xapian')) {
  4136. return false;
  4137. }
  4138. $terms_string = trim($terms_string);
  4139. $terms = explode(',', $terms_string);
  4140. array_walk($terms, 'trim_value');
  4141. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4142. // Don't do anything if no change, verify only at DB, not the search engine.
  4143. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4144. return false;
  4145. }
  4146. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4147. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4148. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4149. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4150. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4151. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4152. $lp_id = intval($_POST['lp_id']);
  4153. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4154. $result = Database::query($sql);
  4155. $di = new ChamiloIndexer();
  4156. while ($lp_item = Database :: fetch_array($result)) {
  4157. // Get search_did.
  4158. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4159. $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';
  4160. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4161. //echo $sql; echo '<br>';
  4162. $res = Database::query($sql);
  4163. if (Database::num_rows($res) > 0) {
  4164. $se_ref = Database :: fetch_array($res);
  4165. // Compare terms.
  4166. $doc = $di->get_document($se_ref['search_did']);
  4167. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4168. $xterms = array();
  4169. foreach ($xapian_terms as $xapian_term) {
  4170. $xterms[] = substr($xapian_term['name'], 1);
  4171. }
  4172. $dterms = $terms;
  4173. $missing_terms = array_diff($dterms, $xterms);
  4174. $deprecated_terms = array_diff($xterms, $dterms);
  4175. // Save it to search engine.
  4176. foreach ($missing_terms as $term) {
  4177. $doc->add_term($prefix.$term, 1);
  4178. }
  4179. foreach ($deprecated_terms as $term) {
  4180. $doc->remove_term($prefix.$term);
  4181. }
  4182. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4183. $di->getDb()->flush();
  4184. } else {
  4185. //@todo What we should do here?
  4186. }
  4187. }
  4188. return true;
  4189. }
  4190. /**
  4191. * Sets the theme of the LP (local/remote) (and save)
  4192. * @param string Optional string giving the new theme of this learnpath
  4193. * @return bool Returns true if theme name is not empty
  4194. */
  4195. public function set_theme($name = '')
  4196. {
  4197. $course_id = api_get_course_int_id();
  4198. if ($this->debug > 0) {
  4199. error_log('New LP - In learnpath::set_theme()', 0);
  4200. }
  4201. $this->theme = Database::escape_string($name);
  4202. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4203. $lp_id = $this->get_id();
  4204. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4205. if ($this->debug > 2) {
  4206. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4207. }
  4208. Database::query($sql);
  4209. return true;
  4210. }
  4211. /**
  4212. * Sets the image of an LP (and save)
  4213. * @param string Optional string giving the new image of this learnpath
  4214. * @return bool Returns true if theme name is not empty
  4215. */
  4216. public function set_preview_image($name = '')
  4217. {
  4218. $course_id = api_get_course_int_id();
  4219. if ($this->debug > 0) {
  4220. error_log('New LP - In learnpath::set_preview_image()', 0);
  4221. }
  4222. $this->preview_image = Database::escape_string($name);
  4223. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4224. $lp_id = $this->get_id();
  4225. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4226. if ($this->debug > 2) {
  4227. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4228. }
  4229. Database::query($sql);
  4230. return true;
  4231. }
  4232. /**
  4233. * Sets the author of a LP (and save)
  4234. * @param string Optional string giving the new author of this learnpath
  4235. * @return bool Returns true if author's name is not empty
  4236. */
  4237. public function set_author($name = '')
  4238. {
  4239. $course_id = api_get_course_int_id();
  4240. if ($this->debug > 0) {
  4241. error_log('New LP - In learnpath::set_author()', 0);
  4242. }
  4243. $this->author = Database::escape_string($name);
  4244. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4245. $lp_id = $this->get_id();
  4246. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4247. if ($this->debug > 2) {
  4248. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4249. }
  4250. Database::query($sql);
  4251. return true;
  4252. }
  4253. /**
  4254. * Sets the hide_toc_frame parameter of a LP (and save)
  4255. * @param int 1 if frame is hidden 0 then else
  4256. * @return bool Returns true if author's name is not empty
  4257. */
  4258. public function set_hide_toc_frame($hide)
  4259. {
  4260. $course_id = api_get_course_int_id();
  4261. if ($this->debug > 0) {
  4262. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4263. }
  4264. if (intval($hide) == $hide) {
  4265. $this->hide_toc_frame = $hide;
  4266. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4267. $lp_id = $this->get_id();
  4268. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4269. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4270. if ($this->debug > 2) {
  4271. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4272. }
  4273. Database::query($sql);
  4274. return true;
  4275. } else {
  4276. return false;
  4277. }
  4278. }
  4279. /**
  4280. * Sets the prerequisite of a LP (and save)
  4281. * @param int integer giving the new prerequisite of this learnpath
  4282. * @return bool returns true if prerequisite is not empty
  4283. */
  4284. public function set_prerequisite($prerequisite)
  4285. {
  4286. $course_id = api_get_course_int_id();
  4287. if ($this->debug > 0) {
  4288. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4289. }
  4290. $this->prerequisite = intval($prerequisite);
  4291. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4292. $lp_id = $this->get_id();
  4293. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4294. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4295. if ($this->debug > 2) {
  4296. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4297. }
  4298. Database::query($sql);
  4299. return true;
  4300. }
  4301. /**
  4302. * Sets the location/proximity of the LP (local/remote) (and save)
  4303. * @param string Optional string giving the new location of this learnpath
  4304. * @return boolean True on success / False on error
  4305. */
  4306. public function set_proximity($name = '')
  4307. {
  4308. $course_id = api_get_course_int_id();
  4309. if ($this->debug > 0) {
  4310. error_log('New LP - In learnpath::set_proximity()', 0);
  4311. }
  4312. if (empty ($name)) {
  4313. return false;
  4314. }
  4315. $this->proximity = Database::escape_string($name);
  4316. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4317. $lp_id = $this->get_id();
  4318. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4319. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4320. if ($this->debug > 2) {
  4321. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4322. }
  4323. Database::query($sql);
  4324. return true;
  4325. }
  4326. /**
  4327. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4328. * @param integer DB ID of the item
  4329. */
  4330. public function set_previous_item($id)
  4331. {
  4332. if ($this->debug > 0) {
  4333. error_log('New LP - In learnpath::set_previous_item()', 0);
  4334. }
  4335. $this->last = $id;
  4336. }
  4337. public function get_max_attempts()
  4338. {
  4339. return $this->max_attempts;
  4340. }
  4341. public function get_subscribe_users()
  4342. {
  4343. return $this->subscribe_users;
  4344. }
  4345. public function check_attempts()
  4346. {
  4347. $max_attempts = $this->get_max_attempts();
  4348. switch ($max_attempts) {
  4349. case 0: //unlimited
  4350. return true;
  4351. break;
  4352. case $max_attempts >= 1:
  4353. if ($this->attempt <= $max_attempts) {
  4354. return true;
  4355. }
  4356. break;
  4357. }
  4358. return false;
  4359. }
  4360. function check_item_attempts($item_id)
  4361. {
  4362. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4363. $count = $this->items[$item_id]->get_view_count();
  4364. $max_attempts = $this->get_max_attempts();
  4365. switch ($max_attempts) {
  4366. case 0: //unlimited
  4367. return true;
  4368. break;
  4369. case $max_attempts >= 1:
  4370. if ($count <= $max_attempts) {
  4371. return true;
  4372. }
  4373. break;
  4374. }
  4375. return false;
  4376. }
  4377. }
  4378. public function set_max_attempts($attempts)
  4379. {
  4380. $mode = 'multiple';
  4381. if ($attempts == 1) {
  4382. $mode = 'single';
  4383. }
  4384. $this->max_attempts = intval($attempts);
  4385. $lp_id = $this->get_id();
  4386. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4387. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4388. Database::query($sql);
  4389. $this->set_attempt_mode($mode);
  4390. }
  4391. /**
  4392. * Sets subscribe_users
  4393. * @param string $value Optional string giving the new location of this learnpath
  4394. * @return bool True on success / False on error
  4395. */
  4396. public function set_subscribe_users($value = 0)
  4397. {
  4398. if ($this->debug > 0) {
  4399. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4400. }
  4401. $this->subscribe_users = intval($value);;
  4402. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4403. $lp_id = $this->get_id();
  4404. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4405. if ($this->debug > 2) {
  4406. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4407. }
  4408. Database::query($sql);
  4409. return true;
  4410. }
  4411. /**
  4412. * Sets use_max_score
  4413. * @param string Optional string giving the new location of this learnpath
  4414. * @return boolean True on success / False on error
  4415. */
  4416. public function set_use_max_score($use_max_score = 1)
  4417. {
  4418. if ($this->debug > 0) {
  4419. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4420. }
  4421. $this->use_max_score = intval($use_max_score);
  4422. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4423. $lp_id = $this->get_id();
  4424. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4425. if ($this->debug > 2) {
  4426. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4427. }
  4428. Database::query($sql);
  4429. return true;
  4430. }
  4431. /**
  4432. * Sets and saves the expired_on date
  4433. * @param string Optional string giving the new author of this learnpath
  4434. * @return bool Returns true if author's name is not empty
  4435. */
  4436. public function set_expired_on($expired_on)
  4437. {
  4438. $course_id = api_get_course_int_id();
  4439. if ($this->debug > 0) {
  4440. error_log('New LP - In learnpath::set_expired_on()', 0);
  4441. }
  4442. if (!empty($expired_on)) {
  4443. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4444. } else {
  4445. $this->expired_on = '';
  4446. }
  4447. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4448. $lp_id = $this->get_id();
  4449. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4450. if ($this->debug > 2) {
  4451. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4452. }
  4453. Database::query($sql);
  4454. return true;
  4455. }
  4456. /**
  4457. * Sets and saves the publicated_on date
  4458. * @param string Optional string giving the new author of this learnpath
  4459. * @return bool Returns true if author's name is not empty
  4460. */
  4461. public function set_publicated_on($publicated_on)
  4462. {
  4463. $course_id = api_get_course_int_id();
  4464. if ($this->debug > 0) {
  4465. error_log('New LP - In learnpath::set_expired_on()', 0);
  4466. }
  4467. if (!empty($publicated_on)) {
  4468. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4469. } else {
  4470. $this->publicated_on = '';
  4471. }
  4472. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4473. $lp_id = $this->get_id();
  4474. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4475. if ($this->debug > 2) {
  4476. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4477. }
  4478. Database::query($sql);
  4479. return true;
  4480. }
  4481. public function set_category_id($category_id)
  4482. {
  4483. $this->category_id = $category_id;
  4484. $course_id = api_get_course_int_id();
  4485. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4486. $lp_id = $this->get_id();
  4487. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4488. $category_id
  4489. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4490. Database::query($sql);
  4491. return true;
  4492. }
  4493. /**
  4494. * Sets and saves the expired_on date
  4495. * @param string Optional string giving the new author of this learnpath
  4496. * @return bool Returns true if author's name is not empty
  4497. */
  4498. public function set_modified_on()
  4499. {
  4500. $course_id = api_get_course_int_id();
  4501. if ($this->debug > 0) {
  4502. error_log('New LP - In learnpath::set_expired_on()', 0);
  4503. }
  4504. $this->modified_on = api_get_utc_datetime();
  4505. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4506. $lp_id = $this->get_id();
  4507. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4508. if ($this->debug > 2) {
  4509. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4510. }
  4511. Database::query($sql);
  4512. return true;
  4513. }
  4514. /**
  4515. * Sets the object's error message
  4516. * @param string Error message. If empty, reinits the error string
  4517. * @return void
  4518. */
  4519. public function set_error_msg($error = '')
  4520. {
  4521. if ($this->debug > 0) {
  4522. error_log('New LP - In learnpath::set_error_msg()', 0);
  4523. }
  4524. if (empty ($error)) {
  4525. $this->error = '';
  4526. } else {
  4527. $this->error .= $error;
  4528. }
  4529. }
  4530. /**
  4531. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4532. * @param boolean Whether to allow a new attempt or not
  4533. * @return boolean True
  4534. */
  4535. public function start_current_item($allow_new_attempt = false)
  4536. {
  4537. if ($this->debug > 0) {
  4538. error_log('New LP - In learnpath::start_current_item()', 0);
  4539. }
  4540. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4541. $type = $this->get_type();
  4542. $item_type = $this->items[$this->current]->get_type();
  4543. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4544. $this->items[$this->current]->open($allow_new_attempt);
  4545. $this->autocomplete_parents($this->current);
  4546. $prereq_check = $this->prerequisites_match($this->current);
  4547. $this->items[$this->current]->save(false, $prereq_check);
  4548. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4549. } else {
  4550. // If sco, then it is supposed to have been updated by some other call.
  4551. }
  4552. if ($item_type == 'sco') {
  4553. $this->items[$this->current]->restart();
  4554. }
  4555. }
  4556. if ($this->debug > 0) {
  4557. error_log('New LP - End of learnpath::start_current_item()', 0);
  4558. }
  4559. return true;
  4560. }
  4561. /**
  4562. * Stops the processing and counters for the old item (as held in $this->last)
  4563. * @return boolean True/False
  4564. */
  4565. public function stop_previous_item()
  4566. {
  4567. if ($this->debug > 0) {
  4568. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4569. }
  4570. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4571. if ($this->debug > 2) {
  4572. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4573. }
  4574. switch ($this->get_type()) {
  4575. case '3' :
  4576. if ($this->items[$this->last]->get_type() != 'au') {
  4577. if ($this->debug > 2) {
  4578. error_log(
  4579. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4580. 0
  4581. );
  4582. }
  4583. $this->items[$this->last]->close();
  4584. //$this->autocomplete_parents($this->last);
  4585. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4586. } else {
  4587. if ($this->debug > 2) {
  4588. error_log(
  4589. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4590. 0
  4591. );
  4592. }
  4593. }
  4594. case '2' :
  4595. if ($this->items[$this->last]->get_type() != 'sco') {
  4596. if ($this->debug > 2) {
  4597. error_log(
  4598. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4599. 0
  4600. );
  4601. }
  4602. $this->items[$this->last]->close();
  4603. //$this->autocomplete_parents($this->last);
  4604. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4605. } else {
  4606. if ($this->debug > 2) {
  4607. error_log(
  4608. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4609. 0
  4610. );
  4611. }
  4612. }
  4613. break;
  4614. case '1' :
  4615. default :
  4616. if ($this->debug > 2) {
  4617. error_log(
  4618. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4619. 0
  4620. );
  4621. }
  4622. $this->items[$this->last]->close();
  4623. break;
  4624. }
  4625. } else {
  4626. if ($this->debug > 2) {
  4627. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4628. }
  4629. return false;
  4630. }
  4631. return true;
  4632. }
  4633. /**
  4634. * Updates the default view mode from fullscreen to embedded and inversely
  4635. * @return string The current default view mode ('fullscreen' or 'embedded')
  4636. */
  4637. public function update_default_view_mode()
  4638. {
  4639. $course_id = api_get_course_int_id();
  4640. if ($this->debug > 0) {
  4641. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4642. }
  4643. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4644. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4645. $res = Database::query($sql);
  4646. if (Database :: num_rows($res) > 0) {
  4647. $row = Database :: fetch_array($res);
  4648. $default_view_mode = $row['default_view_mod'];
  4649. $view_mode = $default_view_mode;
  4650. switch ($default_view_mode) {
  4651. case 'fullscreen':
  4652. $view_mode = 'embedded';
  4653. break;
  4654. case 'embedded':
  4655. $view_mode = 'embedframe';
  4656. break;
  4657. case 'embedframe':
  4658. $view_mode = 'impress';
  4659. break;
  4660. case 'impress':
  4661. $view_mode = 'fullscreen';
  4662. break;
  4663. }
  4664. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4665. );
  4666. $res = Database::query($sql);
  4667. $this->mode = $view_mode;
  4668. return $view_mode;
  4669. } else {
  4670. if ($this->debug > 2) {
  4671. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4672. }
  4673. }
  4674. return -1;
  4675. }
  4676. /**
  4677. * Updates the default behaviour about auto-commiting SCORM updates
  4678. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4679. */
  4680. public function update_default_scorm_commit()
  4681. {
  4682. $course_id = api_get_course_int_id();
  4683. if ($this->debug > 0) {
  4684. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4685. }
  4686. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4687. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4688. $res = Database::query($sql);
  4689. if (Database :: num_rows($res) > 0) {
  4690. $row = Database :: fetch_array($res);
  4691. $force = $row['force_commit'];
  4692. if ($force == 1) {
  4693. $force = 0;
  4694. $force_return = false;
  4695. } elseif ($force == 0) {
  4696. $force = 1;
  4697. $force_return = true;
  4698. }
  4699. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4700. $res = Database::query($sql);
  4701. $this->force_commit = $force_return;
  4702. return $force_return;
  4703. } else {
  4704. if ($this->debug > 2) {
  4705. error_log(
  4706. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4707. 0
  4708. );
  4709. }
  4710. }
  4711. return -1;
  4712. }
  4713. /**
  4714. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4715. * @return bool True on success, false on failure
  4716. */
  4717. public function update_display_order()
  4718. {
  4719. $course_id = api_get_course_int_id();
  4720. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4721. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4722. $res = Database::query($sql);
  4723. if ($res === false) {
  4724. return false;
  4725. }
  4726. $lps = array();
  4727. $lp_order = array();
  4728. $num = Database :: num_rows($res);
  4729. // First check the order is correct, globally (might be wrong because
  4730. // of versions < 1.8.4).
  4731. if ($num > 0) {
  4732. $i = 1;
  4733. while ($row = Database :: fetch_array($res)) {
  4734. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4735. $need_fix = true;
  4736. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4737. $res_u = Database::query($sql_u);
  4738. }
  4739. $i++;
  4740. }
  4741. }
  4742. return true;
  4743. }
  4744. /**
  4745. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4746. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4747. */
  4748. public function update_reinit()
  4749. {
  4750. $course_id = api_get_course_int_id();
  4751. if ($this->debug > 0) {
  4752. error_log('New LP - In learnpath::update_reinit()', 0);
  4753. }
  4754. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4755. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4756. $res = Database::query($sql);
  4757. if (Database :: num_rows($res) > 0) {
  4758. $row = Database :: fetch_array($res);
  4759. $force = $row['prevent_reinit'];
  4760. if ($force == 1) {
  4761. $force = 0;
  4762. } elseif ($force == 0) {
  4763. $force = 1;
  4764. }
  4765. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4766. $res = Database::query($sql);
  4767. $this->prevent_reinit = $force;
  4768. return $force;
  4769. } else {
  4770. if ($this->debug > 2) {
  4771. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4772. }
  4773. }
  4774. return -1;
  4775. }
  4776. /**
  4777. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4778. *
  4779. * @return string 'single', 'multi' or 'seriousgame'
  4780. * @author ndiechburg <noel@cblue.be>
  4781. **/
  4782. public function get_attempt_mode()
  4783. {
  4784. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4785. $this->seriousgame_mode = 0;
  4786. }
  4787. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4788. $this->prevent_reinit = 1;
  4789. }
  4790. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4791. return 'seriousgame';
  4792. }
  4793. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4794. return 'single';
  4795. }
  4796. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4797. return 'multiple';
  4798. }
  4799. return 'single';
  4800. }
  4801. /**
  4802. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4803. *
  4804. * @param string 'seriousgame', 'single' or 'multiple'
  4805. * @return boolean
  4806. * @author ndiechburg <noel@cblue.be>
  4807. **/
  4808. public function set_attempt_mode($mode)
  4809. {
  4810. $course_id = api_get_course_int_id();
  4811. switch ($mode) {
  4812. case 'seriousgame' :
  4813. $sg_mode = 1;
  4814. $prevent_reinit = 1;
  4815. break;
  4816. case 'single' :
  4817. $sg_mode = 0;
  4818. $prevent_reinit = 1;
  4819. break;
  4820. case 'multiple' :
  4821. $sg_mode = 0;
  4822. $prevent_reinit = 0;
  4823. break;
  4824. default :
  4825. $sg_mode = 0;
  4826. $prevent_reinit = 0;
  4827. break;
  4828. }
  4829. $this->prevent_reinit = $prevent_reinit;
  4830. $this->seriousgame_mode = $sg_mode;
  4831. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4832. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4833. );
  4834. $res = Database::query($sql);
  4835. if ($res) {
  4836. return true;
  4837. } else {
  4838. return false;
  4839. }
  4840. }
  4841. /**
  4842. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4843. *
  4844. * @return boolean
  4845. * @author ndiechburg <noel@cblue.be>
  4846. **/
  4847. public function switch_attempt_mode()
  4848. {
  4849. if ($this->debug > 0) {
  4850. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4851. }
  4852. $mode = $this->get_attempt_mode();
  4853. switch ($mode) {
  4854. case 'single' :
  4855. $next_mode = 'multiple';
  4856. break;
  4857. case 'multiple' :
  4858. $next_mode = 'seriousgame';
  4859. break;
  4860. case 'seriousgame' :
  4861. $next_mode = 'single';
  4862. break;
  4863. default :
  4864. $next_mode = 'single';
  4865. break;
  4866. }
  4867. $this->set_attempt_mode($next_mode);
  4868. }
  4869. /**
  4870. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4871. *
  4872. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4873. * @author ndiechburg <noel@cblue.be>
  4874. **/
  4875. public function set_seriousgame_mode()
  4876. {
  4877. $course_id = api_get_course_int_id();
  4878. if ($this->debug > 0) {
  4879. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4880. }
  4881. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4882. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4883. $res = Database::query($sql);
  4884. if (Database :: num_rows($res) > 0) {
  4885. $row = Database :: fetch_array($res);
  4886. $force = $row['seriousgame_mode'];
  4887. if ($force == 1) {
  4888. $force = 0;
  4889. } elseif ($force == 0) {
  4890. $force = 1;
  4891. }
  4892. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4893. );
  4894. $res = Database::query($sql);
  4895. $this->seriousgame_mode = $force;
  4896. return $force;
  4897. } else {
  4898. if ($this->debug > 2) {
  4899. error_log(
  4900. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4901. 0
  4902. );
  4903. }
  4904. }
  4905. return -1;
  4906. }
  4907. /**
  4908. * Updates the "scorm_debug" value that shows or hide the debug window
  4909. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4910. */
  4911. public function update_scorm_debug()
  4912. {
  4913. $course_id = api_get_course_int_id();
  4914. if ($this->debug > 0) {
  4915. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4916. }
  4917. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4918. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4919. $res = Database::query($sql);
  4920. if (Database :: num_rows($res) > 0) {
  4921. $row = Database :: fetch_array($res);
  4922. $force = $row['debug'];
  4923. if ($force == 1) {
  4924. $force = 0;
  4925. } elseif ($force == 0) {
  4926. $force = 1;
  4927. }
  4928. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4929. $res = Database::query($sql);
  4930. $this->scorm_debug = $force;
  4931. return $force;
  4932. } else {
  4933. if ($this->debug > 2) {
  4934. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4935. }
  4936. }
  4937. return -1;
  4938. }
  4939. /**
  4940. * Function that makes a call to the function sort_tree_array and create_tree_array
  4941. * @author Kevin Van Den Haute
  4942. * @param array
  4943. */
  4944. public function tree_array($array)
  4945. {
  4946. if ($this->debug > 1) {
  4947. error_log('New LP - In learnpath::tree_array()', 0);
  4948. }
  4949. $array = $this->sort_tree_array($array);
  4950. $this->create_tree_array($array);
  4951. }
  4952. /**
  4953. * Creates an array with the elements of the learning path tree in it
  4954. *
  4955. * @author Kevin Van Den Haute
  4956. * @param array $array
  4957. * @param int $parent
  4958. * @param int $depth
  4959. * @param array $tmp
  4960. */
  4961. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4962. {
  4963. if ($this->debug > 1) {
  4964. error_log('New LP - In learnpath::create_tree_array())', 0);
  4965. }
  4966. if (is_array($array)) {
  4967. for ($i = 0; $i < count($array); $i++) {
  4968. if ($array[$i]['parent_item_id'] == $parent) {
  4969. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4970. $tmp[] = $array[$i]['parent_item_id'];
  4971. $depth++;
  4972. }
  4973. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4974. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4975. $this->arrMenu[] = array(
  4976. 'id' => $array[$i]['id'],
  4977. 'item_type' => $array[$i]['item_type'],
  4978. 'title' => $array[$i]['title'],
  4979. 'path' => isset($array[$i]['path']) ? $array[$i]['path'] : null,
  4980. 'description' => $array[$i]['description'],
  4981. 'parent_item_id' => $array[$i]['parent_item_id'],
  4982. 'previous_item_id' => $array[$i]['previous_item_id'],
  4983. 'next_item_id' => $array[$i]['next_item_id'],
  4984. 'min_score' => $array[$i]['min_score'],
  4985. 'max_score' => $array[$i]['max_score'],
  4986. 'mastery_score' => $array[$i]['mastery_score'],
  4987. 'display_order' => $array[$i]['display_order'],
  4988. 'prerequisite' => $preq,
  4989. 'depth' => $depth,
  4990. 'audio' => $audio
  4991. );
  4992. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4993. }
  4994. }
  4995. }
  4996. }
  4997. /**
  4998. * Sorts a multi dimensional array by parent id and display order
  4999. * @author Kevin Van Den Haute
  5000. *
  5001. * @param array $array (array with al the learning path items in it)
  5002. *
  5003. * @return array
  5004. */
  5005. public function sort_tree_array($array)
  5006. {
  5007. foreach ($array as $key => $row) {
  5008. $parent[$key] = $row['parent_item_id'];
  5009. $position[$key] = $row['display_order'];
  5010. }
  5011. if (count($array) > 0) {
  5012. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5013. }
  5014. return $array;
  5015. }
  5016. /**
  5017. * Function that creates a table structure with a learning path his modules, chapters and documents.
  5018. * Also the actions for the modules, chapters and documents are in this table.
  5019. * @author Kevin Van Den Haute
  5020. * @param int $lp_id
  5021. * @return string
  5022. */
  5023. public function overview()
  5024. {
  5025. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5026. if ($this->debug > 0) {
  5027. error_log('New LP - In learnpath::overview()', 0);
  5028. }
  5029. $_course = api_get_course_info();
  5030. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5031. $return = '';
  5032. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5033. // we need to start a form when we want to update all the mp3 files
  5034. if ($update_audio == 'true') {
  5035. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5036. $_GET['cidReq']
  5037. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5038. $_GET['action']
  5039. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5040. }
  5041. $return .= '<div id="message"></div>';
  5042. $return_audio = '<table class="data_table">';
  5043. $return_audio .= '<tr>';
  5044. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5045. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5046. $return_audio .= '</tr>';
  5047. if ($update_audio != 'true') {
  5048. $return .= '<div class="span12">';
  5049. $return .= self::return_new_tree($update_audio);
  5050. $return .= '</div>';
  5051. $return .= Display::div(
  5052. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5053. array('style' => 'float:left; margin-top:15px;width:100%')
  5054. );
  5055. } else {
  5056. $return_audio .= self::return_new_tree($update_audio);
  5057. $return .= $return_audio.'</table>';
  5058. }
  5059. // We need to close the form when we are updating the mp3 files.
  5060. if ($update_audio == 'true') {
  5061. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5062. 'SaveAudioAndOrganization'
  5063. ).'</button></div>'; // TODO: What kind of language variable is this?
  5064. }
  5065. // We need to close the form when we are updating the mp3 files.
  5066. if ($update_audio == 'true' && count($arrLP) != 0) {
  5067. $return .= '</form>';
  5068. }
  5069. return $return;
  5070. }
  5071. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5072. {
  5073. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5074. echo '<script>
  5075. var newOrderData= "";
  5076. function processChildren(parentId) {
  5077. //Loop through the children of the UL element defined by the parentId
  5078. var ulParentID= "UL_" + parentId;
  5079. $("#" + ulParentID).children().each(function () {
  5080. /*Only process elements with an id attribute (in order to skip the blank,
  5081. unmovable <li> elements.*/
  5082. if ($(this).attr("id")) {
  5083. /*Build a string of data with the childs ID and parent ID,
  5084. using the "|" as a delimiter between the two IDs and the "^"
  5085. as a record delimiter (these delimiters were chosen in case the data
  5086. involved includes more common delimiters like commas within the content)
  5087. */
  5088. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5089. //Determine if this child is a containter
  5090. if ($(this).is(".container")) {
  5091. //Process the child elements of the container
  5092. processChildren($(this).attr("id"));
  5093. }
  5094. }
  5095. }); //end of children loop
  5096. } //end of processChildren function
  5097. $(function() {
  5098. $(".item_data").on("mouseover", function(event) {
  5099. $(".button_actions", this).show();
  5100. });
  5101. $(".item_data").on("mouseout", function() {
  5102. $(".button_actions",this).hide();
  5103. });
  5104. $(".button_actions").hide();
  5105. $( ".lp_resource" ).sortable({
  5106. items: ".lp_resource_element ",
  5107. handle: ".moved", //only the class "moved"
  5108. cursor: "move",
  5109. connectWith: "#lp_item_list",
  5110. placeholder: "ui-state-highlight", //defines the yellow highlight
  5111. start: function(event, ui) {
  5112. $(ui.item).css("width", "160px");
  5113. $(ui.item).find(".item_data").attr("style", "");
  5114. },
  5115. stop: function(event, ui) {
  5116. $(ui.item).css("width", "100%");
  5117. },
  5118. });
  5119. $("#lp_item_list").sortable({
  5120. items: "li",
  5121. handle: ".moved", //only the class "moved"
  5122. cursor: "move",
  5123. placeholder: "ui-state-highlight", //defines the yellow highlight
  5124. update: function(event, ui) {
  5125. //Walk through the direct descendants of the lp_item_list <ul>
  5126. $("#lp_item_list").children().each(function () {
  5127. /*Only process elements with an id attribute (in order to skip the blank,
  5128. unmovable <li> elements.*/
  5129. if ($(this).attr("id")) {
  5130. /*Build a string of data with the child s ID and parent ID,
  5131. using the "|" as a delimiter between the two IDs and the "^"
  5132. as a record delimiter (these delimiters were chosen in case the data
  5133. involved includes more common delimiters like commas within the content)
  5134. */
  5135. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5136. //Determine if this child is a containter
  5137. if ($(this).is(".li_container")) {
  5138. //Process the child elements of the container
  5139. processChildren($(this).attr("id"));
  5140. }
  5141. }
  5142. }); //end of lp_item_list children loop
  5143. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5144. $.post("'.$ajax_url.'", order, function(reponse){
  5145. $("#message").html(reponse);
  5146. });
  5147. },
  5148. receive: function(event, ui) {
  5149. var id = $(ui.item).attr("data_id");
  5150. var type = $(ui.item).attr("data_type");
  5151. var title = $(ui.item).attr("title");
  5152. if (ui.item.parent()[0]) {
  5153. var parent_id = $(ui.item.parent()[0]).attr("id");
  5154. var previous_id = $(ui.item.prev()).attr("id");
  5155. if (parent_id) {
  5156. parent_id = parent_id.split("_")[1];
  5157. var params = {
  5158. "a": "add_lp_item",
  5159. "id": id,
  5160. "parent_id": parent_id,
  5161. "previous_id": previous_id,
  5162. "type": type,
  5163. "title" : title
  5164. };
  5165. $.ajax({
  5166. type: "GET",
  5167. url: "'.$ajax_url.'",
  5168. data: params,
  5169. async: false,
  5170. success: function(data) {
  5171. if (data == -1) {
  5172. } else {
  5173. $(".normal-message").hide();
  5174. $(ui.item).attr("id", data);
  5175. $(ui.item).addClass("lp_resource_element_new");
  5176. $(ui.item).find(".item_data").attr("style", "");
  5177. $(ui.item).addClass("record li_container");
  5178. $(ui.item).removeClass("lp_resource_element");
  5179. $(ui.item).removeClass("doc_resource");
  5180. }
  5181. }
  5182. });
  5183. }
  5184. }//
  5185. }//end receive
  5186. });
  5187. });
  5188. </script>';
  5189. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5190. $course_id = api_get_course_int_id();
  5191. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5192. $sql = "SELECT * FROM $tbl_lp_item
  5193. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5194. $result = Database::query($sql);
  5195. $arrLP = array();
  5196. while ($row = Database :: fetch_array($result)) {
  5197. $row['title'] = Security :: remove_XSS($row['title']);
  5198. $row['description'] = Security :: remove_XSS($row['description']);
  5199. $arrLP[] = array(
  5200. 'id' => $row['id'],
  5201. 'item_type' => $row['item_type'],
  5202. 'title' => $row['title'],
  5203. 'path' => $row['path'],
  5204. 'description' => $row['description'],
  5205. 'parent_item_id' => $row['parent_item_id'],
  5206. 'previous_item_id' => $row['previous_item_id'],
  5207. 'next_item_id' => $row['next_item_id'],
  5208. 'max_score' => $row['max_score'],
  5209. 'min_score' => $row['min_score'],
  5210. 'mastery_score' => $row['mastery_score'],
  5211. 'prerequisite' => $row['prerequisite'],
  5212. 'display_order' => $row['display_order'],
  5213. 'audio' => $row['audio']
  5214. );
  5215. }
  5216. $this->tree_array($arrLP);
  5217. $arrLP = $this->arrMenu;
  5218. unset ($this->arrMenu);
  5219. $default_data = null;
  5220. $default_content = null;
  5221. $elements = array();
  5222. $return_audio = null;
  5223. $iconSysPath = api_get_path(SYS_IMG_PATH);
  5224. for ($i = 0; $i < count($arrLP); $i++) {
  5225. $title = $arrLP[$i]['title'];
  5226. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5227. //Link for the documents
  5228. if ($arrLP[$i]['item_type'] == 'document') {
  5229. $url = api_get_self().'?'.api_get_cidreq(
  5230. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5231. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5232. }
  5233. if (($i % 2) == 0) {
  5234. $oddclass = 'row_odd';
  5235. } else {
  5236. $oddclass = 'row_even';
  5237. }
  5238. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5239. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5240. $icon = '';
  5241. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5242. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5243. } else {
  5244. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5245. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5246. } else {
  5247. $icon = Display::return_icon('folder_document.gif');
  5248. }
  5249. }
  5250. // The audio column.
  5251. $return_audio .= '<td align="center">';
  5252. $audio = '';
  5253. if (!$update_audio OR $update_audio <> 'true') {
  5254. if (!empty($arrLP[$i]['audio'])) {
  5255. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5256. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5257. $audio .= '<script type="text/javascript">
  5258. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5259. s1.addParam("allowscriptaccess","always");
  5260. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5261. s1.write("container' . $i . '");
  5262. </script>';*/
  5263. } else {
  5264. $audio .= '';
  5265. }
  5266. } else {
  5267. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5268. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5269. if (!empty ($arrLP[$i]['audio'])) {
  5270. $audio .= '<br />'.Security::remove_XSS(
  5271. $arrLP[$i]['audio']
  5272. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5273. 'RemoveAudio'
  5274. );
  5275. }
  5276. }
  5277. }
  5278. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5279. $return_audio .= '</td>';
  5280. $move_icon = '';
  5281. $move_item_icon = '';
  5282. $edit_icon = '';
  5283. $delete_icon = '';
  5284. $audio_icon = '';
  5285. $prerequisities_icon = '';
  5286. if ($is_allowed_to_edit) {
  5287. if (!$update_audio OR $update_audio <> 'true') {
  5288. $move_icon .= '<a class="moved" href="#">';
  5289. $move_icon .= Display::return_icon(
  5290. 'move_everywhere.png',
  5291. get_lang('Move'),
  5292. array(),
  5293. ICON_SIZE_TINY
  5294. );
  5295. $move_icon .= '</a>';
  5296. }
  5297. // No edit for this item types
  5298. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5299. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5300. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=edit_item&amp;view=build&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5301. $edit_icon .= Display::return_icon(
  5302. 'edit.png',
  5303. get_lang('LearnpathEditModule'),
  5304. array(),
  5305. ICON_SIZE_TINY
  5306. );
  5307. $edit_icon .= '</a>';
  5308. } else {
  5309. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=edit_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5310. $edit_icon .= Display::return_icon('edit.png',
  5311. get_lang('LearnpathEditModule'),
  5312. array(),
  5313. ICON_SIZE_TINY
  5314. );
  5315. $edit_icon .= '</a>';
  5316. }
  5317. }
  5318. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=delete_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'" onClick="return confirmation(\''.addslashes(
  5319. $title
  5320. ).'\');">';
  5321. $delete_icon .= Display::return_icon(
  5322. 'delete.png',
  5323. get_lang('LearnpathDeleteModule'),
  5324. array(),
  5325. ICON_SIZE_TINY
  5326. );
  5327. $delete_icon .= '</a>';
  5328. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5329. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5330. $prerequisities_icon = Display::url(
  5331. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5332. $url.'&action=edit_item_prereq'
  5333. );
  5334. $move_item_icon = Display::url(
  5335. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5336. $url.'&action=move_item'
  5337. );
  5338. $audio_icon = Display::url(
  5339. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5340. $url.'&action=add_audio'
  5341. );
  5342. }
  5343. }
  5344. if ($update_audio != 'true') {
  5345. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5346. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5347. array('class' => 'button_actions')
  5348. );
  5349. } else {
  5350. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5351. }
  5352. $parent_id = $arrLP[$i]['parent_item_id'];
  5353. $default_data[$arrLP[$i]['id']] = $row;
  5354. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5355. if (empty($parent_id)) {
  5356. $elements[$arrLP[$i]['id']]['data'] = $row;
  5357. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5358. } else {
  5359. $parent_arrays = array();
  5360. if ($arrLP[$i]['depth'] > 1) {
  5361. //Getting list of parents
  5362. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5363. foreach ($arrLP as $item) {
  5364. if ($item['id'] == $parent_id) {
  5365. if ($item['parent_item_id'] == 0) {
  5366. $parent_id = $item['id'];
  5367. break;
  5368. } else {
  5369. $parent_id = $item['parent_item_id'];
  5370. if (empty($parent_arrays)) {
  5371. $parent_arrays[] = intval($item['id']);
  5372. }
  5373. $parent_arrays[] = $parent_id;
  5374. break;
  5375. }
  5376. }
  5377. }
  5378. }
  5379. }
  5380. if (!empty($parent_arrays)) {
  5381. $parent_arrays = array_reverse($parent_arrays);
  5382. $val = '$elements';
  5383. $x = 0;
  5384. foreach ($parent_arrays as $item) {
  5385. if ($x != count($parent_arrays) - 1) {
  5386. $val .= '["'.$item.'"]["children"]';
  5387. } else {
  5388. $val .= '["'.$item.'"]["children"]';
  5389. }
  5390. $x++;
  5391. }
  5392. $val .= "";
  5393. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5394. eval($code_str);
  5395. } else {
  5396. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5397. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5398. }
  5399. }
  5400. }
  5401. $return = '<div class="lp_tree well">';
  5402. $return .= '<ul id="lp_item_list">';
  5403. $return .= '<h4>'.$this->name.'</h4><br>';
  5404. $tree = self::print_recursive($elements, $default_data, $default_content);
  5405. if (!empty($tree)) {
  5406. $return .= $tree;
  5407. } else {
  5408. if ($drop_element_here) {
  5409. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5410. }
  5411. }
  5412. $return .= '</ul>';
  5413. if ($update_audio == 'true') {
  5414. $return = $return_audio;
  5415. } else {
  5416. $return .= '</div>';
  5417. }
  5418. return $return;
  5419. }
  5420. function print_recursive($elements, $default_data, $default_content)
  5421. {
  5422. $return = '';
  5423. foreach ($elements as $key => $item) {
  5424. if (isset($item['load_data']) || empty($item['data'])) {
  5425. $item['data'] = $default_data[$item['load_data']];
  5426. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5427. }
  5428. $sub_list = '';
  5429. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5430. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5431. }
  5432. if (empty($item['children'])) {
  5433. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5434. $active = null;
  5435. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5436. $active = 'active';
  5437. }
  5438. $return .= Display::tag(
  5439. 'li',
  5440. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5441. array('id' => $key, 'class' => 'record li_container')
  5442. );
  5443. } else {
  5444. //sections
  5445. if (isset($item['children'])) {
  5446. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5447. }
  5448. $sub_list = Display::tag(
  5449. 'ul',
  5450. $sub_list.$data,
  5451. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5452. );
  5453. $return .= Display::tag(
  5454. 'li',
  5455. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5456. array('id' => $key, 'class' => 'record li_container')
  5457. );
  5458. }
  5459. }
  5460. return $return;
  5461. }
  5462. /**
  5463. * This function builds the action menu
  5464. * @return void
  5465. */
  5466. public function build_action_menu()
  5467. {
  5468. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5469. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5470. $return = '<div class="actions">';
  5471. //$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>';
  5472. //$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>';
  5473. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5474. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5475. 'preview_view.png',
  5476. get_lang('Display'),
  5477. '',
  5478. ICON_SIZE_MEDIUM
  5479. ).'</a> ';
  5480. //$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>';
  5481. // 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>';
  5482. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5483. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5484. 'upload_audio.png',
  5485. get_lang('UpdateAllAudioFragments'),
  5486. '',
  5487. ICON_SIZE_MEDIUM
  5488. ).'</a>';*/
  5489. $return .= '<a href="lp_controller.php?referer=add_item&'.api_get_cidreq().'&amp;action=edit&amp;lp_id='.$_SESSION['oLP']->lp_id.'">'.Display :: return_icon(
  5490. 'settings.png',
  5491. get_lang('CourseSettings'),
  5492. '',
  5493. ICON_SIZE_MEDIUM
  5494. ).'</a>';
  5495. $buttons = array(
  5496. array(
  5497. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5498. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5499. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5500. ),
  5501. array(
  5502. 'title' => get_lang('ClearAllPrerequisites'),
  5503. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5504. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5505. ),
  5506. );
  5507. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5508. $return .= '</div>';
  5509. echo $return;
  5510. }
  5511. /**
  5512. * This functions builds the LP tree based on data from the database.
  5513. * @return string
  5514. * @deprecated use the return_new_tree() function
  5515. * @uses dtree.js :: necessary javascript for building this tree
  5516. */
  5517. public function build_tree()
  5518. {
  5519. $iconSysPath = api_get_path(SYS_IMG_PATH);
  5520. $iconWebPath = api_get_path(WEB_IMG_PATH);
  5521. $course_id = api_get_course_int_id();
  5522. $return = "<script type=\"text/javascript\">";
  5523. $return .= "\tm = new dTree('m');\n\n";
  5524. $return .= "\tm.config.folderLinks = true;\n";
  5525. $return .= "\tm.config.useCookies = true;\n";
  5526. $return .= "\tm.config.useIcons = true;\n";
  5527. $return .= "\tm.config.useLines = true;\n";
  5528. $return .= "\tm.config.useSelection = true;\n";
  5529. $return .= "\tm.config.useStatustext = false;\n\n";
  5530. $menu = 0;
  5531. $parent = '';
  5532. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5533. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5534. $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
  5535. FROM $tbl_lp_item
  5536. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5537. $result = Database::query($sql);
  5538. $arrLP = array();
  5539. while ($row = Database :: fetch_array($result)) {
  5540. $row['title'] = Security :: remove_XSS($row['title']);
  5541. $row['description'] = Security :: remove_XSS($row['description']);
  5542. $arrLP[] = array(
  5543. 'id' => $row['id'],
  5544. 'item_type' => $row['item_type'],
  5545. 'title' => $row['title'],
  5546. 'path' => $row['path'],
  5547. 'description' => $row['description'],
  5548. 'parent_item_id' => $row['parent_item_id'],
  5549. 'previous_item_id' => $row['previous_item_id'],
  5550. 'next_item_id' => $row['next_item_id'],
  5551. 'max_score' => $row['max_score'],
  5552. 'min_score' => $row['min_score'],
  5553. 'mastery_score' => $row['mastery_score'],
  5554. 'display_order' => $row['display_order']
  5555. );
  5556. }
  5557. $this->tree_array($arrLP);
  5558. $arrLP = $this->arrMenu;
  5559. unset($this->arrMenu);
  5560. $title = '';
  5561. for ($i = 0; $i < count($arrLP); $i++) {
  5562. $title = addslashes($arrLP[$i]['title']);
  5563. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5564. $_GET['cidReq']
  5565. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5566. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5567. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5568. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."lp_".$icon_name.".png', '".$iconWebPath."lp_".$icon_name.".png');\n";
  5569. } else {
  5570. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5571. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."lp_".$icon_name.".gif', '".$iconWebPath."lp_".$icon_name.".gif');\n";
  5572. } else {
  5573. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."folder_document.gif', '".$iconWebPath."folder_document.gif');\n";
  5574. }
  5575. }
  5576. if ($menu < $arrLP[$i]['id']) {
  5577. $menu = $arrLP[$i]['id'];
  5578. }
  5579. }
  5580. $return .= "\n\tdocument.write(m);\n";
  5581. $return .= "\t if(!m.selectedNode) m.s(1);";
  5582. $return .= "</script>\n";
  5583. return $return;
  5584. }
  5585. /**
  5586. * Creates the default learning path folder
  5587. */
  5588. public static function generate_learning_path_folder($course)
  5589. {
  5590. //Creating learning_path folder
  5591. $dir = '/learning_path';
  5592. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5593. $folder = false;
  5594. if (!is_dir($filepath.'/'.$dir)) {
  5595. $folder = FileManager::create_unexisting_directory(
  5596. $course,
  5597. api_get_user_id(),
  5598. api_get_session_id(),
  5599. 0,
  5600. 0,
  5601. $filepath,
  5602. $dir,
  5603. get_lang('LearningPaths'),
  5604. 0
  5605. );
  5606. } else {
  5607. $folder = true;
  5608. }
  5609. return $folder;
  5610. }
  5611. public function generate_lp_folder($course, $lp_name = null)
  5612. {
  5613. $filepath = '';
  5614. $dir = '/learning_path/';
  5615. if (empty($lp_name)) {
  5616. $lp_name = $this->name;
  5617. }
  5618. $folder = self::generate_learning_path_folder($course);
  5619. //Creating LP folder
  5620. if ($folder) {
  5621. //Limits title size
  5622. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5623. $dir = $dir.$title;
  5624. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5625. if (!is_dir($filepath.'/'.$dir)) {
  5626. $folder = FileManager::create_unexisting_directory(
  5627. $course,
  5628. api_get_user_id(),
  5629. 0,
  5630. 0,
  5631. 0,
  5632. $filepath,
  5633. $dir,
  5634. $lp_name
  5635. );
  5636. } else {
  5637. $folder = true;
  5638. }
  5639. $dir = $dir.'/';
  5640. if ($folder) {
  5641. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5642. }
  5643. }
  5644. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5645. return $array;
  5646. }
  5647. /**
  5648. * Create a new document //still needs some finetuning
  5649. * @param array $_course
  5650. * @return string
  5651. */
  5652. public function create_document($_course)
  5653. {
  5654. $course_id = api_get_course_int_id();
  5655. global $charset;
  5656. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5657. if (strstr($dir, '..')) {
  5658. $dir = '/';
  5659. }
  5660. if ($dir[0] == '.') {
  5661. $dir = substr($dir, 1);
  5662. }
  5663. if ($dir[0] != '/') {
  5664. $dir = '/'.$dir;
  5665. }
  5666. if ($dir[strlen($dir) - 1] != '/') {
  5667. $dir .= '/';
  5668. }
  5669. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5670. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5671. //Generates folder
  5672. $result = $this->generate_lp_folder($_course);
  5673. $dir = $result['dir'];
  5674. $filepath = $result['filepath'];
  5675. }
  5676. if (!is_dir($filepath)) {
  5677. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5678. $dir = '/';
  5679. }
  5680. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5681. // is already escaped twice when it gets here.
  5682. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5683. $title = FileManager::disable_dangerous_file($title);
  5684. $filename = $title;
  5685. $content = $_POST['content_lp'];
  5686. $tmp_filename = $filename;
  5687. $i = 0;
  5688. while (file_exists($filepath.$tmp_filename.'.html')) {
  5689. $tmp_filename = $filename.'_'.++$i;
  5690. }
  5691. $filename = $tmp_filename.'.html';
  5692. $content = stripslashes($content);
  5693. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5694. // Change the path of mp3 to absolute.
  5695. // The first regexp deals with ../../../ urls.
  5696. $content = preg_replace(
  5697. "|(flashvars=\"file=)(\.+/)+|",
  5698. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5699. $content
  5700. );
  5701. // The second regexp deals with audio/ urls.
  5702. $content = preg_replace(
  5703. "|(flashvars=\"file=)([^/]+)/|",
  5704. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5705. $content
  5706. );
  5707. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5708. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5709. if (!file_exists($filepath.$filename)) {
  5710. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5711. fputs($fp, $content);
  5712. fclose($fp);
  5713. $file_size = filesize($filepath.$filename);
  5714. $save_file_path = $dir.$filename;
  5715. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5716. if ($document_id) {
  5717. api_item_property_update(
  5718. $_course,
  5719. TOOL_DOCUMENT,
  5720. $document_id,
  5721. 'DocumentAdded',
  5722. api_get_user_id(),
  5723. null,
  5724. null,
  5725. null,
  5726. null,
  5727. api_get_session_id()
  5728. );
  5729. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5730. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5731. if ($new_comment || $new_title) {
  5732. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5733. $ct = '';
  5734. if ($new_comment) {
  5735. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5736. }
  5737. if ($new_title) {
  5738. $ct .= ", title='".Database::escape_string(
  5739. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5740. )."' ";
  5741. }
  5742. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5743. $ct,
  5744. 1
  5745. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5746. Database::query($sql_update);
  5747. }
  5748. }
  5749. return $document_id;
  5750. }
  5751. }
  5752. }
  5753. /**
  5754. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5755. * @param array $_course array
  5756. * @return void
  5757. */
  5758. public function edit_document($_course)
  5759. {
  5760. $course_id = api_get_course_int_id();
  5761. global $_configuration;
  5762. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5763. if (strstr($dir, '..')) {
  5764. $dir = '/';
  5765. }
  5766. if ($dir[0] == '.') {
  5767. $dir = substr($dir, 1);
  5768. }
  5769. if ($dir[0] != '/') {
  5770. $dir = '/'.$dir;
  5771. }
  5772. if ($dir[strlen($dir) - 1] != '/') {
  5773. $dir .= '/';
  5774. }
  5775. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5776. if (!is_dir($filepath)) {
  5777. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5778. $dir = '/';
  5779. }
  5780. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5781. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5782. $document_id = intval($_POST['path']);
  5783. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5784. $res = Database::query($sql);
  5785. $row = Database :: fetch_array($res);
  5786. $content = stripslashes($_POST['content_lp']);
  5787. $file = $filepath.$row['path'];
  5788. if ($fp = @ fopen($file, 'w')) {
  5789. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/',$content);
  5790. // Change the path of mp3 to absolute.
  5791. // The first regexp deals with ../../../ urls.
  5792. $content = preg_replace(
  5793. "|(flashvars=\"file=)(\.+/)+|",
  5794. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5795. $content
  5796. );
  5797. // The second regexp deals with audio/ urls.
  5798. $content = preg_replace(
  5799. "|(flashvars=\"file=)([^/]+)/|",
  5800. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5801. $content
  5802. );
  5803. fputs($fp, $content);
  5804. fclose($fp);
  5805. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5806. $_POST['title']
  5807. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5808. Database::query($sql_update);
  5809. }
  5810. }
  5811. }
  5812. /**
  5813. * Displays the selected item, with a panel for manipulating the item
  5814. * @param int $item_id
  5815. * @param string $msg
  5816. * @return string
  5817. */
  5818. public function display_item($item_id, $msg = null, $show_actions = true)
  5819. {
  5820. $course_id = api_get_course_int_id();
  5821. $return = '';
  5822. if (is_numeric($item_id)) {
  5823. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5824. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5825. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5826. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5827. $result = Database::query($sql);
  5828. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5829. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5830. // Prevents wrong parent selection for document, see Bug#1251.
  5831. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5832. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5833. }
  5834. if ($show_actions) {
  5835. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5836. }
  5837. $return .= '<div style="padding:10px;">';
  5838. if ($msg != '') {
  5839. $return .= $msg;
  5840. }
  5841. $return .= '<h3>'.$row['title'].'</h3>';
  5842. switch ($row['item_type']) {
  5843. case TOOL_QUIZ:
  5844. if (!empty($row['path'])) {
  5845. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5846. $exercise = new Exercise();
  5847. $exercise->read($row['path']);
  5848. $return .= $exercise->description.'<br />';
  5849. }
  5850. break;
  5851. case TOOL_DOCUMENT:
  5852. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5853. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5854. $row['path']
  5855. );
  5856. $result = Database::query($sql_doc);
  5857. $path_file = Database::result($result, 0, 0);
  5858. $path_parts = pathinfo($path_file);
  5859. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5860. if (in_array(
  5861. $path_parts['extension'],
  5862. array(
  5863. 'html',
  5864. 'txt',
  5865. 'png',
  5866. 'jpg',
  5867. 'JPG',
  5868. 'jpeg',
  5869. 'JPEG',
  5870. 'gif',
  5871. 'swf'
  5872. )
  5873. )
  5874. ) {
  5875. $return .= $this->display_document($row['path'], true, true);
  5876. }
  5877. break;
  5878. }
  5879. $return .= '</div>';
  5880. }
  5881. }
  5882. return $return;
  5883. }
  5884. /**
  5885. * Shows the needed forms for editing a specific item
  5886. * @param int $item_id
  5887. * @return string
  5888. */
  5889. public function display_edit_item($item_id)
  5890. {
  5891. $_course = api_get_course_info(); // It will disappear.
  5892. $course_id = api_get_course_int_id();
  5893. $return = '';
  5894. if (is_numeric($item_id)) {
  5895. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5896. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5897. $res = Database::query($sql);
  5898. $row = Database::fetch_array($res);
  5899. switch ($row['item_type']) {
  5900. case 'dokeos_chapter' :
  5901. case 'dir' :
  5902. case 'asset' :
  5903. case 'sco' :
  5904. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5905. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5906. $return .= $this->display_item_form(
  5907. $row['item_type'],
  5908. get_lang('EditCurrentChapter').' :',
  5909. 'edit',
  5910. $item_id,
  5911. $row
  5912. );
  5913. } else {
  5914. $return .= $this->display_item_small_form(
  5915. $row['item_type'],
  5916. get_lang('EditCurrentChapter').' :',
  5917. $row
  5918. );
  5919. }
  5920. break;
  5921. case TOOL_DOCUMENT :
  5922. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5923. $sql_step = " SELECT lp.*, doc.path as dir
  5924. FROM ".$tbl_lp_item." as lp
  5925. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5926. WHERE lp.c_id = $course_id AND
  5927. doc.c_id = $course_id AND
  5928. lp.id = ".Database::escape_string($item_id);
  5929. $res_step = Database::query($sql_step);
  5930. $row_step = Database :: fetch_array($res_step);
  5931. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5932. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5933. break;
  5934. case TOOL_LINK :
  5935. $link_id = (string)$row['path'];
  5936. if (ctype_digit($link_id)) {
  5937. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5938. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5939. $link_id
  5940. );
  5941. $res_link = Database::query($sql_select);
  5942. $row_link = Database :: fetch_array($res_link);
  5943. if (is_array($row_link)) {
  5944. $row['url'] = $row_link['url'];
  5945. }
  5946. }
  5947. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5948. $return .= $this->display_link_form('edit', $item_id, $row);
  5949. break;
  5950. case 'dokeos_module' :
  5951. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5952. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5953. $return .= $this->display_item_form(
  5954. $row['item_type'],
  5955. get_lang('EditCurrentModule').' :',
  5956. 'edit',
  5957. $item_id,
  5958. $row
  5959. );
  5960. } else {
  5961. $return .= $this->display_item_small_form(
  5962. $row['item_type'],
  5963. get_lang('EditCurrentModule').' :',
  5964. $row
  5965. );
  5966. }
  5967. break;
  5968. case TOOL_QUIZ :
  5969. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5970. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5971. break;
  5972. case TOOL_HOTPOTATOES :
  5973. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5974. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5975. break;
  5976. case TOOL_STUDENTPUBLICATION :
  5977. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5978. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5979. break;
  5980. case TOOL_FORUM :
  5981. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5982. $return .= $this->display_forum_form('edit', $item_id, $row);
  5983. break;
  5984. case TOOL_THREAD :
  5985. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5986. $return .= $this->display_thread_form('edit', $item_id, $row);
  5987. break;
  5988. }
  5989. }
  5990. return $return;
  5991. }
  5992. /**
  5993. * Function that displays a list with al the resources that could be added to the learning path
  5994. * @return string
  5995. */
  5996. public function display_resources()
  5997. {
  5998. $_course = api_get_course_info(); // TODO: Don't use globals.
  5999. $course_code = api_get_course_id();
  6000. //Get all the docs
  6001. $documents = $this->get_documents();
  6002. //Get all the exercises
  6003. $exercises = $this->get_exercises();
  6004. // Get all the links
  6005. $links = $this->get_links();
  6006. //Get al the student publications
  6007. $works = $this->get_student_publications();
  6008. //Get al the forums
  6009. $forums = $this->get_forums(null, $course_code);
  6010. $headers = array(
  6011. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  6012. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  6013. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  6014. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  6015. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  6016. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  6017. );
  6018. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6019. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6020. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6021. return true;
  6022. }
  6023. /**
  6024. * Returns the extension of a document
  6025. * @param string filename
  6026. * @return string Extension (part after the last dot)
  6027. */
  6028. public function get_extension($filename)
  6029. {
  6030. $explode = explode('.', $filename);
  6031. return $explode[count($explode) - 1];
  6032. }
  6033. /**
  6034. * Displays a document by id
  6035. *
  6036. * @param int $id
  6037. * @return string
  6038. */
  6039. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6040. {
  6041. $_course = api_get_course_info(); // It is temporary.
  6042. $course_id = api_get_course_int_id();
  6043. $return = '';
  6044. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6045. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6046. WHERE c_id = ".$course_id." AND id = ".$id;
  6047. $res_doc = Database::query($sql_doc);
  6048. $row_doc = Database :: fetch_array($res_doc);
  6049. // TODO: Add a path filter.
  6050. if ($iframe) {
  6051. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq();
  6052. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.$url.'"></iframe>';
  6053. } else {
  6054. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6055. }
  6056. return $return;
  6057. }
  6058. /**
  6059. * Return HTML form to add/edit a quiz
  6060. * @param string Action (add/edit)
  6061. * @param integer Item ID if already exists
  6062. * @param mixed Extra information (quiz ID if integer)
  6063. * @return string HTML form
  6064. */
  6065. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6066. {
  6067. $course_id = api_get_course_int_id();
  6068. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6069. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6070. if ($id != 0 && is_array($extra_info)) {
  6071. $item_title = $extra_info['title'];
  6072. $item_description = $extra_info['description'];
  6073. } elseif (is_numeric($extra_info)) {
  6074. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz."
  6075. WHERE c_id = ".$course_id." AND iid = ".$extra_info;
  6076. $result = Database::query($sql_quiz);
  6077. $row = Database :: fetch_array($result);
  6078. $item_title = $row['title'];
  6079. $item_description = $row['description'];
  6080. } else {
  6081. $item_title = '';
  6082. $item_description = '';
  6083. }
  6084. $item_title = Security::remove_XSS($item_title);
  6085. $item_description = Security::remove_XSS($item_description);
  6086. $legend = '<legend>';
  6087. if ($id != 0 && is_array($extra_info)) {
  6088. $parent = $extra_info['parent_item_id'];
  6089. } else {
  6090. $parent = 0;
  6091. }
  6092. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6093. $result = Database::query($sql);
  6094. $arrLP = array();
  6095. while ($row = Database :: fetch_array($result)) {
  6096. $arrLP[] = array(
  6097. 'id' => $row['id'],
  6098. 'item_type' => $row['item_type'],
  6099. 'title' => $row['title'],
  6100. 'path' => $row['path'],
  6101. 'description' => $row['description'],
  6102. 'parent_item_id' => $row['parent_item_id'],
  6103. 'previous_item_id' => $row['previous_item_id'],
  6104. 'next_item_id' => $row['next_item_id'],
  6105. 'display_order' => $row['display_order'],
  6106. 'max_score' => $row['max_score'],
  6107. 'min_score' => $row['min_score'],
  6108. 'mastery_score' => $row['mastery_score'],
  6109. 'prerequisite' => $row['prerequisite'],
  6110. 'max_time_allowed' => $row['max_time_allowed']
  6111. );
  6112. }
  6113. $this->tree_array($arrLP);
  6114. $arrLP = $this->arrMenu;
  6115. unset ($this->arrMenu);
  6116. if ($action == 'add') {
  6117. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6118. } elseif ($action == 'move') {
  6119. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6120. } else {
  6121. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6122. }
  6123. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6124. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6125. }
  6126. $legend .= '</legend>';
  6127. $return = '<div class="sectioncomment">';
  6128. $return .= '<form method="POST">';
  6129. $return .= $legend;
  6130. $return .= '<table class="lp_form">';
  6131. if ($action != 'move') {
  6132. $return .= '<tr>';
  6133. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6134. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6135. $return .= '</tr>';
  6136. }
  6137. $return .= '<tr>';
  6138. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6139. $return .= '<td class="input">';
  6140. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6141. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6142. $arrHide = array(
  6143. $id
  6144. );
  6145. //$parent_item_id = $_SESSION['parent_item_id'];
  6146. for ($i = 0; $i < count($arrLP); $i++) {
  6147. if ($action != 'add') {
  6148. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6149. $arrLP[$i]['id'],
  6150. $arrHide
  6151. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6152. ) {
  6153. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6154. } else {
  6155. $arrHide[] = $arrLP[$i]['id'];
  6156. }
  6157. } else {
  6158. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6159. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6160. }
  6161. }
  6162. }
  6163. if (is_array($arrLP)) {
  6164. reset($arrLP);
  6165. }
  6166. $return .= '</select>';
  6167. $return .= '</td>';
  6168. $return .= '</tr>';
  6169. $return .= '<tr>';
  6170. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6171. $return .= '<td class="input">';
  6172. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6173. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6174. for ($i = 0; $i < count($arrLP); $i++) {
  6175. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6176. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6177. $selected = 'selected="selected" ';
  6178. } elseif ($action == 'add') {
  6179. $selected = 'selected="selected" ';
  6180. } else {
  6181. $selected = '';
  6182. }
  6183. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6184. 'After'
  6185. ).' "'.$arrLP[$i]['title'].'"</option>';
  6186. }
  6187. }
  6188. $return .= '</select>';
  6189. $return .= '</td>';
  6190. $return .= '</tr>';
  6191. if ($action != 'move') {
  6192. $id_prerequisite = 0;
  6193. if (is_array($arrLP)) {
  6194. foreach ($arrLP as $key => $value) {
  6195. if ($value['id'] == $id) {
  6196. $id_prerequisite = $value['prerequisite'];
  6197. break;
  6198. }
  6199. }
  6200. }
  6201. $arrHide = array();
  6202. for ($i = 0; $i < count($arrLP); $i++) {
  6203. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6204. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6205. $s_selected_position = $arrLP[$i]['id'];
  6206. } elseif ($action == 'add') {
  6207. $s_selected_position = 0;
  6208. }
  6209. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6210. }
  6211. }
  6212. /*// Commented the prerequisites, only visible in edit (exercise).
  6213. $return .= '<tr>';
  6214. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6215. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6216. foreach($arrHide as $key => $value){
  6217. if($key==$s_selected_position && $action == 'add'){
  6218. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6219. }
  6220. elseif($key==$id_prerequisite && $action == 'edit'){
  6221. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6222. }
  6223. else{
  6224. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6225. }
  6226. }
  6227. $return .= "</select></td>";
  6228. */
  6229. $return .= '</tr>';
  6230. /*$return .= '<tr>';
  6231. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6232. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6233. // Remove temporarily the test description.
  6234. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6235. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6236. $return .= '</tr>'; */
  6237. }
  6238. $return .= '<tr>';
  6239. if ($action == 'add') {
  6240. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6241. 'AddExercise'
  6242. ).'</button></td>';
  6243. } else {
  6244. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6245. 'EditCurrentExecice'
  6246. ).'</button></td>';
  6247. }
  6248. $return .= '</tr>';
  6249. $return .= '</table>';
  6250. if ($action == 'move') {
  6251. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6252. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6253. }
  6254. if (is_numeric($extra_info)) {
  6255. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6256. } elseif (is_array($extra_info)) {
  6257. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6258. }
  6259. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6260. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6261. $return .= '</form>';
  6262. $return .= '</div>';
  6263. return $return;
  6264. }
  6265. /**
  6266. * Addition of Hotpotatoes tests
  6267. * @param string Action
  6268. * @param integer Internal ID of the item
  6269. * @param mixed Extra information - can be an array with title and description indexes
  6270. * @return string HTML structure to display the hotpotatoes addition formular
  6271. */
  6272. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6273. {
  6274. $course_id = api_get_course_int_id();
  6275. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6276. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6277. if ($id != 0 && is_array($extra_info)) {
  6278. $item_title = stripslashes($extra_info['title']);
  6279. $item_description = stripslashes($extra_info['description']);
  6280. } elseif (is_numeric($extra_info)) {
  6281. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6282. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6283. WHERE c_id = ".$course_id." AND
  6284. path LIKE '".$uploadPath."/%/%htm%' AND
  6285. id = ".(int)$extra_info."
  6286. ORDER BY id ASC";
  6287. $res_hot = Database::query($sql_hot);
  6288. $row = Database::fetch_array($res_hot);
  6289. $item_title = $row['title'];
  6290. $item_description = $row['description'];
  6291. if (!empty ($row['comment'])) {
  6292. $item_title = $row['comment'];
  6293. }
  6294. } else {
  6295. $item_title = '';
  6296. $item_description = '';
  6297. }
  6298. if ($id != 0 && is_array($extra_info)) {
  6299. $parent = $extra_info['parent_item_id'];
  6300. } else {
  6301. $parent = 0;
  6302. }
  6303. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6304. $result = Database::query($sql);
  6305. $arrLP = array();
  6306. while ($row = Database :: fetch_array($result)) {
  6307. $arrLP[] = array(
  6308. 'id' => $row['id'],
  6309. 'item_type' => $row['item_type'],
  6310. 'title' => $row['title'],
  6311. 'path' => $row['path'],
  6312. 'description' => $row['description'],
  6313. 'parent_item_id' => $row['parent_item_id'],
  6314. 'previous_item_id' => $row['previous_item_id'],
  6315. 'next_item_id' => $row['next_item_id'],
  6316. 'display_order' => $row['display_order'],
  6317. 'max_score' => $row['max_score'],
  6318. 'min_score' => $row['min_score'],
  6319. 'mastery_score' => $row['mastery_score'],
  6320. 'prerequisite' => $row['prerequisite'],
  6321. 'max_time_allowed' => $row['max_time_allowed']
  6322. );
  6323. }
  6324. $legend = '<legend>';
  6325. if ($action == 'add') {
  6326. $legend .= get_lang('CreateTheExercise');
  6327. } elseif ($action == 'move') {
  6328. $legend .= get_lang('MoveTheCurrentExercise');
  6329. } else {
  6330. $legend .= get_lang('EditCurrentExecice');
  6331. }
  6332. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6333. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6334. }
  6335. $legend .= '</legend>';
  6336. $return .= '<form method="POST">';
  6337. $return .= $legend;
  6338. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6339. $return .= '<tr>';
  6340. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6341. $return .= '<td class="input">';
  6342. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6343. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6344. $arrHide = array(
  6345. $id
  6346. );
  6347. if (count($arrLP) > 0) {
  6348. for ($i = 0; $i < count($arrLP); $i++) {
  6349. if ($action != 'add') {
  6350. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6351. $arrLP[$i]['id'],
  6352. $arrHide
  6353. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6354. ) {
  6355. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6356. } else {
  6357. $arrHide[] = $arrLP[$i]['id'];
  6358. }
  6359. } else {
  6360. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6361. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6362. }
  6363. }
  6364. }
  6365. reset($arrLP);
  6366. }
  6367. $return .= '</select>';
  6368. $return .= '</td>';
  6369. $return .= '</tr>';
  6370. $return .= '<tr>';
  6371. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6372. $return .= '<td class="input">';
  6373. $return .= '<select id="previous" name="previous" size="1">';
  6374. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6375. for ($i = 0; $i < count($arrLP); $i++) {
  6376. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6377. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6378. $selected = 'selected="selected" ';
  6379. } elseif ($action == 'add') {
  6380. $selected = 'selected="selected" ';
  6381. } else {
  6382. $selected = '';
  6383. }
  6384. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6385. 'After'
  6386. ).' "'.$arrLP[$i]['title'].'"</option>';
  6387. }
  6388. }
  6389. $return .= '</select>';
  6390. $return .= '</td>';
  6391. $return .= '</tr>';
  6392. if ($action != 'move') {
  6393. $return .= '<tr>';
  6394. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6395. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6396. $return .= '</tr>';
  6397. $id_prerequisite = 0;
  6398. if (is_array($arrLP) && count($arrLP) > 0) {
  6399. foreach ($arrLP as $key => $value) {
  6400. if ($value['id'] == $id) {
  6401. $id_prerequisite = $value['prerequisite'];
  6402. break;
  6403. }
  6404. }
  6405. $arrHide = array();
  6406. for ($i = 0; $i < count($arrLP); $i++) {
  6407. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6408. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6409. $s_selected_position = $arrLP[$i]['id'];
  6410. } elseif ($action == 'add') {
  6411. $s_selected_position = 0;
  6412. }
  6413. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6414. }
  6415. }
  6416. }
  6417. }
  6418. $return .= '<tr>';
  6419. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6420. 'SaveHotpotatoes'
  6421. ).'</button></td>';
  6422. $return .= '</tr>';
  6423. $return .= '</table>';
  6424. if ($action == 'move') {
  6425. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6426. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6427. }
  6428. if (is_numeric($extra_info)) {
  6429. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6430. } elseif (is_array($extra_info)) {
  6431. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6432. }
  6433. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6434. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6435. $return .= '</form>';
  6436. return $return;
  6437. }
  6438. /**
  6439. * Return the form to display the forum edit/add option
  6440. * @param string Action (add/edit)
  6441. * @param integer ID of the lp_item if already exists
  6442. * @param mixed Forum ID or title
  6443. * @return string HTML form
  6444. */
  6445. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6446. {
  6447. $course_id = api_get_course_int_id();
  6448. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6449. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6450. if ($id != 0 && is_array($extra_info)) {
  6451. $item_title = stripslashes($extra_info['title']);
  6452. } elseif (is_numeric($extra_info)) {
  6453. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6454. FROM ".$tbl_forum."
  6455. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6456. $result = Database::query($sql_forum);
  6457. $row = Database :: fetch_array($result);
  6458. $item_title = $row['title'];
  6459. $item_description = $row['comment'];
  6460. } else {
  6461. $item_title = '';
  6462. $item_description = '';
  6463. }
  6464. $legend = '<legend>';
  6465. if ($id != 0 && is_array($extra_info)) {
  6466. $parent = $extra_info['parent_item_id'];
  6467. } else {
  6468. $parent = 0;
  6469. }
  6470. $sql = "SELECT * FROM ".$tbl_lp_item."
  6471. WHERE c_id = ".$course_id." AND
  6472. lp_id = ".$this->lp_id;
  6473. $result = Database::query($sql);
  6474. $arrLP = array();
  6475. while ($row = Database :: fetch_array($result)) {
  6476. $arrLP[] = array(
  6477. 'id' => $row['id'],
  6478. 'item_type' => $row['item_type'],
  6479. 'title' => $row['title'],
  6480. 'path' => $row['path'],
  6481. 'description' => $row['description'],
  6482. 'parent_item_id' => $row['parent_item_id'],
  6483. 'previous_item_id' => $row['previous_item_id'],
  6484. 'next_item_id' => $row['next_item_id'],
  6485. 'display_order' => $row['display_order'],
  6486. 'max_score' => $row['max_score'],
  6487. 'min_score' => $row['min_score'],
  6488. 'mastery_score' => $row['mastery_score'],
  6489. 'prerequisite' => $row['prerequisite']
  6490. );
  6491. }
  6492. $this->tree_array($arrLP);
  6493. $arrLP = $this->arrMenu;
  6494. unset ($this->arrMenu);
  6495. if ($action == 'add') {
  6496. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6497. } elseif ($action == 'move') {
  6498. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6499. } else {
  6500. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6501. }
  6502. $legend .= '</legend>';
  6503. $return .= '<div class="sectioncomment">';
  6504. $return .= '<form method="POST">';
  6505. $return .= $legend;
  6506. $return .= '<table class="lp_form">';
  6507. if ($action != 'move') {
  6508. $return .= '<tr>';
  6509. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6510. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6511. $return .= '</tr>';
  6512. }
  6513. $return .= '<tr>';
  6514. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6515. $return .= '<td class="input">';
  6516. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6517. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6518. $arrHide = array(
  6519. $id
  6520. );
  6521. //$parent_item_id = $_SESSION['parent_item_id'];
  6522. for ($i = 0; $i < count($arrLP); $i++) {
  6523. if ($action != 'add') {
  6524. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6525. $arrLP[$i]['id'],
  6526. $arrHide
  6527. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6528. ) {
  6529. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6530. } else {
  6531. $arrHide[] = $arrLP[$i]['id'];
  6532. }
  6533. } else {
  6534. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6535. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6536. }
  6537. }
  6538. }
  6539. if (is_array($arrLP)) {
  6540. reset($arrLP);
  6541. }
  6542. $return .= "\t\t\t\t".'</select>';
  6543. $return .= '</td>';
  6544. $return .= '</tr>';
  6545. $return .= '<tr>';
  6546. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6547. $return .= '<td class="input">';
  6548. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6549. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6550. for ($i = 0; $i < count($arrLP); $i++) {
  6551. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6552. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6553. $selected = 'selected="selected" ';
  6554. } elseif ($action == 'add') {
  6555. $selected = 'selected="selected" ';
  6556. } else {
  6557. $selected = '';
  6558. }
  6559. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6560. 'After'
  6561. ).' "'.$arrLP[$i]['title'].'"</option>';
  6562. }
  6563. }
  6564. $return .= "\t\t\t\t".'</select>';
  6565. $return .= '</td>';
  6566. $return .= '</tr>';
  6567. if ($action != 'move') {
  6568. $return .= '<tr>';
  6569. $return .= '</tr>';
  6570. $id_prerequisite = 0;
  6571. if (is_array($arrLP)) {
  6572. foreach ($arrLP as $key => $value) {
  6573. if ($value['id'] == $id) {
  6574. $id_prerequisite = $value['prerequisite'];
  6575. break;
  6576. }
  6577. }
  6578. }
  6579. $arrHide = array();
  6580. for ($i = 0; $i < count($arrLP); $i++) {
  6581. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6582. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6583. $s_selected_position = $arrLP[$i]['id'];
  6584. } elseif ($action == 'add') {
  6585. $s_selected_position = 0;
  6586. }
  6587. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6588. }
  6589. }
  6590. $return .= '</tr>';
  6591. }
  6592. $return .= '<tr>';
  6593. if ($action == 'add') {
  6594. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6595. 'AddForumToCourse'
  6596. ).' </button></td>';
  6597. } else {
  6598. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6599. 'EditCurrentForum'
  6600. ).' </button></td>';
  6601. }
  6602. $return .= '</tr>';
  6603. $return .= '</table>';
  6604. if ($action == 'move') {
  6605. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6606. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6607. }
  6608. if (is_numeric($extra_info)) {
  6609. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6610. } elseif (is_array($extra_info)) {
  6611. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6612. }
  6613. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6614. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6615. $return .= '</form>';
  6616. $return .= '</div>';
  6617. return $return;
  6618. }
  6619. /**
  6620. * Return HTML form to add/edit forum threads
  6621. * @param string Action (add/edit)
  6622. * @param integer Item ID if already exists in learning path
  6623. * @param mixed Extra information (thread ID if integer)
  6624. * @return string HTML form
  6625. */
  6626. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6627. {
  6628. $course_id = api_get_course_int_id();
  6629. if (empty($course_id)) {
  6630. return null;
  6631. }
  6632. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6633. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6634. if ($id != 0 && is_array($extra_info)) {
  6635. $item_title = stripslashes($extra_info['title']);
  6636. } elseif (is_numeric($extra_info)) {
  6637. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6638. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6639. $result = Database::query($sql_forum);
  6640. $row = Database :: fetch_array($result);
  6641. $item_title = $row['title'];
  6642. $item_description = '';
  6643. } else {
  6644. $item_title = '';
  6645. $item_description = '';
  6646. }
  6647. $return = null;
  6648. if ($id != 0 && is_array($extra_info)) {
  6649. $parent = $extra_info['parent_item_id'];
  6650. } else {
  6651. $parent = 0;
  6652. }
  6653. $sql = "SELECT * FROM ".$tbl_lp_item."
  6654. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6655. $result = Database::query($sql);
  6656. $arrLP = array();
  6657. while ($row = Database :: fetch_array($result)) {
  6658. $arrLP[] = array(
  6659. 'id' => $row['id'],
  6660. 'item_type' => $row['item_type'],
  6661. 'title' => $row['title'],
  6662. 'path' => $row['path'],
  6663. 'description' => $row['description'],
  6664. 'parent_item_id' => $row['parent_item_id'],
  6665. 'previous_item_id' => $row['previous_item_id'],
  6666. 'next_item_id' => $row['next_item_id'],
  6667. 'display_order' => $row['display_order'],
  6668. 'max_score' => $row['max_score'],
  6669. 'min_score' => $row['min_score'],
  6670. 'mastery_score' => $row['mastery_score'],
  6671. 'prerequisite' => $row['prerequisite']
  6672. );
  6673. }
  6674. $this->tree_array($arrLP);
  6675. $arrLP = $this->arrMenu;
  6676. unset ($this->arrMenu);
  6677. $return .= '<form method="POST">';
  6678. if ($action == 'add') {
  6679. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6680. } elseif ($action == 'move') {
  6681. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6682. } else {
  6683. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6684. }
  6685. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6686. $return .= '<tr>';
  6687. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6688. $return .= '<td class="input">';
  6689. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6690. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6691. $arrHide = array(
  6692. $id
  6693. );
  6694. for ($i = 0; $i < count($arrLP); $i++) {
  6695. if ($action != 'add') {
  6696. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6697. $arrLP[$i]['id'],
  6698. $arrHide
  6699. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6700. ) {
  6701. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6702. } else {
  6703. $arrHide[] = $arrLP[$i]['id'];
  6704. }
  6705. } else {
  6706. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6707. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6708. }
  6709. }
  6710. }
  6711. if ($arrLP != null) {
  6712. reset($arrLP);
  6713. }
  6714. $return .= '</select>';
  6715. $return .= '</td>';
  6716. $return .= '</tr>';
  6717. $return .= '<tr>';
  6718. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6719. $return .= '<td class="input">';
  6720. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6721. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6722. for ($i = 0; $i < count($arrLP); $i++) {
  6723. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6724. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6725. $selected = 'selected="selected" ';
  6726. } elseif ($action == 'add') {
  6727. $selected = 'selected="selected" ';
  6728. } else {
  6729. $selected = '';
  6730. }
  6731. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6732. 'After'
  6733. ).' "'.$arrLP[$i]['title'].'"</option>';
  6734. }
  6735. }
  6736. $return .= "\t\t\t\t".'</select>';
  6737. $return .= '</td>';
  6738. $return .= '</tr>';
  6739. if ($action != 'move') {
  6740. $return .= '<tr>';
  6741. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6742. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6743. $return .= '</tr>';
  6744. $return .= '<tr>';
  6745. $return .= '</tr>';
  6746. $id_prerequisite = 0;
  6747. if ($arrLP != null) {
  6748. foreach ($arrLP as $key => $value) {
  6749. if ($value['id'] == $id) {
  6750. $id_prerequisite = $value['prerequisite'];
  6751. break;
  6752. }
  6753. }
  6754. }
  6755. $arrHide = array();
  6756. for ($i = 0; $i < count($arrLP); $i++) {
  6757. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6758. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6759. $s_selected_position = $arrLP[$i]['id'];
  6760. } elseif ($action == 'add') {
  6761. $s_selected_position = 0;
  6762. }
  6763. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6764. }
  6765. }
  6766. $return .= '<tr>';
  6767. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6768. 'LearnpathPrerequisites'
  6769. ).'</label></td>';
  6770. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6771. 'NoPrerequisites'
  6772. ).'</option>';
  6773. foreach ($arrHide as $key => $value) {
  6774. if ($key == $s_selected_position && $action == 'add') {
  6775. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6776. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6777. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6778. } else {
  6779. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6780. }
  6781. }
  6782. $return .= "</select></td>";
  6783. $return .= '</tr>';
  6784. }
  6785. $return .= '<tr>';
  6786. $return .= '<td></td><td>
  6787. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6788. 'Ok'
  6789. ).'</button></td>';
  6790. $return .= '</tr>';
  6791. $return .= '</table>';
  6792. if ($action == 'move') {
  6793. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6794. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6795. }
  6796. if (is_numeric($extra_info)) {
  6797. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6798. } elseif (is_array($extra_info)) {
  6799. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6800. }
  6801. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6802. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6803. $return .= '</form>';
  6804. $return .= '</div>';
  6805. return $return;
  6806. }
  6807. /**
  6808. * Return the HTML form to display an item (generally a section/module item)
  6809. * @param string Item type (module/dokeos_module)
  6810. * @param string Title (optional, only when creating)
  6811. * @param string Action ('add'/'edit')
  6812. * @param integer lp_item ID
  6813. * @param mixed Extra info
  6814. * @return string HTML form
  6815. */
  6816. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6817. {
  6818. $course_id = api_get_course_int_id();
  6819. $_course = api_get_course_info();
  6820. global $charset;
  6821. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6822. if ($id != 0 && is_array($extra_info)) {
  6823. $item_title = $extra_info['title'];
  6824. $item_description = $extra_info['description'];
  6825. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6826. $extra_info['path']
  6827. );
  6828. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6829. } else {
  6830. $item_title = '';
  6831. $item_description = '';
  6832. $item_path_fck = '';
  6833. }
  6834. if ($id != 0 && is_array($extra_info)) {
  6835. $parent = $extra_info['parent_item_id'];
  6836. } else {
  6837. $parent = 0;
  6838. }
  6839. $id = intval($id);
  6840. $sql = "SELECT * FROM ".$tbl_lp_item."
  6841. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6842. if ($item_type == 'module') {
  6843. $sql .= " AND parent_item_id = 0";
  6844. }
  6845. $result = Database::query($sql);
  6846. $arrLP = array();
  6847. while ($row = Database :: fetch_array($result)) {
  6848. $arrLP[] = array(
  6849. 'id' => $row['id'],
  6850. 'item_type' => $row['item_type'],
  6851. 'title' => $row['title'],
  6852. 'path' => $row['path'],
  6853. 'description' => $row['description'],
  6854. 'parent_item_id' => $row['parent_item_id'],
  6855. 'previous_item_id' => $row['previous_item_id'],
  6856. 'next_item_id' => $row['next_item_id'],
  6857. 'max_score' => $row['max_score'],
  6858. 'min_score' => $row['min_score'],
  6859. 'mastery_score' => $row['mastery_score'],
  6860. 'prerequisite' => $row['prerequisite'],
  6861. 'display_order' => $row['display_order']
  6862. );
  6863. }
  6864. $this->tree_array($arrLP);
  6865. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6866. unset($this->arrMenu);
  6867. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6868. $url = api_get_self().'?'.api_get_cidreq(
  6869. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6870. $form = new FormValidator('form', 'POST', $url);
  6871. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6872. $defaults['description'] = $item_description;
  6873. $form->addElement('header', $title);
  6874. //$arrHide = array($id);
  6875. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6876. $arrHide[0]['padding'] = 3;
  6877. $charset = api_get_system_encoding();
  6878. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6879. for ($i = 0; $i < count($arrLP); $i++) {
  6880. if ($action != 'add') {
  6881. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6882. $arrLP[$i]['id'],
  6883. $arrHide
  6884. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6885. ) {
  6886. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6887. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6888. if ($parent == $arrLP[$i]['id']) {
  6889. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6890. }
  6891. }
  6892. } else {
  6893. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6894. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6895. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6896. if ($parent == $arrLP[$i]['id']) {
  6897. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6898. }
  6899. }
  6900. }
  6901. }
  6902. if ($action != 'move') {
  6903. $form->addElement(
  6904. 'text',
  6905. 'title',
  6906. get_lang('Title'),
  6907. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6908. );
  6909. $form->applyFilter('title', 'html_filter');
  6910. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6911. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6912. } else {
  6913. $form->addElement('hidden', 'title');
  6914. }
  6915. $parent_select = $form->addElement(
  6916. 'select',
  6917. 'parent',
  6918. get_lang('Parent'),
  6919. '',
  6920. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6921. );
  6922. foreach ($arrHide as $key => $value) {
  6923. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6924. }
  6925. if (!empty($s_selected_parent)) {
  6926. $parent_select->setSelected($s_selected_parent);
  6927. }
  6928. }
  6929. if (is_array($arrLP)) {
  6930. reset($arrLP);
  6931. }
  6932. $arrHide = array();
  6933. // POSITION
  6934. for ($i = 0; $i < count($arrLP); $i++) {
  6935. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6936. //this is the same!
  6937. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6938. $s_selected_position = $arrLP[$i]['id'];
  6939. } elseif ($action == 'add') {
  6940. $s_selected_position = $arrLP[$i]['id'];
  6941. }
  6942. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6943. }
  6944. }
  6945. $position = $form->addElement(
  6946. 'select',
  6947. 'previous',
  6948. get_lang('Position'),
  6949. '',
  6950. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6951. );
  6952. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6953. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6954. foreach ($arrHide as $key => $value) {
  6955. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6956. }
  6957. if (!empty ($s_selected_position)) {
  6958. $position->setSelected($s_selected_position);
  6959. }
  6960. if (is_array($arrLP)) {
  6961. reset($arrLP);
  6962. }
  6963. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6964. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6965. $form->addElement('hidden', 'parent', '0');
  6966. }
  6967. //fix in order to use the tab
  6968. if ($item_type == 'chapter') {
  6969. $form->addElement('hidden', 'type', 'chapter');
  6970. }
  6971. $extension = null;
  6972. if (!empty($item_path)) {
  6973. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6974. }
  6975. //assets can't be modified
  6976. //$item_type == 'asset' ||
  6977. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6978. if ($item_type == 'sco') {
  6979. $form->addElement(
  6980. 'html',
  6981. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  6982. );
  6983. }
  6984. $renderer = $form->defaultRenderer();
  6985. $renderer->setElementTemplate(
  6986. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  6987. 'content_lp'
  6988. );
  6989. $relative_prefix = '';
  6990. $editor_config = array(
  6991. 'ToolbarSet' => 'LearningPathDocuments',
  6992. 'Width' => '100%',
  6993. 'Height' => '500',
  6994. 'FullPage' => true,
  6995. 'CreateDocumentDir' => $relative_prefix,
  6996. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  6997. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  6998. );
  6999. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7000. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  7001. //$defaults['content_lp'] = file_get_contents($item_path);
  7002. $defaults['content_lp'] = file_get_contents($content_path);
  7003. }
  7004. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  7005. $form->addElement('hidden', 'post_time', time());
  7006. $form->setDefaults($defaults);
  7007. return $form->return_form();
  7008. }
  7009. /**
  7010. * Returns the form to update or create a document
  7011. * @param string Action (add/edit)
  7012. * @param integer ID of the lp_item (if already exists)
  7013. * @param mixed Integer if document ID, string if info ('new')
  7014. * @return string HTML form
  7015. */
  7016. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7017. {
  7018. $course_id = api_get_course_int_id();
  7019. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7020. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7021. $no_display_edit_textarea = false;
  7022. //If action==edit document
  7023. //We don't display the document form if it's not an editable document (html or txt file)
  7024. if ($action == "edit") {
  7025. if (is_array($extra_info)) {
  7026. $path_parts = pathinfo($extra_info['dir']);
  7027. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7028. $no_display_edit_textarea = true;
  7029. }
  7030. }
  7031. }
  7032. $no_display_add = false;
  7033. // If action==add an existing document
  7034. // We don't display the document form if it's not an editable document (html or txt file).
  7035. if ($action == "add") {
  7036. if (is_numeric($extra_info)) {
  7037. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  7038. $extra_info
  7039. );
  7040. $result = Database::query($sql_doc);
  7041. $path_file = Database :: result($result, 0, 0);
  7042. $path_parts = pathinfo($path_file);
  7043. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7044. $no_display_add = true;
  7045. }
  7046. }
  7047. }
  7048. if ($id != 0 && is_array($extra_info)) {
  7049. $item_title = stripslashes($extra_info['title']);
  7050. $item_description = stripslashes($extra_info['description']);
  7051. $item_terms = stripslashes($extra_info['terms']);
  7052. if (empty ($item_title)) {
  7053. $path_parts = pathinfo($extra_info['path']);
  7054. $item_title = stripslashes($path_parts['filename']);
  7055. }
  7056. } elseif (is_numeric($extra_info)) {
  7057. $sql_doc = "SELECT path, title FROM ".$tbl_doc."
  7058. WHERE c_id = ".$course_id." AND id = ".Database::escape_string($extra_info);
  7059. $result = Database::query($sql_doc);
  7060. $row = Database::fetch_array($result);
  7061. $explode = explode('.', $row['title']);
  7062. if (count($explode) > 1) {
  7063. for ($i = 0; $i < count($explode) - 1; $i++) {
  7064. $item_title .= $explode[$i];
  7065. }
  7066. } else {
  7067. $item_title = $row['title'];
  7068. }
  7069. $item_title = str_replace('_', ' ', $item_title);
  7070. if (empty ($item_title)) {
  7071. $path_parts = pathinfo($row['path']);
  7072. $item_title = stripslashes($path_parts['filename']);
  7073. }
  7074. } else {
  7075. $item_title = '';
  7076. $item_description = '';
  7077. }
  7078. $return = '<legend>';
  7079. if ($id != 0 && is_array($extra_info)) {
  7080. $parent = $extra_info['parent_item_id'];
  7081. } else {
  7082. $parent = 0;
  7083. }
  7084. $sql = "SELECT * FROM ".$tbl_lp_item."
  7085. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7086. $result = Database::query($sql);
  7087. $arrLP = array();
  7088. while ($row = Database :: fetch_array($result)) {
  7089. $arrLP[] = array(
  7090. 'id' => $row['id'],
  7091. 'item_type' => $row['item_type'],
  7092. 'title' => $row['title'],
  7093. 'path' => $row['path'],
  7094. 'description' => $row['description'],
  7095. 'parent_item_id' => $row['parent_item_id'],
  7096. 'previous_item_id' => $row['previous_item_id'],
  7097. 'next_item_id' => $row['next_item_id'],
  7098. 'display_order' => $row['display_order'],
  7099. 'max_score' => $row['max_score'],
  7100. 'min_score' => $row['min_score'],
  7101. 'mastery_score' => $row['mastery_score'],
  7102. 'prerequisite' => $row['prerequisite']
  7103. );
  7104. }
  7105. $this->tree_array($arrLP);
  7106. $arrLP = null;
  7107. if (isset($this->arrMenu)) {
  7108. $arrLP = $this->arrMenu;
  7109. unset ($this->arrMenu);
  7110. }
  7111. if ($action == 'add') {
  7112. $return .= get_lang('CreateTheDocument');
  7113. } elseif ($action == 'move') {
  7114. $return .= get_lang('MoveTheCurrentDocument');
  7115. } else {
  7116. $return .= get_lang('EditTheCurrentDocument');
  7117. }
  7118. $return .= '</legend>';
  7119. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7120. $return .= Display :: return_warning_message(
  7121. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7122. false
  7123. );
  7124. }
  7125. $form = new FormValidator('form', 'POST', api_get_self(
  7126. ).'?'.$_SERVER['QUERY_STRING'], '', array('enctype' => "multipart/form-data"));
  7127. $defaults['title'] = Security :: remove_XSS($item_title);
  7128. if (empty($item_title)) {
  7129. $defaults['title'] = Security::remove_XSS($item_title);
  7130. }
  7131. $defaults['description'] = $item_description;
  7132. $form->addElement('html', $return);
  7133. if ($action != 'move') {
  7134. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  7135. $form->applyFilter('title', 'html_filter');
  7136. }
  7137. //$arrHide = array($id);
  7138. $arrHide[0]['value'] = $this->name;
  7139. $arrHide[0]['padding'] = 3;
  7140. for ($i = 0; $i < count($arrLP); $i++) {
  7141. if ($action != 'add') {
  7142. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7143. $arrLP[$i]['id'],
  7144. $arrHide
  7145. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7146. ) {
  7147. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7148. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7149. if ($parent == $arrLP[$i]['id']) {
  7150. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7151. }
  7152. }
  7153. } else {
  7154. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7155. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7156. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7157. if ($parent == $arrLP[$i]['id']) {
  7158. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7159. }
  7160. }
  7161. }
  7162. }
  7163. $parent_select = $form->addElement(
  7164. 'select',
  7165. 'parent',
  7166. get_lang('Parent'),
  7167. '',
  7168. 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"'
  7169. );
  7170. $my_count = 0;
  7171. foreach ($arrHide as $key => $value) {
  7172. if ($my_count != 0) {
  7173. // The LP name is also the first section and is not in the same charset like the other sections.
  7174. $value['value'] = Security :: remove_XSS($value['value']);
  7175. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7176. } else {
  7177. $value['value'] = Security :: remove_XSS($value['value']);
  7178. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7179. }
  7180. $my_count++;
  7181. }
  7182. if (!empty($id)) {
  7183. $parent_select->setSelected($parent);
  7184. } else {
  7185. $parent_item_id = Session::read('parent_item_id');
  7186. $parent_select->setSelected($parent_item_id);
  7187. }
  7188. if (is_array($arrLP)) {
  7189. reset($arrLP);
  7190. }
  7191. $arrHide = array();
  7192. $s_selected_position = null;
  7193. //POSITION
  7194. for ($i = 0; $i < count($arrLP); $i++) {
  7195. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7196. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7197. $s_selected_position = $arrLP[$i]['id'];
  7198. } elseif ($action == 'add') {
  7199. $s_selected_position = $arrLP[$i]['id'];
  7200. }
  7201. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7202. }
  7203. }
  7204. $position = $form->addElement(
  7205. 'select',
  7206. 'previous',
  7207. get_lang('Position'),
  7208. '',
  7209. 'id="previous" class="learnpath_item_form" style="width:40%;"'
  7210. );
  7211. $position->addOption(get_lang('FirstPosition'), 0);
  7212. foreach ($arrHide as $key => $value) {
  7213. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7214. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7215. }
  7216. $position->setSelected($s_selected_position);
  7217. if (is_array($arrLP)) {
  7218. reset($arrLP);
  7219. }
  7220. if ($action != 'move') {
  7221. $id_prerequisite = 0;
  7222. if (is_array($arrLP)) {
  7223. foreach ($arrLP as $key => $value) {
  7224. if ($value['id'] == $id) {
  7225. $id_prerequisite = $value['prerequisite'];
  7226. break;
  7227. }
  7228. }
  7229. }
  7230. $arrHide = array();
  7231. for ($i = 0; $i < count($arrLP); $i++) {
  7232. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7233. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7234. $s_selected_position = $arrLP[$i]['id'];
  7235. } elseif ($action == 'add') {
  7236. $s_selected_position = $arrLP[$i]['id'];
  7237. }
  7238. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7239. }
  7240. }
  7241. if (!$no_display_add) {
  7242. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  7243. if (isset ($_POST['content'])) {
  7244. $content = stripslashes($_POST['content']);
  7245. } elseif (is_array($extra_info)) {
  7246. //If it's an html document or a text file
  7247. if (!$no_display_edit_textarea) {
  7248. $content = $this->display_document($extra_info['path'], false, false);
  7249. }
  7250. } elseif (is_numeric($extra_info)) {
  7251. $content = $this->display_document($extra_info, false, false);
  7252. } else {
  7253. $content = '';
  7254. }
  7255. if (!$no_display_edit_textarea) {
  7256. // We need to calculate here some specific settings for the online editor.
  7257. // The calculated settings work for documents in the Documents tool
  7258. // (on the root or in subfolders).
  7259. // For documents in native scorm packages it is unclear whether the
  7260. // online editor should be activated or not.
  7261. // A new document, it is in the root of the repository.
  7262. $relative_path = '';
  7263. $relative_prefix = '';
  7264. if (is_array($extra_info) && $extra_info != 'new') {
  7265. // The document already exists. Whe have to determine its relative path towards the repository root.
  7266. $relative_path = explode('/', $extra_info['dir']);
  7267. $cnt = count($relative_path) - 2;
  7268. if ($cnt < 0) {
  7269. $cnt = 0;
  7270. }
  7271. $relative_prefix = str_repeat('../', $cnt);
  7272. $relative_path = array_slice($relative_path, 1, $cnt);
  7273. $relative_path = implode('/', $relative_path);
  7274. if (strlen($relative_path) > 0) {
  7275. $relative_path = $relative_path.'/';
  7276. }
  7277. } else {
  7278. $_course = api_get_course_info();
  7279. $result = $this->generate_lp_folder($_course);
  7280. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7281. $relative_prefix = '../../';
  7282. }
  7283. $editor_config = array(
  7284. 'ToolbarSet' => 'LearningPathDocuments',
  7285. 'Width' => '100%',
  7286. 'Height' => '500',
  7287. 'FullPage' => true,
  7288. 'CreateDocumentDir' => $relative_prefix,
  7289. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7290. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path(
  7291. ).'/document/'.$relative_path
  7292. );
  7293. if ($_GET['action'] == 'add_item') {
  7294. $class = 'add';
  7295. $text = get_lang('LPCreateDocument');
  7296. } else {
  7297. if ($_GET['action'] == 'edit_item') {
  7298. $class = 'save';
  7299. $text = get_lang('SaveDocument');
  7300. }
  7301. }
  7302. $form->addElement('style_submit_button', 'submit_button', $text, 'class="'.$class.'"');
  7303. $renderer = $form->defaultRenderer();
  7304. $renderer->setElementTemplate(
  7305. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7306. 'content_lp'
  7307. );
  7308. $form->addElement('html', '<div>');
  7309. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7310. $form->addElement('html', '</div>');
  7311. $defaults['content_lp'] = $content;
  7312. }
  7313. } elseif (is_numeric($extra_info)) {
  7314. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7315. $return = $this->display_document($extra_info, true, true, true);
  7316. $form->addElement('html', $return);
  7317. }
  7318. }
  7319. }
  7320. if ($action == 'move') {
  7321. $form->addElement('hidden', 'title', $item_title);
  7322. $form->addElement('hidden', 'description', $item_description);
  7323. }
  7324. if (is_numeric($extra_info)) {
  7325. $form->addElement(
  7326. 'style_submit_button',
  7327. 'submit_button',
  7328. get_lang('SaveDocument'),
  7329. 'value="submit_button", class="save"'
  7330. );
  7331. $form->addElement('hidden', 'path', $extra_info);
  7332. } elseif (is_array($extra_info)) {
  7333. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7334. $form->addElement('hidden', 'path', $extra_info['path']);
  7335. }
  7336. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7337. $form->addElement('hidden', 'post_time', time());
  7338. $form->setDefaults($defaults);
  7339. return $form->return_form();
  7340. }
  7341. /**
  7342. * Return HTML form to add/edit a link item
  7343. * @param string Action (add/edit)
  7344. * @param integer Item ID if exists
  7345. * @param mixed Extra info
  7346. * @return string HTML form
  7347. */
  7348. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7349. {
  7350. $course_id = api_get_course_int_id();
  7351. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7352. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7353. if ($id != 0 && is_array($extra_info)) {
  7354. $item_title = stripslashes($extra_info['title']);
  7355. $item_description = stripslashes($extra_info['description']);
  7356. $item_url = stripslashes($extra_info['url']);
  7357. } elseif (is_numeric($extra_info)) {
  7358. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7359. $result = Database::query($sql_link);
  7360. $row = Database :: fetch_array($result);
  7361. $item_title = $row['title'];
  7362. $item_description = $row['description'];
  7363. $item_url = $row['url'];
  7364. } else {
  7365. $item_title = '';
  7366. $item_description = '';
  7367. $item_url = '';
  7368. }
  7369. $legend = '<legend>';
  7370. if ($id != 0 && is_array($extra_info)) {
  7371. $parent = $extra_info['parent_item_id'];
  7372. } else {
  7373. $parent = 0;
  7374. }
  7375. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7376. $result = Database::query($sql);
  7377. $arrLP = array();
  7378. while ($row = Database :: fetch_array($result)) {
  7379. $arrLP[] = array(
  7380. 'id' => $row['id'],
  7381. 'item_type' => $row['item_type'],
  7382. 'title' => $row['title'],
  7383. 'path' => $row['path'],
  7384. 'description' => $row['description'],
  7385. 'parent_item_id' => $row['parent_item_id'],
  7386. 'previous_item_id' => $row['previous_item_id'],
  7387. 'next_item_id' => $row['next_item_id'],
  7388. 'display_order' => $row['display_order'],
  7389. 'max_score' => $row['max_score'],
  7390. 'min_score' => $row['min_score'],
  7391. 'mastery_score' => $row['mastery_score'],
  7392. 'prerequisite' => $row['prerequisite']
  7393. );
  7394. }
  7395. $this->tree_array($arrLP);
  7396. $arrLP = $this->arrMenu;
  7397. unset ($this->arrMenu);
  7398. if ($action == 'add') {
  7399. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7400. } elseif ($action == 'move') {
  7401. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7402. } else {
  7403. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7404. }
  7405. $legend .= '</legend>';
  7406. $return .= '<div class="sectioncomment">';
  7407. $return .= '<form method="POST">';
  7408. $return .= $legend;
  7409. $return .= '<table>';
  7410. if ($action != 'move') {
  7411. $return .= '<tr>';
  7412. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7413. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7414. $return .= '</tr>';
  7415. }
  7416. $return .= '<tr>';
  7417. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7418. $return .= '<td class="input">';
  7419. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7420. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7421. $arrHide = array(
  7422. $id
  7423. );
  7424. $parent_item_id = $_SESSION['parent_item_id'];
  7425. for ($i = 0; $i < count($arrLP); $i++) {
  7426. if ($action != 'add') {
  7427. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7428. $arrLP[$i]['id'],
  7429. $arrHide
  7430. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7431. ) {
  7432. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7433. } else {
  7434. $arrHide[] = $arrLP[$i]['id'];
  7435. }
  7436. } else {
  7437. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7438. $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>';
  7439. }
  7440. }
  7441. }
  7442. if (is_array($arrLP)) {
  7443. reset($arrLP);
  7444. }
  7445. $return .= '</select>';
  7446. $return .= '</td>';
  7447. $return .= '</tr>';
  7448. $return .= '<tr>';
  7449. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7450. $return .= '<td class="input">';
  7451. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7452. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7453. for ($i = 0; $i < count($arrLP); $i++) {
  7454. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7455. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7456. $selected = 'selected="selected" ';
  7457. } elseif ($action == 'add') {
  7458. $selected = 'selected="selected" ';
  7459. } else {
  7460. $selected = '';
  7461. }
  7462. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7463. 'After'
  7464. ).' "'.$arrLP[$i]['title'].'"</option>';
  7465. }
  7466. }
  7467. $return .= '</select>';
  7468. $return .= '</td>';
  7469. $return .= '</tr>';
  7470. if ($action != 'move') {
  7471. $return .= '<tr>';
  7472. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7473. $return .= '<td class="input"><input'.(is_numeric(
  7474. $extra_info
  7475. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7476. $return .= '</tr>';
  7477. $id_prerequisite = 0;
  7478. if (is_array($arrLP)) {
  7479. foreach ($arrLP as $key => $value) {
  7480. if ($value['id'] == $id) {
  7481. $id_prerequisite = $value['prerequisite'];
  7482. break;
  7483. }
  7484. }
  7485. }
  7486. $arrHide = array();
  7487. for ($i = 0; $i < count($arrLP); $i++) {
  7488. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7489. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7490. $s_selected_position = $arrLP[$i]['id'];
  7491. } elseif ($action == 'add') {
  7492. $s_selected_position = 0;
  7493. }
  7494. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7495. }
  7496. }
  7497. $return .= '</tr>';
  7498. }
  7499. $return .= '<tr>';
  7500. if ($action == 'add') {
  7501. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7502. 'AddLinkToCourse'
  7503. ).'</button></td>';
  7504. } else {
  7505. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7506. 'EditCurrentLink'
  7507. ).'</button></td>';
  7508. }
  7509. $return .= '</tr>';
  7510. $return .= '</table>';
  7511. if ($action == 'move') {
  7512. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7513. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7514. }
  7515. if (is_numeric($extra_info)) {
  7516. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7517. } elseif (is_array($extra_info)) {
  7518. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7519. }
  7520. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7521. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7522. $return .= '</form>';
  7523. $return .= '</div>';
  7524. return $return;
  7525. }
  7526. /**
  7527. * Return HTML form to add/edit a student publication (work)
  7528. * @param string Action (add/edit)
  7529. * @param integer Item ID if already exists
  7530. * @param mixed Extra info (work ID if integer)
  7531. * @return string HTML form
  7532. */
  7533. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7534. {
  7535. $course_id = api_get_course_int_id();
  7536. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7537. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7538. if ($id != 0 && is_array($extra_info)) {
  7539. $item_title = stripslashes($extra_info['title']);
  7540. $item_description = stripslashes($extra_info['description']);
  7541. } elseif (is_numeric($extra_info)) {
  7542. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7543. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7544. $result = Database::query($sql_publication);
  7545. $row = Database :: fetch_array($result);
  7546. $item_title = $row['title'];
  7547. } else {
  7548. $item_title = get_lang('Student_publication');
  7549. }
  7550. $legend = '<legend>';
  7551. if ($id != 0 && is_array($extra_info)) {
  7552. $parent = $extra_info['parent_item_id'];
  7553. } else {
  7554. $parent = 0;
  7555. }
  7556. $sql = "SELECT * FROM ".$tbl_lp_item."
  7557. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7558. $result = Database::query($sql);
  7559. $arrLP = array();
  7560. while ($row = Database :: fetch_array($result)) {
  7561. $arrLP[] = array(
  7562. 'id' => $row['id'],
  7563. 'item_type' => $row['item_type'],
  7564. 'title' => $row['title'],
  7565. 'path' => $row['path'],
  7566. 'description' => $row['description'],
  7567. 'parent_item_id' => $row['parent_item_id'],
  7568. 'previous_item_id' => $row['previous_item_id'],
  7569. 'next_item_id' => $row['next_item_id'],
  7570. 'display_order' => $row['display_order'],
  7571. 'max_score' => $row['max_score'],
  7572. 'min_score' => $row['min_score'],
  7573. 'mastery_score' => $row['mastery_score'],
  7574. 'prerequisite' => $row['prerequisite']
  7575. );
  7576. }
  7577. $this->tree_array($arrLP);
  7578. $arrLP = $this->arrMenu;
  7579. unset ($this->arrMenu);
  7580. if ($action == 'add') {
  7581. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7582. } elseif ($action == 'move') {
  7583. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7584. } else {
  7585. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7586. }
  7587. $legend .= '</legend>';
  7588. $return .= '<div class="sectioncomment">';
  7589. $return .= '<form method="POST">';
  7590. $return .= $legend;
  7591. $return .= '<table class="lp_form">';
  7592. if ($action != 'move') {
  7593. $return .= '<tr>';
  7594. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7595. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7596. $return .= '</tr>';
  7597. }
  7598. $return .= '<tr>';
  7599. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7600. $return .= '<td class="input">';
  7601. $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">';
  7602. //$parent_item_id = $_SESSION['parent_item_id'];
  7603. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7604. $arrHide = array(
  7605. $id
  7606. );
  7607. for ($i = 0; $i < count($arrLP); $i++) {
  7608. if ($action != 'add') {
  7609. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7610. $arrLP[$i]['id'],
  7611. $arrHide
  7612. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7613. ) {
  7614. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7615. } else {
  7616. $arrHide[] = $arrLP[$i]['id'];
  7617. }
  7618. } else {
  7619. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7620. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7621. }
  7622. }
  7623. }
  7624. if (is_array($arrLP)) {
  7625. reset($arrLP);
  7626. }
  7627. $return .= "\t\t\t\t".'</select>';
  7628. $return .= '</td>';
  7629. $return .= '</tr>';
  7630. $return .= '<tr>';
  7631. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7632. $return .= '<td class="input">';
  7633. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7634. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7635. for ($i = 0; $i < count($arrLP); $i++) {
  7636. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7637. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7638. $selected = 'selected="selected" ';
  7639. } elseif ($action == 'add') {
  7640. $selected = 'selected="selected" ';
  7641. } else {
  7642. $selected = '';
  7643. }
  7644. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7645. 'After'
  7646. ).' "'.$arrLP[$i]['title'].'"</option>';
  7647. }
  7648. }
  7649. $return .= "\t\t\t\t".'</select>';
  7650. $return .= '</td>';
  7651. $return .= '</tr>';
  7652. if ($action != 'move') {
  7653. $id_prerequisite = 0;
  7654. if (is_array($arrLP)) {
  7655. foreach ($arrLP as $key => $value) {
  7656. if ($value['id'] == $id) {
  7657. $id_prerequisite = $value['prerequisite'];
  7658. break;
  7659. }
  7660. }
  7661. }
  7662. $arrHide = array();
  7663. for ($i = 0; $i < count($arrLP); $i++) {
  7664. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7665. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7666. $s_selected_position = $arrLP[$i]['id'];
  7667. } elseif ($action == 'add') {
  7668. $s_selected_position = 0;
  7669. }
  7670. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7671. }
  7672. }
  7673. // Commented the prerequisites, only visible in edit (work).
  7674. /*
  7675. $return .= '<tr>';
  7676. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7677. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7678. foreach($arrHide as $key => $value) {
  7679. if ($key == $s_selected_position && $action == 'add') {
  7680. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7681. }
  7682. elseif ($key == $id_prerequisite && $action == 'edit') {
  7683. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7684. }
  7685. else {
  7686. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7687. }
  7688. }
  7689. $return .= "</select></td>";
  7690. */
  7691. $return .= '</tr>';
  7692. }
  7693. $return .= '<tr>';
  7694. if ($action == 'add') {
  7695. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7696. 'AddAssignmentToCourse'
  7697. ).'</button></td>';
  7698. } else {
  7699. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7700. 'EditCurrentStudentPublication'
  7701. ).'</button></td>';
  7702. }
  7703. $return .= '</tr>';
  7704. $return .= '</table>';
  7705. if ($action == 'move') {
  7706. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7707. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7708. }
  7709. if (is_numeric($extra_info)) {
  7710. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7711. } elseif (is_array($extra_info)) {
  7712. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7713. }
  7714. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7715. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7716. $return .= '</form>';
  7717. $return .= '</div>';
  7718. return $return;
  7719. }
  7720. /**
  7721. * Displays the menu for manipulating a step
  7722. * @return string html
  7723. */
  7724. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7725. {
  7726. $course_id = api_get_course_int_id();
  7727. $course_code = api_get_course_id();
  7728. $_course = api_get_course_info();
  7729. $return = '<div class="actions">';
  7730. switch ($item_type) {
  7731. case 'dokeos_chapter' :
  7732. case 'chapter' :
  7733. // Commented the message cause should not show it.
  7734. //$lang = get_lang('TitleManipulateChapter');
  7735. break;
  7736. case 'dokeos_module' :
  7737. case 'module' :
  7738. // Commented the message cause should not show it.
  7739. //$lang = get_lang('TitleManipulateModule');
  7740. break;
  7741. case TOOL_DOCUMENT :
  7742. // Commented the message cause should not show it.
  7743. //$lang = get_lang('TitleManipulateDocument');
  7744. break;
  7745. case TOOL_LINK :
  7746. case 'link' :
  7747. // Commented the message cause should not show it.
  7748. //$lang = get_lang('TitleManipulateLink');
  7749. break;
  7750. case TOOL_QUIZ :
  7751. // Commented the message cause should not show it.
  7752. //$lang = get_lang('TitleManipulateQuiz');
  7753. break;
  7754. case TOOL_STUDENTPUBLICATION :
  7755. // Commented the message cause should not show it.
  7756. //$lang = get_lang('TitleManipulateStudentPublication');
  7757. break;
  7758. }
  7759. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7760. $item_id = intval($item_id);
  7761. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7762. $result = Database::query($sql);
  7763. $row = Database::fetch_assoc($result);
  7764. $webCodepath = api_get_path(WEB_CODE_PATH);
  7765. $audio_player = null;
  7766. // We display an audio player if needed.
  7767. if (!empty($row['audio'])) {
  7768. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7769. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  7770. $audio_player .= '<script type="text/javascript" src="'.$webCodepath.'inc/lib/mediaplayer/swfobject.js"></script>';
  7771. $audio_player .= '<script>
  7772. var s1 = new SWFObject("'.$webCodepath.'inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7773. s1.addParam("allowscriptaccess","always");
  7774. s1.addParam("flashvars","file='.api_get_path(WEB_PUBLIC_PATH).'courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7775. s1.write("container");
  7776. </script>';
  7777. }
  7778. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7779. $_GET['cidReq']
  7780. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7781. $return .= Display::url(
  7782. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7783. $url.'&action=edit_item&path_item='.$row['path']
  7784. );
  7785. $return .= Display::url(
  7786. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7787. $url.'&action=move_item'
  7788. );
  7789. // Commented for now as prerequisites cannot be added to chapters.
  7790. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7791. $return .= Display::url(
  7792. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7793. $url.'&action=edit_item_prereq'
  7794. );
  7795. }
  7796. $return .= Display::url(
  7797. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7798. $url.'&action=delete_item'
  7799. );
  7800. if ($item_type == TOOL_HOTPOTATOES) {
  7801. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7802. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7803. }
  7804. if ($item_type == TOOL_DOCUMENT) {
  7805. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7806. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7807. }
  7808. $return .= '</div>';
  7809. if (!empty($audio_player)) {
  7810. $return .= '<br />'.$audio_player;
  7811. }
  7812. return $return;
  7813. }
  7814. /**
  7815. * Creates the javascript needed for filling up the checkboxes without page reload
  7816. *
  7817. * @return string
  7818. */
  7819. public function get_js_dropdown_array()
  7820. {
  7821. $course_id = api_get_course_int_id();
  7822. $return = 'var child_name = new Array();'."\n";
  7823. $return .= 'var child_value = new Array();'."\n\n";
  7824. $return .= 'child_name[0] = new Array();'."\n";
  7825. $return .= 'child_value[0] = new Array();'."\n\n";
  7826. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7827. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7828. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7829. ORDER BY display_order ASC";
  7830. $res_zero = Database::query($sql_zero);
  7831. $i = 0;
  7832. while ($row_zero = Database :: fetch_array($res_zero)) {
  7833. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7834. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7835. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7836. }
  7837. $return .= "\n";
  7838. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7839. $res = Database::query($sql);
  7840. while ($row = Database :: fetch_array($res)) {
  7841. $sql_parent = "
  7842. SELECT * FROM ".$tbl_lp_item."
  7843. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7844. ORDER BY display_order ASC";
  7845. $res_parent = Database::query($sql_parent);
  7846. $i = 0;
  7847. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7848. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7849. while ($row_parent = Database :: fetch_array($res_parent)) {
  7850. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7851. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7852. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7853. }
  7854. $return .= "\n";
  7855. }
  7856. return $return;
  7857. }
  7858. /**
  7859. * Display the form to allow moving an item
  7860. * @param integer Item ID
  7861. * @return string HTML form
  7862. */
  7863. public function display_move_item($item_id)
  7864. {
  7865. $course_id = api_get_course_int_id();
  7866. $return = '';
  7867. if (is_numeric($item_id)) {
  7868. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7869. $sql = "SELECT * FROM ".$tbl_lp_item."
  7870. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7871. $res = Database::query($sql);
  7872. $row = Database :: fetch_array($res);
  7873. switch ($row['item_type']) {
  7874. case 'dokeos_chapter' :
  7875. case 'dir' :
  7876. case 'asset' :
  7877. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7878. $return .= $this->display_item_form(
  7879. $row['item_type'],
  7880. get_lang('MoveCurrentChapter'),
  7881. 'move',
  7882. $item_id,
  7883. $row
  7884. );
  7885. break;
  7886. case 'dokeos_module' :
  7887. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7888. $return .= $this->display_item_form(
  7889. $row['item_type'],
  7890. 'Move th current module:',
  7891. 'move',
  7892. $item_id,
  7893. $row
  7894. );
  7895. break;
  7896. case TOOL_DOCUMENT :
  7897. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7898. $return .= $this->display_document_form('move', $item_id, $row);
  7899. break;
  7900. case TOOL_LINK :
  7901. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7902. $return .= $this->display_link_form('move', $item_id, $row);
  7903. break;
  7904. case TOOL_HOTPOTATOES :
  7905. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7906. $return .= $this->display_link_form('move', $item_id, $row);
  7907. break;
  7908. case TOOL_QUIZ :
  7909. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7910. $return .= $this->display_quiz_form('move', $item_id, $row);
  7911. break;
  7912. case TOOL_STUDENTPUBLICATION :
  7913. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7914. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7915. break;
  7916. case TOOL_FORUM :
  7917. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7918. $return .= $this->display_forum_form('move', $item_id, $row);
  7919. break;
  7920. case TOOL_THREAD :
  7921. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7922. $return .= $this->display_forum_form('move', $item_id, $row);
  7923. break;
  7924. }
  7925. }
  7926. return $return;
  7927. }
  7928. /**
  7929. * Displays a basic form on the overview page for changing the item title and the item description.
  7930. * @param string $item_type
  7931. * @param string $title
  7932. * @param array $data
  7933. * @return string
  7934. */
  7935. public function display_item_small_form($item_type, $title = '', $data = array())
  7936. {
  7937. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7938. $form = new FormValidator('small_form', 'post', $url);
  7939. $form->addElement('header', $title);
  7940. $form->addElement('text', 'title', get_lang('Title'));
  7941. $form->addElement('button', 'submit_button', get_lang('Save'));
  7942. $form->addElement('hidden', 'id', $data['id']);
  7943. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7944. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7945. $form->setDefaults(array('title' => $data['title']));
  7946. return $form->toHtml();
  7947. }
  7948. /**
  7949. * Return HTML form to allow prerequisites selection
  7950. * @param integer Item ID
  7951. * @return string HTML form
  7952. */
  7953. public function display_item_prerequisites_form($item_id)
  7954. {
  7955. $course_id = api_get_course_int_id();
  7956. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7957. $item_id = intval($item_id);
  7958. /* Current prerequisite */
  7959. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7960. $result = Database::query($sql);
  7961. $row = Database::fetch_array($result);
  7962. $preq_id = $row['prerequisite'];
  7963. $return = '<legend>';
  7964. $return .= get_lang('AddEditPrerequisites');
  7965. $return .= '</legend>';
  7966. $return .= '<form method="POST">';
  7967. $return .= '<table class="data_table">';
  7968. $return .= '<tr>';
  7969. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7970. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  7971. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  7972. $return .= '</tr>';
  7973. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7974. $return .= '<tr >';
  7975. $return .= '<td colspan="3" class="radio">';
  7976. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7977. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  7978. $return .= '</tr>';
  7979. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7980. $result = Database::query($sql);
  7981. $arrLP = array();
  7982. while ($row = Database :: fetch_array($result)) {
  7983. $arrLP[] = array(
  7984. 'id' => $row['id'],
  7985. 'item_type' => $row['item_type'],
  7986. 'title' => $row['title'],
  7987. 'ref' => $row['ref'],
  7988. 'description' => $row['description'],
  7989. 'parent_item_id' => $row['parent_item_id'],
  7990. 'previous_item_id' => $row['previous_item_id'],
  7991. 'next_item_id' => $row['next_item_id'],
  7992. 'max_score' => $row['max_score'],
  7993. 'min_score' => $row['min_score'],
  7994. 'mastery_score' => $row['mastery_score'],
  7995. 'prerequisite' => $row['prerequisite'],
  7996. 'next_item_id' => $row['next_item_id'],
  7997. 'display_order' => $row['display_order']
  7998. );
  7999. if ($row['ref'] == $preq_id) {
  8000. $preq_mastery = $row['mastery_score'];
  8001. $preq_max = $row['max_score'];
  8002. }
  8003. }
  8004. $this->tree_array($arrLP);
  8005. $arrLP = $this->arrMenu;
  8006. unset($this->arrMenu);
  8007. $imgSysPath = api_get_path(SYS_IMG_PATH);
  8008. for ($i = 0; $i < count($arrLP); $i++) {
  8009. if ($arrLP[$i]['id'] == $item_id) {
  8010. break;
  8011. }
  8012. $return .= '<tr>';
  8013. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8014. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  8015. $depth_multi = $arrLP[$i]['depth'] * 10;
  8016. $return .= '<input'.(($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '').(($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ').'id="id'.$arrLP[$i]['id'].'" name="prerequisites" style="margin-left:'.$depth_multi.'px; margin-right:10px;" type="radio" value="'.$arrLP[$i]['id'].'" />';
  8017. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8018. if (file_exists($imgSysPath.'lp_'.$icon_name.'.png')) {
  8019. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8020. } else {
  8021. if (file_exists($imgSysPath.'lp_'.$icon_name.'.gif')) {
  8022. $return .= Display::return_icon('lp_'.$icon_name.'.gif');
  8023. } else {
  8024. $return .= Display::return_icon(
  8025. 'folder_document.gif',
  8026. '',
  8027. array('style' => 'margin-right:5px;')
  8028. );
  8029. }
  8030. }
  8031. $return .= $arrLP[$i]['title'].'</label>';
  8032. $return .= '</td>';
  8033. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8034. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8035. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8036. $return .= '</td>';
  8037. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8038. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8039. $return .= '</td>';
  8040. }
  8041. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8042. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8043. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8044. $return .= '</td>';
  8045. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8046. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8047. $return .= '</td>';
  8048. }
  8049. $return .= '</tr>';
  8050. }
  8051. $return .= '<tr>';
  8052. $return .= '</tr>';
  8053. $return .= '</table>';
  8054. $return .= '<div style="padding-top:3px;">';
  8055. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8056. 'ModifyPrerequisites'
  8057. ).'</button>';
  8058. $return .= '</form>';
  8059. return $return;
  8060. }
  8061. /**
  8062. * Return HTML list to allow prerequisites selection for lp
  8063. * @param integer Item ID
  8064. * @return string HTML form
  8065. */
  8066. public function display_lp_prerequisites_list()
  8067. {
  8068. $course_id = api_get_course_int_id();
  8069. $lp_id = $this->lp_id;
  8070. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8071. // get current prerequisite
  8072. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8073. $result = Database::query($sql);
  8074. $row = Database :: fetch_array($result);
  8075. $preq_id = $row['prerequisite'];
  8076. $session_id = api_get_session_id();
  8077. $session_condition = api_get_session_condition($session_id);
  8078. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8079. $rs = Database::query($sql);
  8080. $return = '';
  8081. $return .= '<select name="prerequisites" >';
  8082. $return .= '<option value="0">'.get_lang('None').'</option>';
  8083. if (Database::num_rows($rs) > 0) {
  8084. while ($row = Database::fetch_array($rs)) {
  8085. if ($row['id'] == $lp_id) {
  8086. continue;
  8087. }
  8088. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8089. }
  8090. }
  8091. $return .= '</select>';
  8092. return $return;
  8093. }
  8094. /**
  8095. * Creates a list with all the documents in it
  8096. * @return string
  8097. */
  8098. public function get_documents()
  8099. {
  8100. $course_info = api_get_course_info();
  8101. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8102. return $document_tree;
  8103. }
  8104. /**
  8105. * Creates a list with all the exercises (quiz) in it
  8106. * @return string
  8107. */
  8108. public function get_exercises()
  8109. {
  8110. $course_id = api_get_course_int_id();
  8111. // New for hotpotatoes.
  8112. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8113. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8114. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8115. $session_id = api_get_session_id();
  8116. $condition_session = api_get_session_condition($session_id);
  8117. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8118. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8119. $res_quiz = Database::query($sql_quiz);
  8120. $res_hot = Database::query($sql_hot);
  8121. $return = '<ul class="lp_resource">';
  8122. $return .= '<li class="lp_resource_element">';
  8123. $return .= Display::return_icon('new_test_small.gif');
  8124. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8125. $return .= '</li>';
  8126. // Display hotpotatoes
  8127. while ($row_hot = Database :: fetch_array($res_hot)) {
  8128. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8129. $return .= '<a class="moved" href="#">';
  8130. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8131. $return .= '</a> ';
  8132. $return .= Display::return_icon('hotpotatoes_s.png');
  8133. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8134. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8135. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8136. $row_hot['title']
  8137. )).'</a>';
  8138. $return .= '</li>';
  8139. }
  8140. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8141. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['iid'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8142. $return .= '<a class="moved" href="#">';
  8143. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8144. $return .= '</a> ';
  8145. $return .= Display::return_icon('quizz_small.gif');
  8146. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8147. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['iid'].'&amp;lp_id='.$this->lp_id.'">'.
  8148. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8149. '</a>';
  8150. $return .= '</li>';
  8151. }
  8152. $return .= '</ul>';
  8153. return $return;
  8154. }
  8155. /**
  8156. * Creates a list with all the links in it
  8157. * @return string
  8158. */
  8159. public function get_links()
  8160. {
  8161. $course_id = api_get_course_int_id();
  8162. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8163. $session_id = api_get_session_id();
  8164. $condition_session = api_get_session_condition($session_id);
  8165. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8166. $res_link = Database::query($sql_link);
  8167. $return = '<ul class="lp_resource">';
  8168. $return .= '<li class="lp_resource_element">';
  8169. $return .= Display::return_icon('linksnew.gif');
  8170. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8171. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8172. $return .= '</li>';
  8173. $course_info = api_get_course_info();
  8174. while ($row_link = Database :: fetch_array($res_link)) {
  8175. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8176. if ($item_visibility != 2) {
  8177. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8178. $return .= '<a class="moved" href="#">';
  8179. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8180. $return .= '</a> ';
  8181. $return .= Display::return_icon('lp_link.gif');
  8182. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_LINK.'&amp;file='.$row_link['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8183. $row_link['title'].
  8184. '</a>';
  8185. $return .= '</li>';
  8186. }
  8187. }
  8188. $return .= '</ul>';
  8189. return $return;
  8190. }
  8191. /**
  8192. * Creates a list with all the student publications in it
  8193. * @return unknown
  8194. */
  8195. public function get_student_publications()
  8196. {
  8197. $return = '<div class="lp_resource" >';
  8198. $return .= '<div class="lp_resource_element">';
  8199. $return .= Display::return_icon('works_small.gif');
  8200. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_STUDENTPUBLICATION.'&amp;lp_id='.$this->lp_id.'">'.get_lang('AddAssignmentPage').'</a>';
  8201. $return .= '</div>';
  8202. $return .= '</div>';
  8203. return $return;
  8204. }
  8205. /**
  8206. * Creates a list with all the forums in it
  8207. * @return string
  8208. */
  8209. public function get_forums()
  8210. {
  8211. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  8212. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  8213. $a_forums = get_forums();
  8214. $return = '<ul class="lp_resource">';
  8215. //First add link
  8216. $return .= '<li class="lp_resource_element">';
  8217. $return .= Display::return_icon('forum_new_small.gif');
  8218. $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').'">
  8219. '.get_lang('CreateANewForum').'</a>';
  8220. $return .= '</li>';
  8221. $return .= '<script>
  8222. function toggle_forum(forum_id){
  8223. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8224. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8225. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8226. } else {
  8227. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8228. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8229. }
  8230. }
  8231. </script>';
  8232. foreach ($a_forums as $forum) {
  8233. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8234. if (!empty($forum['forum_id'])) {
  8235. $return .= '<a class="moved" href="#">';
  8236. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8237. $return .= ' </a>';
  8238. $return .= Display::return_icon('lp_forum.gif');
  8239. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8240. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8241. </a>
  8242. <a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_FORUM.'&amp;forum_id='.$forum['forum_id'].'&amp;lp_id='.$this->lp_id.'" style="vertical-align:middle">'.Security :: remove_XSS(
  8243. $forum['forum_title']
  8244. ).'</a>';
  8245. }
  8246. $return .= '</li>';
  8247. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8248. $a_threads = get_threads($forum['forum_id']);
  8249. if (is_array($a_threads)) {
  8250. foreach ($a_threads as $thread) {
  8251. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8252. $return .= '&nbsp;<a class="moved" href="#">';
  8253. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8254. $return .= ' </a>';
  8255. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8256. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type='.TOOL_THREAD.'&amp;thread_id='.$thread['thread_id'].'&amp;lp_id='.$this->lp_id.'">'.Security :: remove_XSS(
  8257. $thread['thread_title']
  8258. ).'</a>';
  8259. $return .= '</li>';
  8260. }
  8261. }
  8262. $return .= '</div>';
  8263. }
  8264. $return .= '</ul>';
  8265. return $return;
  8266. }
  8267. /**
  8268. * // TODO: The output encoding should be equal to the system encoding.
  8269. *
  8270. * Exports the learning path as a SCORM package. This is the main function that
  8271. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8272. * whole thing and returns the zip.
  8273. *
  8274. * This method needs to be called in PHP5, as it will fail with non-adequate
  8275. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8276. * you need to call it on a learnpath object.
  8277. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8278. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8279. * path has been modified, it should use the generic method here below.
  8280. * @TODO link this function with the export_lp() function in the same class
  8281. * @param string Optional name of zip file. If none, title of learnpath is
  8282. * domesticated and trailed with ".zip"
  8283. * @return string Returns the zip package string, or null if error
  8284. */
  8285. public function scorm_export()
  8286. {
  8287. $_course = api_get_course_info();
  8288. $course_id = api_get_course_int_id();
  8289. $links_to_create = null;
  8290. // Remove memory and time limits as much as possible as this might be a long process...
  8291. if (function_exists('ini_set')) {
  8292. api_set_memory_limit('128M');
  8293. ini_set('max_execution_time', 600);
  8294. }
  8295. // Create the zip handler (this will remain available throughout the method).
  8296. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8297. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8298. $temp_dir_short = uniqid();
  8299. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8300. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8301. $zip_folder = new PclZip($temp_zip_file);
  8302. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8303. $root_path = $main_path = api_get_path(SYS_PATH);
  8304. $files_cleanup = array();
  8305. // Place to temporarily stash the zipfiles.
  8306. // create the temp dir if it doesn't exist
  8307. // or do a cleanup befor creating the zipfile.
  8308. if (!is_dir($temp_zip_dir)) {
  8309. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8310. } else {
  8311. // Cleanup: Check the temp dir for old files and delete them.
  8312. $handle = opendir($temp_zip_dir);
  8313. while (false !== ($file = readdir($handle))) {
  8314. if ($file != '.' && $file != '..') {
  8315. unlink("$temp_zip_dir/$file");
  8316. }
  8317. }
  8318. closedir($handle);
  8319. }
  8320. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8321. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8322. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8323. )
  8324. ) {
  8325. // Remove the possible . at the end of the path.
  8326. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8327. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8328. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8329. $zip_files_dist = api_copyr(
  8330. $current_course_path.'/scorm/'.$this->path,
  8331. $dest_path_to_scorm_folder,
  8332. array('imsmanifest'),
  8333. $zip_files
  8334. );
  8335. }
  8336. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8337. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8338. // Aggregation Model official document, secion "2.3 Content Packaging".
  8339. $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.
  8340. $root = $xmldoc->createElement('manifest');
  8341. $root->setAttribute('identifier', 'SingleCourseManifest');
  8342. $root->setAttribute('version', '1.1');
  8343. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8344. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8345. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8346. $root->setAttribute(
  8347. 'xsi:schemaLocation',
  8348. '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'
  8349. );
  8350. // Build mandatory sub-root container elements.
  8351. $metadata = $xmldoc->createElement('metadata');
  8352. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8353. $metadata->appendChild($md_schema);
  8354. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8355. $metadata->appendChild($md_schemaversion);
  8356. $root->appendChild($metadata);
  8357. $organizations = $xmldoc->createElement('organizations');
  8358. $resources = $xmldoc->createElement('resources');
  8359. // Build the only organization we will use in building our learnpaths.
  8360. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8361. $organization = $xmldoc->createElement('organization');
  8362. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8363. // To set the title of the SCORM entity (=organization), we take the name given
  8364. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8365. // learning path charset) as it is the encoding that defines how it is stored
  8366. // in the database. Then we convert it to HTML entities again as the "&" character
  8367. // alone is not authorized in XML (must be &amp;).
  8368. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8369. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8370. $organization->appendChild($org_title);
  8371. $folder_name = 'document';
  8372. // Removes the learning_path/scorm_folder path when exporting see #4841
  8373. $path_to_remove = null;
  8374. $result = $this->generate_lp_folder($_course);
  8375. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8376. $path_to_remove = 'document'.$result['dir'];
  8377. $path_to_replace = $folder_name.'/';
  8378. }
  8379. //Fixes chamilo scorm exports
  8380. if ($this->ref == 'chamilo_scorm_export') {
  8381. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8382. }
  8383. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8384. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8385. $link_updates = array();
  8386. foreach ($this->items as $index => $item) {
  8387. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8388. // Get included documents from this item.
  8389. if ($item->type == 'sco') {
  8390. $inc_docs = $item->get_resources_from_source(
  8391. null,
  8392. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8393. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8394. );
  8395. } else {
  8396. $inc_docs = $item->get_resources_from_source();
  8397. }
  8398. // Give a child element <item> to the <organization> element.
  8399. $my_item_id = $item->get_id();
  8400. $my_item = $xmldoc->createElement('item');
  8401. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8402. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8403. $my_item->setAttribute('isvisible', 'true');
  8404. // Give a child element <title> to the <item> element.
  8405. $my_title = $xmldoc->createElement(
  8406. 'title',
  8407. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8408. );
  8409. $my_item->appendChild($my_title);
  8410. // Give a child element <adlcp:prerequisites> to the <item> element.
  8411. $my_prereqs = $xmldoc->createElement(
  8412. 'adlcp:prerequisites',
  8413. $this->get_scorm_prereq_string($my_item_id)
  8414. );
  8415. $my_prereqs->setAttribute('type', 'aicc_script');
  8416. $my_item->appendChild($my_prereqs);
  8417. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8418. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8419. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8420. //$xmldoc->createElement('adlcp:timelimitaction','');
  8421. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8422. //$xmldoc->createElement('adlcp:datafromlms','');
  8423. // Give a child element <adlcp:masteryscore> to the <item> element.
  8424. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8425. $my_item->appendChild($my_masteryscore);
  8426. // Attach this item to the organization element or hits parent if there is one.
  8427. if (!empty($item->parent) && $item->parent != 0) {
  8428. $children = $organization->childNodes;
  8429. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8430. if (is_object($possible_parent)) {
  8431. $possible_parent->appendChild($my_item);
  8432. } else {
  8433. if ($this->debug > 0) {
  8434. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8435. }
  8436. }
  8437. } else {
  8438. if ($this->debug > 0) {
  8439. error_log('No parent');
  8440. }
  8441. $organization->appendChild($my_item);
  8442. }
  8443. // Get the path of the file(s) from the course directory root.
  8444. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8445. if (!empty($path_to_remove)) {
  8446. //From docs
  8447. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8448. //From quiz
  8449. if ($this->ref == 'chamilo_scorm_export') {
  8450. $path_to_remove = 'scorm/'.$this->path.'/';
  8451. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8452. }
  8453. } else {
  8454. $my_xml_file_path = $my_file_path;
  8455. }
  8456. $my_sub_dir = dirname($my_file_path);
  8457. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8458. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8459. $my_xml_sub_dir = $my_sub_dir;
  8460. // Give a <resource> child to the <resources> element
  8461. $my_resource = $xmldoc->createElement('resource');
  8462. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8463. $my_resource->setAttribute('type', 'webcontent');
  8464. $my_resource->setAttribute('href', $my_xml_file_path);
  8465. // adlcp:scormtype can be either 'sco' or 'asset'.
  8466. if ($item->type == 'sco') {
  8467. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8468. } else {
  8469. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8470. }
  8471. // xml:base is the base directory to find the files declared in this resource.
  8472. $my_resource->setAttribute('xml:base', '');
  8473. // Give a <file> child to the <resource> element.
  8474. $my_file = $xmldoc->createElement('file');
  8475. $my_file->setAttribute('href', $my_xml_file_path);
  8476. $my_resource->appendChild($my_file);
  8477. // Dependency to other files - not yet supported.
  8478. $i = 1;
  8479. foreach ($inc_docs as $doc_info) {
  8480. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8481. continue;
  8482. }
  8483. $my_dep = $xmldoc->createElement('resource');
  8484. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8485. $my_dep->setAttribute('identifier', $res_id);
  8486. $my_dep->setAttribute('type', 'webcontent');
  8487. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8488. $my_dep_file = $xmldoc->createElement('file');
  8489. // Check type of URL.
  8490. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8491. if ($doc_info[1] == 'remote') {
  8492. // Remote file. Save url as is.
  8493. $my_dep_file->setAttribute('href', $doc_info[0]);
  8494. $my_dep->setAttribute('xml:base', '');
  8495. } elseif ($doc_info[1] == 'local') {
  8496. switch ($doc_info[2]) {
  8497. case 'url': // Local URL - save path as url for now, don't zip file.
  8498. $abs_path = api_get_path(SYS_PATH).str_replace(
  8499. api_get_path(WEB_PATH),
  8500. '',
  8501. $doc_info[0]
  8502. );
  8503. $current_dir = dirname($abs_path);
  8504. $current_dir = str_replace('\\', '/', $current_dir);
  8505. $file_path = realpath($abs_path);
  8506. $file_path = str_replace('\\', '/', $file_path);
  8507. $my_dep_file->setAttribute('href', $file_path);
  8508. $my_dep->setAttribute('xml:base', '');
  8509. if (strstr($file_path, $main_path) !== false) {
  8510. // The calculated real path is really inside Chamilo's root path.
  8511. // Reduce file path to what's under the DocumentRoot.
  8512. $file_path = substr($file_path, strlen($root_path) - 1);
  8513. //echo $file_path;echo '<br /><br />';
  8514. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8515. $zip_files_abs[] = $file_path;
  8516. $link_updates[$my_file_path][] = array(
  8517. 'orig' => $doc_info[0],
  8518. 'dest' => $file_path
  8519. );
  8520. $my_dep_file->setAttribute('href', $file_path);
  8521. $my_dep->setAttribute('xml:base', '');
  8522. } elseif (empty($file_path)) {
  8523. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8524. if (strpos($document_root, -1) == '/') {
  8525. $document_root = substr(0, -1, $document_root);
  8526. }*/
  8527. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8528. $file_path = str_replace('//', '/', $file_path);
  8529. if (file_exists($file_path)) {
  8530. $file_path = substr(
  8531. $file_path,
  8532. strlen($current_dir)
  8533. ); // We get the relative path.
  8534. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8535. $link_updates[$my_file_path][] = array(
  8536. 'orig' => $doc_info[0],
  8537. 'dest' => $file_path
  8538. );
  8539. $my_dep_file->setAttribute('href', $file_path);
  8540. $my_dep->setAttribute('xml:base', '');
  8541. }
  8542. }
  8543. break;
  8544. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8545. $my_dep_file->setAttribute('href', $doc_info[0]);
  8546. $my_dep->setAttribute('xml:base', '');
  8547. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8548. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8549. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8550. $abs_img_path_without_subdir = $doc_info[0];
  8551. $relp = api_get_path(REL_PATH); // The url-append config param.
  8552. $pos = strpos($abs_img_path_without_subdir, $relp);
  8553. if ($pos === 0) {
  8554. $abs_img_path_without_subdir = '/'.substr(
  8555. $abs_img_path_without_subdir,
  8556. strlen($relp)
  8557. );
  8558. }
  8559. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8560. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8561. $file_path = str_replace('\\', '/', $file_path);
  8562. $file_path = str_replace('//', '/', $file_path);
  8563. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8564. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8565. $cur_path = substr(
  8566. $current_course_path,
  8567. -1
  8568. ) == '/' ? $current_course_path : $current_course_path.'/';
  8569. // Check if the current document is in that path.
  8570. if (strstr($file_path, $cur_path) !== false) {
  8571. // The document is in that path, now get the relative path
  8572. // to the containing document.
  8573. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8574. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8575. $relative_path = '';
  8576. if (strstr($file_path, $cur_path) !== false) {
  8577. $relative_path = substr($file_path, strlen($orig_file_path));
  8578. $file_path = substr($file_path, strlen($cur_path));
  8579. } else {
  8580. // This case is still a problem as it's difficult to calculate a relative path easily
  8581. // might still generate wrong links.
  8582. //$file_path = substr($file_path,strlen($cur_path));
  8583. // Calculate the directory path to the current file (without trailing slash).
  8584. $my_relative_path = dirname($file_path);
  8585. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8586. $my_relative_file = basename($file_path);
  8587. // Calculate the directory path to the containing file (without trailing slash).
  8588. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8589. $dotdot = '';
  8590. $subdir = '';
  8591. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8592. $my_orig_file_path
  8593. ) > 1) && (strlen($my_relative_path) > 1)) {
  8594. $my_relative_path2 = dirname($my_relative_path);
  8595. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8596. $my_orig_file_path = dirname($my_orig_file_path);
  8597. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8598. $subdir = substr(
  8599. $my_relative_path,
  8600. strlen($my_relative_path2) + 1
  8601. ).'/'.$subdir;
  8602. $dotdot += '../';
  8603. $my_relative_path = $my_relative_path2;
  8604. }
  8605. $relative_path = $dotdot.$subdir.$my_relative_file;
  8606. }
  8607. // Put the current document in the zip (this array is the array
  8608. // that will manage documents already in the course folder - relative).
  8609. $zip_files[] = $file_path;
  8610. // Update the links to the current document in the containing document (make them relative).
  8611. $link_updates[$my_file_path][] = array(
  8612. 'orig' => $doc_info[0],
  8613. 'dest' => $relative_path
  8614. );
  8615. $my_dep_file->setAttribute('href', $file_path);
  8616. $my_dep->setAttribute('xml:base', '');
  8617. } elseif (strstr($file_path, $main_path) !== false) {
  8618. // The calculated real path is really inside Chamilo's root path.
  8619. // Reduce file path to what's under the DocumentRoot.
  8620. $file_path = substr($file_path, strlen($root_path));
  8621. //echo $file_path;echo '<br /><br />';
  8622. //error_log('Reduced path: '.$file_path, 0);
  8623. $zip_files_abs[] = $file_path;
  8624. $link_updates[$my_file_path][] = array(
  8625. 'orig' => $doc_info[0],
  8626. 'dest' => $file_path
  8627. );
  8628. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8629. $my_dep->setAttribute('xml:base', '');
  8630. } elseif (empty($file_path)) {
  8631. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8632. if(strpos($document_root,-1) == '/') {
  8633. $document_root = substr(0, -1, $document_root);
  8634. }*/
  8635. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8636. $file_path = str_replace('//', '/', $file_path);
  8637. if (file_exists($file_path)) {
  8638. $file_path = substr(
  8639. $file_path,
  8640. strlen($current_dir)
  8641. ); // We get the relative path.
  8642. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8643. $link_updates[$my_file_path][] = array(
  8644. 'orig' => $doc_info[0],
  8645. 'dest' => $file_path
  8646. );
  8647. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8648. $my_dep->setAttribute('xml:base', '');
  8649. }
  8650. }
  8651. break;
  8652. 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
  8653. if (substr($doc_info[0], 0, 2) == '..') {
  8654. // Relative path going up.
  8655. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8656. $current_dir = str_replace('\\', '/', $current_dir);
  8657. $file_path = realpath($current_dir.$doc_info[0]);
  8658. $file_path = str_replace('\\', '/', $file_path);
  8659. //error_log($file_path.' <-> '.$main_path,0);
  8660. if (strstr($file_path, $main_path) !== false) {
  8661. // The calculated real path is really inside Chamilo's root path.
  8662. // Reduce file path to what's under the DocumentRoot.
  8663. $file_path = substr($file_path, strlen($root_path));
  8664. //error_log('Reduced path: '.$file_path, 0);
  8665. $zip_files_abs[] = $file_path;
  8666. $link_updates[$my_file_path][] = array(
  8667. 'orig' => $doc_info[0],
  8668. 'dest' => $file_path
  8669. );
  8670. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8671. $my_dep->setAttribute('xml:base', '');
  8672. }
  8673. } else {
  8674. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8675. $my_dep_file->setAttribute('href', $doc_info[0]);
  8676. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8677. }
  8678. break;
  8679. default:
  8680. $my_dep_file->setAttribute('href', $doc_info[0]);
  8681. $my_dep->setAttribute('xml:base', '');
  8682. break;
  8683. }
  8684. }
  8685. $my_dep->appendChild($my_dep_file);
  8686. $resources->appendChild($my_dep);
  8687. $dependency = $xmldoc->createElement('dependency');
  8688. $dependency->setAttribute('identifierref', $res_id);
  8689. $my_resource->appendChild($dependency);
  8690. $i++;
  8691. }
  8692. //$my_dependency = $xmldoc->createElement('dependency');
  8693. //$my_dependency->setAttribute('identifierref', '');
  8694. $resources->appendChild($my_resource);
  8695. $zip_files[] = $my_file_path;
  8696. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8697. } else {
  8698. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8699. switch ($item->type) {
  8700. case TOOL_LINK:
  8701. $my_item = $xmldoc->createElement('item');
  8702. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8703. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8704. $my_item->setAttribute('isvisible', 'true');
  8705. // Give a child element <title> to the <item> element.
  8706. $my_title = $xmldoc->createElement(
  8707. 'title',
  8708. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8709. );
  8710. $my_item->appendChild($my_title);
  8711. // Give a child element <adlcp:prerequisites> to the <item> element.
  8712. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8713. $my_prereqs->setAttribute('type', 'aicc_script');
  8714. $my_item->appendChild($my_prereqs);
  8715. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8716. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8717. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8718. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8719. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8720. //$xmldoc->createElement('adlcp:datafromlms', '');
  8721. // Give a child element <adlcp:masteryscore> to the <item> element.
  8722. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8723. $my_item->appendChild($my_masteryscore);
  8724. // Attach this item to the organization element or its parent if there is one.
  8725. if (!empty($item->parent) && $item->parent != 0) {
  8726. $children = $organization->childNodes;
  8727. for ($i = 0; $i < $children->length; $i++) {
  8728. $item_temp = $children->item($i);
  8729. if ($item_temp->nodeName == 'item') {
  8730. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8731. $item_temp->appendChild($my_item);
  8732. }
  8733. }
  8734. }
  8735. } else {
  8736. $organization->appendChild($my_item);
  8737. }
  8738. $my_file_path = 'link_'.$item->get_id().'.html';
  8739. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8740. TABLE_LINK
  8741. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8742. $rs = Database::query($sql);
  8743. if ($link = Database :: fetch_array($rs)) {
  8744. $url = $link['url'];
  8745. $title = stripslashes($link['title']);
  8746. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8747. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8748. $my_xml_file_path = $my_file_path;
  8749. $my_sub_dir = dirname($my_file_path);
  8750. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8751. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8752. $my_xml_sub_dir = $my_sub_dir;
  8753. // Give a <resource> child to the <resources> element.
  8754. $my_resource = $xmldoc->createElement('resource');
  8755. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8756. $my_resource->setAttribute('type', 'webcontent');
  8757. $my_resource->setAttribute('href', $my_xml_file_path);
  8758. // adlcp:scormtype can be either 'sco' or 'asset'.
  8759. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8760. // xml:base is the base directory to find the files declared in this resource.
  8761. $my_resource->setAttribute('xml:base', '');
  8762. // give a <file> child to the <resource> element.
  8763. $my_file = $xmldoc->createElement('file');
  8764. $my_file->setAttribute('href', $my_xml_file_path);
  8765. $my_resource->appendChild($my_file);
  8766. $resources->appendChild($my_resource);
  8767. }
  8768. break;
  8769. case TOOL_QUIZ:
  8770. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8771. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8772. $exe = new Exercise();
  8773. $exe->read($exe_id);
  8774. $my_item = $xmldoc->createElement('item');
  8775. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8776. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8777. $my_item->setAttribute('isvisible', 'true');
  8778. // Give a child element <title> to the <item> element.
  8779. $my_title = $xmldoc->createElement(
  8780. 'title',
  8781. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8782. );
  8783. $my_item->appendChild($my_title);
  8784. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8785. //$my_item->appendChild($my_max_score);
  8786. // Give a child element <adlcp:prerequisites> to the <item> element.
  8787. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8788. $my_prereqs->setAttribute('type', 'aicc_script');
  8789. $my_item->appendChild($my_prereqs);
  8790. // Give a child element <adlcp:masteryscore> to the <item> element.
  8791. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8792. $my_item->appendChild($my_masteryscore);
  8793. // Attach this item to the organization element or hits parent if there is one.
  8794. if (!empty($item->parent) && $item->parent != 0) {
  8795. $children = $organization->childNodes;
  8796. for ($i = 0; $i < $children->length; $i++) {
  8797. $item_temp = $children->item($i);
  8798. if ($item_temp->nodeName == 'item') {
  8799. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8800. $item_temp->appendChild($my_item);
  8801. }
  8802. }
  8803. }
  8804. } else {
  8805. $organization->appendChild($my_item);
  8806. }
  8807. // Include export scripts.
  8808. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8809. // Get the path of the file(s) from the course directory root
  8810. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8811. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8812. // Write the contents of the exported exercise into a (big) html file
  8813. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8814. $contents = export_exercise($exe_id, true);
  8815. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8816. $res = file_put_contents($tmp_file_path, $contents);
  8817. if ($res === false) {
  8818. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8819. }
  8820. $files_cleanup[] = $tmp_file_path;
  8821. //error_log($tmp_path); die();
  8822. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8823. $my_xml_file_path = $my_file_path;
  8824. $my_sub_dir = dirname($my_file_path);
  8825. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8826. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8827. $my_xml_sub_dir = $my_sub_dir;
  8828. // Give a <resource> child to the <resources> element.
  8829. $my_resource = $xmldoc->createElement('resource');
  8830. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8831. $my_resource->setAttribute('type', 'webcontent');
  8832. $my_resource->setAttribute('href', $my_xml_file_path);
  8833. // adlcp:scormtype can be either 'sco' or 'asset'.
  8834. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8835. // xml:base is the base directory to find the files declared in this resource.
  8836. $my_resource->setAttribute('xml:base', '');
  8837. // Give a <file> child to the <resource> element.
  8838. $my_file = $xmldoc->createElement('file');
  8839. $my_file->setAttribute('href', $my_xml_file_path);
  8840. $my_resource->appendChild($my_file);
  8841. // Get included docs.
  8842. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8843. // Dependency to other files - not yet supported.
  8844. $i = 1;
  8845. foreach ($inc_docs as $doc_info) {
  8846. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8847. continue;
  8848. }
  8849. $my_dep = $xmldoc->createElement('resource');
  8850. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8851. $my_dep->setAttribute('identifier', $res_id);
  8852. $my_dep->setAttribute('type', 'webcontent');
  8853. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8854. $my_dep_file = $xmldoc->createElement('file');
  8855. // Check type of URL.
  8856. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8857. if ($doc_info[1] == 'remote') {
  8858. // Remote file. Save url as is.
  8859. $my_dep_file->setAttribute('href', $doc_info[0]);
  8860. $my_dep->setAttribute('xml:base', '');
  8861. } elseif ($doc_info[1] == 'local') {
  8862. switch ($doc_info[2]) {
  8863. case 'url': // Local URL - save path as url for now, don't zip file.
  8864. // Save file but as local file (retrieve from URL).
  8865. $abs_path = api_get_path(SYS_PATH).str_replace(
  8866. api_get_path(WEB_PATH),
  8867. '',
  8868. $doc_info[0]
  8869. );
  8870. $current_dir = dirname($abs_path);
  8871. $current_dir = str_replace('\\', '/', $current_dir);
  8872. $file_path = realpath($abs_path);
  8873. $file_path = str_replace('\\', '/', $file_path);
  8874. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8875. $my_dep->setAttribute('xml:base', '');
  8876. if (strstr($file_path, $main_path) !== false) {
  8877. // The calculated real path is really inside the chamilo root path.
  8878. // Reduce file path to what's under the DocumentRoot.
  8879. $file_path = substr($file_path, strlen($root_path));
  8880. //echo $file_path;echo '<br /><br />';
  8881. //error_log('Reduced path: '.$file_path, 0);
  8882. $zip_files_abs[] = $file_path;
  8883. $link_updates[$my_file_path][] = array(
  8884. 'orig' => $doc_info[0],
  8885. 'dest' => 'document/'.$file_path
  8886. );
  8887. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8888. $my_dep->setAttribute('xml:base', '');
  8889. } elseif (empty($file_path)) {
  8890. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8891. if (strpos($document_root,-1) == '/') {
  8892. $document_root = substr(0, -1, $document_root);
  8893. }*/
  8894. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8895. $file_path = str_replace('//', '/', $file_path);
  8896. if (file_exists($file_path)) {
  8897. $file_path = substr(
  8898. $file_path,
  8899. strlen($current_dir)
  8900. ); // We get the relative path.
  8901. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8902. $link_updates[$my_file_path][] = array(
  8903. 'orig' => $doc_info[0],
  8904. 'dest' => 'document/'.$file_path
  8905. );
  8906. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8907. $my_dep->setAttribute('xml:base', '');
  8908. }
  8909. }
  8910. break;
  8911. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8912. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8913. $current_dir = str_replace('\\', '/', $current_dir);
  8914. $file_path = realpath($doc_info[0]);
  8915. $file_path = str_replace('\\', '/', $file_path);
  8916. $my_dep_file->setAttribute('href', $file_path);
  8917. $my_dep->setAttribute('xml:base', '');
  8918. if (strstr($file_path, $main_path) !== false) {
  8919. // The calculated real path is really inside the chamilo root path.
  8920. // Reduce file path to what's under the DocumentRoot.
  8921. $file_path = substr($file_path, strlen($root_path));
  8922. //echo $file_path;echo '<br /><br />';
  8923. //error_log('Reduced path: '.$file_path, 0);
  8924. $zip_files_abs[] = $file_path;
  8925. $link_updates[$my_file_path][] = array(
  8926. 'orig' => $doc_info[0],
  8927. 'dest' => $file_path
  8928. );
  8929. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8930. $my_dep->setAttribute('xml:base', '');
  8931. } elseif (empty($file_path)) {
  8932. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8933. if (strpos($document_root,-1) == '/') {
  8934. $document_root = substr(0, -1, $document_root);
  8935. }*/
  8936. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8937. $file_path = str_replace('//', '/', $file_path);
  8938. if (file_exists($file_path)) {
  8939. $file_path = substr(
  8940. $file_path,
  8941. strlen($current_dir)
  8942. ); // We get the relative path.
  8943. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8944. $link_updates[$my_file_path][] = array(
  8945. 'orig' => $doc_info[0],
  8946. 'dest' => $file_path
  8947. );
  8948. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8949. $my_dep->setAttribute('xml:base', '');
  8950. }
  8951. }
  8952. break;
  8953. 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
  8954. if (substr($doc_info[0], 0, 2) == '..') {
  8955. // Relative path going up.
  8956. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8957. $current_dir = str_replace('\\', '/', $current_dir);
  8958. $file_path = realpath($current_dir.$doc_info[0]);
  8959. $file_path = str_replace('\\', '/', $file_path);
  8960. //error_log($file_path.' <-> '.$main_path, 0);
  8961. if (strstr($file_path, $main_path) !== false) {
  8962. // The calculated real path is really inside Chamilo's root path.
  8963. // Reduce file path to what's under the DocumentRoot.
  8964. $file_path = substr($file_path, strlen($root_path));
  8965. $file_path_dest = $file_path;
  8966. // File path is courses/CHAMILO/document/....
  8967. $info_file_path = explode('/', $file_path);
  8968. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8969. $file_path_dest = 'document/'.$file_path;
  8970. }
  8971. //error_log('Reduced path: '.$file_path, 0);
  8972. $zip_files_abs[] = $file_path;
  8973. $link_updates[$my_file_path][] = array(
  8974. 'orig' => $doc_info[0],
  8975. 'dest' => $file_path_dest
  8976. );
  8977. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8978. $my_dep->setAttribute('xml:base', '');
  8979. }
  8980. } else {
  8981. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8982. $my_dep_file->setAttribute('href', $doc_info[0]);
  8983. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8984. }
  8985. break;
  8986. default:
  8987. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8988. $my_dep->setAttribute('xml:base', '');
  8989. break;
  8990. }
  8991. }
  8992. $my_dep->appendChild($my_dep_file);
  8993. $resources->appendChild($my_dep);
  8994. $dependency = $xmldoc->createElement('dependency');
  8995. $dependency->setAttribute('identifierref', $res_id);
  8996. $my_resource->appendChild($dependency);
  8997. $i++;
  8998. }
  8999. $resources->appendChild($my_resource);
  9000. $zip_files[] = $my_file_path;
  9001. break;
  9002. default:
  9003. // Get the path of the file(s) from the course directory root
  9004. $my_file_path = 'non_exportable.html';
  9005. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9006. $my_xml_file_path = $my_file_path;
  9007. $my_sub_dir = dirname($my_file_path);
  9008. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9009. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9010. $my_xml_sub_dir = $my_sub_dir;
  9011. // Give a <resource> child to the <resources> element.
  9012. $my_resource = $xmldoc->createElement('resource');
  9013. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9014. $my_resource->setAttribute('type', 'webcontent');
  9015. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9016. // adlcp:scormtype can be either 'sco' or 'asset'.
  9017. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9018. // xml:base is the base directory to find the files declared in this resource.
  9019. $my_resource->setAttribute('xml:base', '');
  9020. // Give a <file> child to the <resource> element.
  9021. $my_file = $xmldoc->createElement('file');
  9022. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9023. $my_resource->appendChild($my_file);
  9024. $resources->appendChild($my_resource);
  9025. break;
  9026. }
  9027. }
  9028. }
  9029. $organizations->appendChild($organization);
  9030. $root->appendChild($organizations);
  9031. $root->appendChild($resources);
  9032. $xmldoc->appendChild($root);
  9033. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9034. // then add the file to the zip, then destroy the file (this is done automatically).
  9035. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9036. //error_log(print_r($zip_files,true), 0);
  9037. foreach ($zip_files as $file_path) {
  9038. if (empty($file_path)) {
  9039. continue;
  9040. }
  9041. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9042. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9043. $this->create_path($dest_file);
  9044. //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);
  9045. //echo $main_path.$file_path.'<br />';
  9046. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9047. // Check if the file needs a link update.
  9048. if (in_array($file_path, array_keys($link_updates))) {
  9049. $string = file_get_contents($dest_file);
  9050. unlink($dest_file);
  9051. foreach ($link_updates[$file_path] as $old_new) {
  9052. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9053. // This is an ugly hack that allows .flv files to be found by the flv player that
  9054. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9055. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9056. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9057. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9058. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9059. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9060. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9061. }
  9062. //Fix to avoid problems with default_course_document
  9063. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9064. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9065. } else {
  9066. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9067. $new_dest = $old_new['dest'];
  9068. }
  9069. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9070. $string = str_replace($old_new['orig'], $new_dest, $string);
  9071. //Add files inside the HTMLs
  9072. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9073. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9074. if (file_exists($sys_course_path.$new_path)) {
  9075. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9076. }
  9077. }
  9078. file_put_contents($dest_file, $string);
  9079. }
  9080. }
  9081. foreach ($zip_files_abs as $file_path) {
  9082. if (empty($file_path)) {
  9083. continue;
  9084. }
  9085. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9086. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9087. continue;
  9088. }
  9089. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9090. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9091. $this->create_path($dest_file);
  9092. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9093. //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);
  9094. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9095. copy($main_path.$file_path, $dest_file);
  9096. // Check if the file needs a link update.
  9097. if (in_array($file_path, array_keys($link_updates))) {
  9098. $string = file_get_contents($dest_file);
  9099. unlink($dest_file);
  9100. foreach ($link_updates[$file_path] as $old_new) {
  9101. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9102. // This is an ugly hack that allows .flv files to be found by the flv player that
  9103. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9104. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9105. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9106. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9107. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9108. }
  9109. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9110. }
  9111. file_put_contents($dest_file, $string);
  9112. }
  9113. }
  9114. if (is_array($links_to_create)) {
  9115. foreach ($links_to_create as $file => $link) {
  9116. $file_content = '<!DOCTYPE html>
  9117. <head>
  9118. <meta charset="'.api_get_language_isocode().'" />
  9119. <title>'.$link['title'].'</title>
  9120. </head>
  9121. <body dir="'.api_get_text_direction().'">
  9122. <div style="text-align:center">
  9123. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9124. </body>
  9125. </html>';
  9126. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9127. }
  9128. }
  9129. // Add non exportable message explanation.
  9130. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9131. $file_content = '<!DOCTYPE html>
  9132. <head>
  9133. <meta charset="'.api_get_language_isocode().'" />
  9134. <title>'.$lang_not_exportable.'</title>
  9135. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9136. </head>
  9137. <body dir="'.api_get_text_direction().'">';
  9138. $file_content .=
  9139. <<<EOD
  9140. <style>
  9141. .error-message {
  9142. font-family: arial, verdana, helvetica, sans-serif;
  9143. border-width: 1px;
  9144. border-style: solid;
  9145. left: 50%;
  9146. margin: 10px auto;
  9147. min-height: 30px;
  9148. padding: 5px;
  9149. right: 50%;
  9150. width: 500px;
  9151. background-color: #FFD1D1;
  9152. border-color: #FF0000;
  9153. color: #000;
  9154. }
  9155. </style>
  9156. <body>
  9157. <div class="error-message">
  9158. $lang_not_exportable
  9159. </div>
  9160. </body>
  9161. </html>
  9162. EOD;
  9163. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9164. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9165. }
  9166. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9167. // Add the extra files that go along with a SCORM package.
  9168. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9169. $extra_files = scandir($main_code_path);
  9170. foreach ($extra_files as $extra_file) {
  9171. if (strpos($extra_file, '.') === 0) {
  9172. continue;
  9173. } else {
  9174. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9175. $this->create_path($dest_file);
  9176. copy($main_code_path.$extra_file, $dest_file);
  9177. }
  9178. }
  9179. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9180. $manifest = @$xmldoc->saveXML();
  9181. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9182. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9183. $zip_folder->add(
  9184. $archive_path.'/'.$temp_dir_short,
  9185. PCLZIP_OPT_REMOVE_PATH,
  9186. $archive_path.'/'.$temp_dir_short.'/'
  9187. );
  9188. // Clean possible temporary files.
  9189. foreach ($files_cleanup as $file) {
  9190. $res = unlink($file);
  9191. if ($res === false) {
  9192. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9193. }
  9194. }
  9195. // Send file to client
  9196. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9197. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9198. }
  9199. public function scorm_export_to_pdf($lp_id)
  9200. {
  9201. $lp_id = intval($lp_id);
  9202. $files_to_export = array();
  9203. $course_data = api_get_course_info($this->cc);
  9204. if (!empty($course_data)) {
  9205. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9206. $list = self::get_flat_ordered_items_list($lp_id);
  9207. if (!empty($list)) {
  9208. foreach ($list as $item_id) {
  9209. $item = $this->items[$item_id];
  9210. switch ($item->type) {
  9211. case 'document':
  9212. //Getting documents from a LP with chamilo documents
  9213. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9214. $file_path = api_get_path(
  9215. SYS_COURSE_PATH
  9216. ).$course_data['path'].'/document'.$file_data['path'];
  9217. if (file_exists($file_path)) {
  9218. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9219. }
  9220. break;
  9221. case 'asset': //commes from a scorm package generated by chamilo
  9222. case 'sco':
  9223. $file_path = $scorm_path.'/'.$item->path;
  9224. if (file_exists($file_path)) {
  9225. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9226. }
  9227. break;
  9228. case 'dokeos_chapter':
  9229. case 'dir':
  9230. case 'chapter':
  9231. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9232. break;
  9233. }
  9234. }
  9235. }
  9236. $pdf = new PDF();
  9237. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9238. return $result;
  9239. }
  9240. return false;
  9241. }
  9242. /**
  9243. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9244. * if it doesn't exist
  9245. */
  9246. public function create_path($path)
  9247. {
  9248. $path_bits = explode('/', dirname($path));
  9249. // IS_WINDOWS_OS has been defined in api.lib.php
  9250. $path_built = IS_WINDOWS_OS ? '' : '/';
  9251. foreach ($path_bits as $bit) {
  9252. if (!empty ($bit)) {
  9253. $new_path = $path_built.$bit;
  9254. if (is_dir($new_path)) {
  9255. $path_built = $new_path.'/';
  9256. } else {
  9257. mkdir($new_path, api_get_permissions_for_new_directories());
  9258. $path_built = $new_path.'/';
  9259. }
  9260. }
  9261. }
  9262. }
  9263. /**
  9264. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9265. * @return boolean The results of the unlink function, or false if there was no image to start with
  9266. */
  9267. public function delete_lp_image()
  9268. {
  9269. $img = $this->get_preview_image();
  9270. if ($img != '') {
  9271. $del_file = $this->get_preview_image_path(null, 'sys');
  9272. if (isset($del_file) && file_exists($del_file)) {
  9273. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9274. if (file_exists($del_file_2)) {
  9275. unlink($del_file_2);
  9276. }
  9277. $this->set_preview_image('');
  9278. return @unlink($del_file);
  9279. }
  9280. }
  9281. return false;
  9282. }
  9283. /**
  9284. * Uploads an author image to the upload/learning_path/images directory
  9285. * @param array The image array, coming from the $_FILES superglobal
  9286. * @return boolean True on success, false on error
  9287. */
  9288. public function upload_image($image_array)
  9289. {
  9290. $image_moved = false;
  9291. if (!empty ($image_array['name'])) {
  9292. $upload_ok = FileManager::process_uploaded_file($image_array);
  9293. $has_attachment = true;
  9294. } else {
  9295. $image_moved = true;
  9296. }
  9297. if ($upload_ok) {
  9298. if ($has_attachment) {
  9299. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9300. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9301. $updir = $sys_course_path.$courseDir;
  9302. // Try to add an extension to the file if it hasn't one.
  9303. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9304. if (!FileManager::filter_extension($new_file_name)) {
  9305. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9306. $image_moved = false;
  9307. } else {
  9308. $file_extension = explode('.', $image_array['name']);
  9309. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9310. $filename = uniqid('');
  9311. $new_file_name = $filename.'.'.$file_extension;
  9312. $new_path = $updir.'/'.$new_file_name;
  9313. // Resize the image.
  9314. $temp = new Image($image_array['tmp_name']);
  9315. $picture_infos = $temp->get_image_info();
  9316. if ($picture_infos['width'] > 104) {
  9317. $thumbwidth = 104;
  9318. } else {
  9319. $thumbwidth = $picture_infos['width'];
  9320. }
  9321. if ($picture_infos['height'] > 96) {
  9322. $new_height = 96;
  9323. } else {
  9324. $new_height = $picture_infos['height'];
  9325. }
  9326. $temp->resize($thumbwidth, $new_height, 0);
  9327. $result = $temp->send_image($new_path);
  9328. // Storing the image filename.
  9329. if ($result) {
  9330. $image_moved = true;
  9331. $this->set_preview_image($new_file_name);
  9332. //Resize to 64px to use on course homepage
  9333. $temp->resize(64, 64, 0);
  9334. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9335. return true;
  9336. }
  9337. }
  9338. }
  9339. }
  9340. return false;
  9341. }
  9342. public function set_autolunch($lp_id, $status)
  9343. {
  9344. $course_id = api_get_course_int_id();
  9345. $lp_id = intval($lp_id);
  9346. $status = intval($status);
  9347. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9348. //Setting everything to autolunch = 0
  9349. $attributes['autolunch'] = 0;
  9350. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9351. Database::update($lp_table, $attributes, $where);
  9352. if ($status == 1) {
  9353. //Setting my lp_id to autolunch = 1
  9354. $attributes['autolunch'] = 1;
  9355. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9356. Database::update($lp_table, $attributes, $where);
  9357. }
  9358. }
  9359. /**
  9360. * Gets previous_item_id for the next element of the lp_item table
  9361. * @author Isaac flores paz
  9362. * @return integer Previous item ID
  9363. */
  9364. function select_previous_item_id()
  9365. {
  9366. $course_id = api_get_course_int_id();
  9367. if ($this->debug > 0) {
  9368. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9369. }
  9370. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9371. // Get the max order of the items
  9372. $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."'";
  9373. $rs_max_order = Database::query($sql_max_order);
  9374. $row_max_order = Database::fetch_object($rs_max_order);
  9375. $max_order = $row_max_order->display_order;
  9376. // Get the previous item ID
  9377. $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."' ";
  9378. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9379. $row_max = Database::fetch_object($rs_max);
  9380. // Return the previous item ID
  9381. return $row_max->previous;
  9382. }
  9383. function copy()
  9384. {
  9385. $main_path = api_get_path(SYS_CODE_PATH);
  9386. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9387. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9388. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9389. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9390. //Course builder
  9391. $cb = new CourseBuilder();
  9392. //Setting tools that will be copied
  9393. $cb->set_tools_to_build(array('learnpaths'));
  9394. //Setting elements that will be copied
  9395. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9396. $course = $cb->build();
  9397. //Course restorer
  9398. $course_restorer = new CourseRestorer($course);
  9399. $course_restorer->set_add_text_in_items(true);
  9400. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9401. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9402. }
  9403. function verify_document_size($s)
  9404. {
  9405. $post_max = ini_get('post_max_size');
  9406. $upl_max = ini_get('upload_max_filesize');
  9407. $documents_total_space = DocumentManager::documents_total_space();
  9408. $course_max_space = DocumentManager::get_course_quota();
  9409. $total_size = filesize($s) + $documents_total_space;
  9410. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9411. return true;
  9412. } else {
  9413. return false;
  9414. }
  9415. }
  9416. function clear_prerequisites()
  9417. {
  9418. $course_id = $this->get_course_int_id();
  9419. if ($this->debug > 0) {
  9420. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9421. }
  9422. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9423. $lp_id = $this->get_id();
  9424. //Cleaning prerequisites
  9425. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9426. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9427. Database::query($sql);
  9428. //Cleaning mastery score for exercises
  9429. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9430. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9431. Database::query($sql);
  9432. }
  9433. function set_previous_step_as_prerequisite_for_all_items()
  9434. {
  9435. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9436. $course_id = $this->get_course_int_id();
  9437. $lp_id = $this->get_id();
  9438. if (!empty($this->items)) {
  9439. $previous_item_id = null;
  9440. $previous_item_max = 0;
  9441. $previous_item_type = null;
  9442. $last_item_not_chapter = null;
  9443. $last_item_not_chapter_type = null;
  9444. $last_item_not_chapter_max = null;
  9445. foreach ($this->items as $item) {
  9446. // if there was a previous item... (otherwise jump to set it)
  9447. if (!empty($previous_item_id)) {
  9448. $current_item_id = $item->get_id(); //save current id
  9449. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9450. // Current item is not a folder, so it qualifies to get a prerequisites
  9451. if ($last_item_not_chapter_type == 'quiz') {
  9452. // if previous is quiz, mark its max score as default score to be achieved
  9453. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9454. Database::query($sql);
  9455. }
  9456. // now simply update the prerequisite to set it to the last non-chapter item
  9457. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9458. Database::query($sql);
  9459. // record item as 'non-chapter' reference
  9460. $last_item_not_chapter = $item->get_id();
  9461. $last_item_not_chapter_type = $item->get_type();
  9462. $last_item_not_chapter_max = $item->get_max();
  9463. }
  9464. } else {
  9465. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9466. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9467. $last_item_not_chapter = $item->get_id();
  9468. $last_item_not_chapter_type = $item->get_type();
  9469. $last_item_not_chapter_max = $item->get_max();
  9470. }
  9471. $old_id = $item->get_id();
  9472. $old_max = $item->get_max();
  9473. $old_type = $item->get_type();
  9474. }
  9475. // Saving the item as "previous item" for the next loop
  9476. $previous_item_id = $item->get_id();
  9477. $previous_item_max = $item->get_max();
  9478. $previous_item_type = $item->get_type();
  9479. }
  9480. }
  9481. }
  9482. static function create_category($params)
  9483. {
  9484. $em = Database::getManager();
  9485. $item = new CLpCategory();
  9486. $item->setName($params['name']);
  9487. $item->setCId($params['c_id']);
  9488. $em->persist($item);
  9489. $em->flush();
  9490. }
  9491. static function update_category($params)
  9492. {
  9493. $em = Database::getManager();
  9494. $item = $em->find('ChamiloCoreBundle:CLpCategory', $params['id']);
  9495. if ($item) {
  9496. $item->setName($params['name']);
  9497. $item->setCId($params['c_id']);
  9498. $em->persist($item);
  9499. $em->flush();
  9500. }
  9501. }
  9502. static function move_up_category($id)
  9503. {
  9504. $em = Database::getManager();
  9505. $item = $em->find('ChamiloCoreBundle:CLpCategory', $id);
  9506. if ($item) {
  9507. $position = $item->getPosition() - 1;
  9508. $item->setPosition($position);
  9509. $em->persist($item);
  9510. $em->flush();
  9511. }
  9512. }
  9513. static function move_down_category($id)
  9514. {
  9515. $em = Database::getManager();
  9516. $item = $em->find('ChamiloCoreBundle:CLpCategory', $id);
  9517. if ($item) {
  9518. $position = $item->getPosition() + 1;
  9519. $item->setPosition($position);
  9520. $em->persist($item);
  9521. $em->flush();
  9522. }
  9523. }
  9524. static function get_count_categories($course_id)
  9525. {
  9526. if (empty($course_id)) {
  9527. return 0;
  9528. }
  9529. $em = Database::getManager();
  9530. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCoreBundle:CLpCategory u WHERE u.cId = :id');
  9531. $query->setParameter('id', $course_id);
  9532. return $query->getSingleScalarResult();
  9533. }
  9534. static function get_categories($course_id)
  9535. {
  9536. $em = Database::getManager();
  9537. //Default behaviour
  9538. /*$items = $em->getRepository('ChamiloCoreBundle:CLpCategory')->findBy(
  9539. array('cId' => $course_id),
  9540. array('name' => 'ASC')
  9541. );*/
  9542. //Using doctrine extensions
  9543. $items = $em->getRepository('ChamiloCoreBundle:CLpCategory')->getBySortableGroupsQuery(
  9544. array('cId' => $course_id)
  9545. )->getResult();
  9546. return $items;
  9547. }
  9548. static function get_category($id)
  9549. {
  9550. $em = Database::getManager();
  9551. $item = $em->find('ChamiloCoreBundle:CLpCategory', $id);
  9552. return $item;
  9553. }
  9554. static function get_category_by_course($course_id)
  9555. {
  9556. $em = Database::getManager();
  9557. $items = $em->getRepository('ChamiloCoreBundle:CLpCategory')->findBy(array('cId' => $course_id));
  9558. return $items;
  9559. }
  9560. static function delete_category($id)
  9561. {
  9562. $em = Database::getManager();
  9563. $item = $em->find('ChamiloCoreBundle:CLpCategory', $id);
  9564. if ($item) {
  9565. $courseId = $item->getCId();
  9566. $query = $em->createQuery('SELECT u FROM ChamiloCoreBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9567. $query->setParameter('id', $courseId);
  9568. $query->setParameter('catId', $item->getId());
  9569. $lps = $query->getResult();
  9570. // Setting category = 0.
  9571. if ($lps) {
  9572. foreach ($lps as $lpItem) {
  9573. $lpItem->setCategoryId(0);
  9574. }
  9575. }
  9576. // Removing category.
  9577. $em->remove($item);
  9578. $em->flush();
  9579. }
  9580. }
  9581. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9582. {
  9583. $items = self::get_category_by_course($course_id);
  9584. $cats = array();
  9585. if ($addSelectOption) {
  9586. $cats = array(get_lang('SelectACategory'));
  9587. }
  9588. if (!empty($items)) {
  9589. foreach ($items as $cat) {
  9590. $cats[$cat->getId()] = $cat->getName();
  9591. }
  9592. }
  9593. return $cats;
  9594. }
  9595. public function setError($error) {
  9596. $this->error = $error;
  9597. }
  9598. }
  9599. if (!function_exists('trim_value')) {
  9600. function trim_value(& $value)
  9601. {
  9602. $value = trim($value);
  9603. }
  9604. }