learnpath.class.php 454 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694
  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 \ChamiloSession as Session;
  17. class learnpath
  18. {
  19. public $attempt = 0; // The number for the current ID view.
  20. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  21. public $current; // Id of the current item the user is viewing.
  22. public $current_score; // The score of the current item.
  23. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  24. public $current_time_stop; // The time the user closed this resource.
  25. public $default_status = 'not attempted';
  26. public $encoding = 'UTF-8';
  27. public $error = '';
  28. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  29. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  30. public $index; // The index of the active learnpath_item in $ordered_items array.
  31. public $items = array();
  32. public $last; // item_id of last item viewed in the learning path.
  33. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  34. public $license; // Which license this course has been given - not used yet on 20060522.
  35. public $lp_id; // DB ID for this learnpath.
  36. public $lp_view_id; // DB ID for lp_view
  37. public $log_file; // File where to log learnpath API msg.
  38. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  39. public $message = '';
  40. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  41. public $name; // Learnpath name (they generally have one).
  42. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  43. public $path = ''; // Path inside the scorm directory (if scorm).
  44. public $theme; // The current theme of the learning path.
  45. public $preview_image; // The current image of the learning path.
  46. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  47. public $prevent_reinit = 1;
  48. // Describes the mode of progress bar display.
  49. public $seriousgame_mode = 0;
  50. public $progress_bar_mode = '%';
  51. // Percentage progress as saved in the db.
  52. public $progress_db = '0';
  53. public $proximity; // Wether the content is distant or local or unknown.
  54. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  55. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  56. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  57. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  58. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  59. public $user_id; //ID of the user that is viewing/using the course
  60. public $update_queue = array();
  61. public $scorm_debug = 0;
  62. public $arrMenu = array(); // Array for the menu items.
  63. public $debug = 0; // Logging level.
  64. public $lp_session_id = 0;
  65. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  66. public $prerequisite = 0;
  67. public $use_max_score = 1; // 1 or 0
  68. public $subscribe_users = 0;
  69. public $created_on = '';
  70. public $modified_on = '';
  71. public $publicated_on = '';
  72. public $expired_on = '';
  73. public $ref = null;
  74. public $course_int_id;
  75. public $course_info = array();
  76. public function get_course_int_id()
  77. {
  78. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  79. }
  80. public function set_course_int_id($course_id)
  81. {
  82. return $this->course_int_id = intval($course_id);
  83. }
  84. /**
  85. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  86. * Also builds the list of items into $this->items.
  87. * @param string Course code
  88. * @param integer Learnpath ID
  89. * @param integer User ID
  90. * @return boolean True on success, false on error
  91. */
  92. public function __construct($course, $lp_id, $user_id)
  93. {
  94. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  95. // Check course code.
  96. $course_id = api_get_course_int_id();
  97. if ($this->debug > 0) {
  98. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  99. }
  100. if (empty($course)) {
  101. $this->error = 'Course code is empty';
  102. return false;
  103. } else {
  104. $course_info = api_get_course_info($course);
  105. if (!empty($course_info)) {
  106. $this->cc = $course_info['code'];
  107. $this->course_info = $course_info;
  108. $course_id = $course_info['real_id'];
  109. } else {
  110. $this->error = 'Course code does not exist in database';
  111. return false;
  112. }
  113. }
  114. $this->set_course_int_id($course_id);
  115. // Check learnpath ID.
  116. if (empty($lp_id)) {
  117. $this->error = 'Learnpath ID is empty';
  118. return false;
  119. } else {
  120. // TODO: Make it flexible to use any course_code (still using env course code here).
  121. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  122. $lp_id = intval($lp_id);
  123. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  124. if ($this->debug > 2) {
  125. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  126. }
  127. $res = Database::query($sql);
  128. if (Database::num_rows($res) > 0) {
  129. $this->lp_id = $lp_id;
  130. $row = Database::fetch_array($res);
  131. $this->type = $row['lp_type'];
  132. $this->name = stripslashes($row['name']);
  133. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  134. $this->proximity = $row['content_local'];
  135. $this->theme = $row['theme'];
  136. $this->maker = $row['content_maker'];
  137. $this->prevent_reinit = $row['prevent_reinit'];
  138. $this->seriousgame_mode = $row['seriousgame_mode'];
  139. $this->license = $row['content_license'];
  140. $this->scorm_debug = $row['debug'];
  141. $this->js_lib = $row['js_lib'];
  142. $this->path = $row['path'];
  143. $this->preview_image = $row['preview_image'];
  144. $this->author = $row['author'];
  145. $this->hide_toc_frame = $row['hide_toc_frame'];
  146. $this->lp_session_id = $row['session_id'];
  147. $this->use_max_score = $row['use_max_score'];
  148. $this->subscribe_users = $row['subscribe_users'];
  149. $this->created_on = $row['created_on'];
  150. $this->modified_on = $row['modified_on'];
  151. $this->ref = $row['ref'];
  152. $this->category_id = $row['category_id'];
  153. $this->max_attempts = $row['max_attempts'];
  154. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  155. $this->publicated_on = $row['publicated_on'];
  156. }
  157. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  158. $this->expired_on = $row['expired_on'];
  159. }
  160. if ($this->type == 2) {
  161. if ($row['force_commit'] == 1) {
  162. $this->force_commit = true;
  163. }
  164. }
  165. $this->mode = $row['default_view_mod'];
  166. } else {
  167. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  168. return false;
  169. }
  170. }
  171. // Check user ID.
  172. if (empty($user_id)) {
  173. $this->error = 'User ID is empty';
  174. return false;
  175. } else {
  176. $user_info = api_get_user_info($user_id);
  177. if (!empty($user_info)) {
  178. $this->user_id = $user_info['user_id'];
  179. } else {
  180. $this->error = 'User ID does not exist in database ('.$sql.')';
  181. return false;
  182. }
  183. }
  184. // End of variables checking.
  185. $session_id = api_get_session_id();
  186. // Get the session condition for learning paths of the base + session.
  187. $session = api_get_session_condition($session_id);
  188. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  189. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  190. // Selecting by view_count descending allows to get the highest view_count first.
  191. $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";
  192. $res = Database::query($sql);
  193. if ($this->debug > 2) {
  194. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  195. }
  196. if (Database :: num_rows($res) > 0) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  199. }
  200. $row = Database :: fetch_array($res);
  201. $this->attempt = $row['view_count'];
  202. $this->lp_view_id = $row['id'];
  203. $this->last_item_seen = $row['last_item'];
  204. $this->progress_db = $row['progress'];
  205. $this->lp_view_session_id = $row['session_id'];
  206. } else {
  207. if ($this->debug > 2) {
  208. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  209. }
  210. $this->attempt = 1;
  211. $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)";
  212. $res_ins = Database::query($sql_ins);
  213. $this->lp_view_id = Database :: insert_id();
  214. if ($this->debug > 2) {
  215. error_log('New LP - learnpath::__construct() '.__LINE__.' - inserting new lp_view: '.$sql_ins, 0);
  216. }
  217. }
  218. // Initialise items.
  219. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  220. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  221. $res = Database::query($sql);
  222. if ($this->debug > 2) {
  223. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  224. error_log('-- Start while--', 0);
  225. }
  226. $lp_item_id_list = array();
  227. while ($row = Database::fetch_array($res)) {
  228. $oItem = '';
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - aicc object with id '.$my_item_id.' set in items[]',
  243. 0
  244. );
  245. }
  246. }
  247. break;
  248. case 2:
  249. require_once 'scorm.class.php';
  250. require_once 'scormItem.class.php';
  251. $oItem = new scormItem('db', $row['id'], $course_id);
  252. if (is_object($oItem)) {
  253. $my_item_id = $oItem->get_id();
  254. $oItem->set_lp_view($this->lp_view_id, $course_id);
  255. $oItem->set_prevent_reinit($this->prevent_reinit);
  256. // Don't use reference here as the next loop will make the pointed object change.
  257. $this->items[$my_item_id] = $oItem;
  258. $this->refs_list[$oItem->ref] = $my_item_id;
  259. if ($this->debug > 2) {
  260. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  261. }
  262. }
  263. break;
  264. case 1:
  265. default:
  266. if ($this->debug > 2) {
  267. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  268. }
  269. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  270. if ($this->debug > 2) {
  271. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  272. }
  273. if (is_object($oItem)) {
  274. $my_item_id = $oItem->get_id();
  275. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  276. $oItem->set_prevent_reinit($this->prevent_reinit);
  277. // Don't use reference here as the next loop will make the pointed object change.
  278. $this->items[$my_item_id] = $oItem;
  279. $this->refs_list[$my_item_id] = $my_item_id;
  280. if ($this->debug > 2) {
  281. error_log(
  282. 'New LP - learnpath::__construct() '.__LINE__.' - object with id '.$my_item_id.' set in items[]',
  283. 0
  284. );
  285. }
  286. }
  287. break;
  288. }
  289. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  290. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  291. if (is_object($this->items[$row['id']])) {
  292. $this->items[$row['id']]->set_level(0);
  293. }
  294. } else {
  295. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  296. $this->items[$row['id']]->set_level($level);
  297. if (is_object($this->items[$row['parent_item_id']])) {
  298. // Items is a list of pointers from item DB ids to item objects.
  299. $this->items[$row['parent_item_id']]->add_child($row['id']);
  300. } else {
  301. if ($this->debug > 2) {
  302. error_log(
  303. 'New LP - learnpath::__construct() '.__LINE__.' - The parent item ('.$row['parent_item_id'].') of item '.$row['id'].' could not be found',
  304. 0
  305. );
  306. }
  307. }
  308. }
  309. // Setting the view in the item object.
  310. if (is_object($this->items[$row['id']])) {
  311. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  312. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  313. $this->items[$row['id']]->current_start_time = 0;
  314. $this->items[$row['id']]->current_stop_time = 0;
  315. }
  316. }
  317. }
  318. if ($this->debug > 2) {
  319. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  320. }
  321. if (!empty($lp_item_id_list)) {
  322. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  323. // Get last viewing vars.
  324. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  325. // This query should only return one or zero result.
  326. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  327. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  328. ORDER BY view_count DESC ";
  329. if ($this->debug > 2) {
  330. error_log('New LP - learnpath::__construct() - Selecting item_views: '.$sql, 0);
  331. }
  332. $status_list = array();
  333. $res = Database::query($sql);
  334. while ($row = Database :: fetch_array($res)) {
  335. $status_list[$row['lp_item_id']] = $row['status'];
  336. }
  337. foreach ($lp_item_id_list as $item_id) {
  338. if (isset($status_list[$item_id])) {
  339. $status = $status_list[$item_id];
  340. if (is_object($this->items[$item_id])) {
  341. $this->items[$item_id]->set_status($status);
  342. if (empty($status)) {
  343. $this->items[$item_id]->set_status($this->default_status);
  344. }
  345. }
  346. } else {
  347. if (is_object($this->items[$item_id])) {
  348. $this->items[$item_id]->set_status($this->default_status);
  349. }
  350. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  351. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  352. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  353. if ($this->debug > 2) {
  354. error_log(
  355. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  356. 0
  357. );
  358. }
  359. Database::query($sql_ins);
  360. }
  361. }
  362. }
  363. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  364. $this->max_ordered_items = 0;
  365. foreach ($this->ordered_items as $index => $dummy) {
  366. if ($index > $this->max_ordered_items && !empty($dummy)) {
  367. $this->max_ordered_items = $index;
  368. }
  369. }
  370. // TODO: Define the current item better.
  371. $this->first();
  372. if ($this->debug > 2) {
  373. error_log(
  374. 'New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(
  375. ),
  376. 0
  377. );
  378. }
  379. }
  380. /**
  381. * 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
  382. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  383. *
  384. * @param int $parent
  385. * @param int $previous
  386. * @param string $type
  387. * @param int resource ID (ref)
  388. * @param string $title
  389. * @param string $description
  390. * @return int
  391. */
  392. public function add_item(
  393. $parent,
  394. $previous,
  395. $type = 'dokeos_chapter',
  396. $id,
  397. $title,
  398. $description,
  399. $prerequisites = 0,
  400. $max_time_allowed = 0
  401. ) {
  402. $course_id = api_get_course_int_id();
  403. if ($this->debug > 0) {
  404. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  405. }
  406. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  407. $parent = intval($parent);
  408. $previous = intval($previous);
  409. $type = Database::escape_string($type);
  410. $id = intval($id);
  411. $max_time_allowed = Database::escape_string($max_time_allowed);
  412. if (empty ($max_time_allowed)) {
  413. $max_time_allowed = 0;
  414. }
  415. $title = Database::escape_string($title);
  416. $description = Database::escape_string($description);
  417. $sql_count = " SELECT COUNT(id) AS num
  418. FROM $tbl_lp_item
  419. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND parent_item_id = ".$parent;
  420. $res_count = Database::query($sql_count);
  421. $row = Database :: fetch_array($res_count);
  422. $num = $row['num'];
  423. if ($num > 0) {
  424. if ($previous == 0) {
  425. $sql = "SELECT id, next_item_id, display_order
  426. FROM ".$tbl_lp_item."
  427. WHERE c_id = $course_id AND
  428. lp_id = ".$this->get_id()." AND
  429. parent_item_id = ".$parent." AND
  430. previous_item_id = 0 OR previous_item_id=".$parent;
  431. $result = Database::query($sql);
  432. $row = Database :: fetch_array($result);
  433. $tmp_previous = 0;
  434. $next = $row['id'];
  435. $display_order = 0;
  436. } else {
  437. $previous = (int)$previous;
  438. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  439. FROM $tbl_lp_item
  440. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND id = ".$previous;
  441. $result = Database::query($sql);
  442. $row = Database :: fetch_array($result);
  443. $tmp_previous = $row['id'];
  444. $next = $row['next_item_id'];
  445. $display_order = $row['display_order'];
  446. }
  447. } else {
  448. $tmp_previous = 0;
  449. $next = 0;
  450. $display_order = 0;
  451. }
  452. $new_item_id = -1;
  453. $id = Database::escape_string($id);
  454. if ($type == 'quiz') {
  455. $sql = 'SELECT SUM(ponderation)
  456. FROM '.Database :: get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  457. INNER JOIN '.Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  458. ON quiz_question.iid = quiz_rel_question.question_id
  459. WHERE quiz_rel_question.exercice_id = '.$id." AND
  460. quiz_question.c_id = $course_id AND
  461. quiz_rel_question.c_id = $course_id ";
  462. $rsQuiz = Database::query($sql);
  463. $max_score = Database :: result($rsQuiz, 0, 0);
  464. //Disabling the exercise if we add it inside a LP
  465. $exercise = new Exercise();
  466. $exercise->read($id);
  467. $exercise->disable();
  468. $exercise->save();
  469. } else {
  470. $max_score = 100;
  471. }
  472. if ($prerequisites != 0) {
  473. $sql_ins = "INSERT INTO ".$tbl_lp_item." (
  474. c_id,
  475. lp_id, ".
  476. "item_type, ".
  477. "ref, ".
  478. "title, ".
  479. "description, ".
  480. "path, ".
  481. "max_score, ".
  482. "parent_item_id, ".
  483. "previous_item_id, ".
  484. "next_item_id, ".
  485. "display_order, ".
  486. "prerequisite, ".
  487. "max_time_allowed ".
  488. ") VALUES (
  489. $course_id ,
  490. ".$this->get_id().", ".
  491. "'".$type."', ".
  492. "'', ".
  493. "'".$title."', ".
  494. "'".$description."', ".
  495. "'".$id."', ".
  496. "'".$max_score."', ".
  497. $parent.", ".
  498. $previous.", ".
  499. $next.", ".
  500. ($display_order + 1).", ".
  501. $prerequisites.", ".
  502. $max_time_allowed.
  503. ")";
  504. } else {
  505. // Insert new item.
  506. $sql_ins = "
  507. INSERT INTO ".$tbl_lp_item." ( ".
  508. "c_id, ".
  509. "lp_id, ".
  510. "item_type, ".
  511. "ref, ".
  512. "title, ".
  513. "description, ".
  514. "path, ".
  515. "max_score, ".
  516. "parent_item_id, ".
  517. "previous_item_id, ".
  518. "next_item_id, ".
  519. "display_order, ".
  520. "max_time_allowed ".
  521. ") VALUES (".
  522. $course_id.",".
  523. $this->get_id().",".
  524. "'".$type."',".
  525. "'',".
  526. "'".$title."',".
  527. "'".$description."',".
  528. "'".$id."',".
  529. "'".$max_score."',".
  530. $parent.",".
  531. $previous.",".
  532. $next.",".
  533. ($display_order + 1).",".
  534. $max_time_allowed.
  535. ")";
  536. }
  537. if ($this->debug > 2) {
  538. error_log('New LP - Inserting dokeos_chapter: '.$sql_ins, 0);
  539. }
  540. $res_ins = Database::query($sql_ins);
  541. if ($res_ins) {
  542. $new_item_id = Database::insert_id();
  543. // Update the item that should come after the new item.
  544. $sql_update_next = "
  545. UPDATE ".$tbl_lp_item."
  546. SET previous_item_id = ".$new_item_id."
  547. WHERE c_id = $course_id AND id = ".$next;
  548. Database::query($sql_update_next);
  549. // Update the item that should be before the new item.
  550. $sql_update_previous = "
  551. UPDATE ".$tbl_lp_item."
  552. SET next_item_id = ".$new_item_id."
  553. WHERE c_id = $course_id AND id = ".$tmp_previous;
  554. Database::query($sql_update_previous);
  555. // Update all the items after the new item.
  556. $sql_update_order = "
  557. UPDATE ".$tbl_lp_item."
  558. SET display_order = display_order + 1
  559. WHERE
  560. c_id = $course_id AND
  561. lp_id = ".$this->get_id()." AND
  562. id <> ".$new_item_id." AND
  563. parent_item_id = ".$parent." AND
  564. display_order > ".$display_order;
  565. Database::query($sql_update_order);
  566. // Update the item that should come after the new item.
  567. $sql_update_ref = "UPDATE ".$tbl_lp_item."
  568. SET ref = ".$new_item_id."
  569. WHERE c_id = $course_id AND id = ".$new_item_id;
  570. Database::query($sql_update_ref);
  571. }
  572. // Upload audio.
  573. if (!empty ($_FILES['mp3']['name'])) {
  574. // Create the audio folder if it does not exist yet.
  575. $_course = api_get_course_info();
  576. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  577. if (!is_dir($filepath.'audio')) {
  578. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  579. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  580. api_item_property_update(
  581. $_course,
  582. TOOL_DOCUMENT,
  583. $audio_id,
  584. 'FolderCreated',
  585. api_get_user_id(),
  586. null,
  587. null,
  588. null,
  589. null,
  590. api_get_session_id()
  591. );
  592. api_item_property_update(
  593. $_course,
  594. TOOL_DOCUMENT,
  595. $audio_id,
  596. 'invisible',
  597. api_get_user_id(),
  598. null,
  599. null,
  600. null,
  601. null,
  602. api_get_session_id()
  603. );
  604. }
  605. // Upload the file in the documents tool.
  606. $file_path = FileManager::handle_uploaded_document(
  607. $_course,
  608. $_FILES['mp3'],
  609. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  610. '/audio',
  611. api_get_user_id(),
  612. '',
  613. '',
  614. '',
  615. '',
  616. false
  617. );
  618. // Getting the filename only.
  619. $file_components = explode('/', $file_path);
  620. $file = $file_components[count($file_components) - 1];
  621. // Store the mp3 file in the lp_item table.
  622. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string(
  623. $file
  624. )."' WHERE id = '".Database::escape_string($new_item_id)."'";
  625. Database::query($sql_insert_audio);
  626. }
  627. return $new_item_id;
  628. }
  629. /**
  630. * Static admin function allowing addition of a learnpath to a course.
  631. * @param string Course code
  632. * @param string Learnpath name
  633. * @param string Learnpath description string, if provided
  634. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  635. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  636. * @param string Zip file containing the learnpath or directory containing the learnpath
  637. * @return integer The new learnpath ID on success, 0 on failure
  638. */
  639. public static function add_lp(
  640. $course,
  641. $name,
  642. $description = '',
  643. $learnpath = 'guess',
  644. $origin = 'zip',
  645. $zipname = '',
  646. $publicated_on = '',
  647. $expired_on = '',
  648. $category_id = 0
  649. ) {
  650. global $charset;
  651. $course_id = api_get_course_int_id();
  652. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  653. // Check course code exists.
  654. // Check lp_name doesn't exist, otherwise append something.
  655. $i = 0;
  656. $name = Database::escape_string($name);
  657. $category_id = intval($category_id);
  658. // Session id.
  659. $session_id = api_get_session_id();
  660. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  661. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  662. $res_name = Database::query($check_name);
  663. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  664. //by default the publication date is the same that the creation date
  665. //The behaviour above was changed due BT#2800
  666. global $_custom;
  667. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  668. $publicated_on = '';
  669. } else {
  670. $publicated_on = api_get_utc_datetime();
  671. }
  672. } else {
  673. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  674. }
  675. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  676. $expired_on = '';
  677. } else {
  678. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  679. }
  680. while (Database :: num_rows($res_name)) {
  681. // There is already one such name, update the current one a bit.
  682. $i++;
  683. $name = $name.' - '.$i;
  684. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  685. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  686. $res_name = Database::query($check_name);
  687. }
  688. // New name does not exist yet; keep it.
  689. // Escape description.
  690. $description = Database::escape_string(
  691. api_htmlentities($description, ENT_QUOTES, $charset)
  692. ); // Kevin: added htmlentities().
  693. $type = 1;
  694. switch ($learnpath) {
  695. case 'guess':
  696. break;
  697. case 'dokeos':
  698. case 'chamilo':
  699. $type = 1;
  700. break;
  701. case 'aicc':
  702. break;
  703. }
  704. switch ($origin) {
  705. case 'zip':
  706. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  707. break;
  708. case 'manual':
  709. default:
  710. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  711. $res_max = Database::query($get_max);
  712. if (Database :: num_rows($res_max) < 1) {
  713. $dsp = 1;
  714. } else {
  715. $row = Database :: fetch_array($res_max);
  716. $dsp = $row[0] + 1;
  717. }
  718. $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) ".
  719. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime(
  720. )."' , '".$publicated_on."' , '".$expired_on."', $category_id)";
  721. Database::query($sql_insert);
  722. $id = Database :: insert_id();
  723. if ($id > 0) {
  724. $course_info = api_get_course_info();
  725. // Insert into item_property.
  726. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  727. api_set_default_visibility($course_info, $id, TOOL_LEARNPATH);
  728. return $id;
  729. }
  730. break;
  731. }
  732. }
  733. /**
  734. * Appends a message to the message attribute
  735. * @param string Message to append.
  736. */
  737. public function append_message($string)
  738. {
  739. if ($this->debug > 0) {
  740. error_log('New LP - In learnpath::append_message()', 0);
  741. }
  742. $this->message .= $string;
  743. }
  744. /**
  745. * Autocompletes the parents of an item in case it's been completed or passed
  746. * @param integer Optional ID of the item from which to look for parents
  747. */
  748. public function autocomplete_parents($item)
  749. {
  750. $debug = $this->debug;
  751. if ($debug) {
  752. error_log('Learnpath::autocomplete_parents()', 0);
  753. }
  754. if (empty($item)) {
  755. $item = $this->current;
  756. }
  757. $parent_id = $this->items[$item]->get_parent();
  758. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  759. // if $item points to an object and there is a parent.
  760. if ($debug) {
  761. error_log('Autocompleting parent of item '.$item.' (item '.$parent_id.')', 0);
  762. }
  763. $current_item = $this->items[$item];
  764. $parent = $this->items[$parent_id]; // Get the parent.
  765. // New experiment including failed and browsed in completed status.
  766. $current_status = $current_item->get_status();
  767. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  768. // If the current item is completed or passes or succeeded.
  769. $completed = true;
  770. if ($debug) {
  771. error_log('Status of current item is alright', 0);
  772. }
  773. foreach ($parent->get_children() as $child) {
  774. // Check all his brothers (parent's children) for completion status.
  775. if ($child != $item) {
  776. if ($debug) {
  777. error_log(
  778. 'Looking at brother with ID '.$child.', status is '.$this->items[$child]->get_status(),
  779. 0
  780. );
  781. }
  782. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  783. // Trying completing parents of failed and browsed items as well.
  784. if ($this->items[$child]->status_is(
  785. array(
  786. 'completed',
  787. 'passed',
  788. 'succeeded',
  789. 'browsed',
  790. 'failed'
  791. )
  792. )
  793. ) {
  794. // Keep completion status to true.
  795. } else {
  796. if ($this->debug > 2) {
  797. error_log(
  798. 'Found one incomplete child of '.$parent_id.': '.$child.' is '.$this->items[$child]->get_status(
  799. ),
  800. 0
  801. );
  802. }
  803. $completed = false;
  804. }
  805. }
  806. }
  807. if ($completed) { // If all the children were completed:
  808. $parent->set_status('completed');
  809. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  810. $this->update_queue[$parent->get_id()] = $parent->get_status();
  811. if ($debug) {
  812. error_log('Added parent to update queue '.print_r($this->update_queue, true), 0);
  813. }
  814. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  815. }
  816. } else {
  817. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  818. }
  819. } else {
  820. if ($debug) {
  821. error_log("#$item is an item doesn't have parents");
  822. }
  823. }
  824. }
  825. /**
  826. * Autosaves the current results into the database for the whole learnpath
  827. */
  828. public function autosave()
  829. {
  830. if ($this->debug > 0) {
  831. error_log('New LP - In learnpath::autosave()', 0);
  832. }
  833. // TODO: Add save operations for the learnpath itself.
  834. }
  835. /**
  836. * Clears the message attribute
  837. */
  838. public function clear_message()
  839. {
  840. if ($this->debug > 0) {
  841. error_log('New LP - In learnpath::clear_message()', 0);
  842. }
  843. $this->message = '';
  844. }
  845. /**
  846. * Closes the current resource
  847. *
  848. * Stops the timer
  849. * Saves into the database if required
  850. * Clears the current resource data from this object
  851. * @return boolean True on success, false on failure
  852. */
  853. public function close()
  854. {
  855. $course_id = api_get_course_int_id();
  856. if ($this->debug > 0) {
  857. error_log('New LP - In learnpath::close()', 0);
  858. }
  859. if (empty ($this->lp_id)) {
  860. $this->error = 'Trying to close this learnpath but no ID is set';
  861. return false;
  862. }
  863. $this->current_time_stop = time();
  864. if ($this->save) {
  865. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  866. /*
  867. $sql = "UPDATE $learnpath_view_table " .
  868. "SET " .
  869. "stop_time = ".$this->current_time_stop.", " .
  870. "score = ".$this->current_score.", ".
  871. "WHERE learnpath_id = '".$this->lp_id."'";
  872. //$res = Database::query($sql);
  873. $res = Database::query($res);
  874. if (Database::affected_rows($res) < 1) {
  875. $this->error = 'Could not update learnpath_view table while closing learnpath';
  876. return false;
  877. }
  878. */
  879. }
  880. $this->ordered_items = array();
  881. $this->index = 0;
  882. unset ($this->lp_id);
  883. //unset other stuff
  884. return true;
  885. }
  886. /**
  887. * Static admin function allowing removal of a learnpath
  888. * @param string Course code
  889. * @param integer Learnpath ID
  890. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  891. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  892. */
  893. public function delete($course = null, $id = null, $delete = 'keep')
  894. {
  895. $course_id = api_get_course_int_id();
  896. // TODO: Implement a way of getting this to work when the current object is not set.
  897. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  898. //if (empty($course)) { $course = api_get_course_id(); }
  899. //if (empty($id)) { $id = $this->get_id(); }
  900. // If an ID is specifically given and the current LP is not the same, prevent delete.
  901. if (!empty ($id) && ($id != $this->lp_id)) {
  902. return false;
  903. }
  904. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  905. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  906. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  907. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  908. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  909. // Delete lp item id.
  910. foreach ($this->items as $id => $dummy) {
  911. //$this->items[$id]->delete();
  912. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  913. $res_del_item_view = Database::query($sql_del_view);
  914. }
  915. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  916. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  917. $res_del_item = Database::query($sql_del_item);
  918. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  919. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  920. $res_del_view = Database::query($sql_del_view);
  921. self::toggle_publish($this->lp_id, 'i');
  922. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  923. if ($this->type == 2 || $this->type == 3) {
  924. // This is a scorm learning path, delete the files as well.
  925. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  926. $res = Database::query($sql);
  927. if (Database :: num_rows($res) > 0) {
  928. $row = Database :: fetch_array($res);
  929. $path = $row['path'];
  930. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  931. $res = Database::query($sql);
  932. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  933. if ($this->debug > 2) {
  934. error_log(
  935. 'New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory',
  936. 0
  937. );
  938. }
  939. } else {
  940. // No other LP uses that directory, delete it.
  941. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  942. $course_scorm_dir = api_get_path(
  943. SYS_COURSE_PATH
  944. ).$course_rel_dir; // The absolute system path for this course.
  945. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) and !empty ($course_scorm_dir)) {
  946. if ($this->debug > 2) {
  947. error_log(
  948. 'New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path,
  949. 0
  950. );
  951. }
  952. // Proposed by Christophe (clefevre).
  953. if (strcmp(substr($path, -2), "/.") == 0) {
  954. $path = substr($path, 0, -1); // Remove "." at the end.
  955. }
  956. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  957. api_rmdirr($course_scorm_dir.$path);
  958. }
  959. }
  960. }
  961. }
  962. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  963. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  964. $res_del_lp = Database::query($sql_del_lp);
  965. $this->update_display_order(); // Updates the display order of all lps.
  966. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  967. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  968. // Delete link of gradebook tool
  969. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  970. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  971. $result = Database::query($sql);
  972. $row = Database :: fetch_array($result, 'ASSOC');*/
  973. // Fixing gradebook link deleted see #5229.
  974. /*
  975. if (!empty($row['id'])) {
  976. $link = LinkFactory :: load($row['id']);
  977. if ($link[0] != null) {
  978. $link[0]->delete();
  979. }
  980. }*/
  981. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  982. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4, $id, api_get_session_id());
  983. if ($link_info !== false) {
  984. remove_resource_from_course_gradebook($link_info['id']);
  985. }
  986. if (api_get_setting('search_enabled') == 'true') {
  987. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  988. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  989. }
  990. }
  991. /**
  992. * Removes all the children of one item - dangerous!
  993. * @param integer Element ID of which children have to be removed
  994. * @return integer Total number of children removed
  995. */
  996. public function delete_children_items($id)
  997. {
  998. $course_id = api_get_course_int_id();
  999. if ($this->debug > 0) {
  1000. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  1001. }
  1002. $num = 0;
  1003. if (empty ($id) || $id != strval(intval($id))) {
  1004. return false;
  1005. }
  1006. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1007. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1008. $res = Database::query($sql);
  1009. while ($row = Database :: fetch_array($res)) {
  1010. $num += $this->delete_children_items($row['id']);
  1011. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1012. $res_del = Database::query($sql_del);
  1013. $num++;
  1014. }
  1015. return $num;
  1016. }
  1017. /**
  1018. * Removes an item from the current learnpath
  1019. * @param integer Elem ID (0 if first)
  1020. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  1021. * @return integer Number of elements moved
  1022. * @todo implement resource removal
  1023. */
  1024. public function delete_item($id, $remove = 'keep')
  1025. {
  1026. $course_id = api_get_course_int_id();
  1027. if ($this->debug > 0) {
  1028. error_log('New LP - In learnpath::delete_item()', 0);
  1029. }
  1030. // TODO: Implement the resource removal.
  1031. if (empty ($id) || $id != strval(intval($id))) {
  1032. return false;
  1033. }
  1034. // First select item to get previous, next, and display order.
  1035. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1036. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1037. $res_sel = Database::query($sql_sel);
  1038. if (Database :: num_rows($res_sel) < 1) {
  1039. return false;
  1040. }
  1041. $row = Database :: fetch_array($res_sel);
  1042. $previous = $row['previous_item_id'];
  1043. $next = $row['next_item_id'];
  1044. $display = $row['display_order'];
  1045. $parent = $row['parent_item_id'];
  1046. $lp = $row['lp_id'];
  1047. // Delete children items.
  1048. $num = $this->delete_children_items($id);
  1049. if ($this->debug > 2) {
  1050. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1051. }
  1052. // Now delete the item.
  1053. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1054. if ($this->debug > 2) {
  1055. error_log('New LP - Deleting item: '.$sql_del, 0);
  1056. }
  1057. $res_del = Database::query($sql_del);
  1058. // Now update surrounding items.
  1059. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  1060. $res_upd = Database::query($sql_upd);
  1061. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  1062. $res_upd = Database::query($sql_upd);
  1063. // Now update all following items with new display order.
  1064. $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";
  1065. $res_all = Database::query($sql_all);
  1066. //Removing prerequisites since the item will not longer exist
  1067. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1068. $res_all = Database::query($sql_all);
  1069. // Remove from search engine if enabled.
  1070. if (api_get_setting('search_enabled') == 'true') {
  1071. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1072. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1073. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1074. $res = Database::query($sql);
  1075. if (Database :: num_rows($res) > 0) {
  1076. $row2 = Database :: fetch_array($res);
  1077. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1078. $di = new ChamiloIndexer();
  1079. $di->remove_document((int)$row2['search_did']);
  1080. }
  1081. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1082. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1083. Database::query($sql);
  1084. }
  1085. }
  1086. /**
  1087. * Updates an item's content in place
  1088. * @param integer Element ID
  1089. * @param integer Parent item ID
  1090. * @param integer Previous item ID
  1091. * @param string Item title
  1092. * @param string Item description
  1093. * @param string Prerequisites (optional)
  1094. * @param string Indexing terms (optional)
  1095. * @param array The array resulting of the $_FILES[mp3] element
  1096. * @return boolean True on success, false on error
  1097. */
  1098. public function edit_item(
  1099. $id,
  1100. $parent,
  1101. $previous,
  1102. $title,
  1103. $description,
  1104. $prerequisites = 0,
  1105. $audio = null,
  1106. $max_time_allowed = 0
  1107. ) {
  1108. $course_id = api_get_course_int_id();
  1109. if ($this->debug > 0) {
  1110. error_log('New LP - In learnpath::edit_item()', 0);
  1111. }
  1112. if (empty ($max_time_allowed)) {
  1113. $max_time_allowed = 0;
  1114. }
  1115. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1116. return false;
  1117. }
  1118. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1119. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1120. $res_select = Database::query($sql_select);
  1121. $row_select = Database :: fetch_array($res_select);
  1122. $audio_update_sql = '';
  1123. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1124. // Create the audio folder if it does not exist yet.
  1125. $_course = api_get_course_info();
  1126. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1127. if (!is_dir($filepath.'audio')) {
  1128. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1129. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  1130. api_item_property_update(
  1131. $_course,
  1132. TOOL_DOCUMENT,
  1133. $audio_id,
  1134. 'FolderCreated',
  1135. api_get_user_id(),
  1136. null,
  1137. null,
  1138. null,
  1139. null,
  1140. api_get_session_id()
  1141. );
  1142. api_item_property_update(
  1143. $_course,
  1144. TOOL_DOCUMENT,
  1145. $audio_id,
  1146. 'invisible',
  1147. api_get_user_id(),
  1148. null,
  1149. null,
  1150. null,
  1151. null,
  1152. api_get_session_id()
  1153. );
  1154. }
  1155. // Upload file in documents.
  1156. $pi = pathinfo($audio['name']);
  1157. if ($pi['extension'] == 'mp3') {
  1158. $c_det = api_get_course_info($this->cc);
  1159. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1160. $path = FileManager::handle_uploaded_document(
  1161. $c_det,
  1162. $audio,
  1163. $bp,
  1164. '/audio',
  1165. api_get_user_id(),
  1166. 0,
  1167. null,
  1168. 0,
  1169. 'rename',
  1170. false,
  1171. 0
  1172. );
  1173. $path = substr($path, 7);
  1174. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1175. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1176. }
  1177. }
  1178. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1179. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1180. // TODO: htmlspecialchars to be checked for encoding related problems.
  1181. if ($same_parent && $same_previous) {
  1182. // Only update title and description.
  1183. $sql_update = " UPDATE ".$tbl_lp_item."
  1184. SET title = '".Database::escape_string($title)."',
  1185. prerequisite = '".$prerequisites."',
  1186. description = '".Database::escape_string($description)."'
  1187. ".$audio_update_sql.",
  1188. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1189. WHERE c_id = ".$course_id." AND id = ".$id;
  1190. $res_update = Database::query($sql_update);
  1191. } else {
  1192. $old_parent = $row_select['parent_item_id'];
  1193. $old_previous = $row_select['previous_item_id'];
  1194. $old_next = $row_select['next_item_id'];
  1195. $old_order = $row_select['display_order'];
  1196. $old_prerequisite = $row_select['prerequisite'];
  1197. $old_max_time_allowed = $row_select['max_time_allowed'];
  1198. /* BEGIN -- virtually remove the current item id */
  1199. /* for the next and previous item it is like the current item doesn't exist anymore */
  1200. if ($old_previous != 0) {
  1201. $sql_update_next = "
  1202. UPDATE ".$tbl_lp_item."
  1203. SET next_item_id = ".$old_next."
  1204. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1205. $res_update_next = Database::query($sql_update_next);
  1206. //echo '<p>' . $sql_update_next . '</p>';
  1207. }
  1208. if ($old_next != 0) {
  1209. $sql_update_previous = "
  1210. UPDATE ".$tbl_lp_item."
  1211. SET previous_item_id = ".$old_previous."
  1212. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1213. $res_update_previous = Database::query($sql_update_previous);
  1214. //echo '<p>' . $sql_update_previous . '</p>';
  1215. }
  1216. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1217. $sql_update_order = "
  1218. UPDATE ".$tbl_lp_item."
  1219. SET display_order = display_order - 1
  1220. WHERE
  1221. c_id = ".$course_id." AND
  1222. display_order > ".$old_order." AND lp_id = ".$this->lp_id." AND
  1223. parent_item_id = ".$old_parent;
  1224. $res_update_order = Database::query($sql_update_order);
  1225. //echo '<p>' . $sql_update_order . '</p>';
  1226. /* END -- virtually remove the current item id */
  1227. /* BEGIN -- update the current item id to his new location */
  1228. if ($previous == 0) {
  1229. // Select the data of the item that should come after the current item.
  1230. $sql_select_old = "SELECT id, display_order
  1231. FROM ".$tbl_lp_item."
  1232. WHERE
  1233. c_id = ".$course_id." AND
  1234. lp_id = ".$this->lp_id." AND
  1235. parent_item_id = ".$parent." AND
  1236. previous_item_id = ".$previous;
  1237. $res_select_old = Database::query($sql_select_old);
  1238. $row_select_old = Database :: fetch_array($res_select_old);
  1239. //echo '<p>' . $sql_select_old . '</p>';
  1240. // If the new parent didn't have children before.
  1241. if (Database :: num_rows($res_select_old) == 0) {
  1242. $new_next = 0;
  1243. $new_order = 1;
  1244. } else {
  1245. $new_next = $row_select_old['id'];
  1246. $new_order = $row_select_old['display_order'];
  1247. }
  1248. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1249. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1250. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1251. } else {
  1252. // Select the data of the item that should come before the current item.
  1253. $sql_select_old = " SELECT next_item_id, display_order
  1254. FROM ".$tbl_lp_item."
  1255. WHERE c_id = ".$course_id." AND id = ".$previous;
  1256. $res_select_old = Database::query($sql_select_old);
  1257. $row_select_old = Database :: fetch_array($res_select_old);
  1258. //echo '<p>' . $sql_select_old . '</p>';
  1259. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1260. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1261. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1262. $new_next = $row_select_old['next_item_id'];
  1263. $new_order = $row_select_old['display_order'] + 1;
  1264. }
  1265. // TODO: htmlspecialchars to be checked for encoding related problems.
  1266. // Update the current item with the new data.
  1267. $sql_update = "UPDATE ".$tbl_lp_item."
  1268. SET
  1269. title = '".Database::escape_string($title)."',
  1270. description = '".Database::escape_string($description)."',
  1271. parent_item_id = ".$parent.",
  1272. previous_item_id = ".$previous.",
  1273. next_item_id = ".$new_next.",
  1274. display_order = ".$new_order."
  1275. ".$audio_update_sql."
  1276. WHERE c_id = ".$course_id." AND id = ".$id;
  1277. $res_update_next = Database::query($sql_update);
  1278. //echo '<p>' . $sql_update . '</p>';
  1279. if ($previous != 0) {
  1280. // Update the previous item's next_item_id.
  1281. $sql_update_previous = "
  1282. UPDATE ".$tbl_lp_item."
  1283. SET next_item_id = ".$id."
  1284. WHERE c_id = ".$course_id." AND id = ".$previous;
  1285. $res_update_next = Database::query($sql_update_previous);
  1286. //echo '<p>' . $sql_update_previous . '</p>';
  1287. }
  1288. if ($new_next != 0) {
  1289. // Update the next item's previous_item_id.
  1290. $sql_update_next = "
  1291. UPDATE ".$tbl_lp_item."
  1292. SET previous_item_id = ".$id."
  1293. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1294. $res_update_next = Database::query($sql_update_next);
  1295. //echo '<p>' . $sql_update_next . '</p>';
  1296. }
  1297. if ($old_prerequisite != $prerequisites) {
  1298. $sql_update_next = "
  1299. UPDATE ".$tbl_lp_item."
  1300. SET prerequisite = ".$prerequisites."
  1301. WHERE c_id = ".$course_id." AND id = ".$id;
  1302. $res_update_next = Database::query($sql_update_next);
  1303. }
  1304. if ($old_max_time_allowed != $max_time_allowed) {
  1305. $sql_update_max_time_allowed = "
  1306. UPDATE ".$tbl_lp_item."
  1307. SET max_time_allowed = ".$max_time_allowed."
  1308. WHERE c_id = ".$course_id." AND id = ".$id;
  1309. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1310. }
  1311. // Update all the items with the same or a bigger display_order than the current item.
  1312. $sql_update_order = "
  1313. UPDATE ".$tbl_lp_item."
  1314. SET display_order = display_order + 1
  1315. WHERE
  1316. c_id = ".$course_id." AND
  1317. lp_id = ".$this->get_id()." AND
  1318. id <> ".$id." AND
  1319. parent_item_id = ".$parent." AND
  1320. display_order >= ".$new_order;
  1321. $res_update_next = Database::query($sql_update_order);
  1322. }
  1323. }
  1324. /**
  1325. * Updates an item's prereq in place
  1326. * @param integer Element ID
  1327. * @param string Prerequisite Element ID
  1328. * @param string Prerequisite item type
  1329. * @param string Prerequisite min score
  1330. * @param string Prerequisite max score
  1331. * @return boolean True on success, false on error
  1332. */
  1333. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1334. {
  1335. $course_id = api_get_course_int_id();
  1336. if ($this->debug > 0) {
  1337. error_log(
  1338. 'New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')',
  1339. 0
  1340. );
  1341. }
  1342. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1343. return false;
  1344. }
  1345. $prerequisite_id = Database::escape_string($prerequisite_id);
  1346. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1347. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1348. $mastery_score = 0;
  1349. }
  1350. if (!is_numeric($max_score) || $max_score < 0) {
  1351. $max_score = 100;
  1352. }
  1353. if ($mastery_score > $max_score) {
  1354. $max_score = $mastery_score;
  1355. }
  1356. if (!is_numeric($prerequisite_id)) {
  1357. $prerequisite_id = 'NULL';
  1358. }
  1359. $sql_upd = " UPDATE ".$tbl_lp_item."
  1360. SET prerequisite = ".$prerequisite_id." WHERE c_id = ".$course_id." AND id = ".$id;
  1361. $res_upd = Database::query($sql_upd);
  1362. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1363. $sql_upd = " UPDATE ".$tbl_lp_item." SET
  1364. mastery_score = ".$mastery_score.
  1365. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1366. " WHERE c_id = ".$course_id." AND ref = '".$prerequisite_id."'"; // Will this be enough to ensure unicity?
  1367. Database::query($sql_upd);
  1368. }
  1369. // TODO: Update the item object (can be ignored for now because refreshed).
  1370. return true;
  1371. }
  1372. /**
  1373. * Escapes a string with the available database escape function
  1374. * @param string String to escape
  1375. * @return string String escaped
  1376. * @deprecated use Database::escape_string
  1377. */
  1378. public function escape_string($string)
  1379. {
  1380. return Database::escape_string($string);
  1381. }
  1382. /**
  1383. * Static admin function exporting a learnpath into a zip file
  1384. * @param string Export type (scorm, zip, cd)
  1385. * @param string Course code
  1386. * @param integer Learnpath ID
  1387. * @param string Zip file name
  1388. * @return string Zip file path (or false on error)
  1389. */
  1390. public function export_lp($type, $course, $id, $zipname)
  1391. {
  1392. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1393. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1394. return false;
  1395. }
  1396. $url = '';
  1397. switch ($type) {
  1398. case 'scorm':
  1399. break;
  1400. case 'zip':
  1401. break;
  1402. case 'cdrom':
  1403. break;
  1404. }
  1405. return $url;
  1406. }
  1407. /**
  1408. * Gets all the chapters belonging to the same parent as the item/chapter given
  1409. * Can also be called as abstract method
  1410. * @param integer Item ID
  1411. * @return array A list of all the "brother items" (or an empty array on failure)
  1412. */
  1413. public function get_brother_chapters($id)
  1414. {
  1415. $course_id = api_get_course_int_id();
  1416. if ($this->debug > 0) {
  1417. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1418. }
  1419. if (empty ($id) OR $id != strval(intval($id))) {
  1420. return array();
  1421. }
  1422. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1423. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1424. $res_parent = Database::query($sql_parent);
  1425. if (Database :: num_rows($res_parent) > 0) {
  1426. $row_parent = Database :: fetch_array($res_parent);
  1427. $parent = $row_parent['parent_item_id'];
  1428. $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";
  1429. $res_bros = Database::query($sql_bros);
  1430. $list = array();
  1431. while ($row_bro = Database :: fetch_array($res_bros)) {
  1432. $list[] = $row_bro;
  1433. }
  1434. return $list;
  1435. }
  1436. return array();
  1437. }
  1438. /**
  1439. * Gets all the items belonging to the same parent as the item given
  1440. * Can also be called as abstract method
  1441. * @param integer Item ID
  1442. * @return array A list of all the "brother items" (or an empty array on failure)
  1443. */
  1444. public function get_brother_items($id)
  1445. {
  1446. $course_id = api_get_course_int_id();
  1447. if ($this->debug > 0) {
  1448. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1449. }
  1450. if (empty ($id) OR $id != strval(intval($id))) {
  1451. return array();
  1452. }
  1453. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1454. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1455. $res_parent = Database::query($sql_parent);
  1456. if (Database :: num_rows($res_parent) > 0) {
  1457. $row_parent = Database :: fetch_array($res_parent);
  1458. $parent = $row_parent['parent_item_id'];
  1459. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1460. $res_bros = Database::query($sql_bros);
  1461. $list = array();
  1462. while ($row_bro = Database :: fetch_array($res_bros)) {
  1463. $list[] = $row_bro;
  1464. }
  1465. return $list;
  1466. }
  1467. return array();
  1468. }
  1469. /**
  1470. * Get the specific prefix index terms of this learning path
  1471. * @return array Array of terms
  1472. */
  1473. public function get_common_index_terms_by_prefix($prefix)
  1474. {
  1475. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1476. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1477. $prefix_terms = array();
  1478. if (!empty($terms)) {
  1479. foreach ($terms as $term) {
  1480. $prefix_terms[] = $term['value'];
  1481. }
  1482. }
  1483. return $prefix_terms;
  1484. }
  1485. /**
  1486. * Gets the number of items currently completed
  1487. * @return integer The number of items currently completed
  1488. */
  1489. public function get_complete_items_count()
  1490. {
  1491. if ($this->debug > 0) {
  1492. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1493. }
  1494. $i = 0;
  1495. foreach ($this->items as $id => $dummy) {
  1496. // Trying failed and browsed considered "progressed" as well.
  1497. if ($this->items[$id]->status_is(
  1498. array(
  1499. 'completed',
  1500. 'passed',
  1501. 'succeeded',
  1502. 'browsed',
  1503. 'failed'
  1504. )
  1505. ) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir'
  1506. ) {
  1507. $i++;
  1508. }
  1509. }
  1510. return $i;
  1511. }
  1512. /**
  1513. * Gets the current item ID
  1514. * @return integer The current learnpath item id
  1515. */
  1516. public function get_current_item_id()
  1517. {
  1518. $current = 0;
  1519. if ($this->debug > 0) {
  1520. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1521. }
  1522. if (!empty ($this->current)) {
  1523. $current = $this->current;
  1524. }
  1525. if ($this->debug > 2) {
  1526. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1527. }
  1528. return $current;
  1529. }
  1530. /**
  1531. * Force to get the first learnpath item id
  1532. * @return integer The current learnpath item id
  1533. */
  1534. public function get_first_item_id()
  1535. {
  1536. $current = 0;
  1537. if (is_array($this->ordered_items)) {
  1538. $current = $this->ordered_items[0];
  1539. }
  1540. return $current;
  1541. }
  1542. /**
  1543. * Gets the total number of items available for viewing in this SCORM
  1544. * @return integer The total number of items
  1545. */
  1546. public function get_total_items_count()
  1547. {
  1548. if ($this->debug > 0) {
  1549. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1550. }
  1551. return count($this->items);
  1552. }
  1553. /**
  1554. * Gets the total number of items available for viewing in this SCORM but without chapters
  1555. * @return integer The total no-chapters number of items
  1556. */
  1557. public function get_total_items_count_without_chapters()
  1558. {
  1559. if ($this->debug > 0) {
  1560. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1561. }
  1562. $total = 0;
  1563. foreach ($this->items as $temp2) {
  1564. if (!in_array(
  1565. $temp2->get_type(),
  1566. array(
  1567. 'dokeos_chapter',
  1568. 'chapter',
  1569. 'dir'
  1570. )
  1571. )
  1572. ) {
  1573. $total++;
  1574. }
  1575. }
  1576. return $total;
  1577. }
  1578. /**
  1579. * Gets the first element URL.
  1580. * @return string URL to load into the viewer
  1581. */
  1582. public function first()
  1583. {
  1584. if ($this->debug > 0) {
  1585. error_log('New LP - In learnpath::first()', 0);
  1586. error_log('$this->last_item_seen '.$this->last_item_seen);
  1587. //error_log('$this->items '.print_r($this->items, 1));
  1588. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1589. }
  1590. // Test if the last_item_seen exists and is not a dir.
  1591. if (count($this->ordered_items) == 0) {
  1592. $this->index = 0;
  1593. }
  1594. if ($this->debug > 0) {
  1595. if (isset($this->items[$this->last_item_seen])) {
  1596. $status = $this->items[$this->last_item_seen]->get_status();
  1597. }
  1598. error_log('status '.$status);
  1599. }
  1600. if (!empty($this->last_item_seen) &&
  1601. !empty($this->items[$this->last_item_seen]) &&
  1602. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1603. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1604. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1605. //&& !$this->items[$this->last_item_seen]->is_done()
  1606. ) {
  1607. if ($this->debug > 2) {
  1608. error_log(
  1609. 'New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(
  1610. ),
  1611. 0
  1612. );
  1613. }
  1614. $index = -1;
  1615. foreach ($this->ordered_items as $myindex => $item_id) {
  1616. if ($item_id == $this->last_item_seen) {
  1617. $index = $myindex;
  1618. break;
  1619. }
  1620. }
  1621. if ($index == -1) {
  1622. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1623. if ($this->debug > 2) {
  1624. error_log(
  1625. 'New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!',
  1626. 0
  1627. );
  1628. }
  1629. return false;
  1630. } else {
  1631. $this->last = $this->last_item_seen;
  1632. $this->current = $this->last_item_seen;
  1633. $this->index = $index;
  1634. }
  1635. } else {
  1636. if ($this->debug > 2) {
  1637. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1638. }
  1639. $index = 0;
  1640. // Loop through all ordered items and stop at the first item that is
  1641. // not a directory *and* that has not been completed yet.
  1642. while (!empty($this->ordered_items[$index]) AND
  1643. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1644. (
  1645. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1646. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1647. $this->items[$this->ordered_items[$index]]->is_done() === true
  1648. ) AND $index < $this->max_ordered_items) {
  1649. $index++;
  1650. }
  1651. $this->last = $this->current;
  1652. // current is
  1653. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1654. $this->index = $index;
  1655. if ($this->debug > 2) {
  1656. error_log('$index '.$index);
  1657. }
  1658. if ($this->debug > 2) {
  1659. error_log(
  1660. 'New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')',
  1661. 0
  1662. );
  1663. }
  1664. }
  1665. if ($this->debug > 2) {
  1666. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1667. }
  1668. }
  1669. /**
  1670. * Gets the information about an item in a format usable as JavaScript to update
  1671. * the JS API by just printing this content into the <head> section of the message frame
  1672. * @param integer Item ID
  1673. * @return string
  1674. */
  1675. public function get_js_info($item_id = '')
  1676. {
  1677. if ($this->debug > 0) {
  1678. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1679. }
  1680. $info = '';
  1681. $item_id = Database::escape_string($item_id);
  1682. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1683. //if item is defined, return values from DB
  1684. $oItem = $this->items[$item_id];
  1685. $info .= '<script language="javascript">';
  1686. $info .= "top.set_score(".$oItem->get_score().");\n";
  1687. $info .= "top.set_max(".$oItem->get_max().");\n";
  1688. $info .= "top.set_min(".$oItem->get_min().");\n";
  1689. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1690. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1691. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1692. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1693. $info .= "top.set_flag_synchronized();";
  1694. $info .= '</script>';
  1695. if ($this->debug > 2) {
  1696. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1697. }
  1698. return $info;
  1699. } else {
  1700. // If item_id is empty, just update to default SCORM data.
  1701. $info .= '<script language="javascript">';
  1702. $info .= "top.set_score(".learnpathItem :: get_score().");\n";
  1703. $info .= "top.set_max(".learnpathItem :: get_max().");\n";
  1704. $info .= "top.set_min(".learnpathItem :: get_min().");\n";
  1705. $info .= "top.set_lesson_status('".learnpathItem :: get_status()."');";
  1706. $info .= "top.set_session_time('".learnpathItem :: get_scorm_time('js')."');";
  1707. $info .= "top.set_suspend_data('".learnpathItem :: get_suspend_data()."');";
  1708. $info .= "top.set_saved_lesson_status('".learnpathItem :: get_status()."');";
  1709. $info .= "top.set_flag_synchronized();";
  1710. $info .= '</script>';
  1711. if ($this->debug > 2) {
  1712. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1713. }
  1714. return $info;
  1715. }
  1716. }
  1717. /**
  1718. * Gets the js library from the database
  1719. * @return string The name of the javascript library to be used
  1720. */
  1721. public function get_js_lib()
  1722. {
  1723. $lib = '';
  1724. if (!empty ($this->js_lib)) {
  1725. $lib = $this->js_lib;
  1726. }
  1727. return $lib;
  1728. }
  1729. /**
  1730. * Gets the learnpath database ID
  1731. * @return integer Learnpath ID in the lp table
  1732. */
  1733. public function get_id()
  1734. {
  1735. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1736. if (!empty ($this->lp_id)) {
  1737. return $this->lp_id;
  1738. } else {
  1739. return 0;
  1740. }
  1741. }
  1742. /**
  1743. * Gets the last element URL.
  1744. * @return string URL to load into the viewer
  1745. */
  1746. public function get_last()
  1747. {
  1748. if ($this->debug > 0) {
  1749. error_log('New LP - In learnpath::get_last()', 0);
  1750. }
  1751. $this->index = count($this->ordered_items) - 1;
  1752. return $this->ordered_items[$this->index];
  1753. }
  1754. /**
  1755. * Gets the navigation bar for the learnpath display screen
  1756. * @return string The HTML string to use as a navigation bar
  1757. */
  1758. public function get_navigation_bar()
  1759. {
  1760. if ($this->debug > 0) {
  1761. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1762. }
  1763. $navbar = null;
  1764. $lp_id = $this->lp_id;
  1765. $mycurrentitemid = $this->get_current_item_id();
  1766. if ($this->mode == 'fullscreen') {
  1767. $navbar = '
  1768. <div class="buttons">
  1769. <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">
  1770. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'</a>
  1771. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1772. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1773. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next">
  1774. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).' </a>
  1775. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1776. '.Display::return_icon('view_choose.gif', get_lang('ScormExitFullScreen')).'</a>
  1777. </div>';
  1778. } else {
  1779. $navbar = '
  1780. <div class="buttons">
  1781. <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">
  1782. '.Display::return_icon('lp_stats.gif', get_lang('Reporting')).'
  1783. </a>
  1784. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous">
  1785. '.Display::return_icon('lp_leftarrow.gif', get_lang('ScormPrevious')).'</a>
  1786. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" >
  1787. '.Display::return_icon('lp_rightarrow.gif', get_lang('ScormNext')).'
  1788. </a>
  1789. </div>';
  1790. }
  1791. return $navbar;
  1792. }
  1793. /**
  1794. * Gets the next resource in queue (url).
  1795. * @return string URL to load into the viewer
  1796. */
  1797. public function get_next_index()
  1798. {
  1799. if ($this->debug > 0) {
  1800. error_log('New LP - In learnpath::get_next_index()', 0);
  1801. }
  1802. // TODO
  1803. $index = $this->index;
  1804. $index++;
  1805. if ($this->debug > 2) {
  1806. error_log(
  1807. 'New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type,
  1808. 0
  1809. );
  1810. }
  1811. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1812. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type(
  1813. ) == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1814. $index++;
  1815. if ($index == $this->max_ordered_items) {
  1816. if ($this->items[$this->ordered_items[$index]]->get_type(
  1817. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter'
  1818. ) {
  1819. return $this->index;
  1820. } else {
  1821. return $index;
  1822. }
  1823. }
  1824. }
  1825. if (empty ($this->ordered_items[$index])) {
  1826. return $this->index;
  1827. }
  1828. if ($this->debug > 2) {
  1829. error_log('New LP - index is now '.$index, 0);
  1830. }
  1831. return $index;
  1832. }
  1833. /**
  1834. * Gets item_id for the next element
  1835. * @return integer Next item (DB) ID
  1836. */
  1837. public function get_next_item_id()
  1838. {
  1839. if ($this->debug > 0) {
  1840. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1841. }
  1842. $new_index = $this->get_next_index();
  1843. if (!empty ($new_index)) {
  1844. if (isset ($this->ordered_items[$new_index])) {
  1845. if ($this->debug > 2) {
  1846. error_log(
  1847. 'New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index],
  1848. 0
  1849. );
  1850. }
  1851. return $this->ordered_items[$new_index];
  1852. }
  1853. }
  1854. if ($this->debug > 2) {
  1855. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1856. }
  1857. return 0;
  1858. }
  1859. /**
  1860. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1861. *
  1862. * Generally, the package provided is in the form of a zip file, so the function
  1863. * has been written to test a zip file. If not a zip, the function will return the
  1864. * default return value: ''
  1865. * @param string the path to the file
  1866. * @param string the original name of the file
  1867. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1868. */
  1869. public static function get_package_type($file_path, $file_name)
  1870. {
  1871. // Get name of the zip file without the extension.
  1872. $file_info = pathinfo($file_name);
  1873. $filename = $file_info['basename']; // Name including extension.
  1874. $extension = $file_info['extension']; // Extension only.
  1875. if (!empty($_POST['ppt2lp']) && !in_array(
  1876. strtolower($extension),
  1877. array(
  1878. 'dll',
  1879. 'exe'
  1880. )
  1881. )
  1882. ) {
  1883. return 'oogie';
  1884. }
  1885. if (!empty($_POST['woogie']) && !in_array(
  1886. strtolower($extension),
  1887. array(
  1888. 'dll',
  1889. 'exe'
  1890. )
  1891. )
  1892. ) {
  1893. return 'woogie';
  1894. }
  1895. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  1896. $zipFile = new PclZip($file_path);
  1897. // Check the zip content (real size and file extension).
  1898. $zipContentArray = $zipFile->listContent();
  1899. $package_type = '';
  1900. $at_root = false;
  1901. $manifest = '';
  1902. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1903. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1904. foreach ($zipContentArray as $thisContent) {
  1905. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1906. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1907. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1908. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1909. $package_type = 'scorm';
  1910. break; // Exit the foreach loop.
  1911. } elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1912. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1913. $package_type = 'aicc';
  1914. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1915. } else {
  1916. $package_type = '';
  1917. }
  1918. }
  1919. }
  1920. return $package_type;
  1921. }
  1922. /**
  1923. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1924. * @return string URL to load into the viewer
  1925. */
  1926. public function get_previous_index()
  1927. {
  1928. if ($this->debug > 0) {
  1929. error_log('New LP - In learnpath::get_previous_index()', 0);
  1930. }
  1931. $index = $this->index;
  1932. if (isset ($this->ordered_items[$index - 1])) {
  1933. $index--;
  1934. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1935. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1936. $index--;
  1937. if ($index < 0) {
  1938. return $this->index;
  1939. }
  1940. }
  1941. } else {
  1942. if ($this->debug > 2) {
  1943. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1944. }
  1945. // There is no previous item.
  1946. }
  1947. return $index;
  1948. }
  1949. /**
  1950. * Gets item_id for the next element
  1951. * @return integer Previous item (DB) ID
  1952. */
  1953. public function get_previous_item_id()
  1954. {
  1955. if ($this->debug > 0) {
  1956. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1957. }
  1958. $new_index = $this->get_previous_index();
  1959. return $this->ordered_items[$new_index];
  1960. }
  1961. /**
  1962. * Gets the progress value from the progress_db attribute
  1963. * @return integer Current progress value
  1964. */
  1965. public function get_progress()
  1966. {
  1967. if ($this->debug > 0) {
  1968. error_log('New LP - In learnpath::get_progress()', 0);
  1969. }
  1970. if (!empty ($this->progress_db)) {
  1971. return $this->progress_db;
  1972. }
  1973. return 0;
  1974. }
  1975. /**
  1976. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1977. * @param integer Learnpath ID
  1978. * @param integer User ID
  1979. * @param string Mode of display ('%','abs' or 'both')
  1980. * @param string Course database name (optional, defaults to '')
  1981. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1982. * @return integer Current progress value as found in the database
  1983. */
  1984. public static function get_db_progress(
  1985. $lp_id,
  1986. $user_id,
  1987. $mode = '%',
  1988. $course_code = '',
  1989. $sincere = false,
  1990. $session_id = 0
  1991. ) {
  1992. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  1993. $session_id = intval($session_id);
  1994. $course_info = api_get_course_info($course_code);
  1995. $session_condition = api_get_session_condition($session_id);
  1996. $course_id = $course_info['real_id'];
  1997. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1998. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1999. $res = Database::query($sql);
  2000. $view_id = 0;
  2001. if (Database :: num_rows($res) > 0) {
  2002. $row = Database :: fetch_array($res);
  2003. $progress = $row['progress'];
  2004. $view_id = $row['id'];
  2005. } else {
  2006. if ($sincere) {
  2007. return null;
  2008. }
  2009. }
  2010. if (empty ($progress)) {
  2011. $progress = '0';
  2012. }
  2013. if ($mode == '%') {
  2014. return $progress.'%';
  2015. } else {
  2016. // Get the number of items completed and the number of items total.
  2017. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2018. $sql = "SELECT count(*) FROM $tbl
  2019. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = ".$lp_id." AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  2020. $res = Database::query($sql);
  2021. $row = Database :: fetch_array($res);
  2022. $total = $row[0];
  2023. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2024. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2025. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  2026. // Trying as also counting browsed and failed items.
  2027. $sql = "SELECT count(distinct(lp_item_id))
  2028. FROM $tbl_item_view as item_view
  2029. INNER JOIN $tbl_item as item
  2030. ON item.id = item_view.lp_item_id
  2031. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  2032. WHERE
  2033. item_view.c_id = $course_id AND
  2034. item.c_id = $course_id AND
  2035. lp_view_id = ".$view_id." AND
  2036. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  2037. $res = Database::query($sql);
  2038. $row = Database :: fetch_array($res);
  2039. $completed = $row[0];
  2040. if ($mode == 'abs') {
  2041. return $completed.'/'.$total;
  2042. } elseif ($mode == 'both') {
  2043. if ($progress < ($completed / ($total ? $total : 1))) {
  2044. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2045. }
  2046. return $progress.'% ('.$completed.'/'.$total.')';
  2047. }
  2048. }
  2049. return $progress;
  2050. }
  2051. /**
  2052. * Returns the HTML necessary to print a mediaplayer block inside a page
  2053. * @return string The mediaplayer HTML
  2054. */
  2055. public function get_mediaplayer($autostart = 'true')
  2056. {
  2057. $course_id = api_get_course_int_id();
  2058. $_course = api_get_course_info();
  2059. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2060. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2061. // Getting all the information about the item.
  2062. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2063. INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id
  2064. WHERE lp.id = '".$_SESSION['oLP']->current."' AND
  2065. lp.c_id = $course_id AND
  2066. lp_view.c_id = $course_id";
  2067. $result = Database::query($sql);
  2068. $row = Database::fetch_assoc($result);
  2069. $output = '';
  2070. if (!empty ($row['audio'])) {
  2071. $list = $_SESSION['oLP']->get_toc();
  2072. $type_quiz = false;
  2073. foreach ($list as $toc) {
  2074. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2075. $type_quiz = true;
  2076. }
  2077. }
  2078. if ($type_quiz) {
  2079. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2080. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2081. } else {
  2082. $autostart_audio = $autostart;
  2083. }
  2084. } else {
  2085. $autostart_audio = 'true';
  2086. }
  2087. $courseInfo = api_get_course_info();
  2088. $audio = $row['audio'];
  2089. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2090. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2091. if (!file_exists($file)) {
  2092. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2093. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2094. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2095. }
  2096. $player = Display::getMediaPlayer(
  2097. $file,
  2098. array(
  2099. 'id' => 'lp_audio_media_player',
  2100. 'url' => $url,
  2101. 'autoplay' => $autostart_audio,
  2102. 'width' => '100%'
  2103. )
  2104. );
  2105. // The mp3 player.
  2106. $output = '<div id="container">';
  2107. $output .= $player;
  2108. $output .= '</div>';
  2109. }
  2110. return $output;
  2111. }
  2112. /**
  2113. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  2114. * @param int Learnpath id
  2115. * @param int Student id
  2116. * @param string Course code (optional)
  2117. * @return bool True if
  2118. */
  2119. public static function is_lp_visible_for_student($lp_id, $student_id, $course_code = null)
  2120. {
  2121. $lp_id = (int)$lp_id;
  2122. $course_info = api_get_course_info($course_code);
  2123. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  2124. // Get current prerequisite
  2125. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2126. FROM $tbl_learnpath WHERE c_id = ".$course_info['real_id']." AND id = $lp_id";
  2127. $rs = Database::query($sql);
  2128. $now = time();
  2129. $session_id = api_get_session_id();
  2130. if (Database::num_rows($rs) > 0) {
  2131. $row = Database::fetch_array($rs, 'ASSOC');
  2132. $prerequisite = $row['prerequisite'];
  2133. $is_visible = true;
  2134. $progress = 0;
  2135. //Checking LP prerequisites
  2136. if (!empty($prerequisite)) {
  2137. $progress = intval(self::get_db_progress($prerequisite, $student_id, '%', '', false, $session_id));
  2138. if ($progress < 100) {
  2139. $is_visible = false;
  2140. }
  2141. }
  2142. // Also check the time availability of the LP
  2143. if ($is_visible) {
  2144. //Adding visibility restrictions
  2145. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  2146. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2147. //api_not_allowed();
  2148. $is_visible = false;
  2149. }
  2150. }
  2151. //Blocking empty start times see BT#2800
  2152. global $_custom;
  2153. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  2154. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2155. //api_not_allowed();
  2156. $is_visible = false;
  2157. }
  2158. }
  2159. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2160. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2161. //api_not_allowed();
  2162. $is_visible = false;
  2163. }
  2164. }
  2165. }
  2166. //Check if the subscription users/group to a LP is ON
  2167. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2168. //Checking only the user visibility
  2169. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2170. if ($userVisibility == 1) {
  2171. $is_visible = true;
  2172. } else {
  2173. $is_visible = false;
  2174. }
  2175. //the group visibility is not going to be checked because the user *must* be related to the LP
  2176. /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2177. if (!empty($groupList)) {
  2178. foreach($groupList as $groupId) {
  2179. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2180. if ($groupVisibility == 1) {
  2181. $is_visible = true;
  2182. break;
  2183. }
  2184. }
  2185. }*/
  2186. }
  2187. return $is_visible;
  2188. }
  2189. return false;
  2190. }
  2191. /**
  2192. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2193. * completed so far.
  2194. * @param string Mode in which we want the values
  2195. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2196. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2197. * @param boolean true if it comes from a Diplay LP view
  2198. * @return string HTML string containing the progress bar
  2199. */
  2200. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2201. {
  2202. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2203. global $lp_theme_css;
  2204. // Setting up the CSS path of the current style if exists.
  2205. if (!empty ($lp_theme_css)) {
  2206. //$css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2207. } else {
  2208. //$css_path = '../img/';
  2209. }
  2210. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2211. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2212. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2213. }
  2214. $text = $percentage.$text_add;
  2215. //@todo use Display::display_progress();
  2216. $output = '<div class="progress progress-striped">
  2217. <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="'.$text.'" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.'%">
  2218. <span class="sr-only">'.$text.'% Complete</span>
  2219. </div>
  2220. </div>
  2221. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2222. return $output;
  2223. }
  2224. /**
  2225. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2226. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2227. * @param integer Additional steps to fake as completed
  2228. * @return list Percentage or number and symbol (% or /xx)
  2229. */
  2230. public function get_progress_bar_text($mode = '', $add = 0)
  2231. {
  2232. if ($this->debug > 0) {
  2233. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2234. }
  2235. if (empty ($mode)) {
  2236. $mode = $this->progress_bar_mode;
  2237. }
  2238. $total_items = $this->get_total_items_count_without_chapters();
  2239. if ($this->debug > 2) {
  2240. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2241. }
  2242. $i = $this->get_complete_items_count();
  2243. if ($this->debug > 2) {
  2244. error_log('New LP - Items completed so far: '.$i, 0);
  2245. }
  2246. if ($add != 0) {
  2247. $i += $add;
  2248. if ($this->debug > 2) {
  2249. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2250. }
  2251. }
  2252. $text = '';
  2253. if ($i > $total_items) {
  2254. $i = $total_items;
  2255. }
  2256. if ($mode == '%') {
  2257. if ($total_items > 0) {
  2258. $percentage = ((float)$i / (float)$total_items) * 100;
  2259. } else {
  2260. $percentage = 0;
  2261. }
  2262. $percentage = number_format($percentage, 0);
  2263. $text = '%';
  2264. } elseif ($mode == 'abs') {
  2265. $percentage = $i;
  2266. $text = '/'.$total_items;
  2267. }
  2268. return array(
  2269. $percentage,
  2270. $text
  2271. );
  2272. }
  2273. /**
  2274. * Gets the progress bar mode
  2275. * @return string The progress bar mode attribute
  2276. */
  2277. public function get_progress_bar_mode()
  2278. {
  2279. if ($this->debug > 0) {
  2280. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2281. }
  2282. if (!empty ($this->progress_bar_mode)) {
  2283. return $this->progress_bar_mode;
  2284. } else {
  2285. return '%';
  2286. }
  2287. }
  2288. /**
  2289. * Gets the learnpath proximity (remote or local)
  2290. * @return string Learnpath proximity
  2291. */
  2292. public function get_proximity()
  2293. {
  2294. if ($this->debug > 0) {
  2295. error_log('New LP - In learnpath::get_proximity()', 0);
  2296. }
  2297. if (!empty ($this->proximity)) {
  2298. return $this->proximity;
  2299. } else {
  2300. return '';
  2301. }
  2302. }
  2303. /**
  2304. * Gets the learnpath theme (remote or local)
  2305. * @return string Learnpath theme
  2306. */
  2307. public function get_theme()
  2308. {
  2309. if ($this->debug > 0) {
  2310. error_log('New LP - In learnpath::get_theme()', 0);
  2311. }
  2312. if (!empty ($this->theme)) {
  2313. return $this->theme;
  2314. } else {
  2315. return '';
  2316. }
  2317. }
  2318. /**
  2319. * Gets the learnpath session id
  2320. * @return string Learnpath theme
  2321. */
  2322. public function get_lp_session_id()
  2323. {
  2324. if ($this->debug > 0) {
  2325. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2326. }
  2327. if (!empty ($this->lp_session_id)) {
  2328. return $this->lp_session_id;
  2329. } else {
  2330. return 0;
  2331. }
  2332. }
  2333. /**
  2334. * Gets the learnpath image
  2335. * @return string Web URL of the LP image
  2336. */
  2337. public function get_preview_image()
  2338. {
  2339. if ($this->debug > 0) {
  2340. error_log('New LP - In learnpath::get_preview_image()', 0);
  2341. }
  2342. if (!empty($this->preview_image)) {
  2343. return $this->preview_image;
  2344. } else {
  2345. return '';
  2346. }
  2347. }
  2348. /**
  2349. * @param string $size
  2350. * @param string $path_type
  2351. * @return bool|string
  2352. */
  2353. public function get_preview_image_path($size = null, $path_type = 'web')
  2354. {
  2355. $preview_image = $this->get_preview_image();
  2356. if (isset($preview_image) && !empty($preview_image)) {
  2357. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2358. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2359. if (isset($size)) {
  2360. $info = pathinfo($preview_image);
  2361. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2362. if (file_exists($image_sys_path.$image_custom_size)) {
  2363. if ($path_type == 'web') {
  2364. return $image_path.$image_custom_size;
  2365. } else {
  2366. return $image_sys_path.$image_custom_size;
  2367. }
  2368. }
  2369. } else {
  2370. if ($path_type == 'web') {
  2371. return $image_path.$preview_image;
  2372. } else {
  2373. return $image_sys_path.$preview_image;
  2374. }
  2375. }
  2376. }
  2377. return false;
  2378. }
  2379. public function get_category_id()
  2380. {
  2381. return $this->category_id;
  2382. }
  2383. /**
  2384. * Gets the learnpath author
  2385. * @return string LP's author
  2386. */
  2387. public function get_author()
  2388. {
  2389. if ($this->debug > 0) {
  2390. error_log('New LP - In learnpath::get_author()', 0);
  2391. }
  2392. if (!empty ($this->author)) {
  2393. return $this->author;
  2394. } else {
  2395. return '';
  2396. }
  2397. }
  2398. /**
  2399. * Gets the learnpath author
  2400. * @return string LP's author
  2401. */
  2402. public function get_hide_toc_frame()
  2403. {
  2404. if ($this->debug > 0) {
  2405. error_log('New LP - In learnpath::get_author()', 0);
  2406. }
  2407. if (!empty ($this->hide_toc_frame)) {
  2408. return $this->hide_toc_frame;
  2409. } else {
  2410. return '';
  2411. }
  2412. }
  2413. /**
  2414. * Generate a new prerequisites string for a given item. If this item was a sco and
  2415. * its prerequisites were strings (instead of IDs), then transform those strings into
  2416. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2417. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2418. * same rule as the scorm_export() method
  2419. * @param integer Item ID
  2420. * @return string Prerequisites string ready for the export as SCORM
  2421. */
  2422. public function get_scorm_prereq_string($item_id)
  2423. {
  2424. if ($this->debug > 0) {
  2425. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2426. }
  2427. if (!is_object($this->items[$item_id])) {
  2428. return false;
  2429. }
  2430. $oItem = $this->items[$item_id];
  2431. $prereq = $oItem->get_prereq_string();
  2432. if (empty($prereq)) {
  2433. return '';
  2434. }
  2435. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2436. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2437. // then simply return it (with the ITEM_ prefix).
  2438. //return 'ITEM_' . $prereq;
  2439. return $this->items[$prereq]->ref;
  2440. } else {
  2441. if (isset($this->refs_list[$prereq])) {
  2442. // It's a simple string item from which the ID can be found in the refs list,
  2443. // so we can transform it directly to an ID for export.
  2444. return $this->items[$this->refs_list[$prereq]]->ref;
  2445. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2446. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2447. } else {
  2448. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2449. // and replace them, one by one, by the internal IDs (chamilo db)
  2450. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2451. // by a space as well.
  2452. $find = array(
  2453. '&',
  2454. '|',
  2455. '~',
  2456. '=',
  2457. '<>',
  2458. '{',
  2459. '}',
  2460. '*',
  2461. '(',
  2462. ')'
  2463. );
  2464. $replace = array(
  2465. ' ',
  2466. ' ',
  2467. ' ',
  2468. ' ',
  2469. ' ',
  2470. ' ',
  2471. ' ',
  2472. ' ',
  2473. ' ',
  2474. ' '
  2475. );
  2476. $prereq_mod = str_replace($find, $replace, $prereq);
  2477. $ids = split(' ', $prereq_mod);
  2478. foreach ($ids as $id) {
  2479. $id = trim($id);
  2480. if (isset ($this->refs_list[$id])) {
  2481. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2482. }
  2483. }
  2484. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2485. return $prereq;
  2486. }
  2487. }
  2488. }
  2489. /**
  2490. * Returns the XML DOM document's node
  2491. * @param resource Reference to a list of objects to search for the given ITEM_*
  2492. * @param string The identifier to look for
  2493. * @return mixed The reference to the element found with that identifier. False if not found
  2494. */
  2495. public function get_scorm_xml_node(& $children, $id)
  2496. {
  2497. for ($i = 0; $i < $children->length; $i++) {
  2498. $item_temp = $children->item($i);
  2499. if ($item_temp->nodeName == 'item') {
  2500. if ($item_temp->getAttribute('identifier') == $id) {
  2501. return $item_temp;
  2502. }
  2503. }
  2504. $subchildren = $item_temp->childNodes;
  2505. if ($subchildren->length > 0) {
  2506. $val = $this->get_scorm_xml_node($subchildren, $id);
  2507. if (is_object($val)) {
  2508. return $val;
  2509. }
  2510. }
  2511. }
  2512. return false;
  2513. }
  2514. /**
  2515. * Returns a usable array of stats related to the current learnpath and user
  2516. * @return array Well-formatted array containing status for the current learnpath
  2517. */
  2518. public function get_stats()
  2519. {
  2520. if ($this->debug > 0) {
  2521. error_log('New LP - In learnpath::get_stats()', 0);
  2522. }
  2523. // TODO
  2524. }
  2525. /**
  2526. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2527. * the given course (for all learnpaths and all users)
  2528. * @param string Course code
  2529. * @return array Well-formatted array containing status for the course's learnpaths
  2530. */
  2531. public function get_stats_course($course)
  2532. {
  2533. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2534. // TODO
  2535. }
  2536. /**
  2537. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2538. * the given course and learnpath (for all users)
  2539. * @param string Course code
  2540. * @param integer Learnpath ID
  2541. * @return array Well-formatted array containing status for the specified learnpath
  2542. */
  2543. public function get_stats_lp($course, $lp)
  2544. {
  2545. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2546. // TODO
  2547. }
  2548. /**
  2549. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2550. * the given course, learnpath and user.
  2551. * @param string Course code
  2552. * @param integer Learnpath ID
  2553. * @param integer User ID
  2554. * @return array Well-formatted array containing status for the specified learnpath and user
  2555. */
  2556. public function get_stats_lp_user($course, $lp, $user)
  2557. {
  2558. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2559. // TODO
  2560. }
  2561. /**
  2562. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2563. * the given course and learnpath (for all users)
  2564. * @param string Course code
  2565. * @param integer User ID
  2566. * @return array Well-formatted array containing status for the user's learnpaths
  2567. */
  2568. public function get_stats_user($course, $user)
  2569. {
  2570. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2571. // TODO
  2572. }
  2573. /**
  2574. * Gets the status list for all LP's items
  2575. * @return array Array of [index] => [item ID => current status]
  2576. */
  2577. public function get_items_status_list()
  2578. {
  2579. if ($this->debug > 0) {
  2580. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2581. }
  2582. $list = array();
  2583. foreach ($this->ordered_items as $item_id) {
  2584. $list[] = array(
  2585. $item_id => $this->items[$item_id]->get_status()
  2586. );
  2587. }
  2588. return $list;
  2589. }
  2590. /**
  2591. * Return the number of interactions for the given learnpath Item View ID.
  2592. * This method can be used as static.
  2593. * @param integer Item View ID
  2594. * @param integer course id
  2595. * @return integer Number of interactions
  2596. */
  2597. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2598. {
  2599. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2600. $lp_iv_id = intval($lp_iv_id);
  2601. $course_id = intval($course_id);
  2602. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2603. $res = Database::query($sql);
  2604. $num = 0;
  2605. if (Database::num_rows($res)) {
  2606. $row = Database::fetch_array($res);
  2607. $num = $row[0];
  2608. }
  2609. return $num;
  2610. }
  2611. /**
  2612. * Return the interactions as an array for the given lp_iv_id.
  2613. * This method can be used as static.
  2614. * @param integer Learnpath Item View ID
  2615. * @return array
  2616. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2617. */
  2618. public static function get_iv_interactions_array($lp_iv_id = 0)
  2619. {
  2620. $course_id = api_get_course_int_id();
  2621. $list = array();
  2622. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2623. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2624. $res = Database::query($sql);
  2625. $num = Database :: num_rows($res);
  2626. if ($num > 0) {
  2627. $list[] = array(
  2628. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2629. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2630. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2631. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2632. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2633. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2634. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2635. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2636. );
  2637. while ($row = Database :: fetch_array($res)) {
  2638. $list[] = array(
  2639. 'order_id' => ($row['order_id'] + 1),
  2640. 'id' => urldecode($row['interaction_id']),
  2641. //urldecode because they often have %2F or stuff like that
  2642. 'type' => $row['interaction_type'],
  2643. 'time' => $row['completion_time'],
  2644. //'correct_responses' => $row['correct_responses'],
  2645. 'correct_responses' => '',
  2646. // Hide correct responses from students.
  2647. 'student_response' => $row['student_response'],
  2648. 'result' => $row['result'],
  2649. 'latency' => $row['latency']
  2650. );
  2651. }
  2652. }
  2653. return $list;
  2654. }
  2655. /**
  2656. * Return the number of objectives for the given learnpath Item View ID.
  2657. * This method can be used as static.
  2658. * @param integer Item View ID
  2659. * @return integer Number of objectives
  2660. */
  2661. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2662. {
  2663. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2664. $course_id = intval($course_id);
  2665. $lp_iv_id = intval($lp_iv_id);
  2666. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2667. //@todo seems that this always returns 0
  2668. $res = Database::query($sql);
  2669. $num = 0;
  2670. if (Database::num_rows($res)) {
  2671. $row = Database :: fetch_array($res);
  2672. $num = $row[0];
  2673. }
  2674. return $num;
  2675. }
  2676. /**
  2677. * Return the objectives as an array for the given lp_iv_id.
  2678. * This method can be used as static.
  2679. * @param integer Learnpath Item View ID
  2680. * @return array
  2681. * @todo Translate labels
  2682. */
  2683. public function get_iv_objectives_array($lp_iv_id = 0)
  2684. {
  2685. $course_id = api_get_course_int_id();
  2686. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2687. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2688. $res = Database::query($sql);
  2689. $num = Database :: num_rows($res);
  2690. $list = array();
  2691. if ($num > 0) {
  2692. $list[] = array(
  2693. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2694. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2695. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2696. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2697. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2698. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2699. );
  2700. while ($row = Database :: fetch_array($res)) {
  2701. $list[] = array(
  2702. 'order_id' => ($row['order_id'] + 1),
  2703. 'objective_id' => urldecode($row['objective_id']),
  2704. // urldecode() because they often have %2F or stuff like that.
  2705. 'score_raw' => $row['score_raw'],
  2706. 'score_max' => $row['score_max'],
  2707. 'score_min' => $row['score_min'],
  2708. 'status' => $row['status']
  2709. );
  2710. }
  2711. }
  2712. return $list;
  2713. }
  2714. /**
  2715. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2716. * used by get_html_toc() to be ready to display
  2717. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2718. */
  2719. public function get_toc()
  2720. {
  2721. if ($this->debug > 0) {
  2722. error_log('learnpath::get_toc()', 0);
  2723. }
  2724. $toc = array();
  2725. //echo "<pre>".print_r($this->items,true)."</pre>";
  2726. foreach ($this->ordered_items as $item_id) {
  2727. if ($this->debug > 2) {
  2728. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2729. }
  2730. // TODO: Change this link generation and use new function instead.
  2731. $toc[] = array(
  2732. 'id' => $item_id,
  2733. 'title' => $this->items[$item_id]->get_title(),
  2734. 'status' => $this->items[$item_id]->get_status(),
  2735. 'level' => $this->items[$item_id]->get_level(),
  2736. 'type' => $this->items[$item_id]->get_type(),
  2737. 'description' => $this->items[$item_id]->get_description(),
  2738. 'path' => $this->items[$item_id]->get_path(),
  2739. );
  2740. }
  2741. if ($this->debug > 2) {
  2742. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2743. }
  2744. return $toc;
  2745. }
  2746. /**
  2747. * Generate and return the table of contents for this learnpath. The JS
  2748. * table returned is used inside of scorm_api.php
  2749. * @return string A JS array vairiable construction
  2750. */
  2751. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2752. {
  2753. if ($this->debug > 0) {
  2754. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2755. }
  2756. $toc = $varname.' = new Array();';
  2757. //echo "<pre>".print_r($this->items,true)."</pre>";
  2758. foreach ($this->ordered_items as $item_id) {
  2759. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2760. }
  2761. if ($this->debug > 2) {
  2762. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2763. }
  2764. return $toc;
  2765. }
  2766. /**
  2767. * Gets the learning path type
  2768. * @param boolean Return the name? If false, return the ID. Default is false.
  2769. * @return mixed Type ID or name, depending on the parameter
  2770. */
  2771. public function get_type($get_name = false)
  2772. {
  2773. $res = false;
  2774. if ($this->debug > 0) {
  2775. error_log('New LP - In learnpath::get_type()', 0);
  2776. }
  2777. if (!empty ($this->type)) {
  2778. if ($get_name) {
  2779. // Get it from the lp_type table in main db.
  2780. } else {
  2781. $res = $this->type;
  2782. }
  2783. }
  2784. if ($this->debug > 2) {
  2785. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2786. }
  2787. return $res;
  2788. }
  2789. /**
  2790. * Gets the learning path type as static method
  2791. * @param boolean Return the name? If false, return the ID. Default is false.
  2792. * @return mixed Type ID or name, depending on the parameter
  2793. */
  2794. public static function get_type_static($lp_id = 0)
  2795. {
  2796. $course_id = api_get_course_int_id();
  2797. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2798. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2799. $res = Database::query($sql);
  2800. if ($res === false) {
  2801. return null;
  2802. }
  2803. if (Database :: num_rows($res) <= 0) {
  2804. return null;
  2805. }
  2806. $row = Database :: fetch_array($res);
  2807. return $row['lp_type'];
  2808. }
  2809. /**
  2810. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2811. * This method can be used as abstract and is recursive
  2812. * @param integer Learnpath ID
  2813. * @param integer Parent ID of the items to look for
  2814. * @return mixed Ordered list of item IDs or false on error
  2815. */
  2816. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2817. {
  2818. if (empty($course_id)) {
  2819. $course_id = api_get_course_int_id();
  2820. } else {
  2821. $course_id = intval($course_id);
  2822. }
  2823. $list = array();
  2824. if (empty ($lp)) {
  2825. return false;
  2826. }
  2827. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2828. $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";
  2829. $res = Database::query($sql);
  2830. while ($row = Database :: fetch_array($res)) {
  2831. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2832. $list[] = $row['id'];
  2833. foreach ($sublist as $item) {
  2834. $list[] = $item;
  2835. }
  2836. }
  2837. return $list;
  2838. }
  2839. /**
  2840. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2841. * @param array
  2842. * @return string HTML TOC ready to display
  2843. */
  2844. public function get_html_toc($toc_list = null)
  2845. {
  2846. global $_configuration;
  2847. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2848. if ($this->debug > 0) {
  2849. error_log('In learnpath::get_html_toc()', 0);
  2850. }
  2851. if (empty($toc_list)) {
  2852. $toc_list = $this->get_toc();
  2853. }
  2854. $html = '';
  2855. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2856. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2857. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2858. if ($this->get_lp_session_id() == api_get_session_id()) {
  2859. $html .= '<div id="actions_lp" class="actions_lp">';
  2860. $html .= '<div class="btn-group">';
  2861. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2862. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2863. 'Overview'
  2864. )."</a>";
  2865. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2866. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2867. 'Edit'
  2868. )."</a>";
  2869. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2870. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2871. $html .= '</div>';
  2872. $html .= '</div>';
  2873. }
  2874. }
  2875. require_once 'resourcelinker.inc.php';
  2876. // Temporary variables.
  2877. $mycurrentitemid = $this->get_current_item_id();
  2878. $color_counter = 0;
  2879. $i = 0;
  2880. foreach ($toc_list as $item) {
  2881. // TODO: Complete this
  2882. $icon_name = array(
  2883. 'not attempted' => 'notattempted.gif',
  2884. 'incomplete' => 'incomplete.png',
  2885. 'failed' => 'delete.png',
  2886. 'completed' => 'completed.png',
  2887. 'passed' => 'passed.png',
  2888. 'succeeded' => 'succeeded.png',
  2889. 'browsed' => 'completed.png',
  2890. );
  2891. $style_item = 'scorm_item';
  2892. $active = null;
  2893. if ($item['id'] == $this->current) {
  2894. $scorm_color_background = 'scorm_item_highlight';
  2895. $active = ' active';
  2896. } else {
  2897. if ($color_counter % 2 == 0) {
  2898. $scorm_color_background = 'scorm_item_1';
  2899. } else {
  2900. $scorm_color_background = 'scorm_item_2';
  2901. }
  2902. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2903. $scorm_color_background = ' scorm_item_section ';
  2904. }
  2905. }
  2906. $onClick = null;
  2907. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2908. $onClick = 'onclick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;"';
  2909. }
  2910. $html .= '<a href="" '.$onClick.' class="list-group-item">';
  2911. $imageStatus = null;
  2912. if ($item['type'] == 'quiz') {
  2913. if ($item['status'] == 'completed') {
  2914. $imageStatus = "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2915. } else {
  2916. $imageStatus = "&nbsp;".Display::return_icon($icon_name['not attempted'], substr('not attempted', 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2917. }
  2918. } else {
  2919. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2920. $imageStatus = "&nbsp;".Display::return_icon($icon_name[$item['status']], substr($item['status'], 0, 1), array('id' => "toc_img_".$item['id'], 'width' => '14'));
  2921. }
  2922. }
  2923. $html .= '<span class="badge_lp">'.$imageStatus."</span>";
  2924. if ($scorm_color_background != '') {
  2925. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2926. }
  2927. // The anchor will let us center the TOC on the currently viewed item &^D
  2928. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2929. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2930. //$html .= '<a name="atoc_'.$item['id'].'"></a>';
  2931. } else {
  2932. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2933. }
  2934. $title = $item['title'];
  2935. if (empty($title)) {
  2936. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2937. }
  2938. $title = Security::remove_XSS($title);
  2939. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2940. //$html .= '<a href="" >'.stripslashes($title).'</a>';
  2941. $html .= $title;
  2942. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2943. $html .= Display::return_icon('lp_dokeos_module.png', $title).'&nbsp;'.$title;
  2944. } elseif ($item['type'] == 'dir') {
  2945. $html .= $title;
  2946. }
  2947. $html .= "</div>";
  2948. if ($scorm_color_background != '') {
  2949. $html .= '</div>';
  2950. }
  2951. $html .= '</a>';
  2952. $color_counter++;
  2953. }
  2954. return $html;
  2955. }
  2956. /**
  2957. * Gets the learnpath maker name - generally the editor's name
  2958. * @return string Learnpath maker name
  2959. */
  2960. public function get_maker()
  2961. {
  2962. if ($this->debug > 0) {
  2963. error_log('New LP - In learnpath::get_maker()', 0);
  2964. }
  2965. if (!empty ($this->maker)) {
  2966. return $this->maker;
  2967. } else {
  2968. return '';
  2969. }
  2970. }
  2971. /**
  2972. * Gets the user-friendly message stored in $this->message
  2973. * @return string Message
  2974. */
  2975. public function get_message()
  2976. {
  2977. if ($this->debug > 0) {
  2978. error_log('New LP - In learnpath::get_message()', 0);
  2979. }
  2980. return $this->message;
  2981. }
  2982. /**
  2983. * Gets the learnpath name/title
  2984. * @return string Learnpath name/title
  2985. */
  2986. public function get_name()
  2987. {
  2988. if ($this->debug > 0) {
  2989. error_log('New LP - In learnpath::get_name()', 0);
  2990. }
  2991. if (!empty ($this->name)) {
  2992. return $this->name;
  2993. } else {
  2994. return 'N/A';
  2995. }
  2996. }
  2997. /**
  2998. * Gets a link to the resource from the present location, depending on item ID.
  2999. * @param string Type of link expected
  3000. * @param integer Learnpath item ID
  3001. * @return string Link to the lp_item resource
  3002. */
  3003. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3004. {
  3005. $course_id = $this->get_course_int_id();
  3006. if ($this->debug > 0) {
  3007. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3008. }
  3009. if (empty($item_id)) {
  3010. if ($this->debug > 2) {
  3011. error_log(
  3012. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  3013. ),
  3014. 0
  3015. );
  3016. }
  3017. $item_id = $this->get_current_item_id();
  3018. }
  3019. if (empty($item_id)) {
  3020. if ($this->debug > 2) {
  3021. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3022. }
  3023. //still empty, this means there was no item_id given and we are not in an object context or
  3024. //the object property is empty, return empty link
  3025. $item_id = $this->first();
  3026. return '';
  3027. }
  3028. $file = '';
  3029. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3030. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3031. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3032. $item_id = Database::escape_string($item_id);
  3033. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3034. FROM $lp_table l
  3035. INNER JOIN $lp_item_table li
  3036. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3037. WHERE li.id = $item_id ";
  3038. if ($this->debug > 2) {
  3039. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3040. }
  3041. $res = Database::query($sql);
  3042. if (Database :: num_rows($res) > 0) {
  3043. $row = Database :: fetch_array($res);
  3044. $lp_type = $row['ltype'];
  3045. $lp_path = $row['lpath'];
  3046. $lp_item_type = $row['litype'];
  3047. $lp_item_path = $row['lipath'];
  3048. $lp_item_params = $row['liparams'];
  3049. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3050. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3051. }
  3052. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3053. if ($type == 'http') {
  3054. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3055. } else {
  3056. $course_path = $sys_course_path; //system path
  3057. }
  3058. // 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.
  3059. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3060. $lp_type = 1;
  3061. }
  3062. if ($this->debug > 2) {
  3063. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3064. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3065. }
  3066. // Now go through the specific cases to get the end of the path
  3067. // @todo Use constants instead of int values.
  3068. switch ($lp_type) {
  3069. case 1 :
  3070. if ($lp_item_type == 'dokeos_chapter') {
  3071. $file = 'lp_content.php?type=dir';
  3072. } else {
  3073. require_once 'resourcelinker.inc.php';
  3074. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3075. if ($this->debug > 0) {
  3076. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3077. }
  3078. if ($lp_item_type == 'link') {
  3079. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3080. if (is_youtube_link($file)) {
  3081. $src = get_youtube_video_id($file);
  3082. $file = 'embed.php?type=youtube&src='.$src;
  3083. }
  3084. if (isVimeoLink($file)) {
  3085. $src = getVimeoLinkId($file);
  3086. $file = 'embed.php?type=vimeo&src='.$src;
  3087. }
  3088. } else {
  3089. // check how much attempts of a exercise exits in lp
  3090. $lp_item_id = $this->get_current_item_id();
  3091. $lp_view_id = $this->get_view_id();
  3092. $prevent_reinit = null;
  3093. if (isset($this->items[$this->current])) {
  3094. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3095. }
  3096. if (empty($provided_toc)) {
  3097. if ($this->debug > 0) {
  3098. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3099. }
  3100. $list = $this->get_toc();
  3101. } else {
  3102. if ($this->debug > 0) {
  3103. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3104. }
  3105. $list = $provided_toc;
  3106. }
  3107. $type_quiz = false;
  3108. foreach ($list as $toc) {
  3109. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3110. $type_quiz = true;
  3111. }
  3112. }
  3113. if ($type_quiz) {
  3114. $lp_item_id = Database::escape_string($lp_item_id);
  3115. $lp_view_id = Database::escape_string($lp_view_id);
  3116. $sql = "SELECT count(*) FROM $lp_item_view_table
  3117. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3118. $result = Database::query($sql);
  3119. $row_count = Database :: fetch_row($result);
  3120. $count_item_view = (int)$row_count[0];
  3121. $not_multiple_attempt = 0;
  3122. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3123. $not_multiple_attempt = 1;
  3124. }
  3125. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3126. }
  3127. $tmp_array = explode('/', $file);
  3128. $document_name = $tmp_array[count($tmp_array) - 1];
  3129. if (strpos($document_name, '_DELETED_')) {
  3130. $file = 'blank.php?error=document_deleted';
  3131. }
  3132. }
  3133. }
  3134. break;
  3135. case 2 :
  3136. if ($this->debug > 2) {
  3137. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3138. }
  3139. if ($lp_item_type != 'dir') {
  3140. // Quite complex here:
  3141. // We want to make sure 'http://' (and similar) links can
  3142. // be loaded as is (withouth the Chamilo path in front) but
  3143. // some contents use this form: resource.htm?resource=http://blablabla
  3144. // which means we have to find a protocol at the path's start, otherwise
  3145. // it should not be considered as an external URL.
  3146. //if ($this->prerequisites_match($item_id)) {
  3147. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3148. if ($this->debug > 2) {
  3149. error_log(
  3150. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3151. 0
  3152. );
  3153. }
  3154. // Distant url, return as is.
  3155. $file = $lp_item_path;
  3156. } else {
  3157. if ($this->debug > 2) {
  3158. error_log(
  3159. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3160. 0
  3161. );
  3162. }
  3163. // Prevent getting untranslatable urls.
  3164. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3165. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3166. // Prepare the path.
  3167. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3168. // TODO: Fix this for urls with protocol header.
  3169. $file = str_replace('//', '/', $file);
  3170. $file = str_replace(':/', '://', $file);
  3171. if (substr($lp_path, -1) == '/') {
  3172. $lp_path = substr($lp_path, 0, -1);
  3173. }
  3174. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3175. // if file not found.
  3176. $decoded = html_entity_decode($lp_item_path);
  3177. list ($decoded) = explode('?', $decoded);
  3178. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3179. require_once 'resourcelinker.inc.php';
  3180. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3181. if (empty($file)) {
  3182. $file = 'blank.php?error=document_not_found';
  3183. } else {
  3184. $tmp_array = explode('/', $file);
  3185. $document_name = $tmp_array[count($tmp_array) - 1];
  3186. if (strpos($document_name, '_DELETED_')) {
  3187. $file = 'blank.php?error=document_deleted';
  3188. } else {
  3189. $file = 'blank.php?error=document_not_found';
  3190. }
  3191. }
  3192. } else {
  3193. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3194. }
  3195. }
  3196. }
  3197. //}else{
  3198. //prerequisites did not match
  3199. //$file = 'blank.php';
  3200. //}
  3201. // We want to use parameters if they were defined in the imsmanifest
  3202. if (strpos($file, 'blank.php') === false) {
  3203. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3204. }
  3205. } else {
  3206. $file = 'lp_content.php?type=dir';
  3207. }
  3208. break;
  3209. case 3 :
  3210. if ($this->debug > 2) {
  3211. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3212. }
  3213. // Formatting AICC HACP append URL.
  3214. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3215. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3216. ).'&';
  3217. if ($lp_item_type != 'dir') {
  3218. // Quite complex here:
  3219. // We want to make sure 'http://' (and similar) links can
  3220. // be loaded as is (withouth the Chamilo path in front) but
  3221. // some contents use this form: resource.htm?resource=http://blablabla
  3222. // which means we have to find a protocol at the path's start, otherwise
  3223. // it should not be considered as an external URL.
  3224. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3225. if ($this->debug > 2) {
  3226. error_log(
  3227. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3228. 0
  3229. );
  3230. }
  3231. // Distant url, return as is.
  3232. $file = $lp_item_path;
  3233. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3234. /*
  3235. if (stristr($file,'<servername>') !== false) {
  3236. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3237. }
  3238. */
  3239. if (stripos($file, '<servername>') !== false) {
  3240. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3241. $web_course_path = str_replace(
  3242. 'https://',
  3243. '',
  3244. str_replace('http://', '', $course_path)
  3245. );
  3246. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3247. }
  3248. //
  3249. $file .= $aicc_append;
  3250. } else {
  3251. if ($this->debug > 2) {
  3252. error_log(
  3253. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3254. 0
  3255. );
  3256. }
  3257. // Prevent getting untranslatable urls.
  3258. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3259. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3260. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3261. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3262. // TODO: Fix this for urls with protocol header.
  3263. $file = str_replace('//', '/', $file);
  3264. $file = str_replace(':/', '://', $file);
  3265. $file .= $aicc_append;
  3266. }
  3267. } else {
  3268. $file = 'lp_content.php?type=dir';
  3269. }
  3270. break;
  3271. case 4 :
  3272. break;
  3273. default :
  3274. break;
  3275. }
  3276. }
  3277. if ($this->debug > 2) {
  3278. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3279. }
  3280. return $file;
  3281. }
  3282. /**
  3283. * Gets the latest usable view or generate a new one
  3284. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3285. * @return integer DB lp_view id
  3286. */
  3287. public function get_view($attempt_num = 0)
  3288. {
  3289. if ($this->debug > 0) {
  3290. error_log('New LP - In learnpath::get_view()', 0);
  3291. }
  3292. $search = '';
  3293. // Use $attempt_num to enable multi-views management (disabled so far).
  3294. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3295. $search = 'AND view_count = '.$attempt_num;
  3296. }
  3297. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3298. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3299. $course_id = api_get_course_int_id();
  3300. $sql = "SELECT id, view_count FROM $lp_view_table
  3301. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3302. )." ".$search.
  3303. " ORDER BY view_count DESC";
  3304. $res = Database::query($sql);
  3305. if (Database :: num_rows($res) > 0) {
  3306. $row = Database :: fetch_array($res);
  3307. $this->lp_view_id = $row['id'];
  3308. } else {
  3309. // There is no database record, create one.
  3310. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3311. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3312. $res = Database::query($sql);
  3313. $id = Database :: insert_id();
  3314. $this->lp_view_id = $id;
  3315. }
  3316. return $this->lp_view_id;
  3317. }
  3318. /**
  3319. * Gets the current view id
  3320. * @return integer View ID (from lp_view)
  3321. */
  3322. public function get_view_id()
  3323. {
  3324. if ($this->debug > 0) {
  3325. error_log('New LP - In learnpath::get_view_id()', 0);
  3326. }
  3327. if (!empty ($this->lp_view_id)) {
  3328. return $this->lp_view_id;
  3329. } else {
  3330. return 0;
  3331. }
  3332. }
  3333. /**
  3334. * Gets the update queue
  3335. * @return array Array containing IDs of items to be updated by JavaScript
  3336. */
  3337. public function get_update_queue()
  3338. {
  3339. if ($this->debug > 0) {
  3340. error_log('New LP - In learnpath::get_update_queue()', 0);
  3341. }
  3342. return $this->update_queue;
  3343. }
  3344. /**
  3345. * Gets the user ID
  3346. * @return integer User ID
  3347. */
  3348. public function get_user_id()
  3349. {
  3350. if ($this->debug > 0) {
  3351. error_log('New LP - In learnpath::get_user_id()', 0);
  3352. }
  3353. if (!empty ($this->user_id)) {
  3354. return $this->user_id;
  3355. } else {
  3356. return false;
  3357. }
  3358. }
  3359. /**
  3360. * Checks if any of the items has an audio element attached
  3361. * @return bool True or false
  3362. */
  3363. public function has_audio()
  3364. {
  3365. if ($this->debug > 1) {
  3366. error_log('New LP - In learnpath::has_audio()', 0);
  3367. }
  3368. $has = false;
  3369. foreach ($this->items as $i => $item) {
  3370. if (!empty ($this->items[$i]->audio)) {
  3371. $has = true;
  3372. break;
  3373. }
  3374. }
  3375. return $has;
  3376. }
  3377. /**
  3378. * Logs a message into a file
  3379. * @param string Message to log
  3380. * @return boolean True on success, false on error or if msg empty
  3381. */
  3382. public function log($msg)
  3383. {
  3384. if ($this->debug > 0) {
  3385. error_log('New LP - In learnpath::log()', 0);
  3386. }
  3387. // TODO
  3388. $this->error .= $msg;
  3389. return true;
  3390. }
  3391. /**
  3392. * Moves an item up and down at its level
  3393. * @param integer Item to move up and down
  3394. * @param string Direction 'up' or 'down'
  3395. * @return integer New display order, or false on error
  3396. */
  3397. public function move_item($id, $direction)
  3398. {
  3399. $course_id = api_get_course_int_id();
  3400. if ($this->debug > 0) {
  3401. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3402. }
  3403. if (empty ($id) or empty ($direction)) {
  3404. return false;
  3405. }
  3406. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3407. $sql_sel = "SELECT *
  3408. FROM ".$tbl_lp_item."
  3409. WHERE c_id = ".$course_id." AND id = ".$id;
  3410. $res_sel = Database::query($sql_sel);
  3411. // Check if elem exists.
  3412. if (Database :: num_rows($res_sel) < 1) {
  3413. return false;
  3414. }
  3415. // Gather data.
  3416. $row = Database :: fetch_array($res_sel);
  3417. $previous = $row['previous_item_id'];
  3418. $next = $row['next_item_id'];
  3419. $display = $row['display_order'];
  3420. $parent = $row['parent_item_id'];
  3421. $lp = $row['lp_id'];
  3422. // Update the item (switch with previous/next one).
  3423. switch ($direction) {
  3424. case 'up' :
  3425. if ($this->debug > 2) {
  3426. error_log('Movement up detected', 0);
  3427. }
  3428. if ($display <= 1) { /*do nothing*/
  3429. } else {
  3430. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3431. WHERE c_id = ".$course_id." AND id = $previous";
  3432. if ($this->debug > 2) {
  3433. error_log('Selecting previous: '.$sql_sel2, 0);
  3434. }
  3435. $res_sel2 = Database::query($sql_sel2);
  3436. if (Database :: num_rows($res_sel2) < 1) {
  3437. $previous_previous = 0;
  3438. }
  3439. // Gather data.
  3440. $row2 = Database :: fetch_array($res_sel2);
  3441. $previous_previous = $row2['previous_item_id'];
  3442. // Update previous_previous item (switch "next" with current).
  3443. if ($previous_previous != 0) {
  3444. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3445. if ($this->debug > 2) {
  3446. error_log($sql_upd2, 0);
  3447. }
  3448. $res_upd2 = Database::query($sql_upd2);
  3449. }
  3450. // Update previous item (switch with current).
  3451. if ($previous != 0) {
  3452. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3453. WHERE c_id = ".$course_id." AND id = $previous";
  3454. if ($this->debug > 2) {
  3455. error_log($sql_upd2, 0);
  3456. }
  3457. $res_upd2 = Database::query($sql_upd2);
  3458. }
  3459. // Update current item (switch with previous).
  3460. if ($id != 0) {
  3461. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3462. WHERE c_id = ".$course_id." AND id = $id";
  3463. if ($this->debug > 2) {
  3464. error_log($sql_upd2, 0);
  3465. }
  3466. $res_upd2 = Database::query($sql_upd2);
  3467. }
  3468. // Update next item (new previous item).
  3469. if ($next != 0) {
  3470. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3471. WHERE c_id = ".$course_id." AND id = $next";
  3472. if ($this->debug > 2) {
  3473. error_log($sql_upd2, 0);
  3474. }
  3475. $res_upd2 = Database::query($sql_upd2);
  3476. }
  3477. $display = $display - 1;
  3478. }
  3479. break;
  3480. case 'down' :
  3481. if ($this->debug > 2) {
  3482. error_log('Movement down detected', 0);
  3483. }
  3484. if ($next == 0) { /* Do nothing. */
  3485. } else {
  3486. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3487. if ($this->debug > 2) {
  3488. error_log('Selecting next: '.$sql_sel2, 0);
  3489. }
  3490. $res_sel2 = Database::query($sql_sel2);
  3491. if (Database :: num_rows($res_sel2) < 1) {
  3492. $next_next = 0;
  3493. }
  3494. // Gather data.
  3495. $row2 = Database :: fetch_array($res_sel2);
  3496. $next_next = $row2['next_item_id'];
  3497. // Update previous item (switch with current).
  3498. if ($previous != 0) {
  3499. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3500. WHERE c_id = ".$course_id." AND id = $previous";
  3501. $res_upd2 = Database::query($sql_upd2);
  3502. }
  3503. // Update current item (switch with previous).
  3504. if ($id != 0) {
  3505. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3506. WHERE c_id = ".$course_id." AND id = $id";
  3507. $res_upd2 = Database::query($sql_upd2);
  3508. }
  3509. // Update next item (new previous item).
  3510. if ($next != 0) {
  3511. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3512. WHERE c_id = ".$course_id." AND id = $next";
  3513. $res_upd2 = Database::query($sql_upd2);
  3514. }
  3515. // Update next_next item (switch "previous" with current).
  3516. if ($next_next != 0) {
  3517. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3518. WHERE c_id = ".$course_id." AND id = $next_next";
  3519. $res_upd2 = Database::query($sql_upd2);
  3520. }
  3521. $display = $display + 1;
  3522. }
  3523. break;
  3524. default :
  3525. return false;
  3526. }
  3527. return $display;
  3528. }
  3529. /**
  3530. * Move a learnpath up (display_order)
  3531. * @param integer Learnpath ID
  3532. */
  3533. public function move_up($lp_id)
  3534. {
  3535. $course_id = api_get_course_int_id();
  3536. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3537. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3538. $res = Database::query($sql);
  3539. if ($res === false) {
  3540. return false;
  3541. }
  3542. $lps = array();
  3543. $lp_order = array();
  3544. $num = Database :: num_rows($res);
  3545. // First check the order is correct, globally (might be wrong because
  3546. // of versions < 1.8.4)
  3547. if ($num > 0) {
  3548. $i = 1;
  3549. while ($row = Database :: fetch_array($res)) {
  3550. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3551. $need_fix = true;
  3552. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3553. $res_u = Database::query($sql_u);
  3554. }
  3555. $row['display_order'] = $i;
  3556. $lps[$row['id']] = $row;
  3557. $lp_order[$i] = $row['id'];
  3558. $i++;
  3559. }
  3560. }
  3561. if ($num > 1) { // If there's only one element, no need to sort.
  3562. $order = $lps[$lp_id]['display_order'];
  3563. if ($order > 1) { // If it's the first element, no need to move up.
  3564. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3565. $res_u1 = Database::query($sql_u1);
  3566. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3567. $res_u2 = Database::query($sql_u2);
  3568. }
  3569. }
  3570. }
  3571. /**
  3572. * Move a learnpath down (display_order)
  3573. * @param integer Learnpath ID
  3574. */
  3575. public function move_down($lp_id)
  3576. {
  3577. $course_id = api_get_course_int_id();
  3578. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3579. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3580. $res = Database::query($sql);
  3581. if ($res === false) {
  3582. return false;
  3583. }
  3584. $lps = array();
  3585. $lp_order = array();
  3586. $num = Database :: num_rows($res);
  3587. $max = 0;
  3588. // First check the order is correct, globally (might be wrong because
  3589. // of versions < 1.8.4).
  3590. if ($num > 0) {
  3591. $i = 1;
  3592. while ($row = Database :: fetch_array($res)) {
  3593. $max = $i;
  3594. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3595. $need_fix = true;
  3596. $sql_u = "UPDATE $lp_table SET display_order = $i
  3597. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3598. $res_u = Database::query($sql_u);
  3599. }
  3600. $row['display_order'] = $i;
  3601. $lps[$row['id']] = $row;
  3602. $lp_order[$i] = $row['id'];
  3603. $i++;
  3604. }
  3605. }
  3606. if ($num > 1) { // If there's only one element, no need to sort.
  3607. $order = $lps[$lp_id]['display_order'];
  3608. if ($order < $max) { // If it's the first element, no need to move up.
  3609. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3610. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3611. $res_u1 = Database::query($sql_u1);
  3612. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3613. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3614. $res_u2 = Database::query($sql_u2);
  3615. }
  3616. }
  3617. }
  3618. /**
  3619. * Updates learnpath attributes to point to the next element
  3620. * The last part is similar to set_current_item but processing the other way around
  3621. */
  3622. public function next()
  3623. {
  3624. if ($this->debug > 0) {
  3625. error_log('New LP - In learnpath::next()', 0);
  3626. }
  3627. $this->last = $this->get_current_item_id();
  3628. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3629. $this->autocomplete_parents($this->last);
  3630. $new_index = $this->get_next_index();
  3631. if ($this->debug > 2) {
  3632. error_log('New LP - New index: '.$new_index, 0);
  3633. }
  3634. $this->index = $new_index;
  3635. if ($this->debug > 2) {
  3636. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3637. }
  3638. $this->current = $this->ordered_items[$new_index];
  3639. if ($this->debug > 2) {
  3640. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3641. }
  3642. }
  3643. /**
  3644. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3645. * class, this might be redefined to allow several behaviours depending on the document type.
  3646. * @param integer Resource ID
  3647. * @return boolean True on success, false otherwise
  3648. */
  3649. public function open($id)
  3650. {
  3651. if ($this->debug > 0) {
  3652. error_log('New LP - In learnpath::open()', 0);
  3653. }
  3654. // TODO:
  3655. // set the current resource attribute to this resource
  3656. // switch on element type (redefine in child class?)
  3657. // set status for this item to "opened"
  3658. // start timer
  3659. // initialise score
  3660. $this->index = 0; //or = the last item seen (see $this->last)
  3661. }
  3662. /**
  3663. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3664. * and the prerequisite string on error.
  3665. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3666. * @param integer Optional item ID. If none given, uses the current open item.
  3667. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3668. */
  3669. public function prerequisites_match($item = null)
  3670. {
  3671. if ($this->debug > 0) {
  3672. error_log('In learnpath::prerequisites_match()', 0);
  3673. }
  3674. if (empty($item)) {
  3675. $item = $this->current;
  3676. }
  3677. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3678. if ($this->type == 2) {
  3679. //Getting prereq from scorm
  3680. $prereq_string = $this->get_scorm_prereq_string($item);
  3681. } else {
  3682. $prereq_string = $this->items[$item]->get_prereq_string();
  3683. }
  3684. if (empty($prereq_string)) {
  3685. return true;
  3686. }
  3687. // Clean spaces.
  3688. $prereq_string = str_replace(' ', '', $prereq_string);
  3689. if ($this->debug > 0) {
  3690. error_log('Found prereq_string: '.$prereq_string, 0);
  3691. }
  3692. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3693. $result = $this->items[$item]->parse_prereq(
  3694. $prereq_string,
  3695. $this->items,
  3696. $this->refs_list,
  3697. $this->get_user_id()
  3698. );
  3699. if ($result === false) {
  3700. $this->set_error_msg($this->items[$item]->prereq_alert);
  3701. }
  3702. } else {
  3703. $result = true;
  3704. if ($this->debug > 1) {
  3705. error_log('$this->items['.$item.'] was not an object', 0);
  3706. }
  3707. }
  3708. if ($this->debug > 1) {
  3709. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3710. }
  3711. return $result;
  3712. }
  3713. /**
  3714. * Updates learnpath attributes to point to the previous element
  3715. * The last part is similar to set_current_item but processing the other way around
  3716. */
  3717. public function previous()
  3718. {
  3719. if ($this->debug > 0) {
  3720. error_log('New LP - In learnpath::previous()', 0);
  3721. }
  3722. $this->last = $this->get_current_item_id();
  3723. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3724. $this->autocomplete_parents($this->last);
  3725. $new_index = $this->get_previous_index();
  3726. $this->index = $new_index;
  3727. $this->current = $this->ordered_items[$new_index];
  3728. }
  3729. /**
  3730. * Publishes a learnpath. This basically means show or hide the learnpath
  3731. * to normal users.
  3732. * Can be used as abstract
  3733. * @param integer Learnpath ID
  3734. * @param string New visibility
  3735. */
  3736. public function toggle_visibility($lp_id, $set_visibility = 1, $course_id = null)
  3737. {
  3738. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3739. $action = 'visible';
  3740. if ($set_visibility != 1) {
  3741. $action = 'invisible';
  3742. }
  3743. if (!empty($course_id)) {
  3744. $course = api_get_course_info_by_id($course_id);
  3745. } else {
  3746. $course = api_get_course_info();
  3747. }
  3748. return api_item_property_update($course, TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3749. }
  3750. /**
  3751. * Publishes a learnpath. This basically means show or hide the learnpath
  3752. * on the course homepage
  3753. * Can be used as abstract
  3754. * @param integer Learnpath id
  3755. * @param string New visibility (v/s - visible/invisible)
  3756. */
  3757. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3758. {
  3759. $course_id = api_get_course_int_id();
  3760. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3761. $lp_id = Database::escape_string($lp_id);
  3762. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3763. $result = Database::query($sql);
  3764. if (Database::num_rows($result)) {
  3765. $row = Database :: fetch_array($result);
  3766. $name = Text::domesticate($row['name']);
  3767. if ($set_visibility == 'i') {
  3768. $s = $name." ".get_lang('LearnpathNotPublished');
  3769. $dialogBox = $s;
  3770. $v = 0;
  3771. }
  3772. if ($set_visibility == 'v') {
  3773. $s = $name." ".get_lang('LearnpathPublished');
  3774. $dialogBox = $s;
  3775. $v = 1;
  3776. }
  3777. } else {
  3778. return false;
  3779. }
  3780. $session_id = api_get_session_id();
  3781. $session_condition = api_get_session_condition($session_id);
  3782. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3783. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3784. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3785. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3786. $result = Database::query($sql);
  3787. $num = Database :: num_rows($result);
  3788. if (($set_visibility == 'i') && ($num > 0)) {
  3789. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3790. Database::query($sql);
  3791. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3792. Database::query($sql);
  3793. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3794. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3795. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3796. Database::query($sql);
  3797. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3798. $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
  3799. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3800. Database::query($sql);
  3801. } else {
  3802. // Parameter and database incompatible, do nothing, exit.
  3803. return false;
  3804. }
  3805. }
  3806. /**
  3807. * Restart the whole learnpath. Return the URL of the first element.
  3808. * Make sure the results are saved with anoter method. This method should probably be
  3809. * redefined in children classes.
  3810. * To use a similar method statically, use the create_new_attempt() method
  3811. * @return string URL to load in the viewer
  3812. */
  3813. public function restart()
  3814. {
  3815. if ($this->debug > 0) {
  3816. error_log('New LP - In learnpath::restart()', 0);
  3817. }
  3818. // TODO
  3819. // Call autosave method to save the current progress.
  3820. //$this->index = 0;
  3821. $session_id = api_get_session_id();
  3822. $course_id = api_get_course_int_id();
  3823. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3824. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3825. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3826. if ($this->debug > 2) {
  3827. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3828. }
  3829. $res = Database::query($sql);
  3830. if ($view_id = Database :: insert_id($res)) {
  3831. $this->lp_view_id = $view_id;
  3832. $this->attempt = $this->attempt + 1;
  3833. } else {
  3834. $this->error = 'Could not insert into item_view table...';
  3835. return false;
  3836. }
  3837. $this->autocomplete_parents($this->current);
  3838. foreach ($this->items as $index => $dummy) {
  3839. $this->items[$index]->restart();
  3840. $this->items[$index]->set_lp_view($this->lp_view_id);
  3841. }
  3842. $this->first();
  3843. return true;
  3844. }
  3845. /**
  3846. * Saves the current item
  3847. * @return boolean
  3848. */
  3849. public function save_current()
  3850. {
  3851. if ($this->debug > 0) {
  3852. error_log('learnpath::save_current()', 0);
  3853. }
  3854. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3855. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3856. if ($this->debug > 2) {
  3857. error_log('New LP - save_current() saving item '.$this->current, 0);
  3858. }
  3859. if ($this->debug > 2) {
  3860. error_log(''.print_r($this->items, true), 0);
  3861. }
  3862. if (is_object($this->items[$this->current])) {
  3863. //$res = $this->items[$this->current]->save(false);
  3864. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3865. $this->autocomplete_parents($this->current);
  3866. $status = $this->items[$this->current]->get_status();
  3867. $this->append_message('new_item_status: '.$status);
  3868. $this->update_queue[$this->current] = $status;
  3869. return $res;
  3870. }
  3871. return false;
  3872. }
  3873. /**
  3874. * Saves the given item
  3875. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3876. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3877. * @return boolean
  3878. */
  3879. public function save_item($item_id = null, $from_outside = true)
  3880. {
  3881. $debug = $this->debug;
  3882. if ($debug) {
  3883. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3884. }
  3885. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3886. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3887. if (empty($item_id)) {
  3888. $item_id = intval($_REQUEST['id']);
  3889. }
  3890. if (empty($item_id)) {
  3891. $item_id = $this->get_current_item_id();
  3892. }
  3893. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3894. if ($debug) {
  3895. error_log('Object exists');
  3896. }
  3897. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3898. if ($debug) {
  3899. error_log('update_queue before:');
  3900. error_log(print_r($this->update_queue, 1));
  3901. }
  3902. $this->autocomplete_parents($item_id);
  3903. $status = $this->items[$item_id]->get_status();
  3904. $this->update_queue[$item_id] = $status;
  3905. if ($debug) {
  3906. error_log('get_status(): '.$status);
  3907. error_log('update_queue after:');
  3908. error_log(print_r($this->update_queue, 1));
  3909. }
  3910. return $res;
  3911. }
  3912. return false;
  3913. }
  3914. /**
  3915. * Saves the last item seen's ID only in case
  3916. */
  3917. public function save_last()
  3918. {
  3919. $course_id = api_get_course_int_id();
  3920. if ($this->debug > 0) {
  3921. error_log('New LP - In learnpath::save_last()', 0);
  3922. }
  3923. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3924. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3925. if (isset($this->current)) {
  3926. if ($this->debug > 2) {
  3927. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3928. }
  3929. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3930. WHERE c_id = $course_id AND
  3931. lp_id = ".$this->get_id()." AND
  3932. user_id = ".$this->get_user_id()." ".$session_condition;
  3933. if ($this->debug > 2) {
  3934. error_log('New LP - Saving last item seen : '.$sql, 0);
  3935. }
  3936. $res = Database::query($sql);
  3937. }
  3938. // Save progress.
  3939. list($progress, $text) = $this->get_progress_bar_text('%');
  3940. if ($progress >= 0 && $progress <= 100) {
  3941. $progress = (int)$progress;
  3942. $sql = "UPDATE $table SET progress = $progress
  3943. WHERE c_id = ".$course_id." AND
  3944. lp_id = ".$this->get_id()." AND
  3945. user_id = ".$this->get_user_id()." ".$session_condition;
  3946. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3947. $this->progress_db = $progress;
  3948. }
  3949. }
  3950. /**
  3951. * Sets the current item ID (checks if valid and authorized first)
  3952. * @param integer New item ID. If not given or not authorized, defaults to current
  3953. */
  3954. public function set_current_item($item_id = null)
  3955. {
  3956. if ($this->debug > 0) {
  3957. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3958. }
  3959. if (empty ($item_id)) {
  3960. if ($this->debug > 2) {
  3961. error_log('New LP - No new current item given, ignore...', 0);
  3962. }
  3963. // Do nothing.
  3964. } else {
  3965. if ($this->debug > 2) {
  3966. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3967. }
  3968. if (is_numeric($item_id)) {
  3969. $item_id = Database::escape_string($item_id);
  3970. // TODO: Check in database here.
  3971. $this->last = $this->current;
  3972. $this->current = $item_id;
  3973. // TODO: Update $this->index as well.
  3974. foreach ($this->ordered_items as $index => $item) {
  3975. if ($item == $this->current) {
  3976. $this->index = $index;
  3977. break;
  3978. }
  3979. }
  3980. if ($this->debug > 2) {
  3981. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  3982. }
  3983. } else {
  3984. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  3985. }
  3986. }
  3987. }
  3988. /**
  3989. * Sets the encoding
  3990. * @param string New encoding
  3991. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  3992. */
  3993. public function set_encoding($enc = 'UTF-8')
  3994. {
  3995. if ($this->debug > 0) {
  3996. error_log('New LP - In learnpath::set_encoding()', 0);
  3997. }
  3998. $course_id = api_get_course_int_id();
  3999. /* // Deprecated code (Chamilo 1.8.8).
  4000. $enc = strtoupper($enc);
  4001. $encodings = array (
  4002. 'UTF-8',
  4003. 'ISO-8859-1',
  4004. 'ISO-8859-15',
  4005. 'cp1251',
  4006. 'cp1252',
  4007. 'KOI8-R',
  4008. 'BIG5',
  4009. 'GB2312',
  4010. 'Shift_JIS',
  4011. 'EUC-JP',
  4012. ''
  4013. );
  4014. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  4015. $lp = $this->get_id();
  4016. if ($lp != 0) {
  4017. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4018. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4019. $res = Database::query($sql);
  4020. return $res;
  4021. }
  4022. }
  4023. return false;
  4024. */
  4025. $enc = api_refine_encoding_id($enc);
  4026. if (empty($enc)) {
  4027. $enc = api_get_system_encoding();
  4028. }
  4029. if (api_is_encoding_supported($enc)) {
  4030. $lp = $this->get_id();
  4031. if ($lp != 0) {
  4032. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4033. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4034. $res = Database::query($sql);
  4035. return $res;
  4036. }
  4037. }
  4038. return false;
  4039. }
  4040. /**
  4041. * Sets the JS lib setting in the database directly.
  4042. * This is the JavaScript library file this lp needs to load on startup
  4043. * @param string Proximity setting
  4044. * @return boolean True on update success. False otherwise.
  4045. */
  4046. public function set_jslib($lib = '')
  4047. {
  4048. if ($this->debug > 0) {
  4049. error_log('New LP - In learnpath::set_jslib()', 0);
  4050. }
  4051. $lp = $this->get_id();
  4052. $course_id = api_get_course_int_id();
  4053. if ($lp != 0) {
  4054. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4055. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4056. $res = Database::query($sql);
  4057. return $res;
  4058. } else {
  4059. return false;
  4060. }
  4061. }
  4062. /**
  4063. * Sets the name of the LP maker (publisher) (and save)
  4064. * @param string Optional string giving the new content_maker of this learnpath
  4065. * @return boolean True
  4066. */
  4067. public function set_maker($name = '')
  4068. {
  4069. if ($this->debug > 0) {
  4070. error_log('New LP - In learnpath::set_maker()', 0);
  4071. }
  4072. if (empty ($name)) {
  4073. return false;
  4074. }
  4075. $this->maker = Database::escape_string($name);
  4076. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4077. $course_id = api_get_course_int_id();
  4078. $lp_id = $this->get_id();
  4079. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4080. if ($this->debug > 2) {
  4081. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4082. }
  4083. Database::query($sql);
  4084. return true;
  4085. }
  4086. /**
  4087. * Sets the name of the current learnpath (and save)
  4088. * @param string Optional string giving the new name of this learnpath
  4089. * @return boolean True/False
  4090. */
  4091. public function set_name($name = null)
  4092. {
  4093. if ($this->debug > 0) {
  4094. error_log('New LP - In learnpath::set_name()', 0);
  4095. }
  4096. if (empty($name)) {
  4097. return false;
  4098. }
  4099. $this->name = Database::escape_string($name);
  4100. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4101. $lp_id = $this->get_id();
  4102. $course_id = api_get_course_int_id();
  4103. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4104. if ($this->debug > 2) {
  4105. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4106. }
  4107. $result = Database::query($sql);
  4108. // If the lp is visible on the homepage, change his name there.
  4109. if (Database::affected_rows($result)) {
  4110. $session_id = api_get_session_id();
  4111. $session_condition = api_get_session_condition($session_id);
  4112. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4113. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4114. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4115. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4116. Database::query($sql);
  4117. return true;
  4118. } else {
  4119. return false;
  4120. }
  4121. }
  4122. /**
  4123. * Set index specified prefix terms for all items in this path
  4124. * @param string Comma-separated list of terms
  4125. * @param char Xapian term prefix
  4126. * @return boolean False on error, true otherwise
  4127. */
  4128. public function set_terms_by_prefix($terms_string, $prefix)
  4129. {
  4130. $course_id = api_get_course_int_id();
  4131. if (api_get_setting('search_enabled') !== 'true') {
  4132. return false;
  4133. }
  4134. if (!extension_loaded('xapian')) {
  4135. return false;
  4136. }
  4137. $terms_string = trim($terms_string);
  4138. $terms = explode(',', $terms_string);
  4139. array_walk($terms, 'trim_value');
  4140. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4141. // Don't do anything if no change, verify only at DB, not the search engine.
  4142. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4143. return false;
  4144. }
  4145. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4146. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4147. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4148. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4149. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4150. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4151. $lp_id = intval($_POST['lp_id']);
  4152. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4153. $result = Database::query($sql);
  4154. $di = new ChamiloIndexer();
  4155. while ($lp_item = Database :: fetch_array($result)) {
  4156. // Get search_did.
  4157. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4158. $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';
  4159. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4160. //echo $sql; echo '<br>';
  4161. $res = Database::query($sql);
  4162. if (Database::num_rows($res) > 0) {
  4163. $se_ref = Database :: fetch_array($res);
  4164. // Compare terms.
  4165. $doc = $di->get_document($se_ref['search_did']);
  4166. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4167. $xterms = array();
  4168. foreach ($xapian_terms as $xapian_term) {
  4169. $xterms[] = substr($xapian_term['name'], 1);
  4170. }
  4171. $dterms = $terms;
  4172. $missing_terms = array_diff($dterms, $xterms);
  4173. $deprecated_terms = array_diff($xterms, $dterms);
  4174. // Save it to search engine.
  4175. foreach ($missing_terms as $term) {
  4176. $doc->add_term($prefix.$term, 1);
  4177. }
  4178. foreach ($deprecated_terms as $term) {
  4179. $doc->remove_term($prefix.$term);
  4180. }
  4181. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4182. $di->getDb()->flush();
  4183. } else {
  4184. //@todo What we should do here?
  4185. }
  4186. }
  4187. return true;
  4188. }
  4189. /**
  4190. * Sets the theme of the LP (local/remote) (and save)
  4191. * @param string Optional string giving the new theme of this learnpath
  4192. * @return bool Returns true if theme name is not empty
  4193. */
  4194. public function set_theme($name = '')
  4195. {
  4196. $course_id = api_get_course_int_id();
  4197. if ($this->debug > 0) {
  4198. error_log('New LP - In learnpath::set_theme()', 0);
  4199. }
  4200. $this->theme = Database::escape_string($name);
  4201. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4202. $lp_id = $this->get_id();
  4203. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4204. if ($this->debug > 2) {
  4205. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4206. }
  4207. Database::query($sql);
  4208. return true;
  4209. }
  4210. /**
  4211. * Sets the image of an LP (and save)
  4212. * @param string Optional string giving the new image of this learnpath
  4213. * @return bool Returns true if theme name is not empty
  4214. */
  4215. public function set_preview_image($name = '')
  4216. {
  4217. $course_id = api_get_course_int_id();
  4218. if ($this->debug > 0) {
  4219. error_log('New LP - In learnpath::set_preview_image()', 0);
  4220. }
  4221. $this->preview_image = Database::escape_string($name);
  4222. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4223. $lp_id = $this->get_id();
  4224. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4225. if ($this->debug > 2) {
  4226. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4227. }
  4228. Database::query($sql);
  4229. return true;
  4230. }
  4231. /**
  4232. * Sets the author of a LP (and save)
  4233. * @param string Optional string giving the new author of this learnpath
  4234. * @return bool Returns true if author's name is not empty
  4235. */
  4236. public function set_author($name = '')
  4237. {
  4238. $course_id = api_get_course_int_id();
  4239. if ($this->debug > 0) {
  4240. error_log('New LP - In learnpath::set_author()', 0);
  4241. }
  4242. $this->author = Database::escape_string($name);
  4243. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4244. $lp_id = $this->get_id();
  4245. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4246. if ($this->debug > 2) {
  4247. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4248. }
  4249. Database::query($sql);
  4250. return true;
  4251. }
  4252. /**
  4253. * Sets the hide_toc_frame parameter of a LP (and save)
  4254. * @param int 1 if frame is hidden 0 then else
  4255. * @return bool Returns true if author's name is not empty
  4256. */
  4257. public function set_hide_toc_frame($hide)
  4258. {
  4259. $course_id = api_get_course_int_id();
  4260. if ($this->debug > 0) {
  4261. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4262. }
  4263. if (intval($hide) == $hide) {
  4264. $this->hide_toc_frame = $hide;
  4265. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4266. $lp_id = $this->get_id();
  4267. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4268. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4269. if ($this->debug > 2) {
  4270. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4271. }
  4272. Database::query($sql);
  4273. return true;
  4274. } else {
  4275. return false;
  4276. }
  4277. }
  4278. /**
  4279. * Sets the prerequisite of a LP (and save)
  4280. * @param int integer giving the new prerequisite of this learnpath
  4281. * @return bool returns true if prerequisite is not empty
  4282. */
  4283. public function set_prerequisite($prerequisite)
  4284. {
  4285. $course_id = api_get_course_int_id();
  4286. if ($this->debug > 0) {
  4287. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4288. }
  4289. $this->prerequisite = intval($prerequisite);
  4290. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4291. $lp_id = $this->get_id();
  4292. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4293. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4294. if ($this->debug > 2) {
  4295. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4296. }
  4297. Database::query($sql);
  4298. return true;
  4299. }
  4300. /**
  4301. * Sets the location/proximity of the LP (local/remote) (and save)
  4302. * @param string Optional string giving the new location of this learnpath
  4303. * @return boolean True on success / False on error
  4304. */
  4305. public function set_proximity($name = '')
  4306. {
  4307. $course_id = api_get_course_int_id();
  4308. if ($this->debug > 0) {
  4309. error_log('New LP - In learnpath::set_proximity()', 0);
  4310. }
  4311. if (empty ($name)) {
  4312. return false;
  4313. }
  4314. $this->proximity = Database::escape_string($name);
  4315. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4316. $lp_id = $this->get_id();
  4317. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4318. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4319. if ($this->debug > 2) {
  4320. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4321. }
  4322. Database::query($sql);
  4323. return true;
  4324. }
  4325. /**
  4326. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4327. * @param integer DB ID of the item
  4328. */
  4329. public function set_previous_item($id)
  4330. {
  4331. if ($this->debug > 0) {
  4332. error_log('New LP - In learnpath::set_previous_item()', 0);
  4333. }
  4334. $this->last = $id;
  4335. }
  4336. public function get_max_attempts()
  4337. {
  4338. return $this->max_attempts;
  4339. }
  4340. public function get_subscribe_users()
  4341. {
  4342. return $this->subscribe_users;
  4343. }
  4344. public function check_attempts()
  4345. {
  4346. $max_attempts = $this->get_max_attempts();
  4347. switch ($max_attempts) {
  4348. case 0: //unlimited
  4349. return true;
  4350. break;
  4351. case $max_attempts >= 1:
  4352. if ($this->attempt <= $max_attempts) {
  4353. return true;
  4354. }
  4355. break;
  4356. }
  4357. return false;
  4358. }
  4359. function check_item_attempts($item_id)
  4360. {
  4361. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4362. $count = $this->items[$item_id]->get_view_count();
  4363. $max_attempts = $this->get_max_attempts();
  4364. switch ($max_attempts) {
  4365. case 0: //unlimited
  4366. return true;
  4367. break;
  4368. case $max_attempts >= 1:
  4369. if ($count <= $max_attempts) {
  4370. return true;
  4371. }
  4372. break;
  4373. }
  4374. return false;
  4375. }
  4376. }
  4377. public function set_max_attempts($attempts)
  4378. {
  4379. $mode = 'multiple';
  4380. if ($attempts == 1) {
  4381. $mode = 'single';
  4382. }
  4383. $this->max_attempts = intval($attempts);
  4384. $lp_id = $this->get_id();
  4385. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4386. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4387. Database::query($sql);
  4388. $this->set_attempt_mode($mode);
  4389. }
  4390. /**
  4391. * Sets subscribe_users
  4392. * @param string $value Optional string giving the new location of this learnpath
  4393. * @return bool True on success / False on error
  4394. */
  4395. public function set_subscribe_users($value = 0)
  4396. {
  4397. if ($this->debug > 0) {
  4398. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4399. }
  4400. $this->subscribe_users = intval($value);;
  4401. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4402. $lp_id = $this->get_id();
  4403. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4404. if ($this->debug > 2) {
  4405. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4406. }
  4407. Database::query($sql);
  4408. return true;
  4409. }
  4410. /**
  4411. * Sets use_max_score
  4412. * @param string Optional string giving the new location of this learnpath
  4413. * @return boolean True on success / False on error
  4414. */
  4415. public function set_use_max_score($use_max_score = 1)
  4416. {
  4417. if ($this->debug > 0) {
  4418. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4419. }
  4420. $this->use_max_score = intval($use_max_score);
  4421. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4422. $lp_id = $this->get_id();
  4423. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4424. if ($this->debug > 2) {
  4425. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4426. }
  4427. Database::query($sql);
  4428. return true;
  4429. }
  4430. /**
  4431. * Sets and saves the expired_on date
  4432. * @param string Optional string giving the new author of this learnpath
  4433. * @return bool Returns true if author's name is not empty
  4434. */
  4435. public function set_expired_on($expired_on)
  4436. {
  4437. $course_id = api_get_course_int_id();
  4438. if ($this->debug > 0) {
  4439. error_log('New LP - In learnpath::set_expired_on()', 0);
  4440. }
  4441. if (!empty($expired_on)) {
  4442. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4443. } else {
  4444. $this->expired_on = '';
  4445. }
  4446. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4447. $lp_id = $this->get_id();
  4448. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4449. if ($this->debug > 2) {
  4450. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4451. }
  4452. Database::query($sql);
  4453. return true;
  4454. }
  4455. /**
  4456. * Sets and saves the publicated_on date
  4457. * @param string Optional string giving the new author of this learnpath
  4458. * @return bool Returns true if author's name is not empty
  4459. */
  4460. public function set_publicated_on($publicated_on)
  4461. {
  4462. $course_id = api_get_course_int_id();
  4463. if ($this->debug > 0) {
  4464. error_log('New LP - In learnpath::set_expired_on()', 0);
  4465. }
  4466. if (!empty($publicated_on)) {
  4467. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4468. } else {
  4469. $this->publicated_on = '';
  4470. }
  4471. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4472. $lp_id = $this->get_id();
  4473. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4474. if ($this->debug > 2) {
  4475. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4476. }
  4477. Database::query($sql);
  4478. return true;
  4479. }
  4480. public function set_category_id($category_id)
  4481. {
  4482. $this->category_id = $category_id;
  4483. $course_id = api_get_course_int_id();
  4484. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4485. $lp_id = $this->get_id();
  4486. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4487. $category_id
  4488. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4489. Database::query($sql);
  4490. return true;
  4491. }
  4492. /**
  4493. * Sets and saves the expired_on date
  4494. * @param string Optional string giving the new author of this learnpath
  4495. * @return bool Returns true if author's name is not empty
  4496. */
  4497. public function set_modified_on()
  4498. {
  4499. $course_id = api_get_course_int_id();
  4500. if ($this->debug > 0) {
  4501. error_log('New LP - In learnpath::set_expired_on()', 0);
  4502. }
  4503. $this->modified_on = api_get_utc_datetime();
  4504. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4505. $lp_id = $this->get_id();
  4506. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4507. if ($this->debug > 2) {
  4508. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4509. }
  4510. Database::query($sql);
  4511. return true;
  4512. }
  4513. /**
  4514. * Sets the object's error message
  4515. * @param string Error message. If empty, reinits the error string
  4516. * @return void
  4517. */
  4518. public function set_error_msg($error = '')
  4519. {
  4520. if ($this->debug > 0) {
  4521. error_log('New LP - In learnpath::set_error_msg()', 0);
  4522. }
  4523. if (empty ($error)) {
  4524. $this->error = '';
  4525. } else {
  4526. $this->error .= $error;
  4527. }
  4528. }
  4529. /**
  4530. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4531. * @param boolean Whether to allow a new attempt or not
  4532. * @return boolean True
  4533. */
  4534. public function start_current_item($allow_new_attempt = false)
  4535. {
  4536. if ($this->debug > 0) {
  4537. error_log('New LP - In learnpath::start_current_item()', 0);
  4538. }
  4539. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4540. $type = $this->get_type();
  4541. $item_type = $this->items[$this->current]->get_type();
  4542. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4543. $this->items[$this->current]->open($allow_new_attempt);
  4544. $this->autocomplete_parents($this->current);
  4545. $prereq_check = $this->prerequisites_match($this->current);
  4546. $this->items[$this->current]->save(false, $prereq_check);
  4547. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4548. } else {
  4549. // If sco, then it is supposed to have been updated by some other call.
  4550. }
  4551. if ($item_type == 'sco') {
  4552. $this->items[$this->current]->restart();
  4553. }
  4554. }
  4555. if ($this->debug > 0) {
  4556. error_log('New LP - End of learnpath::start_current_item()', 0);
  4557. }
  4558. return true;
  4559. }
  4560. /**
  4561. * Stops the processing and counters for the old item (as held in $this->last)
  4562. * @return boolean True/False
  4563. */
  4564. public function stop_previous_item()
  4565. {
  4566. if ($this->debug > 0) {
  4567. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4568. }
  4569. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4570. if ($this->debug > 2) {
  4571. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4572. }
  4573. switch ($this->get_type()) {
  4574. case '3' :
  4575. if ($this->items[$this->last]->get_type() != 'au') {
  4576. if ($this->debug > 2) {
  4577. error_log(
  4578. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4579. 0
  4580. );
  4581. }
  4582. $this->items[$this->last]->close();
  4583. //$this->autocomplete_parents($this->last);
  4584. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4585. } else {
  4586. if ($this->debug > 2) {
  4587. error_log(
  4588. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4589. 0
  4590. );
  4591. }
  4592. }
  4593. case '2' :
  4594. if ($this->items[$this->last]->get_type() != 'sco') {
  4595. if ($this->debug > 2) {
  4596. error_log(
  4597. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4598. 0
  4599. );
  4600. }
  4601. $this->items[$this->last]->close();
  4602. //$this->autocomplete_parents($this->last);
  4603. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4604. } else {
  4605. if ($this->debug > 2) {
  4606. error_log(
  4607. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4608. 0
  4609. );
  4610. }
  4611. }
  4612. break;
  4613. case '1' :
  4614. default :
  4615. if ($this->debug > 2) {
  4616. error_log(
  4617. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4618. 0
  4619. );
  4620. }
  4621. $this->items[$this->last]->close();
  4622. break;
  4623. }
  4624. } else {
  4625. if ($this->debug > 2) {
  4626. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4627. }
  4628. return false;
  4629. }
  4630. return true;
  4631. }
  4632. /**
  4633. * Updates the default view mode from fullscreen to embedded and inversely
  4634. * @return string The current default view mode ('fullscreen' or 'embedded')
  4635. */
  4636. public function update_default_view_mode()
  4637. {
  4638. $course_id = api_get_course_int_id();
  4639. if ($this->debug > 0) {
  4640. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4641. }
  4642. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4643. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4644. $res = Database::query($sql);
  4645. if (Database :: num_rows($res) > 0) {
  4646. $row = Database :: fetch_array($res);
  4647. $default_view_mode = $row['default_view_mod'];
  4648. $view_mode = $default_view_mode;
  4649. switch ($default_view_mode) {
  4650. case 'fullscreen':
  4651. $view_mode = 'embedded';
  4652. break;
  4653. case 'embedded':
  4654. $view_mode = 'embedframe';
  4655. break;
  4656. case 'embedframe':
  4657. $view_mode = 'impress';
  4658. break;
  4659. case 'impress':
  4660. $view_mode = 'fullscreen';
  4661. break;
  4662. }
  4663. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4664. );
  4665. $res = Database::query($sql);
  4666. $this->mode = $view_mode;
  4667. return $view_mode;
  4668. } else {
  4669. if ($this->debug > 2) {
  4670. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4671. }
  4672. }
  4673. return -1;
  4674. }
  4675. /**
  4676. * Updates the default behaviour about auto-commiting SCORM updates
  4677. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4678. */
  4679. public function update_default_scorm_commit()
  4680. {
  4681. $course_id = api_get_course_int_id();
  4682. if ($this->debug > 0) {
  4683. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4684. }
  4685. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4686. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4687. $res = Database::query($sql);
  4688. if (Database :: num_rows($res) > 0) {
  4689. $row = Database :: fetch_array($res);
  4690. $force = $row['force_commit'];
  4691. if ($force == 1) {
  4692. $force = 0;
  4693. $force_return = false;
  4694. } elseif ($force == 0) {
  4695. $force = 1;
  4696. $force_return = true;
  4697. }
  4698. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4699. $res = Database::query($sql);
  4700. $this->force_commit = $force_return;
  4701. return $force_return;
  4702. } else {
  4703. if ($this->debug > 2) {
  4704. error_log(
  4705. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4706. 0
  4707. );
  4708. }
  4709. }
  4710. return -1;
  4711. }
  4712. /**
  4713. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4714. * @return bool True on success, false on failure
  4715. */
  4716. public function update_display_order()
  4717. {
  4718. $course_id = api_get_course_int_id();
  4719. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4720. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4721. $res = Database::query($sql);
  4722. if ($res === false) {
  4723. return false;
  4724. }
  4725. $lps = array();
  4726. $lp_order = array();
  4727. $num = Database :: num_rows($res);
  4728. // First check the order is correct, globally (might be wrong because
  4729. // of versions < 1.8.4).
  4730. if ($num > 0) {
  4731. $i = 1;
  4732. while ($row = Database :: fetch_array($res)) {
  4733. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4734. $need_fix = true;
  4735. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4736. $res_u = Database::query($sql_u);
  4737. }
  4738. $i++;
  4739. }
  4740. }
  4741. return true;
  4742. }
  4743. /**
  4744. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4745. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4746. */
  4747. public function update_reinit()
  4748. {
  4749. $course_id = api_get_course_int_id();
  4750. if ($this->debug > 0) {
  4751. error_log('New LP - In learnpath::update_reinit()', 0);
  4752. }
  4753. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4754. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4755. $res = Database::query($sql);
  4756. if (Database :: num_rows($res) > 0) {
  4757. $row = Database :: fetch_array($res);
  4758. $force = $row['prevent_reinit'];
  4759. if ($force == 1) {
  4760. $force = 0;
  4761. } elseif ($force == 0) {
  4762. $force = 1;
  4763. }
  4764. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4765. $res = Database::query($sql);
  4766. $this->prevent_reinit = $force;
  4767. return $force;
  4768. } else {
  4769. if ($this->debug > 2) {
  4770. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4771. }
  4772. }
  4773. return -1;
  4774. }
  4775. /**
  4776. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4777. *
  4778. * @return string 'single', 'multi' or 'seriousgame'
  4779. * @author ndiechburg <noel@cblue.be>
  4780. **/
  4781. public function get_attempt_mode()
  4782. {
  4783. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4784. $this->seriousgame_mode = 0;
  4785. }
  4786. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4787. $this->prevent_reinit = 1;
  4788. }
  4789. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4790. return 'seriousgame';
  4791. }
  4792. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4793. return 'single';
  4794. }
  4795. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4796. return 'multiple';
  4797. }
  4798. return 'single';
  4799. }
  4800. /**
  4801. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4802. *
  4803. * @param string 'seriousgame', 'single' or 'multiple'
  4804. * @return boolean
  4805. * @author ndiechburg <noel@cblue.be>
  4806. **/
  4807. public function set_attempt_mode($mode)
  4808. {
  4809. $course_id = api_get_course_int_id();
  4810. switch ($mode) {
  4811. case 'seriousgame' :
  4812. $sg_mode = 1;
  4813. $prevent_reinit = 1;
  4814. break;
  4815. case 'single' :
  4816. $sg_mode = 0;
  4817. $prevent_reinit = 1;
  4818. break;
  4819. case 'multiple' :
  4820. $sg_mode = 0;
  4821. $prevent_reinit = 0;
  4822. break;
  4823. default :
  4824. $sg_mode = 0;
  4825. $prevent_reinit = 0;
  4826. break;
  4827. }
  4828. $this->prevent_reinit = $prevent_reinit;
  4829. $this->seriousgame_mode = $sg_mode;
  4830. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4831. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4832. );
  4833. $res = Database::query($sql);
  4834. if ($res) {
  4835. return true;
  4836. } else {
  4837. return false;
  4838. }
  4839. }
  4840. /**
  4841. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4842. *
  4843. * @return boolean
  4844. * @author ndiechburg <noel@cblue.be>
  4845. **/
  4846. public function switch_attempt_mode()
  4847. {
  4848. if ($this->debug > 0) {
  4849. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4850. }
  4851. $mode = $this->get_attempt_mode();
  4852. switch ($mode) {
  4853. case 'single' :
  4854. $next_mode = 'multiple';
  4855. break;
  4856. case 'multiple' :
  4857. $next_mode = 'seriousgame';
  4858. break;
  4859. case 'seriousgame' :
  4860. $next_mode = 'single';
  4861. break;
  4862. default :
  4863. $next_mode = 'single';
  4864. break;
  4865. }
  4866. $this->set_attempt_mode($next_mode);
  4867. }
  4868. /**
  4869. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4870. *
  4871. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4872. * @author ndiechburg <noel@cblue.be>
  4873. **/
  4874. public function set_seriousgame_mode()
  4875. {
  4876. $course_id = api_get_course_int_id();
  4877. if ($this->debug > 0) {
  4878. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4879. }
  4880. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4881. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4882. $res = Database::query($sql);
  4883. if (Database :: num_rows($res) > 0) {
  4884. $row = Database :: fetch_array($res);
  4885. $force = $row['seriousgame_mode'];
  4886. if ($force == 1) {
  4887. $force = 0;
  4888. } elseif ($force == 0) {
  4889. $force = 1;
  4890. }
  4891. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4892. );
  4893. $res = Database::query($sql);
  4894. $this->seriousgame_mode = $force;
  4895. return $force;
  4896. } else {
  4897. if ($this->debug > 2) {
  4898. error_log(
  4899. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4900. 0
  4901. );
  4902. }
  4903. }
  4904. return -1;
  4905. }
  4906. /**
  4907. * Updates the "scorm_debug" value that shows or hide the debug window
  4908. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4909. */
  4910. public function update_scorm_debug()
  4911. {
  4912. $course_id = api_get_course_int_id();
  4913. if ($this->debug > 0) {
  4914. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4915. }
  4916. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4917. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4918. $res = Database::query($sql);
  4919. if (Database :: num_rows($res) > 0) {
  4920. $row = Database :: fetch_array($res);
  4921. $force = $row['debug'];
  4922. if ($force == 1) {
  4923. $force = 0;
  4924. } elseif ($force == 0) {
  4925. $force = 1;
  4926. }
  4927. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4928. $res = Database::query($sql);
  4929. $this->scorm_debug = $force;
  4930. return $force;
  4931. } else {
  4932. if ($this->debug > 2) {
  4933. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4934. }
  4935. }
  4936. return -1;
  4937. }
  4938. /**
  4939. * Function that makes a call to the function sort_tree_array and create_tree_array
  4940. * @author Kevin Van Den Haute
  4941. * @param array
  4942. */
  4943. public function tree_array($array)
  4944. {
  4945. if ($this->debug > 1) {
  4946. error_log('New LP - In learnpath::tree_array()', 0);
  4947. }
  4948. $array = $this->sort_tree_array($array);
  4949. $this->create_tree_array($array);
  4950. }
  4951. /**
  4952. * Creates an array with the elements of the learning path tree in it
  4953. *
  4954. * @author Kevin Van Den Haute
  4955. * @param array $array
  4956. * @param int $parent
  4957. * @param int $depth
  4958. * @param array $tmp
  4959. */
  4960. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4961. {
  4962. if ($this->debug > 1) {
  4963. error_log('New LP - In learnpath::create_tree_array())', 0);
  4964. }
  4965. if (is_array($array)) {
  4966. for ($i = 0; $i < count($array); $i++) {
  4967. if ($array[$i]['parent_item_id'] == $parent) {
  4968. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4969. $tmp[] = $array[$i]['parent_item_id'];
  4970. $depth++;
  4971. }
  4972. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4973. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4974. $this->arrMenu[] = array(
  4975. 'id' => $array[$i]['id'],
  4976. 'item_type' => $array[$i]['item_type'],
  4977. 'title' => $array[$i]['title'],
  4978. 'path' => isset($array[$i]['path']) ? $array[$i]['path'] : null,
  4979. 'description' => $array[$i]['description'],
  4980. 'parent_item_id' => $array[$i]['parent_item_id'],
  4981. 'previous_item_id' => $array[$i]['previous_item_id'],
  4982. 'next_item_id' => $array[$i]['next_item_id'],
  4983. 'min_score' => $array[$i]['min_score'],
  4984. 'max_score' => $array[$i]['max_score'],
  4985. 'mastery_score' => $array[$i]['mastery_score'],
  4986. 'display_order' => $array[$i]['display_order'],
  4987. 'prerequisite' => $preq,
  4988. 'depth' => $depth,
  4989. 'audio' => $audio
  4990. );
  4991. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4992. }
  4993. }
  4994. }
  4995. }
  4996. /**
  4997. * Sorts a multi dimensional array by parent id and display order
  4998. * @author Kevin Van Den Haute
  4999. *
  5000. * @param array $array (array with al the learning path items in it)
  5001. *
  5002. * @return array
  5003. */
  5004. public function sort_tree_array($array)
  5005. {
  5006. foreach ($array as $key => $row) {
  5007. $parent[$key] = $row['parent_item_id'];
  5008. $position[$key] = $row['display_order'];
  5009. }
  5010. if (count($array) > 0) {
  5011. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5012. }
  5013. return $array;
  5014. }
  5015. /**
  5016. * Function that creates a table structure with a learning path his modules, chapters and documents.
  5017. * Also the actions for the modules, chapters and documents are in this table.
  5018. * @author Kevin Van Den Haute
  5019. * @param int $lp_id
  5020. * @return string
  5021. */
  5022. public function overview()
  5023. {
  5024. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5025. if ($this->debug > 0) {
  5026. error_log('New LP - In learnpath::overview()', 0);
  5027. }
  5028. $_course = api_get_course_info();
  5029. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5030. $return = '';
  5031. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5032. // we need to start a form when we want to update all the mp3 files
  5033. if ($update_audio == 'true') {
  5034. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5035. $_GET['cidReq']
  5036. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5037. $_GET['action']
  5038. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5039. }
  5040. $return .= '<div id="message"></div>';
  5041. $return_audio = '<table class="data_table">';
  5042. $return_audio .= '<tr>';
  5043. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5044. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5045. $return_audio .= '</tr>';
  5046. if ($update_audio != 'true') {
  5047. $return .= '<div class="span12">';
  5048. $return .= self::return_new_tree($update_audio);
  5049. $return .= '</div>';
  5050. $return .= Display::div(
  5051. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5052. array('style' => 'float:left; margin-top:15px;width:100%')
  5053. );
  5054. } else {
  5055. $return_audio .= self::return_new_tree($update_audio);
  5056. $return .= $return_audio.'</table>';
  5057. }
  5058. // We need to close the form when we are updating the mp3 files.
  5059. if ($update_audio == 'true') {
  5060. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5061. 'SaveAudioAndOrganization'
  5062. ).'</button></div>'; // TODO: What kind of language variable is this?
  5063. }
  5064. // We need to close the form when we are updating the mp3 files.
  5065. if ($update_audio == 'true' && count($arrLP) != 0) {
  5066. $return .= '</form>';
  5067. }
  5068. return $return;
  5069. }
  5070. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5071. {
  5072. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5073. echo '<script>
  5074. var newOrderData= "";
  5075. function processChildren(parentId) {
  5076. //Loop through the children of the UL element defined by the parentId
  5077. var ulParentID= "UL_" + parentId;
  5078. $("#" + ulParentID).children().each(function () {
  5079. /*Only process elements with an id attribute (in order to skip the blank,
  5080. unmovable <li> elements.*/
  5081. if ($(this).attr("id")) {
  5082. /*Build a string of data with the childs ID and parent ID,
  5083. using the "|" as a delimiter between the two IDs and the "^"
  5084. as a record delimiter (these delimiters were chosen in case the data
  5085. involved includes more common delimiters like commas within the content)
  5086. */
  5087. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5088. //Determine if this child is a containter
  5089. if ($(this).is(".container")) {
  5090. //Process the child elements of the container
  5091. processChildren($(this).attr("id"));
  5092. }
  5093. }
  5094. }); //end of children loop
  5095. } //end of processChildren function
  5096. $(function() {
  5097. $(".item_data").on("mouseover", function(event) {
  5098. $(".button_actions", this).show();
  5099. });
  5100. $(".item_data").on("mouseout", function() {
  5101. $(".button_actions",this).hide();
  5102. });
  5103. $(".button_actions").hide();
  5104. $( ".lp_resource" ).sortable({
  5105. items: ".lp_resource_element ",
  5106. handle: ".moved", //only the class "moved"
  5107. cursor: "move",
  5108. connectWith: "#lp_item_list",
  5109. placeholder: "ui-state-highlight", //defines the yellow highlight
  5110. start: function(event, ui) {
  5111. $(ui.item).css("width", "160px");
  5112. $(ui.item).find(".item_data").attr("style", "");
  5113. },
  5114. stop: function(event, ui) {
  5115. $(ui.item).css("width", "100%");
  5116. },
  5117. });
  5118. $("#lp_item_list").sortable({
  5119. items: "li",
  5120. handle: ".moved", //only the class "moved"
  5121. cursor: "move",
  5122. placeholder: "ui-state-highlight", //defines the yellow highlight
  5123. update: function(event, ui) {
  5124. //Walk through the direct descendants of the lp_item_list <ul>
  5125. $("#lp_item_list").children().each(function () {
  5126. /*Only process elements with an id attribute (in order to skip the blank,
  5127. unmovable <li> elements.*/
  5128. if ($(this).attr("id")) {
  5129. /*Build a string of data with the child s ID and parent ID,
  5130. using the "|" as a delimiter between the two IDs and the "^"
  5131. as a record delimiter (these delimiters were chosen in case the data
  5132. involved includes more common delimiters like commas within the content)
  5133. */
  5134. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5135. //Determine if this child is a containter
  5136. if ($(this).is(".li_container")) {
  5137. //Process the child elements of the container
  5138. processChildren($(this).attr("id"));
  5139. }
  5140. }
  5141. }); //end of lp_item_list children loop
  5142. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5143. $.post("'.$ajax_url.'", order, function(reponse){
  5144. $("#message").html(reponse);
  5145. });
  5146. },
  5147. receive: function(event, ui) {
  5148. var id = $(ui.item).attr("data_id");
  5149. var type = $(ui.item).attr("data_type");
  5150. var title = $(ui.item).attr("title");
  5151. if (ui.item.parent()[0]) {
  5152. var parent_id = $(ui.item.parent()[0]).attr("id");
  5153. var previous_id = $(ui.item.prev()).attr("id");
  5154. if (parent_id) {
  5155. parent_id = parent_id.split("_")[1];
  5156. var params = {
  5157. "a": "add_lp_item",
  5158. "id": id,
  5159. "parent_id": parent_id,
  5160. "previous_id": previous_id,
  5161. "type": type,
  5162. "title" : title
  5163. };
  5164. $.ajax({
  5165. type: "GET",
  5166. url: "'.$ajax_url.'",
  5167. data: params,
  5168. async: false,
  5169. success: function(data) {
  5170. if (data == -1) {
  5171. } else {
  5172. $(".normal-message").hide();
  5173. $(ui.item).attr("id", data);
  5174. $(ui.item).addClass("lp_resource_element_new");
  5175. $(ui.item).find(".item_data").attr("style", "");
  5176. $(ui.item).addClass("record li_container");
  5177. $(ui.item).removeClass("lp_resource_element");
  5178. $(ui.item).removeClass("doc_resource");
  5179. }
  5180. }
  5181. });
  5182. }
  5183. }//
  5184. }//end receive
  5185. });
  5186. });
  5187. </script>';
  5188. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5189. $course_id = api_get_course_int_id();
  5190. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5191. $sql = "SELECT * FROM $tbl_lp_item
  5192. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5193. $result = Database::query($sql);
  5194. $arrLP = array();
  5195. while ($row = Database :: fetch_array($result)) {
  5196. $row['title'] = Security :: remove_XSS($row['title']);
  5197. $row['description'] = Security :: remove_XSS($row['description']);
  5198. $arrLP[] = array(
  5199. 'id' => $row['id'],
  5200. 'item_type' => $row['item_type'],
  5201. 'title' => $row['title'],
  5202. 'path' => $row['path'],
  5203. 'description' => $row['description'],
  5204. 'parent_item_id' => $row['parent_item_id'],
  5205. 'previous_item_id' => $row['previous_item_id'],
  5206. 'next_item_id' => $row['next_item_id'],
  5207. 'max_score' => $row['max_score'],
  5208. 'min_score' => $row['min_score'],
  5209. 'mastery_score' => $row['mastery_score'],
  5210. 'prerequisite' => $row['prerequisite'],
  5211. 'display_order' => $row['display_order'],
  5212. 'audio' => $row['audio']
  5213. );
  5214. }
  5215. $this->tree_array($arrLP);
  5216. $arrLP = $this->arrMenu;
  5217. unset ($this->arrMenu);
  5218. $default_data = null;
  5219. $default_content = null;
  5220. $elements = array();
  5221. $return_audio = null;
  5222. $iconSysPath = api_get_path(SYS_IMG_PATH);
  5223. for ($i = 0; $i < count($arrLP); $i++) {
  5224. $title = $arrLP[$i]['title'];
  5225. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5226. //Link for the documents
  5227. if ($arrLP[$i]['item_type'] == 'document') {
  5228. $url = api_get_self().'?'.api_get_cidreq(
  5229. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5230. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5231. }
  5232. if (($i % 2) == 0) {
  5233. $oddclass = 'row_odd';
  5234. } else {
  5235. $oddclass = 'row_even';
  5236. }
  5237. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5238. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5239. $icon = '';
  5240. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5241. $icon = Display::return_icon('lp_'.$icon_name.'.png');
  5242. } else {
  5243. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5244. $icon = Display::return_icon('lp_'.$icon_name.'.gif');
  5245. } else {
  5246. $icon = Display::return_icon('folder_document.gif');
  5247. }
  5248. }
  5249. // The audio column.
  5250. $return_audio .= '<td align="center">';
  5251. $audio = '';
  5252. if (!$update_audio OR $update_audio <> 'true') {
  5253. if (!empty($arrLP[$i]['audio'])) {
  5254. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5255. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5256. $audio .= '<script type="text/javascript">
  5257. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5258. s1.addParam("allowscriptaccess","always");
  5259. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5260. s1.write("container' . $i . '");
  5261. </script>';*/
  5262. } else {
  5263. $audio .= '';
  5264. }
  5265. } else {
  5266. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5267. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5268. if (!empty ($arrLP[$i]['audio'])) {
  5269. $audio .= '<br />'.Security::remove_XSS(
  5270. $arrLP[$i]['audio']
  5271. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5272. 'RemoveAudio'
  5273. );
  5274. }
  5275. }
  5276. }
  5277. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5278. $return_audio .= '</td>';
  5279. $move_icon = '';
  5280. $move_item_icon = '';
  5281. $edit_icon = '';
  5282. $delete_icon = '';
  5283. $audio_icon = '';
  5284. $prerequisities_icon = '';
  5285. if ($is_allowed_to_edit) {
  5286. if (!$update_audio OR $update_audio <> 'true') {
  5287. $move_icon .= '<a class="moved" href="#">';
  5288. $move_icon .= Display::return_icon(
  5289. 'move_everywhere.png',
  5290. get_lang('Move'),
  5291. array(),
  5292. ICON_SIZE_TINY
  5293. );
  5294. $move_icon .= '</a>';
  5295. }
  5296. // No edit for this item types
  5297. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5298. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5299. $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'].'">';
  5300. $edit_icon .= Display::return_icon(
  5301. 'edit.png',
  5302. get_lang('LearnpathEditModule'),
  5303. array(),
  5304. ICON_SIZE_TINY
  5305. );
  5306. $edit_icon .= '</a>';
  5307. } else {
  5308. $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'].'">';
  5309. $edit_icon .= Display::return_icon('edit.png',
  5310. get_lang('LearnpathEditModule'),
  5311. array(),
  5312. ICON_SIZE_TINY
  5313. );
  5314. $edit_icon .= '</a>';
  5315. }
  5316. }
  5317. $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(
  5318. $title
  5319. ).'\');">';
  5320. $delete_icon .= Display::return_icon(
  5321. 'delete.png',
  5322. get_lang('LearnpathDeleteModule'),
  5323. array(),
  5324. ICON_SIZE_TINY
  5325. );
  5326. $delete_icon .= '</a>';
  5327. $url = api_get_self().'?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5328. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5329. $prerequisities_icon = Display::url(
  5330. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5331. $url.'&action=edit_item_prereq'
  5332. );
  5333. $move_item_icon = Display::url(
  5334. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5335. $url.'&action=move_item'
  5336. );
  5337. $audio_icon = Display::url(
  5338. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5339. $url.'&action=add_audio'
  5340. );
  5341. }
  5342. }
  5343. if ($update_audio != 'true') {
  5344. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5345. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5346. array('class' => 'button_actions')
  5347. );
  5348. } else {
  5349. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5350. }
  5351. $parent_id = $arrLP[$i]['parent_item_id'];
  5352. $default_data[$arrLP[$i]['id']] = $row;
  5353. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5354. if (empty($parent_id)) {
  5355. $elements[$arrLP[$i]['id']]['data'] = $row;
  5356. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5357. } else {
  5358. $parent_arrays = array();
  5359. if ($arrLP[$i]['depth'] > 1) {
  5360. //Getting list of parents
  5361. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5362. foreach ($arrLP as $item) {
  5363. if ($item['id'] == $parent_id) {
  5364. if ($item['parent_item_id'] == 0) {
  5365. $parent_id = $item['id'];
  5366. break;
  5367. } else {
  5368. $parent_id = $item['parent_item_id'];
  5369. if (empty($parent_arrays)) {
  5370. $parent_arrays[] = intval($item['id']);
  5371. }
  5372. $parent_arrays[] = $parent_id;
  5373. break;
  5374. }
  5375. }
  5376. }
  5377. }
  5378. }
  5379. if (!empty($parent_arrays)) {
  5380. $parent_arrays = array_reverse($parent_arrays);
  5381. $val = '$elements';
  5382. $x = 0;
  5383. foreach ($parent_arrays as $item) {
  5384. if ($x != count($parent_arrays) - 1) {
  5385. $val .= '["'.$item.'"]["children"]';
  5386. } else {
  5387. $val .= '["'.$item.'"]["children"]';
  5388. }
  5389. $x++;
  5390. }
  5391. $val .= "";
  5392. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5393. eval($code_str);
  5394. } else {
  5395. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5396. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5397. }
  5398. }
  5399. }
  5400. $return = '<div class="lp_tree well">';
  5401. $return .= '<ul id="lp_item_list">';
  5402. $return .= '<h4>'.$this->name.'</h4><br>';
  5403. $tree = self::print_recursive($elements, $default_data, $default_content);
  5404. if (!empty($tree)) {
  5405. $return .= $tree;
  5406. } else {
  5407. if ($drop_element_here) {
  5408. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5409. }
  5410. }
  5411. $return .= '</ul>';
  5412. if ($update_audio == 'true') {
  5413. $return = $return_audio;
  5414. } else {
  5415. $return .= '</div>';
  5416. }
  5417. return $return;
  5418. }
  5419. function print_recursive($elements, $default_data, $default_content)
  5420. {
  5421. $return = '';
  5422. foreach ($elements as $key => $item) {
  5423. if (isset($item['load_data']) || empty($item['data'])) {
  5424. $item['data'] = $default_data[$item['load_data']];
  5425. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5426. }
  5427. $sub_list = '';
  5428. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5429. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5430. }
  5431. if (empty($item['children'])) {
  5432. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5433. $active = null;
  5434. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5435. $active = 'active';
  5436. }
  5437. $return .= Display::tag(
  5438. 'li',
  5439. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5440. array('id' => $key, 'class' => 'record li_container')
  5441. );
  5442. } else {
  5443. //sections
  5444. if (isset($item['children'])) {
  5445. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5446. }
  5447. $sub_list = Display::tag(
  5448. 'ul',
  5449. $sub_list.$data,
  5450. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5451. );
  5452. $return .= Display::tag(
  5453. 'li',
  5454. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5455. array('id' => $key, 'class' => 'record li_container')
  5456. );
  5457. }
  5458. }
  5459. return $return;
  5460. }
  5461. /**
  5462. * This function builds the action menu
  5463. * @return void
  5464. */
  5465. public function build_action_menu()
  5466. {
  5467. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5468. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5469. $return = '<div class="actions">';
  5470. //$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>';
  5471. //$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>';
  5472. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5473. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5474. 'preview_view.png',
  5475. get_lang('Display'),
  5476. '',
  5477. ICON_SIZE_MEDIUM
  5478. ).'</a> ';
  5479. //$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>';
  5480. // 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>';
  5481. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5482. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5483. 'upload_audio.png',
  5484. get_lang('UpdateAllAudioFragments'),
  5485. '',
  5486. ICON_SIZE_MEDIUM
  5487. ).'</a>';*/
  5488. $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(
  5489. 'settings.png',
  5490. get_lang('CourseSettings'),
  5491. '',
  5492. ICON_SIZE_MEDIUM
  5493. ).'</a>';
  5494. $buttons = array(
  5495. array(
  5496. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5497. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5498. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5499. ),
  5500. array(
  5501. 'title' => get_lang('ClearAllPrerequisites'),
  5502. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5503. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5504. ),
  5505. );
  5506. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5507. $return .= '</div>';
  5508. echo $return;
  5509. }
  5510. /**
  5511. * This functions builds the LP tree based on data from the database.
  5512. * @return string
  5513. * @deprecated use the return_new_tree() function
  5514. * @uses dtree.js :: necessary javascript for building this tree
  5515. */
  5516. public function build_tree()
  5517. {
  5518. $iconSysPath = api_get_path(SYS_IMG_PATH);
  5519. $iconWebPath = api_get_path(WEB_IMG_PATH);
  5520. $course_id = api_get_course_int_id();
  5521. $return = "<script type=\"text/javascript\">";
  5522. $return .= "\tm = new dTree('m');\n\n";
  5523. $return .= "\tm.config.folderLinks = true;\n";
  5524. $return .= "\tm.config.useCookies = true;\n";
  5525. $return .= "\tm.config.useIcons = true;\n";
  5526. $return .= "\tm.config.useLines = true;\n";
  5527. $return .= "\tm.config.useSelection = true;\n";
  5528. $return .= "\tm.config.useStatustext = false;\n\n";
  5529. $menu = 0;
  5530. $parent = '';
  5531. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5532. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5533. $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
  5534. FROM $tbl_lp_item
  5535. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5536. $result = Database::query($sql);
  5537. $arrLP = array();
  5538. while ($row = Database :: fetch_array($result)) {
  5539. $row['title'] = Security :: remove_XSS($row['title']);
  5540. $row['description'] = Security :: remove_XSS($row['description']);
  5541. $arrLP[] = array(
  5542. 'id' => $row['id'],
  5543. 'item_type' => $row['item_type'],
  5544. 'title' => $row['title'],
  5545. 'path' => $row['path'],
  5546. 'description' => $row['description'],
  5547. 'parent_item_id' => $row['parent_item_id'],
  5548. 'previous_item_id' => $row['previous_item_id'],
  5549. 'next_item_id' => $row['next_item_id'],
  5550. 'max_score' => $row['max_score'],
  5551. 'min_score' => $row['min_score'],
  5552. 'mastery_score' => $row['mastery_score'],
  5553. 'display_order' => $row['display_order']
  5554. );
  5555. }
  5556. $this->tree_array($arrLP);
  5557. $arrLP = $this->arrMenu;
  5558. unset($this->arrMenu);
  5559. $title = '';
  5560. for ($i = 0; $i < count($arrLP); $i++) {
  5561. $title = addslashes($arrLP[$i]['title']);
  5562. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5563. $_GET['cidReq']
  5564. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5565. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5566. if (file_exists($iconSysPath.'lp_'.$icon_name.'.png')) {
  5567. $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";
  5568. } else {
  5569. if (file_exists($iconSysPath.'lp_'.$icon_name.'.gif')) {
  5570. $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";
  5571. } else {
  5572. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '".$iconWebPath."folder_document.gif', '".$iconWebPath."folder_document.gif');\n";
  5573. }
  5574. }
  5575. if ($menu < $arrLP[$i]['id']) {
  5576. $menu = $arrLP[$i]['id'];
  5577. }
  5578. }
  5579. $return .= "\n\tdocument.write(m);\n";
  5580. $return .= "\t if(!m.selectedNode) m.s(1);";
  5581. $return .= "</script>\n";
  5582. return $return;
  5583. }
  5584. /**
  5585. * Creates the default learning path folder
  5586. */
  5587. public static function generate_learning_path_folder($course)
  5588. {
  5589. //Creating learning_path folder
  5590. $dir = '/learning_path';
  5591. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5592. $folder = false;
  5593. if (!is_dir($filepath.'/'.$dir)) {
  5594. $folder = FileManager::create_unexisting_directory(
  5595. $course,
  5596. api_get_user_id(),
  5597. api_get_session_id(),
  5598. 0,
  5599. 0,
  5600. $filepath,
  5601. $dir,
  5602. get_lang('LearningPaths'),
  5603. 0
  5604. );
  5605. } else {
  5606. $folder = true;
  5607. }
  5608. return $folder;
  5609. }
  5610. public function generate_lp_folder($course, $lp_name = null)
  5611. {
  5612. $filepath = '';
  5613. $dir = '/learning_path/';
  5614. if (empty($lp_name)) {
  5615. $lp_name = $this->name;
  5616. }
  5617. $folder = self::generate_learning_path_folder($course);
  5618. //Creating LP folder
  5619. if ($folder) {
  5620. //Limits title size
  5621. $title = api_substr(api_replace_dangerous_char($lp_name), 0, 80);
  5622. $dir = $dir.$title;
  5623. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5624. if (!is_dir($filepath.'/'.$dir)) {
  5625. $folder = FileManager::create_unexisting_directory(
  5626. $course,
  5627. api_get_user_id(),
  5628. 0,
  5629. 0,
  5630. 0,
  5631. $filepath,
  5632. $dir,
  5633. $lp_name
  5634. );
  5635. } else {
  5636. $folder = true;
  5637. }
  5638. $dir = $dir.'/';
  5639. if ($folder) {
  5640. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5641. }
  5642. }
  5643. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5644. return $array;
  5645. }
  5646. /**
  5647. * Create a new document //still needs some finetuning
  5648. * @param array $_course
  5649. * @return string
  5650. */
  5651. public function create_document($_course)
  5652. {
  5653. $course_id = api_get_course_int_id();
  5654. global $charset;
  5655. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5656. if (strstr($dir, '..')) {
  5657. $dir = '/';
  5658. }
  5659. if ($dir[0] == '.') {
  5660. $dir = substr($dir, 1);
  5661. }
  5662. if ($dir[0] != '/') {
  5663. $dir = '/'.$dir;
  5664. }
  5665. if ($dir[strlen($dir) - 1] != '/') {
  5666. $dir .= '/';
  5667. }
  5668. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5669. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5670. //Generates folder
  5671. $result = $this->generate_lp_folder($_course);
  5672. $dir = $result['dir'];
  5673. $filepath = $result['filepath'];
  5674. }
  5675. if (!is_dir($filepath)) {
  5676. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5677. $dir = '/';
  5678. }
  5679. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5680. // is already escaped twice when it gets here.
  5681. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5682. $title = FileManager::disable_dangerous_file($title);
  5683. $filename = $title;
  5684. $content = $_POST['content_lp'];
  5685. $tmp_filename = $filename;
  5686. $i = 0;
  5687. while (file_exists($filepath.$tmp_filename.'.html')) {
  5688. $tmp_filename = $filename.'_'.++$i;
  5689. }
  5690. $filename = $tmp_filename.'.html';
  5691. $content = stripslashes($content);
  5692. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5693. // Change the path of mp3 to absolute.
  5694. // The first regexp deals with ../../../ urls.
  5695. $content = preg_replace(
  5696. "|(flashvars=\"file=)(\.+/)+|",
  5697. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5698. $content
  5699. );
  5700. // The second regexp deals with audio/ urls.
  5701. $content = preg_replace(
  5702. "|(flashvars=\"file=)([^/]+)/|",
  5703. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5704. $content
  5705. );
  5706. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5707. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5708. if (!file_exists($filepath.$filename)) {
  5709. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5710. fputs($fp, $content);
  5711. fclose($fp);
  5712. $file_size = filesize($filepath.$filename);
  5713. $save_file_path = $dir.$filename;
  5714. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5715. if ($document_id) {
  5716. api_item_property_update(
  5717. $_course,
  5718. TOOL_DOCUMENT,
  5719. $document_id,
  5720. 'DocumentAdded',
  5721. api_get_user_id(),
  5722. null,
  5723. null,
  5724. null,
  5725. null,
  5726. api_get_session_id()
  5727. );
  5728. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5729. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5730. if ($new_comment || $new_title) {
  5731. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5732. $ct = '';
  5733. if ($new_comment) {
  5734. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5735. }
  5736. if ($new_title) {
  5737. $ct .= ", title='".Database::escape_string(
  5738. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5739. )."' ";
  5740. }
  5741. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5742. $ct,
  5743. 1
  5744. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5745. Database::query($sql_update);
  5746. }
  5747. }
  5748. return $document_id;
  5749. }
  5750. }
  5751. }
  5752. /**
  5753. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5754. * @param array $_course array
  5755. * @return void
  5756. */
  5757. public function edit_document($_course)
  5758. {
  5759. $course_id = api_get_course_int_id();
  5760. global $_configuration;
  5761. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5762. if (strstr($dir, '..')) {
  5763. $dir = '/';
  5764. }
  5765. if ($dir[0] == '.') {
  5766. $dir = substr($dir, 1);
  5767. }
  5768. if ($dir[0] != '/') {
  5769. $dir = '/'.$dir;
  5770. }
  5771. if ($dir[strlen($dir) - 1] != '/') {
  5772. $dir .= '/';
  5773. }
  5774. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5775. if (!is_dir($filepath)) {
  5776. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5777. $dir = '/';
  5778. }
  5779. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5780. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5781. $document_id = intval($_POST['path']);
  5782. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5783. $res = Database::query($sql);
  5784. $row = Database :: fetch_array($res);
  5785. $content = stripslashes($_POST['content_lp']);
  5786. $file = $filepath.$row['path'];
  5787. if ($fp = @ fopen($file, 'w')) {
  5788. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/',$content);
  5789. // Change the path of mp3 to absolute.
  5790. // The first regexp deals with ../../../ urls.
  5791. $content = preg_replace(
  5792. "|(flashvars=\"file=)(\.+/)+|",
  5793. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5794. $content
  5795. );
  5796. // The second regexp deals with audio/ urls.
  5797. $content = preg_replace(
  5798. "|(flashvars=\"file=)([^/]+)/|",
  5799. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5800. $content
  5801. );
  5802. fputs($fp, $content);
  5803. fclose($fp);
  5804. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5805. $_POST['title']
  5806. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5807. Database::query($sql_update);
  5808. }
  5809. }
  5810. }
  5811. /**
  5812. * Displays the selected item, with a panel for manipulating the item
  5813. * @param int $item_id
  5814. * @param string $msg
  5815. * @return string
  5816. */
  5817. public function display_item($item_id, $msg = null, $show_actions = true)
  5818. {
  5819. $course_id = api_get_course_int_id();
  5820. $return = '';
  5821. if (is_numeric($item_id)) {
  5822. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5823. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5824. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5825. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5826. $result = Database::query($sql);
  5827. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5828. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5829. // Prevents wrong parent selection for document, see Bug#1251.
  5830. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5831. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5832. }
  5833. if ($show_actions) {
  5834. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5835. }
  5836. $return .= '<div style="padding:10px;">';
  5837. if ($msg != '') {
  5838. $return .= $msg;
  5839. }
  5840. $return .= '<h3>'.$row['title'].'</h3>';
  5841. switch ($row['item_type']) {
  5842. case TOOL_QUIZ:
  5843. if (!empty($row['path'])) {
  5844. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5845. $exercise = new Exercise();
  5846. $exercise->read($row['path']);
  5847. $return .= $exercise->description.'<br />';
  5848. }
  5849. break;
  5850. case TOOL_DOCUMENT:
  5851. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5852. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5853. $row['path']
  5854. );
  5855. $result = Database::query($sql_doc);
  5856. $path_file = Database::result($result, 0, 0);
  5857. $path_parts = pathinfo($path_file);
  5858. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5859. if (in_array(
  5860. $path_parts['extension'],
  5861. array(
  5862. 'html',
  5863. 'txt',
  5864. 'png',
  5865. 'jpg',
  5866. 'JPG',
  5867. 'jpeg',
  5868. 'JPEG',
  5869. 'gif',
  5870. 'swf'
  5871. )
  5872. )
  5873. ) {
  5874. $return .= $this->display_document($row['path'], true, true);
  5875. }
  5876. break;
  5877. }
  5878. $return .= '</div>';
  5879. }
  5880. }
  5881. return $return;
  5882. }
  5883. /**
  5884. * Shows the needed forms for editing a specific item
  5885. * @param int $item_id
  5886. * @return string
  5887. */
  5888. public function display_edit_item($item_id)
  5889. {
  5890. $_course = api_get_course_info(); // It will disappear.
  5891. $course_id = api_get_course_int_id();
  5892. $return = '';
  5893. if (is_numeric($item_id)) {
  5894. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5895. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5896. $res = Database::query($sql);
  5897. $row = Database::fetch_array($res);
  5898. switch ($row['item_type']) {
  5899. case 'dokeos_chapter' :
  5900. case 'dir' :
  5901. case 'asset' :
  5902. case 'sco' :
  5903. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5904. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5905. $return .= $this->display_item_form(
  5906. $row['item_type'],
  5907. get_lang('EditCurrentChapter').' :',
  5908. 'edit',
  5909. $item_id,
  5910. $row
  5911. );
  5912. } else {
  5913. $return .= $this->display_item_small_form(
  5914. $row['item_type'],
  5915. get_lang('EditCurrentChapter').' :',
  5916. $row
  5917. );
  5918. }
  5919. break;
  5920. case TOOL_DOCUMENT :
  5921. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5922. $sql_step = " SELECT lp.*, doc.path as dir
  5923. FROM ".$tbl_lp_item." as lp
  5924. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5925. WHERE lp.c_id = $course_id AND
  5926. doc.c_id = $course_id AND
  5927. lp.id = ".Database::escape_string($item_id);
  5928. $res_step = Database::query($sql_step);
  5929. $row_step = Database :: fetch_array($res_step);
  5930. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5931. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5932. break;
  5933. case TOOL_LINK :
  5934. $link_id = (string)$row['path'];
  5935. if (ctype_digit($link_id)) {
  5936. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5937. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5938. $link_id
  5939. );
  5940. $res_link = Database::query($sql_select);
  5941. $row_link = Database :: fetch_array($res_link);
  5942. if (is_array($row_link)) {
  5943. $row['url'] = $row_link['url'];
  5944. }
  5945. }
  5946. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5947. $return .= $this->display_link_form('edit', $item_id, $row);
  5948. break;
  5949. case 'dokeos_module' :
  5950. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5951. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5952. $return .= $this->display_item_form(
  5953. $row['item_type'],
  5954. get_lang('EditCurrentModule').' :',
  5955. 'edit',
  5956. $item_id,
  5957. $row
  5958. );
  5959. } else {
  5960. $return .= $this->display_item_small_form(
  5961. $row['item_type'],
  5962. get_lang('EditCurrentModule').' :',
  5963. $row
  5964. );
  5965. }
  5966. break;
  5967. case TOOL_QUIZ :
  5968. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5969. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5970. break;
  5971. case TOOL_HOTPOTATOES :
  5972. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5973. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5974. break;
  5975. case TOOL_STUDENTPUBLICATION :
  5976. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5977. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5978. break;
  5979. case TOOL_FORUM :
  5980. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5981. $return .= $this->display_forum_form('edit', $item_id, $row);
  5982. break;
  5983. case TOOL_THREAD :
  5984. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5985. $return .= $this->display_thread_form('edit', $item_id, $row);
  5986. break;
  5987. }
  5988. }
  5989. return $return;
  5990. }
  5991. /**
  5992. * Function that displays a list with al the resources that could be added to the learning path
  5993. * @return string
  5994. */
  5995. public function display_resources()
  5996. {
  5997. $_course = api_get_course_info(); // TODO: Don't use globals.
  5998. $course_code = api_get_course_id();
  5999. //Get all the docs
  6000. $documents = $this->get_documents();
  6001. //Get all the exercises
  6002. $exercises = $this->get_exercises();
  6003. // Get all the links
  6004. $links = $this->get_links();
  6005. //Get al the student publications
  6006. $works = $this->get_student_publications();
  6007. //Get al the forums
  6008. $forums = $this->get_forums(null, $course_code);
  6009. $headers = array(
  6010. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  6011. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  6012. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  6013. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  6014. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  6015. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  6016. );
  6017. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6018. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6019. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6020. return true;
  6021. }
  6022. /**
  6023. * Returns the extension of a document
  6024. * @param string filename
  6025. * @return string Extension (part after the last dot)
  6026. */
  6027. public function get_extension($filename)
  6028. {
  6029. $explode = explode('.', $filename);
  6030. return $explode[count($explode) - 1];
  6031. }
  6032. /**
  6033. * Displays a document by id
  6034. *
  6035. * @param int $id
  6036. * @return string
  6037. */
  6038. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6039. {
  6040. $_course = api_get_course_info(); // It is temporary.
  6041. $course_id = api_get_course_int_id();
  6042. $return = '';
  6043. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6044. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6045. WHERE c_id = ".$course_id." AND id = ".$id;
  6046. $res_doc = Database::query($sql_doc);
  6047. $row_doc = Database :: fetch_array($res_doc);
  6048. // TODO: Add a path filter.
  6049. if ($iframe) {
  6050. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq();
  6051. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.$url.'"></iframe>';
  6052. } else {
  6053. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6054. }
  6055. return $return;
  6056. }
  6057. /**
  6058. * Return HTML form to add/edit a quiz
  6059. * @param string Action (add/edit)
  6060. * @param integer Item ID if already exists
  6061. * @param mixed Extra information (quiz ID if integer)
  6062. * @return string HTML form
  6063. */
  6064. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6065. {
  6066. $course_id = api_get_course_int_id();
  6067. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6068. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6069. if ($id != 0 && is_array($extra_info)) {
  6070. $item_title = $extra_info['title'];
  6071. $item_description = $extra_info['description'];
  6072. } elseif (is_numeric($extra_info)) {
  6073. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz."
  6074. WHERE c_id = ".$course_id." AND iid = ".$extra_info;
  6075. $result = Database::query($sql_quiz);
  6076. $row = Database :: fetch_array($result);
  6077. $item_title = $row['title'];
  6078. $item_description = $row['description'];
  6079. } else {
  6080. $item_title = '';
  6081. $item_description = '';
  6082. }
  6083. $item_title = Security::remove_XSS($item_title);
  6084. $item_description = Security::remove_XSS($item_description);
  6085. $legend = '<legend>';
  6086. if ($id != 0 && is_array($extra_info)) {
  6087. $parent = $extra_info['parent_item_id'];
  6088. } else {
  6089. $parent = 0;
  6090. }
  6091. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6092. $result = Database::query($sql);
  6093. $arrLP = array();
  6094. while ($row = Database :: fetch_array($result)) {
  6095. $arrLP[] = array(
  6096. 'id' => $row['id'],
  6097. 'item_type' => $row['item_type'],
  6098. 'title' => $row['title'],
  6099. 'path' => $row['path'],
  6100. 'description' => $row['description'],
  6101. 'parent_item_id' => $row['parent_item_id'],
  6102. 'previous_item_id' => $row['previous_item_id'],
  6103. 'next_item_id' => $row['next_item_id'],
  6104. 'display_order' => $row['display_order'],
  6105. 'max_score' => $row['max_score'],
  6106. 'min_score' => $row['min_score'],
  6107. 'mastery_score' => $row['mastery_score'],
  6108. 'prerequisite' => $row['prerequisite'],
  6109. 'max_time_allowed' => $row['max_time_allowed']
  6110. );
  6111. }
  6112. $this->tree_array($arrLP);
  6113. $arrLP = $this->arrMenu;
  6114. unset ($this->arrMenu);
  6115. if ($action == 'add') {
  6116. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6117. } elseif ($action == 'move') {
  6118. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6119. } else {
  6120. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6121. }
  6122. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6123. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6124. }
  6125. $legend .= '</legend>';
  6126. $return = '<div class="sectioncomment">';
  6127. $return .= '<form method="POST">';
  6128. $return .= $legend;
  6129. $return .= '<table class="lp_form">';
  6130. if ($action != 'move') {
  6131. $return .= '<tr>';
  6132. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6133. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6134. $return .= '</tr>';
  6135. }
  6136. $return .= '<tr>';
  6137. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6138. $return .= '<td class="input">';
  6139. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6140. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6141. $arrHide = array(
  6142. $id
  6143. );
  6144. //$parent_item_id = $_SESSION['parent_item_id'];
  6145. for ($i = 0; $i < count($arrLP); $i++) {
  6146. if ($action != 'add') {
  6147. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6148. $arrLP[$i]['id'],
  6149. $arrHide
  6150. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6151. ) {
  6152. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6153. } else {
  6154. $arrHide[] = $arrLP[$i]['id'];
  6155. }
  6156. } else {
  6157. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6158. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6159. }
  6160. }
  6161. }
  6162. if (is_array($arrLP)) {
  6163. reset($arrLP);
  6164. }
  6165. $return .= '</select>';
  6166. $return .= '</td>';
  6167. $return .= '</tr>';
  6168. $return .= '<tr>';
  6169. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6170. $return .= '<td class="input">';
  6171. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6172. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6173. for ($i = 0; $i < count($arrLP); $i++) {
  6174. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6175. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6176. $selected = 'selected="selected" ';
  6177. } elseif ($action == 'add') {
  6178. $selected = 'selected="selected" ';
  6179. } else {
  6180. $selected = '';
  6181. }
  6182. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6183. 'After'
  6184. ).' "'.$arrLP[$i]['title'].'"</option>';
  6185. }
  6186. }
  6187. $return .= '</select>';
  6188. $return .= '</td>';
  6189. $return .= '</tr>';
  6190. if ($action != 'move') {
  6191. $id_prerequisite = 0;
  6192. if (is_array($arrLP)) {
  6193. foreach ($arrLP as $key => $value) {
  6194. if ($value['id'] == $id) {
  6195. $id_prerequisite = $value['prerequisite'];
  6196. break;
  6197. }
  6198. }
  6199. }
  6200. $arrHide = array();
  6201. for ($i = 0; $i < count($arrLP); $i++) {
  6202. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6203. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6204. $s_selected_position = $arrLP[$i]['id'];
  6205. } elseif ($action == 'add') {
  6206. $s_selected_position = 0;
  6207. }
  6208. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6209. }
  6210. }
  6211. /*// Commented the prerequisites, only visible in edit (exercise).
  6212. $return .= '<tr>';
  6213. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6214. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6215. foreach($arrHide as $key => $value){
  6216. if($key==$s_selected_position && $action == 'add'){
  6217. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6218. }
  6219. elseif($key==$id_prerequisite && $action == 'edit'){
  6220. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6221. }
  6222. else{
  6223. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6224. }
  6225. }
  6226. $return .= "</select></td>";
  6227. */
  6228. $return .= '</tr>';
  6229. /*$return .= '<tr>';
  6230. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6231. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6232. // Remove temporarily the test description.
  6233. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6234. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6235. $return .= '</tr>'; */
  6236. }
  6237. $return .= '<tr>';
  6238. if ($action == 'add') {
  6239. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6240. 'AddExercise'
  6241. ).'</button></td>';
  6242. } else {
  6243. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6244. 'EditCurrentExecice'
  6245. ).'</button></td>';
  6246. }
  6247. $return .= '</tr>';
  6248. $return .= '</table>';
  6249. if ($action == 'move') {
  6250. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6251. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6252. }
  6253. if (is_numeric($extra_info)) {
  6254. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6255. } elseif (is_array($extra_info)) {
  6256. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6257. }
  6258. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6259. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6260. $return .= '</form>';
  6261. $return .= '</div>';
  6262. return $return;
  6263. }
  6264. /**
  6265. * Addition of Hotpotatoes tests
  6266. * @param string Action
  6267. * @param integer Internal ID of the item
  6268. * @param mixed Extra information - can be an array with title and description indexes
  6269. * @return string HTML structure to display the hotpotatoes addition formular
  6270. */
  6271. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6272. {
  6273. $course_id = api_get_course_int_id();
  6274. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6275. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6276. if ($id != 0 && is_array($extra_info)) {
  6277. $item_title = stripslashes($extra_info['title']);
  6278. $item_description = stripslashes($extra_info['description']);
  6279. } elseif (is_numeric($extra_info)) {
  6280. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6281. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6282. WHERE c_id = ".$course_id." AND
  6283. path LIKE '".$uploadPath."/%/%htm%' AND
  6284. id = ".(int)$extra_info."
  6285. ORDER BY id ASC";
  6286. $res_hot = Database::query($sql_hot);
  6287. $row = Database::fetch_array($res_hot);
  6288. $item_title = $row['title'];
  6289. $item_description = $row['description'];
  6290. if (!empty ($row['comment'])) {
  6291. $item_title = $row['comment'];
  6292. }
  6293. } else {
  6294. $item_title = '';
  6295. $item_description = '';
  6296. }
  6297. if ($id != 0 && is_array($extra_info)) {
  6298. $parent = $extra_info['parent_item_id'];
  6299. } else {
  6300. $parent = 0;
  6301. }
  6302. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6303. $result = Database::query($sql);
  6304. $arrLP = array();
  6305. while ($row = Database :: fetch_array($result)) {
  6306. $arrLP[] = array(
  6307. 'id' => $row['id'],
  6308. 'item_type' => $row['item_type'],
  6309. 'title' => $row['title'],
  6310. 'path' => $row['path'],
  6311. 'description' => $row['description'],
  6312. 'parent_item_id' => $row['parent_item_id'],
  6313. 'previous_item_id' => $row['previous_item_id'],
  6314. 'next_item_id' => $row['next_item_id'],
  6315. 'display_order' => $row['display_order'],
  6316. 'max_score' => $row['max_score'],
  6317. 'min_score' => $row['min_score'],
  6318. 'mastery_score' => $row['mastery_score'],
  6319. 'prerequisite' => $row['prerequisite'],
  6320. 'max_time_allowed' => $row['max_time_allowed']
  6321. );
  6322. }
  6323. $legend = '<legend>';
  6324. if ($action == 'add') {
  6325. $legend .= get_lang('CreateTheExercise');
  6326. } elseif ($action == 'move') {
  6327. $legend .= get_lang('MoveTheCurrentExercise');
  6328. } else {
  6329. $legend .= get_lang('EditCurrentExecice');
  6330. }
  6331. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6332. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6333. }
  6334. $legend .= '</legend>';
  6335. $return .= '<form method="POST">';
  6336. $return .= $legend;
  6337. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6338. $return .= '<tr>';
  6339. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6340. $return .= '<td class="input">';
  6341. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6342. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6343. $arrHide = array(
  6344. $id
  6345. );
  6346. if (count($arrLP) > 0) {
  6347. for ($i = 0; $i < count($arrLP); $i++) {
  6348. if ($action != 'add') {
  6349. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6350. $arrLP[$i]['id'],
  6351. $arrHide
  6352. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6353. ) {
  6354. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6355. } else {
  6356. $arrHide[] = $arrLP[$i]['id'];
  6357. }
  6358. } else {
  6359. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6360. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6361. }
  6362. }
  6363. }
  6364. reset($arrLP);
  6365. }
  6366. $return .= '</select>';
  6367. $return .= '</td>';
  6368. $return .= '</tr>';
  6369. $return .= '<tr>';
  6370. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6371. $return .= '<td class="input">';
  6372. $return .= '<select id="previous" name="previous" size="1">';
  6373. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6374. for ($i = 0; $i < count($arrLP); $i++) {
  6375. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6376. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6377. $selected = 'selected="selected" ';
  6378. } elseif ($action == 'add') {
  6379. $selected = 'selected="selected" ';
  6380. } else {
  6381. $selected = '';
  6382. }
  6383. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6384. 'After'
  6385. ).' "'.$arrLP[$i]['title'].'"</option>';
  6386. }
  6387. }
  6388. $return .= '</select>';
  6389. $return .= '</td>';
  6390. $return .= '</tr>';
  6391. if ($action != 'move') {
  6392. $return .= '<tr>';
  6393. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6394. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6395. $return .= '</tr>';
  6396. $id_prerequisite = 0;
  6397. if (is_array($arrLP) && count($arrLP) > 0) {
  6398. foreach ($arrLP as $key => $value) {
  6399. if ($value['id'] == $id) {
  6400. $id_prerequisite = $value['prerequisite'];
  6401. break;
  6402. }
  6403. }
  6404. $arrHide = array();
  6405. for ($i = 0; $i < count($arrLP); $i++) {
  6406. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6407. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6408. $s_selected_position = $arrLP[$i]['id'];
  6409. } elseif ($action == 'add') {
  6410. $s_selected_position = 0;
  6411. }
  6412. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6413. }
  6414. }
  6415. }
  6416. }
  6417. $return .= '<tr>';
  6418. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6419. 'SaveHotpotatoes'
  6420. ).'</button></td>';
  6421. $return .= '</tr>';
  6422. $return .= '</table>';
  6423. if ($action == 'move') {
  6424. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6425. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6426. }
  6427. if (is_numeric($extra_info)) {
  6428. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6429. } elseif (is_array($extra_info)) {
  6430. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6431. }
  6432. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6433. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6434. $return .= '</form>';
  6435. return $return;
  6436. }
  6437. /**
  6438. * Return the form to display the forum edit/add option
  6439. * @param string Action (add/edit)
  6440. * @param integer ID of the lp_item if already exists
  6441. * @param mixed Forum ID or title
  6442. * @return string HTML form
  6443. */
  6444. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6445. {
  6446. $course_id = api_get_course_int_id();
  6447. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6448. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6449. if ($id != 0 && is_array($extra_info)) {
  6450. $item_title = stripslashes($extra_info['title']);
  6451. } elseif (is_numeric($extra_info)) {
  6452. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6453. FROM ".$tbl_forum."
  6454. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6455. $result = Database::query($sql_forum);
  6456. $row = Database :: fetch_array($result);
  6457. $item_title = $row['title'];
  6458. $item_description = $row['comment'];
  6459. } else {
  6460. $item_title = '';
  6461. $item_description = '';
  6462. }
  6463. $legend = '<legend>';
  6464. if ($id != 0 && is_array($extra_info)) {
  6465. $parent = $extra_info['parent_item_id'];
  6466. } else {
  6467. $parent = 0;
  6468. }
  6469. $sql = "SELECT * FROM ".$tbl_lp_item."
  6470. WHERE c_id = ".$course_id." AND
  6471. lp_id = ".$this->lp_id;
  6472. $result = Database::query($sql);
  6473. $arrLP = array();
  6474. while ($row = Database :: fetch_array($result)) {
  6475. $arrLP[] = array(
  6476. 'id' => $row['id'],
  6477. 'item_type' => $row['item_type'],
  6478. 'title' => $row['title'],
  6479. 'path' => $row['path'],
  6480. 'description' => $row['description'],
  6481. 'parent_item_id' => $row['parent_item_id'],
  6482. 'previous_item_id' => $row['previous_item_id'],
  6483. 'next_item_id' => $row['next_item_id'],
  6484. 'display_order' => $row['display_order'],
  6485. 'max_score' => $row['max_score'],
  6486. 'min_score' => $row['min_score'],
  6487. 'mastery_score' => $row['mastery_score'],
  6488. 'prerequisite' => $row['prerequisite']
  6489. );
  6490. }
  6491. $this->tree_array($arrLP);
  6492. $arrLP = $this->arrMenu;
  6493. unset ($this->arrMenu);
  6494. if ($action == 'add') {
  6495. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6496. } elseif ($action == 'move') {
  6497. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6498. } else {
  6499. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6500. }
  6501. $legend .= '</legend>';
  6502. $return .= '<div class="sectioncomment">';
  6503. $return .= '<form method="POST">';
  6504. $return .= $legend;
  6505. $return .= '<table class="lp_form">';
  6506. if ($action != 'move') {
  6507. $return .= '<tr>';
  6508. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6509. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6510. $return .= '</tr>';
  6511. }
  6512. $return .= '<tr>';
  6513. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6514. $return .= '<td class="input">';
  6515. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6516. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6517. $arrHide = array(
  6518. $id
  6519. );
  6520. //$parent_item_id = $_SESSION['parent_item_id'];
  6521. for ($i = 0; $i < count($arrLP); $i++) {
  6522. if ($action != 'add') {
  6523. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6524. $arrLP[$i]['id'],
  6525. $arrHide
  6526. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6527. ) {
  6528. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6529. } else {
  6530. $arrHide[] = $arrLP[$i]['id'];
  6531. }
  6532. } else {
  6533. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6534. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6535. }
  6536. }
  6537. }
  6538. if (is_array($arrLP)) {
  6539. reset($arrLP);
  6540. }
  6541. $return .= "\t\t\t\t".'</select>';
  6542. $return .= '</td>';
  6543. $return .= '</tr>';
  6544. $return .= '<tr>';
  6545. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6546. $return .= '<td class="input">';
  6547. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6548. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6549. for ($i = 0; $i < count($arrLP); $i++) {
  6550. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6551. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6552. $selected = 'selected="selected" ';
  6553. } elseif ($action == 'add') {
  6554. $selected = 'selected="selected" ';
  6555. } else {
  6556. $selected = '';
  6557. }
  6558. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6559. 'After'
  6560. ).' "'.$arrLP[$i]['title'].'"</option>';
  6561. }
  6562. }
  6563. $return .= "\t\t\t\t".'</select>';
  6564. $return .= '</td>';
  6565. $return .= '</tr>';
  6566. if ($action != 'move') {
  6567. $return .= '<tr>';
  6568. $return .= '</tr>';
  6569. $id_prerequisite = 0;
  6570. if (is_array($arrLP)) {
  6571. foreach ($arrLP as $key => $value) {
  6572. if ($value['id'] == $id) {
  6573. $id_prerequisite = $value['prerequisite'];
  6574. break;
  6575. }
  6576. }
  6577. }
  6578. $arrHide = array();
  6579. for ($i = 0; $i < count($arrLP); $i++) {
  6580. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6581. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6582. $s_selected_position = $arrLP[$i]['id'];
  6583. } elseif ($action == 'add') {
  6584. $s_selected_position = 0;
  6585. }
  6586. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6587. }
  6588. }
  6589. $return .= '</tr>';
  6590. }
  6591. $return .= '<tr>';
  6592. if ($action == 'add') {
  6593. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6594. 'AddForumToCourse'
  6595. ).' </button></td>';
  6596. } else {
  6597. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6598. 'EditCurrentForum'
  6599. ).' </button></td>';
  6600. }
  6601. $return .= '</tr>';
  6602. $return .= '</table>';
  6603. if ($action == 'move') {
  6604. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6605. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6606. }
  6607. if (is_numeric($extra_info)) {
  6608. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6609. } elseif (is_array($extra_info)) {
  6610. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6611. }
  6612. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6613. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6614. $return .= '</form>';
  6615. $return .= '</div>';
  6616. return $return;
  6617. }
  6618. /**
  6619. * Return HTML form to add/edit forum threads
  6620. * @param string Action (add/edit)
  6621. * @param integer Item ID if already exists in learning path
  6622. * @param mixed Extra information (thread ID if integer)
  6623. * @return string HTML form
  6624. */
  6625. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6626. {
  6627. $course_id = api_get_course_int_id();
  6628. if (empty($course_id)) {
  6629. return null;
  6630. }
  6631. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6632. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6633. if ($id != 0 && is_array($extra_info)) {
  6634. $item_title = stripslashes($extra_info['title']);
  6635. } elseif (is_numeric($extra_info)) {
  6636. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6637. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6638. $result = Database::query($sql_forum);
  6639. $row = Database :: fetch_array($result);
  6640. $item_title = $row['title'];
  6641. $item_description = '';
  6642. } else {
  6643. $item_title = '';
  6644. $item_description = '';
  6645. }
  6646. $return = null;
  6647. if ($id != 0 && is_array($extra_info)) {
  6648. $parent = $extra_info['parent_item_id'];
  6649. } else {
  6650. $parent = 0;
  6651. }
  6652. $sql = "SELECT * FROM ".$tbl_lp_item."
  6653. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6654. $result = Database::query($sql);
  6655. $arrLP = array();
  6656. while ($row = Database :: fetch_array($result)) {
  6657. $arrLP[] = array(
  6658. 'id' => $row['id'],
  6659. 'item_type' => $row['item_type'],
  6660. 'title' => $row['title'],
  6661. 'path' => $row['path'],
  6662. 'description' => $row['description'],
  6663. 'parent_item_id' => $row['parent_item_id'],
  6664. 'previous_item_id' => $row['previous_item_id'],
  6665. 'next_item_id' => $row['next_item_id'],
  6666. 'display_order' => $row['display_order'],
  6667. 'max_score' => $row['max_score'],
  6668. 'min_score' => $row['min_score'],
  6669. 'mastery_score' => $row['mastery_score'],
  6670. 'prerequisite' => $row['prerequisite']
  6671. );
  6672. }
  6673. $this->tree_array($arrLP);
  6674. $arrLP = $this->arrMenu;
  6675. unset ($this->arrMenu);
  6676. $return .= '<form method="POST">';
  6677. if ($action == 'add') {
  6678. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6679. } elseif ($action == 'move') {
  6680. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6681. } else {
  6682. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6683. }
  6684. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6685. $return .= '<tr>';
  6686. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6687. $return .= '<td class="input">';
  6688. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6689. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6690. $arrHide = array(
  6691. $id
  6692. );
  6693. for ($i = 0; $i < count($arrLP); $i++) {
  6694. if ($action != 'add') {
  6695. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6696. $arrLP[$i]['id'],
  6697. $arrHide
  6698. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6699. ) {
  6700. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6701. } else {
  6702. $arrHide[] = $arrLP[$i]['id'];
  6703. }
  6704. } else {
  6705. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6706. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6707. }
  6708. }
  6709. }
  6710. if ($arrLP != null) {
  6711. reset($arrLP);
  6712. }
  6713. $return .= '</select>';
  6714. $return .= '</td>';
  6715. $return .= '</tr>';
  6716. $return .= '<tr>';
  6717. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6718. $return .= '<td class="input">';
  6719. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6720. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6721. for ($i = 0; $i < count($arrLP); $i++) {
  6722. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6723. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6724. $selected = 'selected="selected" ';
  6725. } elseif ($action == 'add') {
  6726. $selected = 'selected="selected" ';
  6727. } else {
  6728. $selected = '';
  6729. }
  6730. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6731. 'After'
  6732. ).' "'.$arrLP[$i]['title'].'"</option>';
  6733. }
  6734. }
  6735. $return .= "\t\t\t\t".'</select>';
  6736. $return .= '</td>';
  6737. $return .= '</tr>';
  6738. if ($action != 'move') {
  6739. $return .= '<tr>';
  6740. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6741. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6742. $return .= '</tr>';
  6743. $return .= '<tr>';
  6744. $return .= '</tr>';
  6745. $id_prerequisite = 0;
  6746. if ($arrLP != null) {
  6747. foreach ($arrLP as $key => $value) {
  6748. if ($value['id'] == $id) {
  6749. $id_prerequisite = $value['prerequisite'];
  6750. break;
  6751. }
  6752. }
  6753. }
  6754. $arrHide = array();
  6755. for ($i = 0; $i < count($arrLP); $i++) {
  6756. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6757. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6758. $s_selected_position = $arrLP[$i]['id'];
  6759. } elseif ($action == 'add') {
  6760. $s_selected_position = 0;
  6761. }
  6762. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6763. }
  6764. }
  6765. $return .= '<tr>';
  6766. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6767. 'LearnpathPrerequisites'
  6768. ).'</label></td>';
  6769. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6770. 'NoPrerequisites'
  6771. ).'</option>';
  6772. foreach ($arrHide as $key => $value) {
  6773. if ($key == $s_selected_position && $action == 'add') {
  6774. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6775. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6776. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6777. } else {
  6778. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6779. }
  6780. }
  6781. $return .= "</select></td>";
  6782. $return .= '</tr>';
  6783. }
  6784. $return .= '<tr>';
  6785. $return .= '<td></td><td>
  6786. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6787. 'Ok'
  6788. ).'</button></td>';
  6789. $return .= '</tr>';
  6790. $return .= '</table>';
  6791. if ($action == 'move') {
  6792. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6793. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6794. }
  6795. if (is_numeric($extra_info)) {
  6796. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6797. } elseif (is_array($extra_info)) {
  6798. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6799. }
  6800. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6801. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6802. $return .= '</form>';
  6803. $return .= '</div>';
  6804. return $return;
  6805. }
  6806. /**
  6807. * Return the HTML form to display an item (generally a section/module item)
  6808. * @param string Item type (module/dokeos_module)
  6809. * @param string Title (optional, only when creating)
  6810. * @param string Action ('add'/'edit')
  6811. * @param integer lp_item ID
  6812. * @param mixed Extra info
  6813. * @return string HTML form
  6814. */
  6815. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6816. {
  6817. $course_id = api_get_course_int_id();
  6818. $_course = api_get_course_info();
  6819. global $charset;
  6820. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6821. if ($id != 0 && is_array($extra_info)) {
  6822. $item_title = $extra_info['title'];
  6823. $item_description = $extra_info['description'];
  6824. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6825. $extra_info['path']
  6826. );
  6827. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6828. } else {
  6829. $item_title = '';
  6830. $item_description = '';
  6831. $item_path_fck = '';
  6832. }
  6833. if ($id != 0 && is_array($extra_info)) {
  6834. $parent = $extra_info['parent_item_id'];
  6835. } else {
  6836. $parent = 0;
  6837. }
  6838. $id = intval($id);
  6839. $sql = "SELECT * FROM ".$tbl_lp_item."
  6840. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6841. if ($item_type == 'module') {
  6842. $sql .= " AND parent_item_id = 0";
  6843. }
  6844. $result = Database::query($sql);
  6845. $arrLP = array();
  6846. while ($row = Database :: fetch_array($result)) {
  6847. $arrLP[] = array(
  6848. 'id' => $row['id'],
  6849. 'item_type' => $row['item_type'],
  6850. 'title' => $row['title'],
  6851. 'path' => $row['path'],
  6852. 'description' => $row['description'],
  6853. 'parent_item_id' => $row['parent_item_id'],
  6854. 'previous_item_id' => $row['previous_item_id'],
  6855. 'next_item_id' => $row['next_item_id'],
  6856. 'max_score' => $row['max_score'],
  6857. 'min_score' => $row['min_score'],
  6858. 'mastery_score' => $row['mastery_score'],
  6859. 'prerequisite' => $row['prerequisite'],
  6860. 'display_order' => $row['display_order']
  6861. );
  6862. }
  6863. $this->tree_array($arrLP);
  6864. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6865. unset($this->arrMenu);
  6866. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6867. $url = api_get_self().'?'.api_get_cidreq(
  6868. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6869. $form = new FormValidator('form', 'POST', $url);
  6870. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6871. $defaults['description'] = $item_description;
  6872. $form->addElement('header', $title);
  6873. //$arrHide = array($id);
  6874. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6875. $arrHide[0]['padding'] = 3;
  6876. $charset = api_get_system_encoding();
  6877. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6878. for ($i = 0; $i < count($arrLP); $i++) {
  6879. if ($action != 'add') {
  6880. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6881. $arrLP[$i]['id'],
  6882. $arrHide
  6883. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6884. ) {
  6885. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6886. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6887. if ($parent == $arrLP[$i]['id']) {
  6888. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6889. }
  6890. }
  6891. } else {
  6892. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6893. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6894. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6895. if ($parent == $arrLP[$i]['id']) {
  6896. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6897. }
  6898. }
  6899. }
  6900. }
  6901. if ($action != 'move') {
  6902. $form->addElement(
  6903. 'text',
  6904. 'title',
  6905. get_lang('Title'),
  6906. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6907. );
  6908. $form->applyFilter('title', 'html_filter');
  6909. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6910. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6911. } else {
  6912. $form->addElement('hidden', 'title');
  6913. }
  6914. $parent_select = $form->addElement(
  6915. 'select',
  6916. 'parent',
  6917. get_lang('Parent'),
  6918. '',
  6919. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6920. );
  6921. foreach ($arrHide as $key => $value) {
  6922. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6923. }
  6924. if (!empty($s_selected_parent)) {
  6925. $parent_select->setSelected($s_selected_parent);
  6926. }
  6927. }
  6928. if (is_array($arrLP)) {
  6929. reset($arrLP);
  6930. }
  6931. $arrHide = array();
  6932. // POSITION
  6933. for ($i = 0; $i < count($arrLP); $i++) {
  6934. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6935. //this is the same!
  6936. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6937. $s_selected_position = $arrLP[$i]['id'];
  6938. } elseif ($action == 'add') {
  6939. $s_selected_position = $arrLP[$i]['id'];
  6940. }
  6941. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6942. }
  6943. }
  6944. $position = $form->addElement(
  6945. 'select',
  6946. 'previous',
  6947. get_lang('Position'),
  6948. '',
  6949. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6950. );
  6951. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6952. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6953. foreach ($arrHide as $key => $value) {
  6954. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6955. }
  6956. if (!empty ($s_selected_position)) {
  6957. $position->setSelected($s_selected_position);
  6958. }
  6959. if (is_array($arrLP)) {
  6960. reset($arrLP);
  6961. }
  6962. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6963. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6964. $form->addElement('hidden', 'parent', '0');
  6965. }
  6966. //fix in order to use the tab
  6967. if ($item_type == 'chapter') {
  6968. $form->addElement('hidden', 'type', 'chapter');
  6969. }
  6970. $extension = null;
  6971. if (!empty($item_path)) {
  6972. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6973. }
  6974. //assets can't be modified
  6975. //$item_type == 'asset' ||
  6976. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6977. if ($item_type == 'sco') {
  6978. $form->addElement(
  6979. 'html',
  6980. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  6981. );
  6982. }
  6983. $renderer = $form->defaultRenderer();
  6984. $renderer->setElementTemplate(
  6985. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  6986. 'content_lp'
  6987. );
  6988. $relative_prefix = '';
  6989. $editor_config = array(
  6990. 'ToolbarSet' => 'LearningPathDocuments',
  6991. 'Width' => '100%',
  6992. 'Height' => '500',
  6993. 'FullPage' => true,
  6994. 'CreateDocumentDir' => $relative_prefix,
  6995. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  6996. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  6997. );
  6998. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6999. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  7000. //$defaults['content_lp'] = file_get_contents($item_path);
  7001. $defaults['content_lp'] = file_get_contents($content_path);
  7002. }
  7003. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  7004. $form->addElement('hidden', 'post_time', time());
  7005. $form->setDefaults($defaults);
  7006. return $form->return_form();
  7007. }
  7008. /**
  7009. * Returns the form to update or create a document
  7010. * @param string Action (add/edit)
  7011. * @param integer ID of the lp_item (if already exists)
  7012. * @param mixed Integer if document ID, string if info ('new')
  7013. * @return string HTML form
  7014. */
  7015. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7016. {
  7017. $course_id = api_get_course_int_id();
  7018. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7019. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7020. $no_display_edit_textarea = false;
  7021. //If action==edit document
  7022. //We don't display the document form if it's not an editable document (html or txt file)
  7023. if ($action == "edit") {
  7024. if (is_array($extra_info)) {
  7025. $path_parts = pathinfo($extra_info['dir']);
  7026. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7027. $no_display_edit_textarea = true;
  7028. }
  7029. }
  7030. }
  7031. $no_display_add = false;
  7032. // If action==add an existing document
  7033. // We don't display the document form if it's not an editable document (html or txt file).
  7034. if ($action == "add") {
  7035. if (is_numeric($extra_info)) {
  7036. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  7037. $extra_info
  7038. );
  7039. $result = Database::query($sql_doc);
  7040. $path_file = Database :: result($result, 0, 0);
  7041. $path_parts = pathinfo($path_file);
  7042. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7043. $no_display_add = true;
  7044. }
  7045. }
  7046. }
  7047. if ($id != 0 && is_array($extra_info)) {
  7048. $item_title = stripslashes($extra_info['title']);
  7049. $item_description = stripslashes($extra_info['description']);
  7050. $item_terms = stripslashes($extra_info['terms']);
  7051. if (empty ($item_title)) {
  7052. $path_parts = pathinfo($extra_info['path']);
  7053. $item_title = stripslashes($path_parts['filename']);
  7054. }
  7055. } elseif (is_numeric($extra_info)) {
  7056. $sql_doc = "SELECT path, title FROM ".$tbl_doc."
  7057. WHERE c_id = ".$course_id." AND id = ".Database::escape_string($extra_info);
  7058. $result = Database::query($sql_doc);
  7059. $row = Database::fetch_array($result);
  7060. $explode = explode('.', $row['title']);
  7061. if (count($explode) > 1) {
  7062. for ($i = 0; $i < count($explode) - 1; $i++) {
  7063. $item_title .= $explode[$i];
  7064. }
  7065. } else {
  7066. $item_title = $row['title'];
  7067. }
  7068. $item_title = str_replace('_', ' ', $item_title);
  7069. if (empty ($item_title)) {
  7070. $path_parts = pathinfo($row['path']);
  7071. $item_title = stripslashes($path_parts['filename']);
  7072. }
  7073. } else {
  7074. $item_title = '';
  7075. $item_description = '';
  7076. }
  7077. $return = '<legend>';
  7078. if ($id != 0 && is_array($extra_info)) {
  7079. $parent = $extra_info['parent_item_id'];
  7080. } else {
  7081. $parent = 0;
  7082. }
  7083. $sql = "SELECT * FROM ".$tbl_lp_item."
  7084. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7085. $result = Database::query($sql);
  7086. $arrLP = array();
  7087. while ($row = Database :: fetch_array($result)) {
  7088. $arrLP[] = array(
  7089. 'id' => $row['id'],
  7090. 'item_type' => $row['item_type'],
  7091. 'title' => $row['title'],
  7092. 'path' => $row['path'],
  7093. 'description' => $row['description'],
  7094. 'parent_item_id' => $row['parent_item_id'],
  7095. 'previous_item_id' => $row['previous_item_id'],
  7096. 'next_item_id' => $row['next_item_id'],
  7097. 'display_order' => $row['display_order'],
  7098. 'max_score' => $row['max_score'],
  7099. 'min_score' => $row['min_score'],
  7100. 'mastery_score' => $row['mastery_score'],
  7101. 'prerequisite' => $row['prerequisite']
  7102. );
  7103. }
  7104. $this->tree_array($arrLP);
  7105. $arrLP = null;
  7106. if (isset($this->arrMenu)) {
  7107. $arrLP = $this->arrMenu;
  7108. unset ($this->arrMenu);
  7109. }
  7110. if ($action == 'add') {
  7111. $return .= get_lang('CreateTheDocument');
  7112. } elseif ($action == 'move') {
  7113. $return .= get_lang('MoveTheCurrentDocument');
  7114. } else {
  7115. $return .= get_lang('EditTheCurrentDocument');
  7116. }
  7117. $return .= '</legend>';
  7118. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7119. $return .= Display :: return_warning_message(
  7120. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7121. false
  7122. );
  7123. }
  7124. $form = new FormValidator('form', 'POST', api_get_self(
  7125. ).'?'.$_SERVER['QUERY_STRING'], '', array('enctype' => "multipart/form-data"));
  7126. $defaults['title'] = Security :: remove_XSS($item_title);
  7127. if (empty($item_title)) {
  7128. $defaults['title'] = Security::remove_XSS($item_title);
  7129. }
  7130. $defaults['description'] = $item_description;
  7131. $form->addElement('html', $return);
  7132. if ($action != 'move') {
  7133. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  7134. $form->applyFilter('title', 'html_filter');
  7135. }
  7136. //$arrHide = array($id);
  7137. $arrHide[0]['value'] = $this->name;
  7138. $arrHide[0]['padding'] = 3;
  7139. for ($i = 0; $i < count($arrLP); $i++) {
  7140. if ($action != 'add') {
  7141. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7142. $arrLP[$i]['id'],
  7143. $arrHide
  7144. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7145. ) {
  7146. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7147. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7148. if ($parent == $arrLP[$i]['id']) {
  7149. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7150. }
  7151. }
  7152. } else {
  7153. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7154. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7155. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7156. if ($parent == $arrLP[$i]['id']) {
  7157. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7158. }
  7159. }
  7160. }
  7161. }
  7162. $parent_select = $form->addElement(
  7163. 'select',
  7164. 'parent',
  7165. get_lang('Parent'),
  7166. '',
  7167. 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"'
  7168. );
  7169. $my_count = 0;
  7170. foreach ($arrHide as $key => $value) {
  7171. if ($my_count != 0) {
  7172. // The LP name is also the first section and is not in the same charset like the other sections.
  7173. $value['value'] = Security :: remove_XSS($value['value']);
  7174. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7175. } else {
  7176. $value['value'] = Security :: remove_XSS($value['value']);
  7177. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7178. }
  7179. $my_count++;
  7180. }
  7181. if (!empty($id)) {
  7182. $parent_select->setSelected($parent);
  7183. } else {
  7184. $parent_item_id = Session::read('parent_item_id');
  7185. $parent_select->setSelected($parent_item_id);
  7186. }
  7187. if (is_array($arrLP)) {
  7188. reset($arrLP);
  7189. }
  7190. $arrHide = array();
  7191. $s_selected_position = null;
  7192. //POSITION
  7193. for ($i = 0; $i < count($arrLP); $i++) {
  7194. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7195. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7196. $s_selected_position = $arrLP[$i]['id'];
  7197. } elseif ($action == 'add') {
  7198. $s_selected_position = $arrLP[$i]['id'];
  7199. }
  7200. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7201. }
  7202. }
  7203. $position = $form->addElement(
  7204. 'select',
  7205. 'previous',
  7206. get_lang('Position'),
  7207. '',
  7208. 'id="previous" class="learnpath_item_form" style="width:40%;"'
  7209. );
  7210. $position->addOption(get_lang('FirstPosition'), 0);
  7211. foreach ($arrHide as $key => $value) {
  7212. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7213. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7214. }
  7215. $position->setSelected($s_selected_position);
  7216. if (is_array($arrLP)) {
  7217. reset($arrLP);
  7218. }
  7219. if ($action != 'move') {
  7220. $id_prerequisite = 0;
  7221. if (is_array($arrLP)) {
  7222. foreach ($arrLP as $key => $value) {
  7223. if ($value['id'] == $id) {
  7224. $id_prerequisite = $value['prerequisite'];
  7225. break;
  7226. }
  7227. }
  7228. }
  7229. $arrHide = array();
  7230. for ($i = 0; $i < count($arrLP); $i++) {
  7231. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7232. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7233. $s_selected_position = $arrLP[$i]['id'];
  7234. } elseif ($action == 'add') {
  7235. $s_selected_position = $arrLP[$i]['id'];
  7236. }
  7237. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7238. }
  7239. }
  7240. if (!$no_display_add) {
  7241. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  7242. if (isset ($_POST['content'])) {
  7243. $content = stripslashes($_POST['content']);
  7244. } elseif (is_array($extra_info)) {
  7245. //If it's an html document or a text file
  7246. if (!$no_display_edit_textarea) {
  7247. $content = $this->display_document($extra_info['path'], false, false);
  7248. }
  7249. } elseif (is_numeric($extra_info)) {
  7250. $content = $this->display_document($extra_info, false, false);
  7251. } else {
  7252. $content = '';
  7253. }
  7254. if (!$no_display_edit_textarea) {
  7255. // We need to calculate here some specific settings for the online editor.
  7256. // The calculated settings work for documents in the Documents tool
  7257. // (on the root or in subfolders).
  7258. // For documents in native scorm packages it is unclear whether the
  7259. // online editor should be activated or not.
  7260. // A new document, it is in the root of the repository.
  7261. $relative_path = '';
  7262. $relative_prefix = '';
  7263. if (is_array($extra_info) && $extra_info != 'new') {
  7264. // The document already exists. Whe have to determine its relative path towards the repository root.
  7265. $relative_path = explode('/', $extra_info['dir']);
  7266. $cnt = count($relative_path) - 2;
  7267. if ($cnt < 0) {
  7268. $cnt = 0;
  7269. }
  7270. $relative_prefix = str_repeat('../', $cnt);
  7271. $relative_path = array_slice($relative_path, 1, $cnt);
  7272. $relative_path = implode('/', $relative_path);
  7273. if (strlen($relative_path) > 0) {
  7274. $relative_path = $relative_path.'/';
  7275. }
  7276. } else {
  7277. $_course = api_get_course_info();
  7278. $result = $this->generate_lp_folder($_course);
  7279. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7280. $relative_prefix = '../../';
  7281. }
  7282. $editor_config = array(
  7283. 'ToolbarSet' => 'LearningPathDocuments',
  7284. 'Width' => '100%',
  7285. 'Height' => '500',
  7286. 'FullPage' => true,
  7287. 'CreateDocumentDir' => $relative_prefix,
  7288. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7289. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path(
  7290. ).'/document/'.$relative_path
  7291. );
  7292. if ($_GET['action'] == 'add_item') {
  7293. $class = 'add';
  7294. $text = get_lang('LPCreateDocument');
  7295. } else {
  7296. if ($_GET['action'] == 'edit_item') {
  7297. $class = 'save';
  7298. $text = get_lang('SaveDocument');
  7299. }
  7300. }
  7301. $form->addElement('style_submit_button', 'submit_button', $text, 'class="'.$class.'"');
  7302. $renderer = $form->defaultRenderer();
  7303. $renderer->setElementTemplate(
  7304. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7305. 'content_lp'
  7306. );
  7307. $form->addElement('html', '<div>');
  7308. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7309. $form->addElement('html', '</div>');
  7310. $defaults['content_lp'] = $content;
  7311. }
  7312. } elseif (is_numeric($extra_info)) {
  7313. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7314. $return = $this->display_document($extra_info, true, true, true);
  7315. $form->addElement('html', $return);
  7316. }
  7317. }
  7318. }
  7319. if ($action == 'move') {
  7320. $form->addElement('hidden', 'title', $item_title);
  7321. $form->addElement('hidden', 'description', $item_description);
  7322. }
  7323. if (is_numeric($extra_info)) {
  7324. $form->addElement(
  7325. 'style_submit_button',
  7326. 'submit_button',
  7327. get_lang('SaveDocument'),
  7328. 'value="submit_button", class="save"'
  7329. );
  7330. $form->addElement('hidden', 'path', $extra_info);
  7331. } elseif (is_array($extra_info)) {
  7332. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7333. $form->addElement('hidden', 'path', $extra_info['path']);
  7334. }
  7335. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7336. $form->addElement('hidden', 'post_time', time());
  7337. $form->setDefaults($defaults);
  7338. return $form->return_form();
  7339. }
  7340. /**
  7341. * Return HTML form to add/edit a link item
  7342. * @param string Action (add/edit)
  7343. * @param integer Item ID if exists
  7344. * @param mixed Extra info
  7345. * @return string HTML form
  7346. */
  7347. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7348. {
  7349. $course_id = api_get_course_int_id();
  7350. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7351. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7352. if ($id != 0 && is_array($extra_info)) {
  7353. $item_title = stripslashes($extra_info['title']);
  7354. $item_description = stripslashes($extra_info['description']);
  7355. $item_url = stripslashes($extra_info['url']);
  7356. } elseif (is_numeric($extra_info)) {
  7357. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7358. $result = Database::query($sql_link);
  7359. $row = Database :: fetch_array($result);
  7360. $item_title = $row['title'];
  7361. $item_description = $row['description'];
  7362. $item_url = $row['url'];
  7363. } else {
  7364. $item_title = '';
  7365. $item_description = '';
  7366. $item_url = '';
  7367. }
  7368. $legend = '<legend>';
  7369. if ($id != 0 && is_array($extra_info)) {
  7370. $parent = $extra_info['parent_item_id'];
  7371. } else {
  7372. $parent = 0;
  7373. }
  7374. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7375. $result = Database::query($sql);
  7376. $arrLP = array();
  7377. while ($row = Database :: fetch_array($result)) {
  7378. $arrLP[] = array(
  7379. 'id' => $row['id'],
  7380. 'item_type' => $row['item_type'],
  7381. 'title' => $row['title'],
  7382. 'path' => $row['path'],
  7383. 'description' => $row['description'],
  7384. 'parent_item_id' => $row['parent_item_id'],
  7385. 'previous_item_id' => $row['previous_item_id'],
  7386. 'next_item_id' => $row['next_item_id'],
  7387. 'display_order' => $row['display_order'],
  7388. 'max_score' => $row['max_score'],
  7389. 'min_score' => $row['min_score'],
  7390. 'mastery_score' => $row['mastery_score'],
  7391. 'prerequisite' => $row['prerequisite']
  7392. );
  7393. }
  7394. $this->tree_array($arrLP);
  7395. $arrLP = $this->arrMenu;
  7396. unset ($this->arrMenu);
  7397. if ($action == 'add') {
  7398. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7399. } elseif ($action == 'move') {
  7400. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7401. } else {
  7402. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7403. }
  7404. $legend .= '</legend>';
  7405. $return .= '<div class="sectioncomment">';
  7406. $return .= '<form method="POST">';
  7407. $return .= $legend;
  7408. $return .= '<table>';
  7409. if ($action != 'move') {
  7410. $return .= '<tr>';
  7411. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7412. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7413. $return .= '</tr>';
  7414. }
  7415. $return .= '<tr>';
  7416. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7417. $return .= '<td class="input">';
  7418. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7419. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7420. $arrHide = array(
  7421. $id
  7422. );
  7423. $parent_item_id = $_SESSION['parent_item_id'];
  7424. for ($i = 0; $i < count($arrLP); $i++) {
  7425. if ($action != 'add') {
  7426. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7427. $arrLP[$i]['id'],
  7428. $arrHide
  7429. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7430. ) {
  7431. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7432. } else {
  7433. $arrHide[] = $arrLP[$i]['id'];
  7434. }
  7435. } else {
  7436. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7437. $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>';
  7438. }
  7439. }
  7440. }
  7441. if (is_array($arrLP)) {
  7442. reset($arrLP);
  7443. }
  7444. $return .= '</select>';
  7445. $return .= '</td>';
  7446. $return .= '</tr>';
  7447. $return .= '<tr>';
  7448. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7449. $return .= '<td class="input">';
  7450. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7451. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7452. for ($i = 0; $i < count($arrLP); $i++) {
  7453. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7454. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7455. $selected = 'selected="selected" ';
  7456. } elseif ($action == 'add') {
  7457. $selected = 'selected="selected" ';
  7458. } else {
  7459. $selected = '';
  7460. }
  7461. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7462. 'After'
  7463. ).' "'.$arrLP[$i]['title'].'"</option>';
  7464. }
  7465. }
  7466. $return .= '</select>';
  7467. $return .= '</td>';
  7468. $return .= '</tr>';
  7469. if ($action != 'move') {
  7470. $return .= '<tr>';
  7471. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7472. $return .= '<td class="input"><input'.(is_numeric(
  7473. $extra_info
  7474. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7475. $return .= '</tr>';
  7476. $id_prerequisite = 0;
  7477. if (is_array($arrLP)) {
  7478. foreach ($arrLP as $key => $value) {
  7479. if ($value['id'] == $id) {
  7480. $id_prerequisite = $value['prerequisite'];
  7481. break;
  7482. }
  7483. }
  7484. }
  7485. $arrHide = array();
  7486. for ($i = 0; $i < count($arrLP); $i++) {
  7487. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7488. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7489. $s_selected_position = $arrLP[$i]['id'];
  7490. } elseif ($action == 'add') {
  7491. $s_selected_position = 0;
  7492. }
  7493. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7494. }
  7495. }
  7496. $return .= '</tr>';
  7497. }
  7498. $return .= '<tr>';
  7499. if ($action == 'add') {
  7500. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7501. 'AddLinkToCourse'
  7502. ).'</button></td>';
  7503. } else {
  7504. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7505. 'EditCurrentLink'
  7506. ).'</button></td>';
  7507. }
  7508. $return .= '</tr>';
  7509. $return .= '</table>';
  7510. if ($action == 'move') {
  7511. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7512. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7513. }
  7514. if (is_numeric($extra_info)) {
  7515. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7516. } elseif (is_array($extra_info)) {
  7517. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7518. }
  7519. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7520. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7521. $return .= '</form>';
  7522. $return .= '</div>';
  7523. return $return;
  7524. }
  7525. /**
  7526. * Return HTML form to add/edit a student publication (work)
  7527. * @param string Action (add/edit)
  7528. * @param integer Item ID if already exists
  7529. * @param mixed Extra info (work ID if integer)
  7530. * @return string HTML form
  7531. */
  7532. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7533. {
  7534. $course_id = api_get_course_int_id();
  7535. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7536. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7537. if ($id != 0 && is_array($extra_info)) {
  7538. $item_title = stripslashes($extra_info['title']);
  7539. $item_description = stripslashes($extra_info['description']);
  7540. } elseif (is_numeric($extra_info)) {
  7541. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7542. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7543. $result = Database::query($sql_publication);
  7544. $row = Database :: fetch_array($result);
  7545. $item_title = $row['title'];
  7546. } else {
  7547. $item_title = get_lang('Student_publication');
  7548. }
  7549. $legend = '<legend>';
  7550. if ($id != 0 && is_array($extra_info)) {
  7551. $parent = $extra_info['parent_item_id'];
  7552. } else {
  7553. $parent = 0;
  7554. }
  7555. $sql = "SELECT * FROM ".$tbl_lp_item."
  7556. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7557. $result = Database::query($sql);
  7558. $arrLP = array();
  7559. while ($row = Database :: fetch_array($result)) {
  7560. $arrLP[] = array(
  7561. 'id' => $row['id'],
  7562. 'item_type' => $row['item_type'],
  7563. 'title' => $row['title'],
  7564. 'path' => $row['path'],
  7565. 'description' => $row['description'],
  7566. 'parent_item_id' => $row['parent_item_id'],
  7567. 'previous_item_id' => $row['previous_item_id'],
  7568. 'next_item_id' => $row['next_item_id'],
  7569. 'display_order' => $row['display_order'],
  7570. 'max_score' => $row['max_score'],
  7571. 'min_score' => $row['min_score'],
  7572. 'mastery_score' => $row['mastery_score'],
  7573. 'prerequisite' => $row['prerequisite']
  7574. );
  7575. }
  7576. $this->tree_array($arrLP);
  7577. $arrLP = $this->arrMenu;
  7578. unset ($this->arrMenu);
  7579. if ($action == 'add') {
  7580. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7581. } elseif ($action == 'move') {
  7582. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7583. } else {
  7584. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7585. }
  7586. $legend .= '</legend>';
  7587. $return .= '<div class="sectioncomment">';
  7588. $return .= '<form method="POST">';
  7589. $return .= $legend;
  7590. $return .= '<table class="lp_form">';
  7591. if ($action != 'move') {
  7592. $return .= '<tr>';
  7593. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7594. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7595. $return .= '</tr>';
  7596. }
  7597. $return .= '<tr>';
  7598. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7599. $return .= '<td class="input">';
  7600. $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">';
  7601. //$parent_item_id = $_SESSION['parent_item_id'];
  7602. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7603. $arrHide = array(
  7604. $id
  7605. );
  7606. for ($i = 0; $i < count($arrLP); $i++) {
  7607. if ($action != 'add') {
  7608. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7609. $arrLP[$i]['id'],
  7610. $arrHide
  7611. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7612. ) {
  7613. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7614. } else {
  7615. $arrHide[] = $arrLP[$i]['id'];
  7616. }
  7617. } else {
  7618. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7619. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7620. }
  7621. }
  7622. }
  7623. if (is_array($arrLP)) {
  7624. reset($arrLP);
  7625. }
  7626. $return .= "\t\t\t\t".'</select>';
  7627. $return .= '</td>';
  7628. $return .= '</tr>';
  7629. $return .= '<tr>';
  7630. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7631. $return .= '<td class="input">';
  7632. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7633. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7634. for ($i = 0; $i < count($arrLP); $i++) {
  7635. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7636. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7637. $selected = 'selected="selected" ';
  7638. } elseif ($action == 'add') {
  7639. $selected = 'selected="selected" ';
  7640. } else {
  7641. $selected = '';
  7642. }
  7643. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7644. 'After'
  7645. ).' "'.$arrLP[$i]['title'].'"</option>';
  7646. }
  7647. }
  7648. $return .= "\t\t\t\t".'</select>';
  7649. $return .= '</td>';
  7650. $return .= '</tr>';
  7651. if ($action != 'move') {
  7652. $id_prerequisite = 0;
  7653. if (is_array($arrLP)) {
  7654. foreach ($arrLP as $key => $value) {
  7655. if ($value['id'] == $id) {
  7656. $id_prerequisite = $value['prerequisite'];
  7657. break;
  7658. }
  7659. }
  7660. }
  7661. $arrHide = array();
  7662. for ($i = 0; $i < count($arrLP); $i++) {
  7663. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7664. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7665. $s_selected_position = $arrLP[$i]['id'];
  7666. } elseif ($action == 'add') {
  7667. $s_selected_position = 0;
  7668. }
  7669. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7670. }
  7671. }
  7672. // Commented the prerequisites, only visible in edit (work).
  7673. /*
  7674. $return .= '<tr>';
  7675. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7676. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7677. foreach($arrHide as $key => $value) {
  7678. if ($key == $s_selected_position && $action == 'add') {
  7679. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7680. }
  7681. elseif ($key == $id_prerequisite && $action == 'edit') {
  7682. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7683. }
  7684. else {
  7685. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7686. }
  7687. }
  7688. $return .= "</select></td>";
  7689. */
  7690. $return .= '</tr>';
  7691. }
  7692. $return .= '<tr>';
  7693. if ($action == 'add') {
  7694. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7695. 'AddAssignmentToCourse'
  7696. ).'</button></td>';
  7697. } else {
  7698. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7699. 'EditCurrentStudentPublication'
  7700. ).'</button></td>';
  7701. }
  7702. $return .= '</tr>';
  7703. $return .= '</table>';
  7704. if ($action == 'move') {
  7705. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7706. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7707. }
  7708. if (is_numeric($extra_info)) {
  7709. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7710. } elseif (is_array($extra_info)) {
  7711. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7712. }
  7713. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7714. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7715. $return .= '</form>';
  7716. $return .= '</div>';
  7717. return $return;
  7718. }
  7719. /**
  7720. * Displays the menu for manipulating a step
  7721. * @return string html
  7722. */
  7723. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7724. {
  7725. $course_id = api_get_course_int_id();
  7726. $course_code = api_get_course_id();
  7727. $_course = api_get_course_info();
  7728. $return = '<div class="actions">';
  7729. switch ($item_type) {
  7730. case 'dokeos_chapter' :
  7731. case 'chapter' :
  7732. // Commented the message cause should not show it.
  7733. //$lang = get_lang('TitleManipulateChapter');
  7734. break;
  7735. case 'dokeos_module' :
  7736. case 'module' :
  7737. // Commented the message cause should not show it.
  7738. //$lang = get_lang('TitleManipulateModule');
  7739. break;
  7740. case TOOL_DOCUMENT :
  7741. // Commented the message cause should not show it.
  7742. //$lang = get_lang('TitleManipulateDocument');
  7743. break;
  7744. case TOOL_LINK :
  7745. case 'link' :
  7746. // Commented the message cause should not show it.
  7747. //$lang = get_lang('TitleManipulateLink');
  7748. break;
  7749. case TOOL_QUIZ :
  7750. // Commented the message cause should not show it.
  7751. //$lang = get_lang('TitleManipulateQuiz');
  7752. break;
  7753. case TOOL_STUDENTPUBLICATION :
  7754. // Commented the message cause should not show it.
  7755. //$lang = get_lang('TitleManipulateStudentPublication');
  7756. break;
  7757. }
  7758. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7759. $item_id = intval($item_id);
  7760. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7761. $result = Database::query($sql);
  7762. $row = Database::fetch_assoc($result);
  7763. $webCodepath = api_get_path(WEB_CODE_PATH);
  7764. $audio_player = null;
  7765. // We display an audio player if needed.
  7766. if (!empty($row['audio'])) {
  7767. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7768. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  7769. $audio_player .= '<script type="text/javascript" src="'.$webCodepath.'inc/lib/mediaplayer/swfobject.js"></script>';
  7770. $audio_player .= '<script>
  7771. var s1 = new SWFObject("'.$webCodepath.'inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7772. s1.addParam("allowscriptaccess","always");
  7773. s1.addParam("flashvars","file='.api_get_path(WEB_PUBLIC_PATH).'courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7774. s1.write("container");
  7775. </script>';
  7776. }
  7777. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7778. $_GET['cidReq']
  7779. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7780. $return .= Display::url(
  7781. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7782. $url.'&action=edit_item&path_item='.$row['path']
  7783. );
  7784. $return .= Display::url(
  7785. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7786. $url.'&action=move_item'
  7787. );
  7788. // Commented for now as prerequisites cannot be added to chapters.
  7789. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7790. $return .= Display::url(
  7791. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7792. $url.'&action=edit_item_prereq'
  7793. );
  7794. }
  7795. $return .= Display::url(
  7796. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7797. $url.'&action=delete_item'
  7798. );
  7799. if ($item_type == TOOL_HOTPOTATOES) {
  7800. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7801. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7802. }
  7803. if ($item_type == TOOL_DOCUMENT) {
  7804. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7805. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7806. }
  7807. $return .= '</div>';
  7808. if (!empty($audio_player)) {
  7809. $return .= '<br />'.$audio_player;
  7810. }
  7811. return $return;
  7812. }
  7813. /**
  7814. * Creates the javascript needed for filling up the checkboxes without page reload
  7815. *
  7816. * @return string
  7817. */
  7818. public function get_js_dropdown_array()
  7819. {
  7820. $course_id = api_get_course_int_id();
  7821. $return = 'var child_name = new Array();'."\n";
  7822. $return .= 'var child_value = new Array();'."\n\n";
  7823. $return .= 'child_name[0] = new Array();'."\n";
  7824. $return .= 'child_value[0] = new Array();'."\n\n";
  7825. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7826. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7827. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7828. ORDER BY display_order ASC";
  7829. $res_zero = Database::query($sql_zero);
  7830. $i = 0;
  7831. while ($row_zero = Database :: fetch_array($res_zero)) {
  7832. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7833. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7834. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7835. }
  7836. $return .= "\n";
  7837. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7838. $res = Database::query($sql);
  7839. while ($row = Database :: fetch_array($res)) {
  7840. $sql_parent = "
  7841. SELECT * FROM ".$tbl_lp_item."
  7842. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7843. ORDER BY display_order ASC";
  7844. $res_parent = Database::query($sql_parent);
  7845. $i = 0;
  7846. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7847. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7848. while ($row_parent = Database :: fetch_array($res_parent)) {
  7849. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7850. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7851. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7852. }
  7853. $return .= "\n";
  7854. }
  7855. return $return;
  7856. }
  7857. /**
  7858. * Display the form to allow moving an item
  7859. * @param integer Item ID
  7860. * @return string HTML form
  7861. */
  7862. public function display_move_item($item_id)
  7863. {
  7864. $course_id = api_get_course_int_id();
  7865. $return = '';
  7866. if (is_numeric($item_id)) {
  7867. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7868. $sql = "SELECT * FROM ".$tbl_lp_item."
  7869. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7870. $res = Database::query($sql);
  7871. $row = Database :: fetch_array($res);
  7872. switch ($row['item_type']) {
  7873. case 'dokeos_chapter' :
  7874. case 'dir' :
  7875. case 'asset' :
  7876. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7877. $return .= $this->display_item_form(
  7878. $row['item_type'],
  7879. get_lang('MoveCurrentChapter'),
  7880. 'move',
  7881. $item_id,
  7882. $row
  7883. );
  7884. break;
  7885. case 'dokeos_module' :
  7886. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7887. $return .= $this->display_item_form(
  7888. $row['item_type'],
  7889. 'Move th current module:',
  7890. 'move',
  7891. $item_id,
  7892. $row
  7893. );
  7894. break;
  7895. case TOOL_DOCUMENT :
  7896. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7897. $return .= $this->display_document_form('move', $item_id, $row);
  7898. break;
  7899. case TOOL_LINK :
  7900. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7901. $return .= $this->display_link_form('move', $item_id, $row);
  7902. break;
  7903. case TOOL_HOTPOTATOES :
  7904. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7905. $return .= $this->display_link_form('move', $item_id, $row);
  7906. break;
  7907. case TOOL_QUIZ :
  7908. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7909. $return .= $this->display_quiz_form('move', $item_id, $row);
  7910. break;
  7911. case TOOL_STUDENTPUBLICATION :
  7912. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7913. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7914. break;
  7915. case TOOL_FORUM :
  7916. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7917. $return .= $this->display_forum_form('move', $item_id, $row);
  7918. break;
  7919. case TOOL_THREAD :
  7920. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7921. $return .= $this->display_forum_form('move', $item_id, $row);
  7922. break;
  7923. }
  7924. }
  7925. return $return;
  7926. }
  7927. /**
  7928. * Displays a basic form on the overview page for changing the item title and the item description.
  7929. * @param string $item_type
  7930. * @param string $title
  7931. * @param array $data
  7932. * @return string
  7933. */
  7934. public function display_item_small_form($item_type, $title = '', $data = array())
  7935. {
  7936. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7937. $form = new FormValidator('small_form', 'post', $url);
  7938. $form->addElement('header', $title);
  7939. $form->addElement('text', 'title', get_lang('Title'));
  7940. $form->addElement('button', 'submit_button', get_lang('Save'));
  7941. $form->addElement('hidden', 'id', $data['id']);
  7942. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7943. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7944. $form->setDefaults(array('title' => $data['title']));
  7945. return $form->toHtml();
  7946. }
  7947. /**
  7948. * Return HTML form to allow prerequisites selection
  7949. * @param integer Item ID
  7950. * @return string HTML form
  7951. */
  7952. public function display_item_prerequisites_form($item_id)
  7953. {
  7954. $course_id = api_get_course_int_id();
  7955. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7956. $item_id = intval($item_id);
  7957. /* Current prerequisite */
  7958. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7959. $result = Database::query($sql);
  7960. $row = Database::fetch_array($result);
  7961. $preq_id = $row['prerequisite'];
  7962. $return = '<legend>';
  7963. $return .= get_lang('AddEditPrerequisites');
  7964. $return .= '</legend>';
  7965. $return .= '<form method="POST">';
  7966. $return .= '<table class="data_table">';
  7967. $return .= '<tr>';
  7968. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7969. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  7970. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  7971. $return .= '</tr>';
  7972. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7973. $return .= '<tr >';
  7974. $return .= '<td colspan="3" class="radio">';
  7975. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7976. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  7977. $return .= '</tr>';
  7978. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7979. $result = Database::query($sql);
  7980. $arrLP = array();
  7981. while ($row = Database :: fetch_array($result)) {
  7982. $arrLP[] = array(
  7983. 'id' => $row['id'],
  7984. 'item_type' => $row['item_type'],
  7985. 'title' => $row['title'],
  7986. 'ref' => $row['ref'],
  7987. 'description' => $row['description'],
  7988. 'parent_item_id' => $row['parent_item_id'],
  7989. 'previous_item_id' => $row['previous_item_id'],
  7990. 'next_item_id' => $row['next_item_id'],
  7991. 'max_score' => $row['max_score'],
  7992. 'min_score' => $row['min_score'],
  7993. 'mastery_score' => $row['mastery_score'],
  7994. 'prerequisite' => $row['prerequisite'],
  7995. 'next_item_id' => $row['next_item_id'],
  7996. 'display_order' => $row['display_order']
  7997. );
  7998. if ($row['ref'] == $preq_id) {
  7999. $preq_mastery = $row['mastery_score'];
  8000. $preq_max = $row['max_score'];
  8001. }
  8002. }
  8003. $this->tree_array($arrLP);
  8004. $arrLP = $this->arrMenu;
  8005. unset($this->arrMenu);
  8006. $imgSysPath = api_get_path(SYS_IMG_PATH);
  8007. for ($i = 0; $i < count($arrLP); $i++) {
  8008. if ($arrLP[$i]['id'] == $item_id) {
  8009. break;
  8010. }
  8011. $return .= '<tr>';
  8012. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8013. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  8014. $depth_multi = $arrLP[$i]['depth'] * 10;
  8015. $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'].'" />';
  8016. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8017. if (file_exists($imgSysPath.'lp_'.$icon_name.'.png')) {
  8018. $return .= Display::return_icon('lp_'.$icon_name.'.png');
  8019. } else {
  8020. if (file_exists($imgSysPath.'lp_'.$icon_name.'.gif')) {
  8021. $return .= Display::return_icon('lp_'.$icon_name.'.gif');
  8022. } else {
  8023. $return .= Display::return_icon(
  8024. 'folder_document.gif',
  8025. '',
  8026. array('style' => 'margin-right:5px;')
  8027. );
  8028. }
  8029. }
  8030. $return .= $arrLP[$i]['title'].'</label>';
  8031. $return .= '</td>';
  8032. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8033. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8034. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8035. $return .= '</td>';
  8036. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8037. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8038. $return .= '</td>';
  8039. }
  8040. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8041. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8042. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8043. $return .= '</td>';
  8044. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8045. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8046. $return .= '</td>';
  8047. }
  8048. $return .= '</tr>';
  8049. }
  8050. $return .= '<tr>';
  8051. $return .= '</tr>';
  8052. $return .= '</table>';
  8053. $return .= '<div style="padding-top:3px;">';
  8054. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8055. 'ModifyPrerequisites'
  8056. ).'</button>';
  8057. $return .= '</form>';
  8058. return $return;
  8059. }
  8060. /**
  8061. * Return HTML list to allow prerequisites selection for lp
  8062. * @param integer Item ID
  8063. * @return string HTML form
  8064. */
  8065. public function display_lp_prerequisites_list()
  8066. {
  8067. $course_id = api_get_course_int_id();
  8068. $lp_id = $this->lp_id;
  8069. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8070. // get current prerequisite
  8071. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8072. $result = Database::query($sql);
  8073. $row = Database :: fetch_array($result);
  8074. $preq_id = $row['prerequisite'];
  8075. $session_id = api_get_session_id();
  8076. $session_condition = api_get_session_condition($session_id);
  8077. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8078. $rs = Database::query($sql);
  8079. $return = '';
  8080. $return .= '<select name="prerequisites" >';
  8081. $return .= '<option value="0">'.get_lang('None').'</option>';
  8082. if (Database::num_rows($rs) > 0) {
  8083. while ($row = Database::fetch_array($rs)) {
  8084. if ($row['id'] == $lp_id) {
  8085. continue;
  8086. }
  8087. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8088. }
  8089. }
  8090. $return .= '</select>';
  8091. return $return;
  8092. }
  8093. /**
  8094. * Creates a list with all the documents in it
  8095. * @return string
  8096. */
  8097. public function get_documents()
  8098. {
  8099. $course_info = api_get_course_info();
  8100. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8101. return $document_tree;
  8102. }
  8103. /**
  8104. * Creates a list with all the exercises (quiz) in it
  8105. * @return string
  8106. */
  8107. public function get_exercises()
  8108. {
  8109. $course_id = api_get_course_int_id();
  8110. // New for hotpotatoes.
  8111. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8112. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8113. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8114. $session_id = api_get_session_id();
  8115. $condition_session = api_get_session_condition($session_id);
  8116. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8117. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8118. $res_quiz = Database::query($sql_quiz);
  8119. $res_hot = Database::query($sql_hot);
  8120. $return = '<ul class="lp_resource">';
  8121. $return .= '<li class="lp_resource_element">';
  8122. $return .= Display::return_icon('new_test_small.gif');
  8123. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8124. $return .= '</li>';
  8125. // Display hotpotatoes
  8126. while ($row_hot = Database :: fetch_array($res_hot)) {
  8127. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8128. $return .= '<a class="moved" href="#">';
  8129. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8130. $return .= '</a> ';
  8131. $return .= Display::return_icon('hotpotatoes_s.png');
  8132. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8133. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8134. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8135. $row_hot['title']
  8136. )).'</a>';
  8137. $return .= '</li>';
  8138. }
  8139. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8140. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['iid'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8141. $return .= '<a class="moved" href="#">';
  8142. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8143. $return .= '</a> ';
  8144. $return .= Display::return_icon('quizz_small.gif');
  8145. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8146. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['iid'].'&amp;lp_id='.$this->lp_id.'">'.
  8147. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8148. '</a>';
  8149. $return .= '</li>';
  8150. }
  8151. $return .= '</ul>';
  8152. return $return;
  8153. }
  8154. /**
  8155. * Creates a list with all the links in it
  8156. * @return string
  8157. */
  8158. public function get_links()
  8159. {
  8160. $course_id = api_get_course_int_id();
  8161. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8162. $session_id = api_get_session_id();
  8163. $condition_session = api_get_session_condition($session_id);
  8164. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8165. $res_link = Database::query($sql_link);
  8166. $return = '<ul class="lp_resource">';
  8167. $return .= '<li class="lp_resource_element">';
  8168. $return .= Display::return_icon('linksnew.gif');
  8169. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8170. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8171. $return .= '</li>';
  8172. $course_info = api_get_course_info();
  8173. while ($row_link = Database :: fetch_array($res_link)) {
  8174. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8175. if ($item_visibility != 2) {
  8176. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8177. $return .= '<a class="moved" href="#">';
  8178. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8179. $return .= '</a> ';
  8180. $return .= Display::return_icon('lp_link.gif');
  8181. $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.'">'.
  8182. $row_link['title'].
  8183. '</a>';
  8184. $return .= '</li>';
  8185. }
  8186. }
  8187. $return .= '</ul>';
  8188. return $return;
  8189. }
  8190. /**
  8191. * Creates a list with all the student publications in it
  8192. * @return unknown
  8193. */
  8194. public function get_student_publications()
  8195. {
  8196. $return = '<div class="lp_resource" >';
  8197. $return .= '<div class="lp_resource_element">';
  8198. $return .= Display::return_icon('works_small.gif');
  8199. $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>';
  8200. $return .= '</div>';
  8201. $return .= '</div>';
  8202. return $return;
  8203. }
  8204. /**
  8205. * Creates a list with all the forums in it
  8206. * @return string
  8207. */
  8208. public function get_forums()
  8209. {
  8210. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  8211. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  8212. $a_forums = get_forums();
  8213. $return = '<ul class="lp_resource">';
  8214. //First add link
  8215. $return .= '<li class="lp_resource_element">';
  8216. $return .= Display::return_icon('forum_new_small.gif');
  8217. $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').'">
  8218. '.get_lang('CreateANewForum').'</a>';
  8219. $return .= '</li>';
  8220. $return .= '<script>
  8221. function toggle_forum(forum_id){
  8222. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8223. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8224. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8225. } else {
  8226. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8227. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8228. }
  8229. }
  8230. </script>';
  8231. foreach ($a_forums as $forum) {
  8232. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8233. if (!empty($forum['forum_id'])) {
  8234. $return .= '<a class="moved" href="#">';
  8235. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8236. $return .= ' </a>';
  8237. $return .= Display::return_icon('lp_forum.gif');
  8238. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8239. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8240. </a>
  8241. <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(
  8242. $forum['forum_title']
  8243. ).'</a>';
  8244. }
  8245. $return .= '</li>';
  8246. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8247. $a_threads = get_threads($forum['forum_id']);
  8248. if (is_array($a_threads)) {
  8249. foreach ($a_threads as $thread) {
  8250. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8251. $return .= '&nbsp;<a class="moved" href="#">';
  8252. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8253. $return .= ' </a>';
  8254. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8255. $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(
  8256. $thread['thread_title']
  8257. ).'</a>';
  8258. $return .= '</li>';
  8259. }
  8260. }
  8261. $return .= '</div>';
  8262. }
  8263. $return .= '</ul>';
  8264. return $return;
  8265. }
  8266. /**
  8267. * // TODO: The output encoding should be equal to the system encoding.
  8268. *
  8269. * Exports the learning path as a SCORM package. This is the main function that
  8270. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8271. * whole thing and returns the zip.
  8272. *
  8273. * This method needs to be called in PHP5, as it will fail with non-adequate
  8274. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8275. * you need to call it on a learnpath object.
  8276. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8277. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8278. * path has been modified, it should use the generic method here below.
  8279. * @TODO link this function with the export_lp() function in the same class
  8280. * @param string Optional name of zip file. If none, title of learnpath is
  8281. * domesticated and trailed with ".zip"
  8282. * @return string Returns the zip package string, or null if error
  8283. */
  8284. public function scorm_export()
  8285. {
  8286. $_course = api_get_course_info();
  8287. $course_id = api_get_course_int_id();
  8288. $links_to_create = null;
  8289. // Remove memory and time limits as much as possible as this might be a long process...
  8290. if (function_exists('ini_set')) {
  8291. api_set_memory_limit('128M');
  8292. ini_set('max_execution_time', 600);
  8293. }
  8294. // Create the zip handler (this will remain available throughout the method).
  8295. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8296. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8297. $temp_dir_short = uniqid();
  8298. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8299. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8300. $zip_folder = new PclZip($temp_zip_file);
  8301. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8302. $root_path = $main_path = api_get_path(SYS_PATH);
  8303. $files_cleanup = array();
  8304. // Place to temporarily stash the zipfiles.
  8305. // create the temp dir if it doesn't exist
  8306. // or do a cleanup befor creating the zipfile.
  8307. if (!is_dir($temp_zip_dir)) {
  8308. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8309. } else {
  8310. // Cleanup: Check the temp dir for old files and delete them.
  8311. $handle = opendir($temp_zip_dir);
  8312. while (false !== ($file = readdir($handle))) {
  8313. if ($file != '.' && $file != '..') {
  8314. unlink("$temp_zip_dir/$file");
  8315. }
  8316. }
  8317. closedir($handle);
  8318. }
  8319. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8320. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8321. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8322. )
  8323. ) {
  8324. // Remove the possible . at the end of the path.
  8325. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8326. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8327. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8328. $zip_files_dist = api_copyr(
  8329. $current_course_path.'/scorm/'.$this->path,
  8330. $dest_path_to_scorm_folder,
  8331. array('imsmanifest'),
  8332. $zip_files
  8333. );
  8334. }
  8335. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8336. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8337. // Aggregation Model official document, secion "2.3 Content Packaging".
  8338. $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.
  8339. $root = $xmldoc->createElement('manifest');
  8340. $root->setAttribute('identifier', 'SingleCourseManifest');
  8341. $root->setAttribute('version', '1.1');
  8342. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8343. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8344. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8345. $root->setAttribute(
  8346. 'xsi:schemaLocation',
  8347. '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'
  8348. );
  8349. // Build mandatory sub-root container elements.
  8350. $metadata = $xmldoc->createElement('metadata');
  8351. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8352. $metadata->appendChild($md_schema);
  8353. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8354. $metadata->appendChild($md_schemaversion);
  8355. $root->appendChild($metadata);
  8356. $organizations = $xmldoc->createElement('organizations');
  8357. $resources = $xmldoc->createElement('resources');
  8358. // Build the only organization we will use in building our learnpaths.
  8359. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8360. $organization = $xmldoc->createElement('organization');
  8361. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8362. // To set the title of the SCORM entity (=organization), we take the name given
  8363. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8364. // learning path charset) as it is the encoding that defines how it is stored
  8365. // in the database. Then we convert it to HTML entities again as the "&" character
  8366. // alone is not authorized in XML (must be &amp;).
  8367. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8368. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8369. $organization->appendChild($org_title);
  8370. $folder_name = 'document';
  8371. // Removes the learning_path/scorm_folder path when exporting see #4841
  8372. $path_to_remove = null;
  8373. $result = $this->generate_lp_folder($_course);
  8374. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8375. $path_to_remove = 'document'.$result['dir'];
  8376. $path_to_replace = $folder_name.'/';
  8377. }
  8378. //Fixes chamilo scorm exports
  8379. if ($this->ref == 'chamilo_scorm_export') {
  8380. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8381. }
  8382. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8383. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8384. $link_updates = array();
  8385. foreach ($this->items as $index => $item) {
  8386. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8387. // Get included documents from this item.
  8388. if ($item->type == 'sco') {
  8389. $inc_docs = $item->get_resources_from_source(
  8390. null,
  8391. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8392. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8393. );
  8394. } else {
  8395. $inc_docs = $item->get_resources_from_source();
  8396. }
  8397. // Give a child element <item> to the <organization> element.
  8398. $my_item_id = $item->get_id();
  8399. $my_item = $xmldoc->createElement('item');
  8400. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8401. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8402. $my_item->setAttribute('isvisible', 'true');
  8403. // Give a child element <title> to the <item> element.
  8404. $my_title = $xmldoc->createElement(
  8405. 'title',
  8406. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8407. );
  8408. $my_item->appendChild($my_title);
  8409. // Give a child element <adlcp:prerequisites> to the <item> element.
  8410. $my_prereqs = $xmldoc->createElement(
  8411. 'adlcp:prerequisites',
  8412. $this->get_scorm_prereq_string($my_item_id)
  8413. );
  8414. $my_prereqs->setAttribute('type', 'aicc_script');
  8415. $my_item->appendChild($my_prereqs);
  8416. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8417. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8418. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8419. //$xmldoc->createElement('adlcp:timelimitaction','');
  8420. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8421. //$xmldoc->createElement('adlcp:datafromlms','');
  8422. // Give a child element <adlcp:masteryscore> to the <item> element.
  8423. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8424. $my_item->appendChild($my_masteryscore);
  8425. // Attach this item to the organization element or hits parent if there is one.
  8426. if (!empty($item->parent) && $item->parent != 0) {
  8427. $children = $organization->childNodes;
  8428. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8429. if (is_object($possible_parent)) {
  8430. $possible_parent->appendChild($my_item);
  8431. } else {
  8432. if ($this->debug > 0) {
  8433. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8434. }
  8435. }
  8436. } else {
  8437. if ($this->debug > 0) {
  8438. error_log('No parent');
  8439. }
  8440. $organization->appendChild($my_item);
  8441. }
  8442. // Get the path of the file(s) from the course directory root.
  8443. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8444. if (!empty($path_to_remove)) {
  8445. //From docs
  8446. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8447. //From quiz
  8448. if ($this->ref == 'chamilo_scorm_export') {
  8449. $path_to_remove = 'scorm/'.$this->path.'/';
  8450. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8451. }
  8452. } else {
  8453. $my_xml_file_path = $my_file_path;
  8454. }
  8455. $my_sub_dir = dirname($my_file_path);
  8456. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8457. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8458. $my_xml_sub_dir = $my_sub_dir;
  8459. // Give a <resource> child to the <resources> element
  8460. $my_resource = $xmldoc->createElement('resource');
  8461. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8462. $my_resource->setAttribute('type', 'webcontent');
  8463. $my_resource->setAttribute('href', $my_xml_file_path);
  8464. // adlcp:scormtype can be either 'sco' or 'asset'.
  8465. if ($item->type == 'sco') {
  8466. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8467. } else {
  8468. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8469. }
  8470. // xml:base is the base directory to find the files declared in this resource.
  8471. $my_resource->setAttribute('xml:base', '');
  8472. // Give a <file> child to the <resource> element.
  8473. $my_file = $xmldoc->createElement('file');
  8474. $my_file->setAttribute('href', $my_xml_file_path);
  8475. $my_resource->appendChild($my_file);
  8476. // Dependency to other files - not yet supported.
  8477. $i = 1;
  8478. foreach ($inc_docs as $doc_info) {
  8479. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8480. continue;
  8481. }
  8482. $my_dep = $xmldoc->createElement('resource');
  8483. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8484. $my_dep->setAttribute('identifier', $res_id);
  8485. $my_dep->setAttribute('type', 'webcontent');
  8486. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8487. $my_dep_file = $xmldoc->createElement('file');
  8488. // Check type of URL.
  8489. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8490. if ($doc_info[1] == 'remote') {
  8491. // Remote file. Save url as is.
  8492. $my_dep_file->setAttribute('href', $doc_info[0]);
  8493. $my_dep->setAttribute('xml:base', '');
  8494. } elseif ($doc_info[1] == 'local') {
  8495. switch ($doc_info[2]) {
  8496. case 'url': // Local URL - save path as url for now, don't zip file.
  8497. $abs_path = api_get_path(SYS_PATH).str_replace(
  8498. api_get_path(WEB_PATH),
  8499. '',
  8500. $doc_info[0]
  8501. );
  8502. $current_dir = dirname($abs_path);
  8503. $current_dir = str_replace('\\', '/', $current_dir);
  8504. $file_path = realpath($abs_path);
  8505. $file_path = str_replace('\\', '/', $file_path);
  8506. $my_dep_file->setAttribute('href', $file_path);
  8507. $my_dep->setAttribute('xml:base', '');
  8508. if (strstr($file_path, $main_path) !== false) {
  8509. // The calculated real path is really inside Chamilo's root path.
  8510. // Reduce file path to what's under the DocumentRoot.
  8511. $file_path = substr($file_path, strlen($root_path) - 1);
  8512. //echo $file_path;echo '<br /><br />';
  8513. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8514. $zip_files_abs[] = $file_path;
  8515. $link_updates[$my_file_path][] = array(
  8516. 'orig' => $doc_info[0],
  8517. 'dest' => $file_path
  8518. );
  8519. $my_dep_file->setAttribute('href', $file_path);
  8520. $my_dep->setAttribute('xml:base', '');
  8521. } elseif (empty($file_path)) {
  8522. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8523. if (strpos($document_root, -1) == '/') {
  8524. $document_root = substr(0, -1, $document_root);
  8525. }*/
  8526. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8527. $file_path = str_replace('//', '/', $file_path);
  8528. if (file_exists($file_path)) {
  8529. $file_path = substr(
  8530. $file_path,
  8531. strlen($current_dir)
  8532. ); // We get the relative path.
  8533. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8534. $link_updates[$my_file_path][] = array(
  8535. 'orig' => $doc_info[0],
  8536. 'dest' => $file_path
  8537. );
  8538. $my_dep_file->setAttribute('href', $file_path);
  8539. $my_dep->setAttribute('xml:base', '');
  8540. }
  8541. }
  8542. break;
  8543. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8544. $my_dep_file->setAttribute('href', $doc_info[0]);
  8545. $my_dep->setAttribute('xml:base', '');
  8546. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8547. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8548. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8549. $abs_img_path_without_subdir = $doc_info[0];
  8550. $relp = api_get_path(REL_PATH); // The url-append config param.
  8551. $pos = strpos($abs_img_path_without_subdir, $relp);
  8552. if ($pos === 0) {
  8553. $abs_img_path_without_subdir = '/'.substr(
  8554. $abs_img_path_without_subdir,
  8555. strlen($relp)
  8556. );
  8557. }
  8558. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8559. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8560. $file_path = str_replace('\\', '/', $file_path);
  8561. $file_path = str_replace('//', '/', $file_path);
  8562. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8563. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8564. $cur_path = substr(
  8565. $current_course_path,
  8566. -1
  8567. ) == '/' ? $current_course_path : $current_course_path.'/';
  8568. // Check if the current document is in that path.
  8569. if (strstr($file_path, $cur_path) !== false) {
  8570. // The document is in that path, now get the relative path
  8571. // to the containing document.
  8572. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8573. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8574. $relative_path = '';
  8575. if (strstr($file_path, $cur_path) !== false) {
  8576. $relative_path = substr($file_path, strlen($orig_file_path));
  8577. $file_path = substr($file_path, strlen($cur_path));
  8578. } else {
  8579. // This case is still a problem as it's difficult to calculate a relative path easily
  8580. // might still generate wrong links.
  8581. //$file_path = substr($file_path,strlen($cur_path));
  8582. // Calculate the directory path to the current file (without trailing slash).
  8583. $my_relative_path = dirname($file_path);
  8584. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8585. $my_relative_file = basename($file_path);
  8586. // Calculate the directory path to the containing file (without trailing slash).
  8587. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8588. $dotdot = '';
  8589. $subdir = '';
  8590. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8591. $my_orig_file_path
  8592. ) > 1) && (strlen($my_relative_path) > 1)) {
  8593. $my_relative_path2 = dirname($my_relative_path);
  8594. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8595. $my_orig_file_path = dirname($my_orig_file_path);
  8596. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8597. $subdir = substr(
  8598. $my_relative_path,
  8599. strlen($my_relative_path2) + 1
  8600. ).'/'.$subdir;
  8601. $dotdot += '../';
  8602. $my_relative_path = $my_relative_path2;
  8603. }
  8604. $relative_path = $dotdot.$subdir.$my_relative_file;
  8605. }
  8606. // Put the current document in the zip (this array is the array
  8607. // that will manage documents already in the course folder - relative).
  8608. $zip_files[] = $file_path;
  8609. // Update the links to the current document in the containing document (make them relative).
  8610. $link_updates[$my_file_path][] = array(
  8611. 'orig' => $doc_info[0],
  8612. 'dest' => $relative_path
  8613. );
  8614. $my_dep_file->setAttribute('href', $file_path);
  8615. $my_dep->setAttribute('xml:base', '');
  8616. } elseif (strstr($file_path, $main_path) !== false) {
  8617. // The calculated real path is really inside Chamilo's root path.
  8618. // Reduce file path to what's under the DocumentRoot.
  8619. $file_path = substr($file_path, strlen($root_path));
  8620. //echo $file_path;echo '<br /><br />';
  8621. //error_log('Reduced path: '.$file_path, 0);
  8622. $zip_files_abs[] = $file_path;
  8623. $link_updates[$my_file_path][] = array(
  8624. 'orig' => $doc_info[0],
  8625. 'dest' => $file_path
  8626. );
  8627. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8628. $my_dep->setAttribute('xml:base', '');
  8629. } elseif (empty($file_path)) {
  8630. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8631. if(strpos($document_root,-1) == '/') {
  8632. $document_root = substr(0, -1, $document_root);
  8633. }*/
  8634. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8635. $file_path = str_replace('//', '/', $file_path);
  8636. if (file_exists($file_path)) {
  8637. $file_path = substr(
  8638. $file_path,
  8639. strlen($current_dir)
  8640. ); // We get the relative path.
  8641. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8642. $link_updates[$my_file_path][] = array(
  8643. 'orig' => $doc_info[0],
  8644. 'dest' => $file_path
  8645. );
  8646. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8647. $my_dep->setAttribute('xml:base', '');
  8648. }
  8649. }
  8650. break;
  8651. 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
  8652. if (substr($doc_info[0], 0, 2) == '..') {
  8653. // Relative path going up.
  8654. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8655. $current_dir = str_replace('\\', '/', $current_dir);
  8656. $file_path = realpath($current_dir.$doc_info[0]);
  8657. $file_path = str_replace('\\', '/', $file_path);
  8658. //error_log($file_path.' <-> '.$main_path,0);
  8659. if (strstr($file_path, $main_path) !== false) {
  8660. // The calculated real path is really inside Chamilo's root path.
  8661. // Reduce file path to what's under the DocumentRoot.
  8662. $file_path = substr($file_path, strlen($root_path));
  8663. //error_log('Reduced path: '.$file_path, 0);
  8664. $zip_files_abs[] = $file_path;
  8665. $link_updates[$my_file_path][] = array(
  8666. 'orig' => $doc_info[0],
  8667. 'dest' => $file_path
  8668. );
  8669. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8670. $my_dep->setAttribute('xml:base', '');
  8671. }
  8672. } else {
  8673. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8674. $my_dep_file->setAttribute('href', $doc_info[0]);
  8675. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8676. }
  8677. break;
  8678. default:
  8679. $my_dep_file->setAttribute('href', $doc_info[0]);
  8680. $my_dep->setAttribute('xml:base', '');
  8681. break;
  8682. }
  8683. }
  8684. $my_dep->appendChild($my_dep_file);
  8685. $resources->appendChild($my_dep);
  8686. $dependency = $xmldoc->createElement('dependency');
  8687. $dependency->setAttribute('identifierref', $res_id);
  8688. $my_resource->appendChild($dependency);
  8689. $i++;
  8690. }
  8691. //$my_dependency = $xmldoc->createElement('dependency');
  8692. //$my_dependency->setAttribute('identifierref', '');
  8693. $resources->appendChild($my_resource);
  8694. $zip_files[] = $my_file_path;
  8695. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8696. } else {
  8697. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8698. switch ($item->type) {
  8699. case TOOL_LINK:
  8700. $my_item = $xmldoc->createElement('item');
  8701. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8702. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8703. $my_item->setAttribute('isvisible', 'true');
  8704. // Give a child element <title> to the <item> element.
  8705. $my_title = $xmldoc->createElement(
  8706. 'title',
  8707. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8708. );
  8709. $my_item->appendChild($my_title);
  8710. // Give a child element <adlcp:prerequisites> to the <item> element.
  8711. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8712. $my_prereqs->setAttribute('type', 'aicc_script');
  8713. $my_item->appendChild($my_prereqs);
  8714. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8715. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8716. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8717. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8718. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8719. //$xmldoc->createElement('adlcp:datafromlms', '');
  8720. // Give a child element <adlcp:masteryscore> to the <item> element.
  8721. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8722. $my_item->appendChild($my_masteryscore);
  8723. // Attach this item to the organization element or its parent if there is one.
  8724. if (!empty($item->parent) && $item->parent != 0) {
  8725. $children = $organization->childNodes;
  8726. for ($i = 0; $i < $children->length; $i++) {
  8727. $item_temp = $children->item($i);
  8728. if ($item_temp->nodeName == 'item') {
  8729. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8730. $item_temp->appendChild($my_item);
  8731. }
  8732. }
  8733. }
  8734. } else {
  8735. $organization->appendChild($my_item);
  8736. }
  8737. $my_file_path = 'link_'.$item->get_id().'.html';
  8738. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8739. TABLE_LINK
  8740. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8741. $rs = Database::query($sql);
  8742. if ($link = Database :: fetch_array($rs)) {
  8743. $url = $link['url'];
  8744. $title = stripslashes($link['title']);
  8745. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8746. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8747. $my_xml_file_path = $my_file_path;
  8748. $my_sub_dir = dirname($my_file_path);
  8749. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8750. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8751. $my_xml_sub_dir = $my_sub_dir;
  8752. // Give a <resource> child to the <resources> element.
  8753. $my_resource = $xmldoc->createElement('resource');
  8754. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8755. $my_resource->setAttribute('type', 'webcontent');
  8756. $my_resource->setAttribute('href', $my_xml_file_path);
  8757. // adlcp:scormtype can be either 'sco' or 'asset'.
  8758. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8759. // xml:base is the base directory to find the files declared in this resource.
  8760. $my_resource->setAttribute('xml:base', '');
  8761. // give a <file> child to the <resource> element.
  8762. $my_file = $xmldoc->createElement('file');
  8763. $my_file->setAttribute('href', $my_xml_file_path);
  8764. $my_resource->appendChild($my_file);
  8765. $resources->appendChild($my_resource);
  8766. }
  8767. break;
  8768. case TOOL_QUIZ:
  8769. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8770. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8771. $exe = new Exercise();
  8772. $exe->read($exe_id);
  8773. $my_item = $xmldoc->createElement('item');
  8774. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8775. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8776. $my_item->setAttribute('isvisible', 'true');
  8777. // Give a child element <title> to the <item> element.
  8778. $my_title = $xmldoc->createElement(
  8779. 'title',
  8780. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8781. );
  8782. $my_item->appendChild($my_title);
  8783. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8784. //$my_item->appendChild($my_max_score);
  8785. // Give a child element <adlcp:prerequisites> to the <item> element.
  8786. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8787. $my_prereqs->setAttribute('type', 'aicc_script');
  8788. $my_item->appendChild($my_prereqs);
  8789. // Give a child element <adlcp:masteryscore> to the <item> element.
  8790. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8791. $my_item->appendChild($my_masteryscore);
  8792. // Attach this item to the organization element or hits parent if there is one.
  8793. if (!empty($item->parent) && $item->parent != 0) {
  8794. $children = $organization->childNodes;
  8795. for ($i = 0; $i < $children->length; $i++) {
  8796. $item_temp = $children->item($i);
  8797. if ($item_temp->nodeName == 'item') {
  8798. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8799. $item_temp->appendChild($my_item);
  8800. }
  8801. }
  8802. }
  8803. } else {
  8804. $organization->appendChild($my_item);
  8805. }
  8806. // Include export scripts.
  8807. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8808. // Get the path of the file(s) from the course directory root
  8809. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8810. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8811. // Write the contents of the exported exercise into a (big) html file
  8812. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8813. $contents = export_exercise($exe_id, true);
  8814. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8815. $res = file_put_contents($tmp_file_path, $contents);
  8816. if ($res === false) {
  8817. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8818. }
  8819. $files_cleanup[] = $tmp_file_path;
  8820. //error_log($tmp_path); die();
  8821. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8822. $my_xml_file_path = $my_file_path;
  8823. $my_sub_dir = dirname($my_file_path);
  8824. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8825. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8826. $my_xml_sub_dir = $my_sub_dir;
  8827. // Give a <resource> child to the <resources> element.
  8828. $my_resource = $xmldoc->createElement('resource');
  8829. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8830. $my_resource->setAttribute('type', 'webcontent');
  8831. $my_resource->setAttribute('href', $my_xml_file_path);
  8832. // adlcp:scormtype can be either 'sco' or 'asset'.
  8833. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8834. // xml:base is the base directory to find the files declared in this resource.
  8835. $my_resource->setAttribute('xml:base', '');
  8836. // Give a <file> child to the <resource> element.
  8837. $my_file = $xmldoc->createElement('file');
  8838. $my_file->setAttribute('href', $my_xml_file_path);
  8839. $my_resource->appendChild($my_file);
  8840. // Get included docs.
  8841. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8842. // Dependency to other files - not yet supported.
  8843. $i = 1;
  8844. foreach ($inc_docs as $doc_info) {
  8845. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8846. continue;
  8847. }
  8848. $my_dep = $xmldoc->createElement('resource');
  8849. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8850. $my_dep->setAttribute('identifier', $res_id);
  8851. $my_dep->setAttribute('type', 'webcontent');
  8852. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8853. $my_dep_file = $xmldoc->createElement('file');
  8854. // Check type of URL.
  8855. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8856. if ($doc_info[1] == 'remote') {
  8857. // Remote file. Save url as is.
  8858. $my_dep_file->setAttribute('href', $doc_info[0]);
  8859. $my_dep->setAttribute('xml:base', '');
  8860. } elseif ($doc_info[1] == 'local') {
  8861. switch ($doc_info[2]) {
  8862. case 'url': // Local URL - save path as url for now, don't zip file.
  8863. // Save file but as local file (retrieve from URL).
  8864. $abs_path = api_get_path(SYS_PATH).str_replace(
  8865. api_get_path(WEB_PATH),
  8866. '',
  8867. $doc_info[0]
  8868. );
  8869. $current_dir = dirname($abs_path);
  8870. $current_dir = str_replace('\\', '/', $current_dir);
  8871. $file_path = realpath($abs_path);
  8872. $file_path = str_replace('\\', '/', $file_path);
  8873. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8874. $my_dep->setAttribute('xml:base', '');
  8875. if (strstr($file_path, $main_path) !== false) {
  8876. // The calculated real path is really inside the chamilo root path.
  8877. // Reduce file path to what's under the DocumentRoot.
  8878. $file_path = substr($file_path, strlen($root_path));
  8879. //echo $file_path;echo '<br /><br />';
  8880. //error_log('Reduced path: '.$file_path, 0);
  8881. $zip_files_abs[] = $file_path;
  8882. $link_updates[$my_file_path][] = array(
  8883. 'orig' => $doc_info[0],
  8884. 'dest' => 'document/'.$file_path
  8885. );
  8886. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8887. $my_dep->setAttribute('xml:base', '');
  8888. } elseif (empty($file_path)) {
  8889. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8890. if (strpos($document_root,-1) == '/') {
  8891. $document_root = substr(0, -1, $document_root);
  8892. }*/
  8893. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8894. $file_path = str_replace('//', '/', $file_path);
  8895. if (file_exists($file_path)) {
  8896. $file_path = substr(
  8897. $file_path,
  8898. strlen($current_dir)
  8899. ); // We get the relative path.
  8900. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8901. $link_updates[$my_file_path][] = array(
  8902. 'orig' => $doc_info[0],
  8903. 'dest' => 'document/'.$file_path
  8904. );
  8905. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8906. $my_dep->setAttribute('xml:base', '');
  8907. }
  8908. }
  8909. break;
  8910. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8911. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8912. $current_dir = str_replace('\\', '/', $current_dir);
  8913. $file_path = realpath($doc_info[0]);
  8914. $file_path = str_replace('\\', '/', $file_path);
  8915. $my_dep_file->setAttribute('href', $file_path);
  8916. $my_dep->setAttribute('xml:base', '');
  8917. if (strstr($file_path, $main_path) !== false) {
  8918. // The calculated real path is really inside the chamilo root path.
  8919. // Reduce file path to what's under the DocumentRoot.
  8920. $file_path = substr($file_path, strlen($root_path));
  8921. //echo $file_path;echo '<br /><br />';
  8922. //error_log('Reduced path: '.$file_path, 0);
  8923. $zip_files_abs[] = $file_path;
  8924. $link_updates[$my_file_path][] = array(
  8925. 'orig' => $doc_info[0],
  8926. 'dest' => $file_path
  8927. );
  8928. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8929. $my_dep->setAttribute('xml:base', '');
  8930. } elseif (empty($file_path)) {
  8931. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8932. if (strpos($document_root,-1) == '/') {
  8933. $document_root = substr(0, -1, $document_root);
  8934. }*/
  8935. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8936. $file_path = str_replace('//', '/', $file_path);
  8937. if (file_exists($file_path)) {
  8938. $file_path = substr(
  8939. $file_path,
  8940. strlen($current_dir)
  8941. ); // We get the relative path.
  8942. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8943. $link_updates[$my_file_path][] = array(
  8944. 'orig' => $doc_info[0],
  8945. 'dest' => $file_path
  8946. );
  8947. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8948. $my_dep->setAttribute('xml:base', '');
  8949. }
  8950. }
  8951. break;
  8952. 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
  8953. if (substr($doc_info[0], 0, 2) == '..') {
  8954. // Relative path going up.
  8955. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8956. $current_dir = str_replace('\\', '/', $current_dir);
  8957. $file_path = realpath($current_dir.$doc_info[0]);
  8958. $file_path = str_replace('\\', '/', $file_path);
  8959. //error_log($file_path.' <-> '.$main_path, 0);
  8960. if (strstr($file_path, $main_path) !== false) {
  8961. // The calculated real path is really inside Chamilo's root path.
  8962. // Reduce file path to what's under the DocumentRoot.
  8963. $file_path = substr($file_path, strlen($root_path));
  8964. $file_path_dest = $file_path;
  8965. // File path is courses/CHAMILO/document/....
  8966. $info_file_path = explode('/', $file_path);
  8967. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8968. $file_path_dest = 'document/'.$file_path;
  8969. }
  8970. //error_log('Reduced path: '.$file_path, 0);
  8971. $zip_files_abs[] = $file_path;
  8972. $link_updates[$my_file_path][] = array(
  8973. 'orig' => $doc_info[0],
  8974. 'dest' => $file_path_dest
  8975. );
  8976. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8977. $my_dep->setAttribute('xml:base', '');
  8978. }
  8979. } else {
  8980. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8981. $my_dep_file->setAttribute('href', $doc_info[0]);
  8982. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8983. }
  8984. break;
  8985. default:
  8986. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8987. $my_dep->setAttribute('xml:base', '');
  8988. break;
  8989. }
  8990. }
  8991. $my_dep->appendChild($my_dep_file);
  8992. $resources->appendChild($my_dep);
  8993. $dependency = $xmldoc->createElement('dependency');
  8994. $dependency->setAttribute('identifierref', $res_id);
  8995. $my_resource->appendChild($dependency);
  8996. $i++;
  8997. }
  8998. $resources->appendChild($my_resource);
  8999. $zip_files[] = $my_file_path;
  9000. break;
  9001. default:
  9002. // Get the path of the file(s) from the course directory root
  9003. $my_file_path = 'non_exportable.html';
  9004. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9005. $my_xml_file_path = $my_file_path;
  9006. $my_sub_dir = dirname($my_file_path);
  9007. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9008. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9009. $my_xml_sub_dir = $my_sub_dir;
  9010. // Give a <resource> child to the <resources> element.
  9011. $my_resource = $xmldoc->createElement('resource');
  9012. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9013. $my_resource->setAttribute('type', 'webcontent');
  9014. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9015. // adlcp:scormtype can be either 'sco' or 'asset'.
  9016. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9017. // xml:base is the base directory to find the files declared in this resource.
  9018. $my_resource->setAttribute('xml:base', '');
  9019. // Give a <file> child to the <resource> element.
  9020. $my_file = $xmldoc->createElement('file');
  9021. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9022. $my_resource->appendChild($my_file);
  9023. $resources->appendChild($my_resource);
  9024. break;
  9025. }
  9026. }
  9027. }
  9028. $organizations->appendChild($organization);
  9029. $root->appendChild($organizations);
  9030. $root->appendChild($resources);
  9031. $xmldoc->appendChild($root);
  9032. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9033. // then add the file to the zip, then destroy the file (this is done automatically).
  9034. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9035. //error_log(print_r($zip_files,true), 0);
  9036. foreach ($zip_files as $file_path) {
  9037. if (empty($file_path)) {
  9038. continue;
  9039. }
  9040. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9041. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9042. $this->create_path($dest_file);
  9043. //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);
  9044. //echo $main_path.$file_path.'<br />';
  9045. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9046. // Check if the file needs a link update.
  9047. if (in_array($file_path, array_keys($link_updates))) {
  9048. $string = file_get_contents($dest_file);
  9049. unlink($dest_file);
  9050. foreach ($link_updates[$file_path] as $old_new) {
  9051. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9052. // This is an ugly hack that allows .flv files to be found by the flv player that
  9053. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9054. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9055. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9056. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9057. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9058. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9059. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9060. }
  9061. //Fix to avoid problems with default_course_document
  9062. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9063. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9064. } else {
  9065. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9066. $new_dest = $old_new['dest'];
  9067. }
  9068. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9069. $string = str_replace($old_new['orig'], $new_dest, $string);
  9070. //Add files inside the HTMLs
  9071. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9072. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9073. if (file_exists($sys_course_path.$new_path)) {
  9074. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9075. }
  9076. }
  9077. file_put_contents($dest_file, $string);
  9078. }
  9079. }
  9080. foreach ($zip_files_abs as $file_path) {
  9081. if (empty($file_path)) {
  9082. continue;
  9083. }
  9084. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9085. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9086. continue;
  9087. }
  9088. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9089. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9090. $this->create_path($dest_file);
  9091. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9092. //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);
  9093. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9094. copy($main_path.$file_path, $dest_file);
  9095. // Check if the file needs a link update.
  9096. if (in_array($file_path, array_keys($link_updates))) {
  9097. $string = file_get_contents($dest_file);
  9098. unlink($dest_file);
  9099. foreach ($link_updates[$file_path] as $old_new) {
  9100. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9101. // This is an ugly hack that allows .flv files to be found by the flv player that
  9102. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9103. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9104. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9105. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9106. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9107. }
  9108. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9109. }
  9110. file_put_contents($dest_file, $string);
  9111. }
  9112. }
  9113. if (is_array($links_to_create)) {
  9114. foreach ($links_to_create as $file => $link) {
  9115. $file_content = '<!DOCTYPE html>
  9116. <head>
  9117. <meta charset="'.api_get_language_isocode().'" />
  9118. <title>'.$link['title'].'</title>
  9119. </head>
  9120. <body dir="'.api_get_text_direction().'">
  9121. <div style="text-align:center">
  9122. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9123. </body>
  9124. </html>';
  9125. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9126. }
  9127. }
  9128. // Add non exportable message explanation.
  9129. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9130. $file_content = '<!DOCTYPE html>
  9131. <head>
  9132. <meta charset="'.api_get_language_isocode().'" />
  9133. <title>'.$lang_not_exportable.'</title>
  9134. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9135. </head>
  9136. <body dir="'.api_get_text_direction().'">';
  9137. $file_content .=
  9138. <<<EOD
  9139. <style>
  9140. .error-message {
  9141. font-family: arial, verdana, helvetica, sans-serif;
  9142. border-width: 1px;
  9143. border-style: solid;
  9144. left: 50%;
  9145. margin: 10px auto;
  9146. min-height: 30px;
  9147. padding: 5px;
  9148. right: 50%;
  9149. width: 500px;
  9150. background-color: #FFD1D1;
  9151. border-color: #FF0000;
  9152. color: #000;
  9153. }
  9154. </style>
  9155. <body>
  9156. <div class="error-message">
  9157. $lang_not_exportable
  9158. </div>
  9159. </body>
  9160. </html>
  9161. EOD;
  9162. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9163. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9164. }
  9165. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9166. // Add the extra files that go along with a SCORM package.
  9167. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9168. $extra_files = scandir($main_code_path);
  9169. foreach ($extra_files as $extra_file) {
  9170. if (strpos($extra_file, '.') === 0) {
  9171. continue;
  9172. } else {
  9173. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9174. $this->create_path($dest_file);
  9175. copy($main_code_path.$extra_file, $dest_file);
  9176. }
  9177. }
  9178. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9179. $manifest = @$xmldoc->saveXML();
  9180. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9181. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9182. $zip_folder->add(
  9183. $archive_path.'/'.$temp_dir_short,
  9184. PCLZIP_OPT_REMOVE_PATH,
  9185. $archive_path.'/'.$temp_dir_short.'/'
  9186. );
  9187. // Clean possible temporary files.
  9188. foreach ($files_cleanup as $file) {
  9189. $res = unlink($file);
  9190. if ($res === false) {
  9191. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9192. }
  9193. }
  9194. // Send file to client
  9195. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9196. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9197. }
  9198. public function scorm_export_to_pdf($lp_id)
  9199. {
  9200. $lp_id = intval($lp_id);
  9201. $files_to_export = array();
  9202. $course_data = api_get_course_info($this->cc);
  9203. if (!empty($course_data)) {
  9204. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9205. $list = self::get_flat_ordered_items_list($lp_id);
  9206. if (!empty($list)) {
  9207. foreach ($list as $item_id) {
  9208. $item = $this->items[$item_id];
  9209. switch ($item->type) {
  9210. case 'document':
  9211. //Getting documents from a LP with chamilo documents
  9212. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9213. $file_path = api_get_path(
  9214. SYS_COURSE_PATH
  9215. ).$course_data['path'].'/document'.$file_data['path'];
  9216. if (file_exists($file_path)) {
  9217. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9218. }
  9219. break;
  9220. case 'asset': //commes from a scorm package generated by chamilo
  9221. case 'sco':
  9222. $file_path = $scorm_path.'/'.$item->path;
  9223. if (file_exists($file_path)) {
  9224. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9225. }
  9226. break;
  9227. case 'dokeos_chapter':
  9228. case 'dir':
  9229. case 'chapter':
  9230. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9231. break;
  9232. }
  9233. }
  9234. }
  9235. $pdf = new PDF();
  9236. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9237. return $result;
  9238. }
  9239. return false;
  9240. }
  9241. /**
  9242. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9243. * if it doesn't exist
  9244. */
  9245. public function create_path($path)
  9246. {
  9247. $path_bits = explode('/', dirname($path));
  9248. // IS_WINDOWS_OS has been defined in api.lib.php
  9249. $path_built = IS_WINDOWS_OS ? '' : '/';
  9250. foreach ($path_bits as $bit) {
  9251. if (!empty ($bit)) {
  9252. $new_path = $path_built.$bit;
  9253. if (is_dir($new_path)) {
  9254. $path_built = $new_path.'/';
  9255. } else {
  9256. mkdir($new_path, api_get_permissions_for_new_directories());
  9257. $path_built = $new_path.'/';
  9258. }
  9259. }
  9260. }
  9261. }
  9262. /**
  9263. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9264. * @return boolean The results of the unlink function, or false if there was no image to start with
  9265. */
  9266. public function delete_lp_image()
  9267. {
  9268. $img = $this->get_preview_image();
  9269. if ($img != '') {
  9270. $del_file = $this->get_preview_image_path(null, 'sys');
  9271. if (isset($del_file) && file_exists($del_file)) {
  9272. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9273. if (file_exists($del_file_2)) {
  9274. unlink($del_file_2);
  9275. }
  9276. $this->set_preview_image('');
  9277. return @unlink($del_file);
  9278. }
  9279. }
  9280. return false;
  9281. }
  9282. /**
  9283. * Uploads an author image to the upload/learning_path/images directory
  9284. * @param array The image array, coming from the $_FILES superglobal
  9285. * @return boolean True on success, false on error
  9286. */
  9287. public function upload_image($image_array)
  9288. {
  9289. $image_moved = false;
  9290. if (!empty ($image_array['name'])) {
  9291. $upload_ok = FileManager::process_uploaded_file($image_array);
  9292. $has_attachment = true;
  9293. } else {
  9294. $image_moved = true;
  9295. }
  9296. if ($upload_ok) {
  9297. if ($has_attachment) {
  9298. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9299. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9300. $updir = $sys_course_path.$courseDir;
  9301. // Try to add an extension to the file if it hasn't one.
  9302. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9303. if (!FileManager::filter_extension($new_file_name)) {
  9304. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9305. $image_moved = false;
  9306. } else {
  9307. $file_extension = explode('.', $image_array['name']);
  9308. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9309. $filename = uniqid('');
  9310. $new_file_name = $filename.'.'.$file_extension;
  9311. $new_path = $updir.'/'.$new_file_name;
  9312. // Resize the image.
  9313. $temp = new Image($image_array['tmp_name']);
  9314. $picture_infos = $temp->get_image_info();
  9315. if ($picture_infos['width'] > 104) {
  9316. $thumbwidth = 104;
  9317. } else {
  9318. $thumbwidth = $picture_infos['width'];
  9319. }
  9320. if ($picture_infos['height'] > 96) {
  9321. $new_height = 96;
  9322. } else {
  9323. $new_height = $picture_infos['height'];
  9324. }
  9325. $temp->resize($thumbwidth, $new_height, 0);
  9326. $result = $temp->send_image($new_path);
  9327. // Storing the image filename.
  9328. if ($result) {
  9329. $image_moved = true;
  9330. $this->set_preview_image($new_file_name);
  9331. //Resize to 64px to use on course homepage
  9332. $temp->resize(64, 64, 0);
  9333. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9334. return true;
  9335. }
  9336. }
  9337. }
  9338. }
  9339. return false;
  9340. }
  9341. public function set_autolunch($lp_id, $status)
  9342. {
  9343. $course_id = api_get_course_int_id();
  9344. $lp_id = intval($lp_id);
  9345. $status = intval($status);
  9346. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9347. //Setting everything to autolunch = 0
  9348. $attributes['autolunch'] = 0;
  9349. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9350. Database::update($lp_table, $attributes, $where);
  9351. if ($status == 1) {
  9352. //Setting my lp_id to autolunch = 1
  9353. $attributes['autolunch'] = 1;
  9354. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9355. Database::update($lp_table, $attributes, $where);
  9356. }
  9357. }
  9358. /**
  9359. * Gets previous_item_id for the next element of the lp_item table
  9360. * @author Isaac flores paz
  9361. * @return integer Previous item ID
  9362. */
  9363. function select_previous_item_id()
  9364. {
  9365. $course_id = api_get_course_int_id();
  9366. if ($this->debug > 0) {
  9367. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9368. }
  9369. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9370. // Get the max order of the items
  9371. $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."'";
  9372. $rs_max_order = Database::query($sql_max_order);
  9373. $row_max_order = Database::fetch_object($rs_max_order);
  9374. $max_order = $row_max_order->display_order;
  9375. // Get the previous item ID
  9376. $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."' ";
  9377. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9378. $row_max = Database::fetch_object($rs_max);
  9379. // Return the previous item ID
  9380. return $row_max->previous;
  9381. }
  9382. function copy()
  9383. {
  9384. $main_path = api_get_path(SYS_CODE_PATH);
  9385. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9386. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9387. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9388. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9389. //Course builder
  9390. $cb = new CourseBuilder();
  9391. //Setting tools that will be copied
  9392. $cb->set_tools_to_build(array('learnpaths'));
  9393. //Setting elements that will be copied
  9394. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9395. $course = $cb->build();
  9396. //Course restorer
  9397. $course_restorer = new CourseRestorer($course);
  9398. $course_restorer->set_add_text_in_items(true);
  9399. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9400. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9401. }
  9402. function verify_document_size($s)
  9403. {
  9404. $post_max = ini_get('post_max_size');
  9405. $upl_max = ini_get('upload_max_filesize');
  9406. $documents_total_space = DocumentManager::documents_total_space();
  9407. $course_max_space = DocumentManager::get_course_quota();
  9408. $total_size = filesize($s) + $documents_total_space;
  9409. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9410. return true;
  9411. } else {
  9412. return false;
  9413. }
  9414. }
  9415. function clear_prerequisites()
  9416. {
  9417. $course_id = $this->get_course_int_id();
  9418. if ($this->debug > 0) {
  9419. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9420. }
  9421. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9422. $lp_id = $this->get_id();
  9423. //Cleaning prerequisites
  9424. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9425. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9426. Database::query($sql);
  9427. //Cleaning mastery score for exercises
  9428. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9429. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9430. Database::query($sql);
  9431. }
  9432. function set_previous_step_as_prerequisite_for_all_items()
  9433. {
  9434. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9435. $course_id = $this->get_course_int_id();
  9436. $lp_id = $this->get_id();
  9437. if (!empty($this->items)) {
  9438. $previous_item_id = null;
  9439. $previous_item_max = 0;
  9440. $previous_item_type = null;
  9441. $last_item_not_chapter = null;
  9442. $last_item_not_chapter_type = null;
  9443. $last_item_not_chapter_max = null;
  9444. foreach ($this->items as $item) {
  9445. // if there was a previous item... (otherwise jump to set it)
  9446. if (!empty($previous_item_id)) {
  9447. $current_item_id = $item->get_id(); //save current id
  9448. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9449. // Current item is not a folder, so it qualifies to get a prerequisites
  9450. if ($last_item_not_chapter_type == 'quiz') {
  9451. // if previous is quiz, mark its max score as default score to be achieved
  9452. $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'";
  9453. Database::query($sql);
  9454. }
  9455. // now simply update the prerequisite to set it to the last non-chapter item
  9456. $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'";
  9457. Database::query($sql);
  9458. // record item as 'non-chapter' reference
  9459. $last_item_not_chapter = $item->get_id();
  9460. $last_item_not_chapter_type = $item->get_type();
  9461. $last_item_not_chapter_max = $item->get_max();
  9462. }
  9463. } else {
  9464. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9465. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9466. $last_item_not_chapter = $item->get_id();
  9467. $last_item_not_chapter_type = $item->get_type();
  9468. $last_item_not_chapter_max = $item->get_max();
  9469. }
  9470. $old_id = $item->get_id();
  9471. $old_max = $item->get_max();
  9472. $old_type = $item->get_type();
  9473. }
  9474. // Saving the item as "previous item" for the next loop
  9475. $previous_item_id = $item->get_id();
  9476. $previous_item_max = $item->get_max();
  9477. $previous_item_type = $item->get_type();
  9478. }
  9479. }
  9480. }
  9481. static function create_category($params)
  9482. {
  9483. global $app;
  9484. $em = $app['orm.ems']['db_write'];
  9485. $item = new ChamiloLMS\Entity\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. global $app;
  9494. $em = $app['orm.ems']['db_write'];
  9495. $item = $em->find('ChamiloLMS\Entity\CLpCategory', $params['id']);
  9496. if ($item) {
  9497. $item->setName($params['name']);
  9498. $item->setCId($params['c_id']);
  9499. $em->persist($item);
  9500. $em->flush();
  9501. }
  9502. }
  9503. static function move_up_category($id)
  9504. {
  9505. global $app;
  9506. $em = $app['orm.ems']['db_write'];
  9507. $item = $em->find('ChamiloLMS\Entity\CLpCategory', $id);
  9508. if ($item) {
  9509. $position = $item->getPosition() - 1;
  9510. $item->setPosition($position);
  9511. $em->persist($item);
  9512. $em->flush();
  9513. }
  9514. }
  9515. static function move_down_category($id)
  9516. {
  9517. global $app;
  9518. $em = $app['orm.ems']['db_write'];
  9519. $item = $em->find('ChamiloLMS\Entity\CLpCategory', $id);
  9520. if ($item) {
  9521. $position = $item->getPosition() + 1;
  9522. $item->setPosition($position);
  9523. $em->persist($item);
  9524. $em->flush();
  9525. }
  9526. }
  9527. static function get_count_categories($course_id)
  9528. {
  9529. global $app;
  9530. if (empty($course_id)) {
  9531. return 0;
  9532. }
  9533. $em = $app['orm.em'];
  9534. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloLMS\Entity\CLpCategory u WHERE u.cId = :id');
  9535. $query->setParameter('id', $course_id);
  9536. return $query->getSingleScalarResult();
  9537. }
  9538. static function get_categories($course_id)
  9539. {
  9540. global $app;
  9541. $em = $app['orm.em'];
  9542. //Default behaviour
  9543. /*$items = $em->getRepository('ChamiloLMS\Entity\CLpCategory')->findBy(
  9544. array('cId' => $course_id),
  9545. array('name' => 'ASC')
  9546. );*/
  9547. //Using doctrine extensions
  9548. $items = $em->getRepository('ChamiloLMS\Entity\CLpCategory')->getBySortableGroupsQuery(
  9549. array('cId' => $course_id)
  9550. )->getResult();
  9551. return $items;
  9552. }
  9553. static function get_category($id)
  9554. {
  9555. global $app;
  9556. $em = $app['orm.em'];
  9557. $item = $em->find('ChamiloLMS\Entity\CLpCategory', $id);
  9558. return $item;
  9559. }
  9560. static function get_category_by_course($course_id)
  9561. {
  9562. global $app;
  9563. $items = $app['orm.em']->getRepository('ChamiloLMS\Entity\CLpCategory')->findBy(array('cId' => $course_id));
  9564. return $items;
  9565. }
  9566. static function delete_category($id)
  9567. {
  9568. global $app;
  9569. $em = $app['orm.ems']['db_write'];
  9570. $item = $em->find('ChamiloLMS\Entity\CLpCategory', $id);
  9571. if ($item) {
  9572. $courseId = $item->getCId();
  9573. $query = $em->createQuery('SELECT u FROM ChamiloLMS\Entity\CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9574. $query->setParameter('id', $courseId);
  9575. $query->setParameter('catId', $item->getId());
  9576. $lps = $query->getResult();
  9577. // Setting category = 0.
  9578. if ($lps) {
  9579. foreach ($lps as $lpItem) {
  9580. $lpItem->setCategoryId(0);
  9581. }
  9582. }
  9583. // Removing category.
  9584. $em->remove($item);
  9585. $em->flush();
  9586. }
  9587. }
  9588. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9589. {
  9590. $items = self::get_category_by_course($course_id);
  9591. $cats = array();
  9592. if ($addSelectOption) {
  9593. $cats = array(get_lang('SelectACategory'));
  9594. }
  9595. if (!empty($items)) {
  9596. foreach ($items as $cat) {
  9597. $cats[$cat->getId()] = $cat->getName();
  9598. }
  9599. }
  9600. return $cats;
  9601. }
  9602. public function setError($error) {
  9603. $this->error = $error;
  9604. }
  9605. }
  9606. if (!function_exists('trim_value')) {
  9607. function trim_value(& $value)
  9608. {
  9609. $value = trim($value);
  9610. }
  9611. }