learnpath.class.php 436 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use \ChamiloSession as Session;
  4. /**
  5. * This class defines the parent attributes and methods for Chamilo learnpaths
  6. * and SCORM learnpaths. It is used by the scorm class.
  7. *
  8. * @package chamilo.learnpath
  9. * @author Yannick Warnier <ywarnier@beeznest.org>
  10. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  11. */
  12. class learnpath
  13. {
  14. public $attempt = 0; // The number for the current ID view.
  15. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  16. public $current; // Id of the current item the user is viewing.
  17. public $current_score; // The score of the current item.
  18. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  19. public $current_time_stop; // The time the user closed this resource.
  20. public $default_status = 'not attempted';
  21. public $encoding = 'UTF-8';
  22. public $error = '';
  23. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  24. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  25. public $index; // The index of the active learnpath_item in $ordered_items array.
  26. public $items = array();
  27. public $last; // item_id of last item viewed in the learning path.
  28. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  29. public $license; // Which license this course has been given - not used yet on 20060522.
  30. public $lp_id; // DB ID for this learnpath.
  31. public $lp_view_id; // DB ID for lp_view
  32. public $log_file; // File where to log learnpath API msg.
  33. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  34. public $message = '';
  35. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  36. public $name; // Learnpath name (they generally have one).
  37. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  38. public $path = ''; // Path inside the scorm directory (if scorm).
  39. public $theme; // The current theme of the learning path.
  40. public $preview_image; // The current image of the learning path.
  41. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  42. public $prevent_reinit = 1;
  43. // Describes the mode of progress bar display.
  44. public $seriousgame_mode = 0;
  45. public $progress_bar_mode = '%';
  46. // Percentage progress as saved in the db.
  47. public $progress_db = '0';
  48. public $proximity; // Wether the content is distant or local or unknown.
  49. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  50. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  51. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  52. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  53. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  54. public $user_id; //ID of the user that is viewing/using the course
  55. public $update_queue = array();
  56. public $scorm_debug = 0;
  57. public $arrMenu = array(); // Array for the menu items.
  58. public $debug = 0; // Logging level.
  59. public $lp_session_id = 0;
  60. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  61. public $prerequisite = 0;
  62. public $use_max_score = 1; // 1 or 0
  63. public $created_on = '';
  64. public $modified_on = '';
  65. public $publicated_on = '';
  66. public $expired_on = '';
  67. public $ref = null;
  68. public $course_int_id;
  69. public $course_info = array();
  70. /**
  71. * Constructor.
  72. * Needs a database handler, a course code and a learnpath id from the database.
  73. * Also builds the list of items into $this->items.
  74. * @param string $course Course code
  75. * @param integer $lp_id
  76. * @param integer $user_id
  77. * @return boolean True on success, false on error
  78. */
  79. public function __construct($course, $lp_id, $user_id)
  80. {
  81. $this->encoding = api_get_system_encoding();
  82. if ($this->debug > 0) {
  83. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  84. }
  85. if (empty($course)) {
  86. $this->error = 'Course code is empty';
  87. return false;
  88. } else {
  89. $course_info = api_get_course_info($course);
  90. if (!empty($course_info)) {
  91. $this->cc = $course_info['code'];
  92. $this->course_info = $course_info;
  93. $course_id = $course_info['real_id'];
  94. } else {
  95. $this->error = 'Course code does not exist in database.';
  96. return false;
  97. }
  98. }
  99. $this->set_course_int_id($course_id);
  100. // Check learnpath ID.
  101. if (empty($lp_id)) {
  102. $this->error = 'Learnpath ID is empty';
  103. return false;
  104. } else {
  105. // TODO: Make it flexible to use any course_code (still using env course code here).
  106. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  107. $lp_id = intval($lp_id);
  108. $sql = "SELECT * FROM $lp_table
  109. WHERE id = '$lp_id' AND c_id = $course_id";
  110. if ($this->debug > 2) {
  111. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  112. }
  113. $res = Database::query($sql);
  114. if (Database::num_rows($res) > 0) {
  115. $this->lp_id = $lp_id;
  116. $row = Database::fetch_array($res);
  117. $this->type = $row['lp_type'];
  118. $this->name = stripslashes($row['name']);
  119. $this->proximity = $row['content_local'];
  120. $this->theme = $row['theme'];
  121. $this->maker = $row['content_maker'];
  122. $this->prevent_reinit = $row['prevent_reinit'];
  123. $this->seriousgame_mode = $row['seriousgame_mode'];
  124. $this->license = $row['content_license'];
  125. $this->scorm_debug = $row['debug'];
  126. $this->js_lib = $row['js_lib'];
  127. $this->path = $row['path'];
  128. $this->preview_image = $row['preview_image'];
  129. $this->author = $row['author'];
  130. $this->hide_toc_frame = $row['hide_toc_frame'];
  131. $this->lp_session_id = $row['session_id'];
  132. $this->use_max_score = $row['use_max_score'];
  133. $this->created_on = $row['created_on'];
  134. $this->modified_on = $row['modified_on'];
  135. $this->ref = $row['ref'];
  136. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  137. $this->publicated_on = $row['publicated_on'];
  138. }
  139. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  140. $this->expired_on = $row['expired_on'];
  141. }
  142. if ($this->type == 2) {
  143. if ($row['force_commit'] == 1) {
  144. $this->force_commit = true;
  145. }
  146. }
  147. $this->mode = $row['default_view_mod'];
  148. } else {
  149. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  150. return false;
  151. }
  152. }
  153. // Check user ID.
  154. if (empty($user_id)) {
  155. $this->error = 'User ID is empty';
  156. return false;
  157. } else {
  158. $user_info = api_get_user_info($user_id);
  159. if (!empty($user_info)) {
  160. $this->user_id = $user_info['user_id'];
  161. } else {
  162. $this->error = 'User ID does not exist in database ('.$sql.')';
  163. return false;
  164. }
  165. }
  166. // End of variables checking.
  167. $session_id = api_get_session_id();
  168. // Get the session condition for learning paths of the base + session.
  169. $session = api_get_session_condition($session_id);
  170. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  171. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  172. // Selecting by view_count descending allows to get the highest view_count first.
  173. $sql = "SELECT * FROM $lp_table
  174. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  175. ORDER BY view_count DESC";
  176. $res = Database::query($sql);
  177. if ($this->debug > 2) {
  178. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  179. }
  180. if (Database :: num_rows($res) > 0) {
  181. if ($this->debug > 2) {
  182. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  183. }
  184. $row = Database :: fetch_array($res);
  185. $this->attempt = $row['view_count'];
  186. $this->lp_view_id = $row['id'];
  187. $this->last_item_seen = $row['last_item'];
  188. $this->progress_db = $row['progress'];
  189. $this->lp_view_session_id = $row['session_id'];
  190. } else {
  191. if ($this->debug > 2) {
  192. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  193. }
  194. $this->attempt = 1;
  195. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  196. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  197. Database::query($sql);
  198. $this->lp_view_id = Database::insert_id();
  199. if ($this->debug > 2) {
  200. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  201. }
  202. }
  203. // Initialise items.
  204. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  205. $sql = "SELECT * FROM $lp_item_table
  206. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  207. ORDER BY parent_item_id, display_order";
  208. $res = Database::query($sql);
  209. if ($this->debug > 2) {
  210. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  211. error_log('-- Start while--', 0);
  212. }
  213. $lp_item_id_list = array();
  214. while ($row = Database::fetch_array($res)) {
  215. $lp_item_id_list[] = $row['id'];
  216. switch ($this->type) {
  217. case 3: //aicc
  218. $oItem = new aiccItem('db', $row['id'], $course_id);
  219. if (is_object($oItem)) {
  220. $my_item_id = $oItem->get_id();
  221. $oItem->set_lp_view($this->lp_view_id, $course_id);
  222. $oItem->set_prevent_reinit($this->prevent_reinit);
  223. // Don't use reference here as the next loop will make the pointed object change.
  224. $this->items[$my_item_id] = $oItem;
  225. $this->refs_list[$oItem->ref] = $my_item_id;
  226. if ($this->debug > 2) {
  227. error_log(
  228. 'New LP - learnpath::__construct() - ' .
  229. 'aicc object with id ' . $my_item_id .
  230. ' set in items[]', 0);
  231. }
  232. }
  233. break;
  234. case 2:
  235. require_once 'scorm.class.php';
  236. require_once 'scormItem.class.php';
  237. $oItem = new scormItem('db', $row['id'], $course_id);
  238. if (is_object($oItem)) {
  239. $my_item_id = $oItem->get_id();
  240. $oItem->set_lp_view($this->lp_view_id, $course_id);
  241. $oItem->set_prevent_reinit($this->prevent_reinit);
  242. // Don't use reference here as the next loop will make the pointed object change.
  243. $this->items[$my_item_id] = $oItem;
  244. $this->refs_list[$oItem->ref] = $my_item_id;
  245. if ($this->debug > 2) {
  246. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  247. }
  248. }
  249. break;
  250. case 1:
  251. default:
  252. if ($this->debug > 2) {
  253. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  254. }
  255. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  256. if ($this->debug > 2) {
  257. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  258. }
  259. if (is_object($oItem)) {
  260. $my_item_id = $oItem->get_id();
  261. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  262. $oItem->set_prevent_reinit($this->prevent_reinit);
  263. // Don't use reference here as the next loop will make the pointed object change.
  264. $this->items[$my_item_id] = $oItem;
  265. $this->refs_list[$my_item_id] = $my_item_id;
  266. if ($this->debug > 2) {
  267. error_log(
  268. 'New LP - learnpath::__construct() ' . __LINE__ .
  269. ' - object with id ' . $my_item_id . ' set in items[]',
  270. 0);
  271. }
  272. }
  273. break;
  274. }
  275. // Setting the object level with variable $this->items[$i][parent]
  276. foreach ($this->items as $itemLPObject) {
  277. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  278. $itemLPObject->level = $level;
  279. }
  280. // Setting the view in the item object.
  281. if (is_object($this->items[$row['id']])) {
  282. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  283. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  284. $this->items[$row['id']]->current_start_time = 0;
  285. $this->items[$row['id']]->current_stop_time = 0;
  286. }
  287. }
  288. }
  289. if ($this->debug > 2) {
  290. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  291. }
  292. if (!empty($lp_item_id_list)) {
  293. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  294. // Get last viewing vars.
  295. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  296. // This query should only return one or zero result.
  297. $sql = "SELECT lp_item_id, status
  298. FROM $lp_item_view_table
  299. WHERE
  300. c_id = $course_id AND
  301. lp_view_id = ".$this->lp_view_id." AND
  302. lp_item_id IN ('".$lp_item_id_list_to_string."')
  303. ORDER BY view_count DESC ";
  304. if ($this->debug > 2) {
  305. error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
  306. }
  307. $status_list = array();
  308. $res = Database::query($sql);
  309. while ($row = Database :: fetch_array($res) ) {
  310. $status_list[$row['lp_item_id']] = $row['status'];
  311. }
  312. foreach ($lp_item_id_list as $item_id) {
  313. if (isset($status_list[$item_id])) {
  314. $status = $status_list[$item_id];
  315. if (is_object($this->items[$item_id])) {
  316. $this->items[$item_id]->set_status($status);
  317. if (empty($status)) {
  318. $this->items[$item_id]->set_status($this->default_status);
  319. }
  320. }
  321. } else {
  322. if (is_object($this->items[$item_id])) {
  323. $this->items[$item_id]->set_status($this->default_status);
  324. }
  325. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  326. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  327. VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')";
  328. if ($this->debug > 2) {
  329. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql, 0);
  330. }
  331. $this->items[$item_id]->set_lp_view($this->lp_view_id, $course_id);
  332. Database::query($sql);
  333. }
  334. }
  335. }
  336. $this->ordered_items = self::get_flat_ordered_items_list(
  337. $this->get_id(),
  338. 0,
  339. $course_id
  340. );
  341. $this->max_ordered_items = 0;
  342. foreach ($this->ordered_items as $index => $dummy) {
  343. if ($index > $this->max_ordered_items && !empty($dummy)) {
  344. $this->max_ordered_items = $index;
  345. }
  346. }
  347. // TODO: Define the current item better.
  348. $this->first();
  349. if ($this->debug > 2) {
  350. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  351. }
  352. }
  353. /**
  354. * @return string
  355. */
  356. public function getCourseCode()
  357. {
  358. return $this->cc;
  359. }
  360. /**
  361. * @return int
  362. */
  363. public function get_course_int_id()
  364. {
  365. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  366. }
  367. /**
  368. * @param $course_id
  369. * @return int
  370. */
  371. public function set_course_int_id($course_id)
  372. {
  373. return $this->course_int_id = intval($course_id);
  374. }
  375. /**
  376. * Get the depth level of LP item
  377. * @param $in_tab_items
  378. * @param $in_current_item_id
  379. * @return int
  380. */
  381. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  382. {
  383. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  384. if ($parent_item_id == 0) {
  385. return 0;
  386. } else {
  387. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  388. }
  389. }
  390. /**
  391. * Function rewritten based on old_add_item() from Yannick Warnier.
  392. * Due the fact that users can decide where the item should come, I had to overlook this function and
  393. * I found it better to rewrite it. Old function is still available.
  394. * Added also the possibility to add a description.
  395. *
  396. * @param int $parent
  397. * @param int $previous
  398. * @param string $type
  399. * @param int resource ID (ref)
  400. * @param string $title
  401. * @param string $description
  402. * @param int $prerequisites
  403. * @param int $max_time_allowed
  404. * @return int
  405. */
  406. public function add_item(
  407. $parent,
  408. $previous,
  409. $type = 'dokeos_chapter',
  410. $id,
  411. $title,
  412. $description,
  413. $prerequisites = 0,
  414. $max_time_allowed = 0
  415. ) {
  416. $course_id = $this->course_info['real_id'];
  417. if ($this->debug > 0) {
  418. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  419. }
  420. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  421. $_course = $this->course_info;
  422. $parent = intval($parent);
  423. $previous = intval($previous);
  424. $id = intval($id);
  425. $max_time_allowed = htmlentities($max_time_allowed);
  426. if (empty($max_time_allowed)) {
  427. $max_time_allowed = 0;
  428. }
  429. $sql_count = " SELECT COUNT(id) AS num
  430. FROM $tbl_lp_item
  431. WHERE
  432. c_id = $course_id AND
  433. lp_id = " . $this->get_id() . " AND
  434. parent_item_id = " . $parent;
  435. $res_count = Database::query($sql_count);
  436. $row = Database :: fetch_array($res_count);
  437. $num = $row['num'];
  438. if ($num > 0) {
  439. if ($previous == 0) {
  440. $sql = "SELECT id, next_item_id, display_order
  441. FROM " . $tbl_lp_item . "
  442. WHERE
  443. c_id = $course_id AND
  444. lp_id = " . $this->get_id() . " AND
  445. parent_item_id = " . $parent . " AND
  446. previous_item_id = 0 OR
  447. previous_item_id=" . $parent;
  448. $result = Database::query($sql);
  449. $row = Database :: fetch_array($result);
  450. $tmp_previous = 0;
  451. $next = $row['id'];
  452. $display_order = 0;
  453. } else {
  454. $previous = (int) $previous;
  455. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  456. FROM $tbl_lp_item
  457. WHERE
  458. c_id = $course_id AND
  459. lp_id = " . $this->get_id() . " AND
  460. id = " . $previous;
  461. $result = Database::query($sql);
  462. $row = Database:: fetch_array($result);
  463. $tmp_previous = $row['id'];
  464. $next = $row['next_item_id'];
  465. $display_order = $row['display_order'];
  466. }
  467. } else {
  468. $tmp_previous = 0;
  469. $next = 0;
  470. $display_order = 0;
  471. }
  472. $id = intval($id);
  473. $typeCleaned = Database::escape_string($type);
  474. if ($type == 'quiz') {
  475. $sql = 'SELECT SUM(ponderation)
  476. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  477. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  478. ON quiz_question.id = quiz_rel_question.question_id
  479. WHERE quiz_rel_question.exercice_id = '.$id." AND
  480. quiz_question.c_id = $course_id AND
  481. quiz_rel_question.c_id = $course_id ";
  482. $rsQuiz = Database::query($sql);
  483. $max_score = Database :: result($rsQuiz, 0, 0);
  484. //Disabling the exercise if we add it inside a LP
  485. $exercise = new Exercise($course_id);
  486. $exercise->read($id);
  487. $exercise->disable();
  488. $exercise->save();
  489. } else {
  490. $max_score = 100;
  491. }
  492. $params = array(
  493. "c_id" => $course_id,
  494. "lp_id" => $this->get_id(),
  495. "item_type" =>$typeCleaned ,
  496. "ref" => '',
  497. "title" =>$title ,
  498. "description" => $description,
  499. "path" => $id,
  500. "max_score" => $max_score,
  501. "parent_item_id" => $parent,
  502. "previous_item_id" => $previous,
  503. "next_item_id" => $next,
  504. "display_order" => $display_order +1,
  505. "prerequisite" => $prerequisites,
  506. "max_time_allowed" => $max_time_allowed
  507. );
  508. if ($prerequisites != 0) {
  509. $params['prerequisite'] = $prerequisites;
  510. }
  511. $new_item_id = Database::insert($tbl_lp_item, $params);
  512. if ($this->debug > 2) {
  513. error_log('New LP - Inserting dokeos_chapter: ' . $new_item_id, 0);
  514. }
  515. if ($new_item_id) {
  516. // Update the item that should come after the new item.
  517. $sql_update_next = "
  518. UPDATE " . $tbl_lp_item . "
  519. SET previous_item_id = " . $new_item_id . "
  520. WHERE c_id = $course_id AND id = " . $next;
  521. Database::query($sql_update_next);
  522. // Update the item that should be before the new item.
  523. $sql_update_previous = "
  524. UPDATE " . $tbl_lp_item . "
  525. SET next_item_id = " . $new_item_id . "
  526. WHERE c_id = $course_id AND id = " . $tmp_previous;
  527. Database::query($sql_update_previous);
  528. // Update all the items after the new item.
  529. $sql_update_order = "
  530. UPDATE " . $tbl_lp_item . "
  531. SET display_order = display_order + 1
  532. WHERE
  533. c_id = $course_id AND
  534. lp_id = " . $this->get_id() . " AND
  535. id <> " . $new_item_id . " AND
  536. parent_item_id = " . $parent . " AND
  537. display_order > " . $display_order;
  538. Database::query($sql_update_order);
  539. // Update the item that should come after the new item.
  540. $sql_update_ref = "UPDATE " . $tbl_lp_item . "
  541. SET ref = " . $new_item_id . "
  542. WHERE c_id = $course_id AND id = " . $new_item_id;
  543. Database::query($sql_update_ref);
  544. // Upload audio.
  545. if (!empty($_FILES['mp3']['name'])) {
  546. // Create the audio folder if it does not exist yet.
  547. $filepath = api_get_path(SYS_COURSE_PATH) . $this->course_info['path'] . '/document/';
  548. if (!is_dir($filepath . 'audio')) {
  549. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  550. $audio_id = add_document(
  551. $this->course_info,
  552. '/audio',
  553. 'folder',
  554. 0,
  555. 'audio'
  556. );
  557. api_item_property_update(
  558. $this->course_info,
  559. TOOL_DOCUMENT,
  560. $audio_id,
  561. 'FolderCreated',
  562. api_get_user_id(),
  563. null,
  564. null,
  565. null,
  566. null,
  567. api_get_session_id()
  568. );
  569. api_item_property_update(
  570. $this->course_info,
  571. TOOL_DOCUMENT,
  572. $audio_id,
  573. 'invisible',
  574. api_get_user_id(),
  575. null,
  576. null,
  577. null,
  578. null,
  579. api_get_session_id()
  580. );
  581. }
  582. // Upload the file in the documents tool.
  583. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  584. $file_path = handle_uploaded_document(
  585. $_course,
  586. $_FILES['mp3'],
  587. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  588. '/audio',
  589. api_get_user_id(),
  590. '',
  591. '',
  592. '',
  593. '',
  594. false
  595. );
  596. // Getting the filename only.
  597. $file_components = explode('/', $file_path);
  598. $file = $file_components[count($file_components) - 1];
  599. // Store the mp3 file in the lp_item table.
  600. $sql = "UPDATE $tbl_lp_item SET
  601. audio = '" . Database::escape_string($file) . "'
  602. WHERE id = '" . intval($new_item_id) . "'";
  603. Database::query($sql);
  604. }
  605. }
  606. return $new_item_id;
  607. }
  608. /**
  609. * Static admin function allowing addition of a learnpath to a course.
  610. * @param string Course code
  611. * @param string Learnpath name
  612. * @param string Learnpath description string, if provided
  613. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  614. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  615. * @param string Zip file containing the learnpath or directory containing the learnpath
  616. * @return integer The new learnpath ID on success, 0 on failure
  617. */
  618. public static function add_lp(
  619. $courseCode,
  620. $name,
  621. $description = '',
  622. $learnpath = 'guess',
  623. $origin = 'zip',
  624. $zipname = '',
  625. $publicated_on = '',
  626. $expired_on = ''
  627. ) {
  628. global $charset;
  629. if (!empty($courseCode)) {
  630. $courseInfo = api_get_course_info($courseCode);
  631. $course_id = $courseInfo['real_id'];
  632. } else {
  633. $course_id = api_get_course_int_id();
  634. $courseInfo = api_get_course_info();
  635. }
  636. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  637. // Check course code exists.
  638. // Check lp_name doesn't exist, otherwise append something.
  639. $i = 0;
  640. $name = Database::escape_string($name);
  641. // Session id.
  642. $session_id = api_get_session_id();
  643. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  644. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  645. $res_name = Database::query($check_name);
  646. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  647. //by default the publication date is the same that the creation date
  648. //The behaviour above was changed due BT#2800
  649. global $_custom;
  650. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  651. $publicated_on = '';
  652. } else {
  653. $publicated_on = api_get_utc_datetime();
  654. }
  655. } else {
  656. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  657. }
  658. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  659. $expired_on = '';
  660. } else {
  661. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  662. }
  663. while (Database :: num_rows($res_name)) {
  664. // There is already one such name, update the current one a bit.
  665. $i++;
  666. $name = $name . ' - ' . $i;
  667. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  668. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  669. $res_name = Database::query($check_name);
  670. }
  671. // New name does not exist yet; keep it.
  672. // Escape description.
  673. // Kevin: added htmlentities().
  674. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  675. $type = 1;
  676. switch ($learnpath) {
  677. case 'guess':
  678. break;
  679. case 'dokeos':
  680. case 'chamilo':
  681. $type = 1;
  682. break;
  683. case 'aicc':
  684. break;
  685. }
  686. switch ($origin) {
  687. case 'zip':
  688. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  689. break;
  690. case 'manual':
  691. default:
  692. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  693. $res_max = Database::query($get_max);
  694. if (Database :: num_rows($res_max) < 1) {
  695. $dsp = 1;
  696. } else {
  697. $row = Database :: fetch_array($res_max);
  698. $dsp = $row[0] + 1;
  699. }
  700. $sql = "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) " .
  701. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
  702. Database::query($sql);
  703. $id = Database :: insert_id();
  704. if ($id > 0) {
  705. // Insert into item_property.
  706. api_item_property_update(
  707. $courseInfo,
  708. TOOL_LEARNPATH,
  709. $id,
  710. 'LearnpathAdded',
  711. api_get_user_id()
  712. );
  713. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo);
  714. return $id;
  715. }
  716. break;
  717. }
  718. }
  719. /**
  720. * Appends a message to the message attribute
  721. * @param string $string Message to append.
  722. */
  723. public function append_message($string)
  724. {
  725. if ($this->debug > 0) {
  726. error_log('New LP - In learnpath::append_message()', 0);
  727. }
  728. $this->message .= $string;
  729. }
  730. /**
  731. * Auto completes the parents of an item in case it's been completed or passed
  732. * @param integer $item Optional ID of the item from which to look for parents
  733. */
  734. public function autocomplete_parents($item)
  735. {
  736. $debug = $this->debug;
  737. if ($debug) {
  738. error_log('Learnpath::autocomplete_parents()', 0);
  739. }
  740. if (empty($item)) {
  741. $item = $this->current;
  742. }
  743. $currentItem = $this->getItem($item);
  744. if ($currentItem) {
  745. $parent_id = $currentItem->get_parent();
  746. $parent = $this->getItem($parent_id);
  747. if ($parent) {
  748. // if $item points to an object and there is a parent.
  749. if ($debug) {
  750. error_log(
  751. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  752. 0
  753. );
  754. }
  755. // New experiment including failed and browsed in completed status.
  756. //$current_status = $currentItem->get_status();
  757. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  758. // Fixes chapter auto complete
  759. if (true) {
  760. // If the current item is completed or passes or succeeded.
  761. $updateParentStatus = true;
  762. if ($debug) {
  763. error_log('Status of current item is alright', 0);
  764. }
  765. foreach ($parent->get_children() as $childItemId) {
  766. $childItem = $this->getItem($childItemId);
  767. // If children was not set try to get the info
  768. if (empty($childItem->db_item_view_id)) {
  769. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  770. }
  771. // Check all his brothers (parent's children) for completion status.
  772. if ($childItemId != $item) {
  773. if ($debug) {
  774. error_log(
  775. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  776. 0
  777. );
  778. }
  779. // Trying completing parents of failed and browsed items as well.
  780. if ($childItem->status_is(
  781. array(
  782. 'completed',
  783. 'passed',
  784. 'succeeded',
  785. 'browsed',
  786. 'failed'
  787. )
  788. )
  789. ) {
  790. // Keep completion status to true.
  791. continue;
  792. } else {
  793. if ($debug > 2) {
  794. error_log(
  795. 'Found one incomplete child of parent #' . $parent_id . ': child #'.$childItemId . ' "' . $childItem->get_title() . '", is ' . $childItem->get_status().' db_item_view_id:#'.$childItem->db_item_view_id,
  796. 0
  797. );
  798. }
  799. $updateParentStatus = false;
  800. break;
  801. }
  802. }
  803. }
  804. if ($updateParentStatus) {
  805. // If all the children were completed:
  806. $parent->set_status('completed');
  807. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  808. // Force the status to "completed"
  809. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  810. $this->update_queue[$parent->get_id()] = 'completed';
  811. if ($debug) {
  812. error_log(
  813. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  814. print_r($this->update_queue, 1),
  815. 0
  816. );
  817. }
  818. // Recursive call.
  819. $this->autocomplete_parents($parent->get_id());
  820. }
  821. }
  822. } else {
  823. if ($debug) {
  824. error_log("Parent #$parent_id does not exists");
  825. }
  826. }
  827. } else {
  828. if ($debug) {
  829. error_log("#$item is an item that doesn't have parents");
  830. }
  831. }
  832. }
  833. /**
  834. * Auto saves the current results into the database for the whole learnpath
  835. */
  836. public function autosave()
  837. {
  838. if ($this->debug > 0) {
  839. error_log('New LP - In learnpath::autosave()', 0);
  840. }
  841. // TODO: Add save operations for the learnpath itself.
  842. }
  843. /**
  844. * Clears the message attribute
  845. */
  846. public function clear_message()
  847. {
  848. if ($this->debug > 0) {
  849. error_log('New LP - In learnpath::clear_message()', 0);
  850. }
  851. $this->message = '';
  852. }
  853. /**
  854. * Closes the current resource
  855. *
  856. * Stops the timer
  857. * Saves into the database if required
  858. * Clears the current resource data from this object
  859. * @return boolean True on success, false on failure
  860. */
  861. public function close()
  862. {
  863. $course_id = api_get_course_int_id();
  864. if ($this->debug > 0) {
  865. error_log('New LP - In learnpath::close()', 0);
  866. }
  867. if (empty ($this->lp_id)) {
  868. $this->error = 'Trying to close this learnpath but no ID is set';
  869. return false;
  870. }
  871. $this->current_time_stop = time();
  872. if ($this->save) {
  873. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  874. /*
  875. $sql = "UPDATE $learnpath_view_table " .
  876. "SET " .
  877. "stop_time = ".$this->current_time_stop.", " .
  878. "score = ".$this->current_score.", ".
  879. "WHERE learnpath_id = '".$this->lp_id."'";
  880. //$res = Database::query($sql);
  881. $res = Database::query($res);
  882. if (Database::affected_rows($res) < 1) {
  883. $this->error = 'Could not update learnpath_view table while closing learnpath';
  884. return false;
  885. }
  886. */
  887. }
  888. $this->ordered_items = array();
  889. $this->index = 0;
  890. unset ($this->lp_id);
  891. //unset other stuff
  892. return true;
  893. }
  894. /**
  895. * Static admin function allowing removal of a learnpath
  896. * @param array $courseInfo
  897. * @param integer Learnpath ID
  898. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  899. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  900. */
  901. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  902. {
  903. $course_id = api_get_course_int_id();
  904. if (!empty($courseInfo)) {
  905. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  906. }
  907. // TODO: Implement a way of getting this to work when the current object is not set.
  908. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  909. // If an ID is specifically given and the current LP is not the same, prevent delete.
  910. if (!empty ($id) && ($id != $this->lp_id)) {
  911. return false;
  912. }
  913. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  914. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  915. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  916. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  917. // Delete lp item id.
  918. foreach ($this->items as $id => $dummy) {
  919. $sql = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  920. Database::query($sql);
  921. }
  922. // Proposed by Christophe (nickname: clefevre)
  923. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  924. Database::query($sql);
  925. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  926. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  927. Database::query($sql);
  928. self::toggle_publish($this->lp_id, 'i');
  929. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  930. if ($this->type == 2 || $this->type == 3) {
  931. // This is a scorm learning path, delete the files as well.
  932. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  933. $res = Database::query($sql);
  934. if (Database :: num_rows($res) > 0) {
  935. $row = Database :: fetch_array($res);
  936. $path = $row['path'];
  937. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  938. $res = Database::query($sql);
  939. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  940. if ($this->debug > 2) {
  941. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  942. }
  943. } else {
  944. // No other LP uses that directory, delete it.
  945. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  946. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  947. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  948. if ($this->debug > 2) {
  949. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  950. }
  951. // Proposed by Christophe (clefevre).
  952. if (strcmp(substr($path, -2), "/.") == 0) {
  953. $path = substr($path, 0, -1); // Remove "." at the end.
  954. }
  955. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  956. rmdirr($course_scorm_dir . $path);
  957. }
  958. }
  959. }
  960. }
  961. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  962. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  963. // Delete tools
  964. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  965. Database::query($sql);
  966. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  967. Database::query($sql);
  968. // Updates the display order of all lps.
  969. $this->update_display_order();
  970. api_item_property_update(
  971. api_get_course_info(),
  972. TOOL_LEARNPATH,
  973. $this->lp_id,
  974. 'delete',
  975. api_get_user_id()
  976. );
  977. require_once '../gradebook/lib/be.inc.php';
  978. // Delete link of gradebook tool
  979. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  980. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  981. $result = Database::query($sql);
  982. $row = Database :: fetch_array($result, 'ASSOC');*/
  983. // Fixing gradebook link deleted see #5229.
  984. /*
  985. if (!empty($row['id'])) {
  986. $link = LinkFactory :: load($row['id']);
  987. if ($link[0] != null) {
  988. $link[0]->delete();
  989. }
  990. }*/
  991. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  992. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  993. if ($link_info !== false) {
  994. remove_resource_from_course_gradebook($link_info['id']);
  995. }
  996. if (api_get_setting('search_enabled') == 'true') {
  997. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  998. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  999. }
  1000. }
  1001. /**
  1002. * Removes all the children of one item - dangerous!
  1003. * @param integer $id Element ID of which children have to be removed
  1004. * @return integer Total number of children removed
  1005. */
  1006. public function delete_children_items($id)
  1007. {
  1008. $course_id = api_get_course_int_id();
  1009. if ($this->debug > 0) {
  1010. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1011. }
  1012. $num = 0;
  1013. if (empty ($id) || $id != strval(intval($id))) {
  1014. return false;
  1015. }
  1016. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1017. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1018. $res = Database::query($sql);
  1019. while ($row = Database :: fetch_array($res)) {
  1020. $num += $this->delete_children_items($row['id']);
  1021. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1022. Database::query($sql_del);
  1023. $num++;
  1024. }
  1025. return $num;
  1026. }
  1027. /**
  1028. * Removes an item from the current learnpath
  1029. * @param integer $id Elem ID (0 if first)
  1030. * @param integer $remove Whether to remove the resource/data from the
  1031. * system or leave it (default: 'keep', others 'remove')
  1032. * @return integer Number of elements moved
  1033. * @todo implement resource removal
  1034. */
  1035. public function delete_item($id, $remove = 'keep')
  1036. {
  1037. $course_id = api_get_course_int_id();
  1038. if ($this->debug > 0) {
  1039. error_log('New LP - In learnpath::delete_item()', 0);
  1040. }
  1041. // TODO: Implement the resource removal.
  1042. if (empty ($id) || $id != strval(intval($id))) {
  1043. return false;
  1044. }
  1045. // First select item to get previous, next, and display order.
  1046. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1047. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1048. $res_sel = Database::query($sql_sel);
  1049. if (Database :: num_rows($res_sel) < 1) {
  1050. return false;
  1051. }
  1052. $row = Database :: fetch_array($res_sel);
  1053. $previous = $row['previous_item_id'];
  1054. $next = $row['next_item_id'];
  1055. $display = $row['display_order'];
  1056. $parent = $row['parent_item_id'];
  1057. $lp = $row['lp_id'];
  1058. // Delete children items.
  1059. $num = $this->delete_children_items($id);
  1060. if ($this->debug > 2) {
  1061. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1062. }
  1063. // Now delete the item.
  1064. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1065. if ($this->debug > 2) {
  1066. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1067. }
  1068. Database::query($sql_del);
  1069. // Now update surrounding items.
  1070. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1071. WHERE c_id = ".$course_id." AND id = $previous";
  1072. Database::query($sql_upd);
  1073. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1074. WHERE c_id = ".$course_id." AND id = $next";
  1075. Database::query($sql_upd);
  1076. // Now update all following items with new display order.
  1077. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1078. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1079. Database::query($sql_all);
  1080. //Removing prerequisites since the item will not longer exist
  1081. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1082. Database::query($sql_all);
  1083. // Remove from search engine if enabled.
  1084. if (api_get_setting('search_enabled') == 'true') {
  1085. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1086. $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';
  1087. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1088. $res = Database::query($sql);
  1089. if (Database :: num_rows($res) > 0) {
  1090. $row2 = Database :: fetch_array($res);
  1091. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1092. $di = new ChamiloIndexer();
  1093. $di->remove_document((int) $row2['search_did']);
  1094. }
  1095. $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';
  1096. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1097. Database::query($sql);
  1098. }
  1099. }
  1100. /**
  1101. * Updates an item's content in place
  1102. * @param integer Element ID
  1103. * @param integer Parent item ID
  1104. * @param integer Previous item ID
  1105. * @param string Item title
  1106. * @param string Item description
  1107. * @param string Prerequisites (optional)
  1108. * @param string Indexing terms (optional)
  1109. * @param array The array resulting of the $_FILES[mp3] element
  1110. * @return boolean True on success, false on error
  1111. */
  1112. public function edit_item(
  1113. $id,
  1114. $parent,
  1115. $previous,
  1116. $title,
  1117. $description,
  1118. $prerequisites = 0,
  1119. $audio = null,
  1120. $max_time_allowed = 0,
  1121. $url = ''
  1122. ) {
  1123. $course_id = api_get_course_int_id();
  1124. if ($this->debug > 0) {
  1125. error_log('New LP - In learnpath::edit_item()', 0);
  1126. }
  1127. if (empty ($max_time_allowed)) {
  1128. $max_time_allowed = 0;
  1129. }
  1130. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1131. return false;
  1132. }
  1133. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1134. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1135. $res_select = Database::query($sql_select);
  1136. $row_select = Database :: fetch_array($res_select);
  1137. $audio_update_sql = '';
  1138. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1139. // Create the audio folder if it does not exist yet.
  1140. global $_course;
  1141. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1142. if (!is_dir($filepath . 'audio')) {
  1143. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1144. $audio_id = add_document(
  1145. $_course,
  1146. '/audio',
  1147. 'folder',
  1148. 0,
  1149. 'audio'
  1150. );
  1151. api_item_property_update(
  1152. $_course,
  1153. TOOL_DOCUMENT,
  1154. $audio_id,
  1155. 'FolderCreated',
  1156. api_get_user_id(),
  1157. null,
  1158. null,
  1159. null,
  1160. null,
  1161. api_get_session_id()
  1162. );
  1163. api_item_property_update(
  1164. $_course,
  1165. TOOL_DOCUMENT,
  1166. $audio_id,
  1167. 'invisible',
  1168. api_get_user_id(),
  1169. null,
  1170. null,
  1171. null,
  1172. null,
  1173. api_get_session_id()
  1174. );
  1175. }
  1176. // Upload file in documents.
  1177. $pi = pathinfo($audio['name']);
  1178. if ($pi['extension'] == 'mp3') {
  1179. $c_det = api_get_course_info($this->cc);
  1180. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1181. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1182. $path = substr($path, 7);
  1183. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1184. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1185. }
  1186. }
  1187. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1188. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1189. // TODO: htmlspecialchars to be checked for encoding related problems.
  1190. if ($same_parent && $same_previous) {
  1191. // Only update title and description.
  1192. $sql_update = " UPDATE " . $tbl_lp_item . "
  1193. SET title = '" . Database::escape_string($title) . "',
  1194. prerequisite = '" . $prerequisites . "',
  1195. description = '" . Database::escape_string($description) . "'
  1196. " . $audio_update_sql . ",
  1197. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1198. WHERE c_id = ".$course_id." AND id = " . $id;
  1199. Database::query($sql_update);
  1200. } else {
  1201. $old_parent = $row_select['parent_item_id'];
  1202. $old_previous = $row_select['previous_item_id'];
  1203. $old_next = $row_select['next_item_id'];
  1204. $old_order = $row_select['display_order'];
  1205. $old_prerequisite = $row_select['prerequisite'];
  1206. $old_max_time_allowed = $row_select['max_time_allowed'];
  1207. /* BEGIN -- virtually remove the current item id */
  1208. /* for the next and previous item it is like the current item doesn't exist anymore */
  1209. if ($old_previous != 0) {
  1210. $sql_update_next = "
  1211. UPDATE " . $tbl_lp_item . "
  1212. SET next_item_id = " . $old_next . "
  1213. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1214. Database::query($sql_update_next);
  1215. //echo '<p>' . $sql_update_next . '</p>';
  1216. }
  1217. if ($old_next != 0) {
  1218. $sql_update_previous = "
  1219. UPDATE " . $tbl_lp_item . "
  1220. SET previous_item_id = " . $old_previous . "
  1221. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1222. Database::query($sql_update_previous);
  1223. }
  1224. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1225. $sql_update_order = "
  1226. UPDATE " . $tbl_lp_item . "
  1227. SET display_order = display_order - 1
  1228. WHERE
  1229. c_id = ".$course_id." AND
  1230. display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
  1231. parent_item_id = " . $old_parent;
  1232. Database::query($sql_update_order);
  1233. /* END -- virtually remove the current item id */
  1234. /* BEGIN -- update the current item id to his new location */
  1235. if ($previous == 0) {
  1236. // Select the data of the item that should come after the current item.
  1237. $sql_select_old = "SELECT id, display_order
  1238. FROM " . $tbl_lp_item . "
  1239. WHERE
  1240. c_id = ".$course_id." AND
  1241. lp_id = " . $this->lp_id . " AND
  1242. parent_item_id = " . $parent . " AND
  1243. previous_item_id = " . $previous;
  1244. $res_select_old = Database::query($sql_select_old);
  1245. $row_select_old = Database::fetch_array($res_select_old);
  1246. //echo '<p>' . $sql_select_old . '</p>';
  1247. // If the new parent didn't have children before.
  1248. if (Database :: num_rows($res_select_old) == 0) {
  1249. $new_next = 0;
  1250. $new_order = 1;
  1251. } else {
  1252. $new_next = $row_select_old['id'];
  1253. $new_order = $row_select_old['display_order'];
  1254. }
  1255. } else {
  1256. // Select the data of the item that should come before the current item.
  1257. $sql_select_old = " SELECT next_item_id, display_order
  1258. FROM " . $tbl_lp_item . "
  1259. WHERE c_id = ".$course_id." AND id = " . $previous;
  1260. $res_select_old = Database::query($sql_select_old);
  1261. $row_select_old = Database :: fetch_array($res_select_old);
  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. Database::query($sql_update);
  1278. if ($previous != 0) {
  1279. // Update the previous item's next_item_id.
  1280. $sql_update_previous = "
  1281. UPDATE " . $tbl_lp_item . "
  1282. SET next_item_id = " . $id . "
  1283. WHERE c_id = ".$course_id." AND id = " . $previous;
  1284. Database::query($sql_update_previous);
  1285. }
  1286. if ($new_next != 0) {
  1287. // Update the next item's previous_item_id.
  1288. $sql_update_next = "
  1289. UPDATE " . $tbl_lp_item . "
  1290. SET previous_item_id = " . $id . "
  1291. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1292. Database::query($sql_update_next);
  1293. }
  1294. if ($old_prerequisite != $prerequisites) {
  1295. $sql_update_next = "
  1296. UPDATE " . $tbl_lp_item . "
  1297. SET prerequisite = " . $prerequisites . "
  1298. WHERE c_id = ".$course_id." AND id = " . $id;
  1299. Database::query($sql_update_next);
  1300. }
  1301. if ($old_max_time_allowed != $max_time_allowed) {
  1302. $sql_update_max_time_allowed = "
  1303. UPDATE " . $tbl_lp_item . "
  1304. SET max_time_allowed = " . $max_time_allowed . "
  1305. WHERE c_id = ".$course_id." AND id = " . $id;
  1306. Database::query($sql_update_max_time_allowed);
  1307. }
  1308. // Update all the items with the same or a bigger display_order than the current item.
  1309. $sql_update_order = "
  1310. UPDATE " . $tbl_lp_item . "
  1311. SET display_order = display_order + 1
  1312. WHERE
  1313. c_id = ".$course_id." AND
  1314. lp_id = " . $this->get_id() . " AND
  1315. id <> " . $id . " AND
  1316. parent_item_id = " . $parent . " AND
  1317. display_order >= " . $new_order;
  1318. Database::query($sql_update_order);
  1319. }
  1320. if ($row_select['item_type'] == 'link') {
  1321. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  1322. $link = new Link();
  1323. $linkId = $row_select['path'];
  1324. $link->updateLink($linkId, $url);
  1325. }
  1326. }
  1327. /**
  1328. * Updates an item's prereq in place
  1329. * @param integer Element ID
  1330. * @param string Prerequisite Element ID
  1331. * @param string Prerequisite item type
  1332. * @param string Prerequisite min score
  1333. * @param string Prerequisite max score
  1334. * @return boolean True on success, false on error
  1335. */
  1336. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1337. {
  1338. $course_id = api_get_course_int_id();
  1339. if ($this->debug > 0) {
  1340. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1341. }
  1342. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1343. return false;
  1344. }
  1345. $prerequisite_id = intval($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 = " UPDATE " . $tbl_lp_item . "
  1360. SET prerequisite = " . $prerequisite_id . "
  1361. WHERE c_id = ".$course_id." AND id = " . $id;
  1362. Database::query($sql);
  1363. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1364. $sql = " UPDATE " . $tbl_lp_item . " SET
  1365. mastery_score = " . $mastery_score .
  1366. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1367. " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
  1368. Database::query($sql);
  1369. }
  1370. // TODO: Update the item object (can be ignored for now because refreshed).
  1371. return true;
  1372. }
  1373. /**
  1374. * Escapes a string with the available database escape function
  1375. * @param string String to escape
  1376. * @return string String escaped
  1377. * @deprecated use Database::escape_string
  1378. */
  1379. public function escape_string($string)
  1380. {
  1381. return Database::escape_string($string);
  1382. }
  1383. /**
  1384. * Static admin function exporting a learnpath into a zip file
  1385. * @param string Export type (scorm, zip, cd)
  1386. * @param string Course code
  1387. * @param integer Learnpath ID
  1388. * @param string Zip file name
  1389. * @return string Zip file path (or false on error)
  1390. */
  1391. public function export_lp($type, $course, $id, $zipname)
  1392. {
  1393. if (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
  1424. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1425. $res_parent = Database::query($sql_parent);
  1426. if (Database :: num_rows($res_parent) > 0) {
  1427. $row_parent = Database :: fetch_array($res_parent);
  1428. $parent = $row_parent['parent_item_id'];
  1429. $sql_bros = "SELECT * FROM $lp_item
  1430. WHERE
  1431. c_id = ".$course_id." AND
  1432. parent_item_id = $parent AND
  1433. id = $id AND
  1434. item_type='dokeos_chapter'
  1435. ORDER BY display_order";
  1436. $res_bros = Database::query($sql_bros);
  1437. $list = array ();
  1438. while ($row_bro = Database :: fetch_array($res_bros)) {
  1439. $list[] = $row_bro;
  1440. }
  1441. return $list;
  1442. }
  1443. return array ();
  1444. }
  1445. /**
  1446. * Gets all the items belonging to the same parent as the item given
  1447. * Can also be called as abstract method
  1448. * @param integer Item ID
  1449. * @return array A list of all the "brother items" (or an empty array on failure)
  1450. */
  1451. public function get_brother_items($id) {
  1452. $course_id = api_get_course_int_id();
  1453. if ($this->debug > 0) {
  1454. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1455. }
  1456. if (empty ($id) OR $id != strval(intval($id))) {
  1457. return array ();
  1458. }
  1459. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1460. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1461. $res_parent = Database::query($sql_parent);
  1462. if (Database :: num_rows($res_parent) > 0) {
  1463. $row_parent = Database :: fetch_array($res_parent);
  1464. $parent = $row_parent['parent_item_id'];
  1465. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1466. ORDER BY display_order";
  1467. $res_bros = Database::query($sql_bros);
  1468. $list = array ();
  1469. while ($row_bro = Database :: fetch_array($res_bros)) {
  1470. $list[] = $row_bro;
  1471. }
  1472. return $list;
  1473. }
  1474. return array ();
  1475. }
  1476. /**
  1477. * Get the specific prefix index terms of this learning path
  1478. * @param string $prefix
  1479. * @return array Array of terms
  1480. */
  1481. public function get_common_index_terms_by_prefix($prefix)
  1482. {
  1483. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1484. $terms = get_specific_field_values_list_by_prefix(
  1485. $prefix,
  1486. $this->cc,
  1487. TOOL_LEARNPATH,
  1488. $this->lp_id
  1489. );
  1490. $prefix_terms = array();
  1491. if (!empty($terms)) {
  1492. foreach ($terms as $term) {
  1493. $prefix_terms[] = $term['value'];
  1494. }
  1495. }
  1496. return $prefix_terms;
  1497. }
  1498. /**
  1499. * Gets the number of items currently completed
  1500. * @return integer The number of items currently completed
  1501. */
  1502. public function get_complete_items_count()
  1503. {
  1504. if ($this->debug > 0) {
  1505. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1506. }
  1507. $i = 0;
  1508. $completedStatusList = array(
  1509. 'completed',
  1510. 'passed',
  1511. 'succeeded',
  1512. 'browsed',
  1513. 'failed'
  1514. );
  1515. foreach ($this->items as $id => $dummy) {
  1516. // Trying failed and browsed considered "progressed" as well.
  1517. if ($this->items[$id]->status_is($completedStatusList) &&
  1518. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1519. $this->items[$id]->get_type() != 'dir'
  1520. ) {
  1521. $i++;
  1522. }
  1523. }
  1524. return $i;
  1525. }
  1526. /**
  1527. * Gets the current item ID
  1528. * @return integer The current learnpath item id
  1529. */
  1530. public function get_current_item_id()
  1531. {
  1532. $current = 0;
  1533. if ($this->debug > 0) {
  1534. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1535. }
  1536. if (!empty($this->current)) {
  1537. $current = $this->current;
  1538. }
  1539. if ($this->debug > 2) {
  1540. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1541. }
  1542. return $current;
  1543. }
  1544. /**
  1545. * Force to get the first learnpath item id
  1546. * @return integer The current learnpath item id
  1547. */
  1548. public function get_first_item_id()
  1549. {
  1550. $current = 0;
  1551. if (is_array($this->ordered_items)) {
  1552. $current = $this->ordered_items[0];
  1553. }
  1554. return $current;
  1555. }
  1556. /**
  1557. * Gets the total number of items available for viewing in this SCORM
  1558. * @return integer The total number of items
  1559. */
  1560. public function get_total_items_count()
  1561. {
  1562. if ($this->debug > 0) {
  1563. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1564. }
  1565. return count($this->items);
  1566. }
  1567. /**
  1568. * Gets the total number of items available for viewing in this SCORM but without chapters
  1569. * @return integer The total no-chapters number of items
  1570. */
  1571. public function get_total_items_count_without_chapters()
  1572. {
  1573. if ($this->debug > 0) {
  1574. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1575. }
  1576. $total = 0;
  1577. $typeListNotToCount = self::getChapterTypes();
  1578. foreach ($this->items as $temp2) {
  1579. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1580. $total++;
  1581. }
  1582. }
  1583. return $total;
  1584. }
  1585. /**
  1586. * Gets the first element URL.
  1587. * @return string URL to load into the viewer
  1588. */
  1589. public function first()
  1590. {
  1591. if ($this->debug > 0) {
  1592. error_log('New LP - In learnpath::first()', 0);
  1593. error_log('$this->last_item_seen '.$this->last_item_seen);
  1594. }
  1595. // Test if the last_item_seen exists and is not a dir.
  1596. if (count($this->ordered_items) == 0) {
  1597. $this->index = 0;
  1598. }
  1599. if ($this->debug > 0) {
  1600. if (isset($this->items[$this->last_item_seen])) {
  1601. $status = $this->items[$this->last_item_seen]->get_status();
  1602. }
  1603. error_log('status '.$status);
  1604. }
  1605. if (!empty($this->last_item_seen) &&
  1606. !empty($this->items[$this->last_item_seen]) &&
  1607. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1608. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1609. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1610. //&& !$this->items[$this->last_item_seen]->is_done()
  1611. ) {
  1612. if ($this->debug > 2) {
  1613. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1614. }
  1615. $index = -1;
  1616. foreach ($this->ordered_items as $myindex => $item_id) {
  1617. if ($item_id == $this->last_item_seen) {
  1618. $index = $myindex;
  1619. break;
  1620. }
  1621. }
  1622. if ($index == -1) {
  1623. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1624. if ($this->debug > 2) {
  1625. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1626. }
  1627. return false;
  1628. } else {
  1629. $this->last = $this->last_item_seen;
  1630. $this->current = $this->last_item_seen;
  1631. $this->index = $index;
  1632. }
  1633. } else {
  1634. if ($this->debug > 2) {
  1635. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1636. }
  1637. $index = 0;
  1638. // Loop through all ordered items and stop at the first item that is
  1639. // not a directory *and* that has not been completed yet.
  1640. while ( !empty($this->ordered_items[$index]) AND
  1641. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1642. (
  1643. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1644. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1645. $this->items[$this->ordered_items[$index]]->is_done() === true
  1646. ) AND $index < $this->max_ordered_items) {
  1647. $index++;
  1648. }
  1649. $this->last = $this->current;
  1650. // current is
  1651. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1652. $this->index = $index;
  1653. if ($this->debug > 2) {
  1654. error_log('$index ' . $index);
  1655. }
  1656. if ($this->debug > 2) {
  1657. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1658. }
  1659. }
  1660. if ($this->debug > 2) {
  1661. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1662. }
  1663. }
  1664. /**
  1665. * Gets the information about an item in a format usable as JavaScript to update
  1666. * the JS API by just printing this content into the <head> section of the message frame
  1667. * @param int $item_id
  1668. * @return string
  1669. */
  1670. public function get_js_info($item_id = '')
  1671. {
  1672. if ($this->debug > 0) {
  1673. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1674. }
  1675. $info = '';
  1676. $item_id = intval($item_id);
  1677. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1678. //if item is defined, return values from DB
  1679. $oItem = $this->items[$item_id];
  1680. $info .= '<script language="javascript">';
  1681. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1682. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1683. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1684. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1685. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1686. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1687. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1688. $info .= "top.set_flag_synchronized();";
  1689. $info .= '</script>';
  1690. if ($this->debug > 2) {
  1691. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1692. }
  1693. return $info;
  1694. } else {
  1695. // If item_id is empty, just update to default SCORM data.
  1696. $info .= '<script language="javascript">';
  1697. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1698. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1699. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1700. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1701. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1702. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1703. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1704. $info .= "top.set_flag_synchronized();";
  1705. $info .= '</script>';
  1706. if ($this->debug > 2) {
  1707. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1708. }
  1709. return $info;
  1710. }
  1711. }
  1712. /**
  1713. * Gets the js library from the database
  1714. * @return string The name of the javascript library to be used
  1715. */
  1716. public function get_js_lib()
  1717. {
  1718. $lib = '';
  1719. if (!empty ($this->js_lib)) {
  1720. $lib = $this->js_lib;
  1721. }
  1722. return $lib;
  1723. }
  1724. /**
  1725. * Gets the learnpath database ID
  1726. * @return integer Learnpath ID in the lp table
  1727. */
  1728. public function get_id()
  1729. {
  1730. if (!empty ($this->lp_id)) {
  1731. return $this->lp_id;
  1732. } else {
  1733. return 0;
  1734. }
  1735. }
  1736. /**
  1737. * Gets the last element URL.
  1738. * @return string URL to load into the viewer
  1739. */
  1740. public function get_last()
  1741. {
  1742. if ($this->debug > 0) {
  1743. error_log('New LP - In learnpath::get_last()', 0);
  1744. }
  1745. $this->index = count($this->ordered_items) - 1;
  1746. return $this->ordered_items[$this->index];
  1747. }
  1748. /**
  1749. * Gets the navigation bar for the learnpath display screen
  1750. * @return string The HTML string to use as a navigation bar
  1751. */
  1752. public function get_navigation_bar() {
  1753. if ($this->debug > 0) {
  1754. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1755. }
  1756. $navbar = null;
  1757. $lp_id = $this->lp_id;
  1758. $mycurrentitemid = $this->get_current_item_id();
  1759. if ($this->mode == 'fullscreen') {
  1760. $navbar = '
  1761. <div class="buttons">
  1762. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1763. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1764. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1765. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang('ScormExitFullScreen').'"></a>
  1766. </div>';
  1767. } else {
  1768. $navbar = '
  1769. <div class="buttons">
  1770. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1771. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1772. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>
  1773. </div>';
  1774. }
  1775. return $navbar;
  1776. }
  1777. /**
  1778. * Gets the next resource in queue (url).
  1779. * @return string URL to load into the viewer
  1780. */
  1781. public function get_next_index()
  1782. {
  1783. if ($this->debug > 0) {
  1784. error_log('New LP - In learnpath::get_next_index()', 0);
  1785. }
  1786. // TODO
  1787. $index = $this->index;
  1788. $index++;
  1789. if ($this->debug > 2) {
  1790. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1791. }
  1792. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1793. $index++;
  1794. if ($index == $this->max_ordered_items){
  1795. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1796. return $this->index;
  1797. } else {
  1798. return $index;
  1799. }
  1800. }
  1801. }
  1802. if (empty ($this->ordered_items[$index])) {
  1803. return $this->index;
  1804. }
  1805. if ($this->debug > 2) {
  1806. error_log('New LP - index is now ' . $index, 0);
  1807. }
  1808. return $index;
  1809. }
  1810. /**
  1811. * Gets item_id for the next element
  1812. * @return integer Next item (DB) ID
  1813. */
  1814. public function get_next_item_id()
  1815. {
  1816. if ($this->debug > 0) {
  1817. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1818. }
  1819. $new_index = $this->get_next_index();
  1820. if (!empty ($new_index)) {
  1821. if (isset ($this->ordered_items[$new_index])) {
  1822. if ($this->debug > 2) {
  1823. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1824. }
  1825. return $this->ordered_items[$new_index];
  1826. }
  1827. }
  1828. if ($this->debug > 2) {
  1829. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1830. }
  1831. return 0;
  1832. }
  1833. /**
  1834. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1835. *
  1836. * Generally, the package provided is in the form of a zip file, so the function
  1837. * has been written to test a zip file. If not a zip, the function will return the
  1838. * default return value: ''
  1839. * @param string the path to the file
  1840. * @param string the original name of the file
  1841. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1842. */
  1843. public static function get_package_type($file_path, $file_name)
  1844. {
  1845. // Get name of the zip file without the extension.
  1846. $file_info = pathinfo($file_name);
  1847. $filename = $file_info['basename']; // Name including extension.
  1848. $extension = $file_info['extension']; // Extension only.
  1849. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1850. 'dll',
  1851. 'exe'
  1852. ))) {
  1853. return 'oogie';
  1854. }
  1855. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1856. 'dll',
  1857. 'exe'
  1858. ))) {
  1859. return 'woogie';
  1860. }
  1861. // Filename without its extension.
  1862. $file_base_name = str_replace('.' . $extension, '', $filename);
  1863. $zipFile = new PclZip($file_path);
  1864. // Check the zip content (real size and file extension).
  1865. $zipContentArray = $zipFile->listContent();
  1866. $package_type = '';
  1867. $at_root = false;
  1868. $manifest = '';
  1869. $aicc_match_crs = 0;
  1870. $aicc_match_au = 0;
  1871. $aicc_match_des = 0;
  1872. $aicc_match_cst = 0;
  1873. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1874. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1875. foreach ($zipContentArray as $thisContent) {
  1876. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1877. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1878. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1879. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1880. $package_type = 'scorm';
  1881. break; // Exit the foreach loop.
  1882. } elseif (
  1883. preg_match('/aicc\//i', $thisContent['filename']) ||
  1884. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1885. ) {
  1886. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1887. switch ($ext) {
  1888. case 'crs':
  1889. $aicc_match_crs = 1;
  1890. break;
  1891. case 'au':
  1892. $aicc_match_au = 1;
  1893. break;
  1894. case 'des':
  1895. $aicc_match_des = 1;
  1896. break;
  1897. case 'cst':
  1898. $aicc_match_cst = 1;
  1899. break;
  1900. default:
  1901. break;
  1902. }
  1903. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1904. } else {
  1905. $package_type = '';
  1906. }
  1907. }
  1908. }
  1909. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1910. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1911. $package_type = 'aicc';
  1912. }
  1913. return $package_type;
  1914. }
  1915. /**
  1916. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1917. * @return string URL to load into the viewer
  1918. */
  1919. public function get_previous_index()
  1920. {
  1921. if ($this->debug > 0) {
  1922. error_log('New LP - In learnpath::get_previous_index()', 0);
  1923. }
  1924. $index = $this->index;
  1925. if (isset ($this->ordered_items[$index -1])) {
  1926. $index--;
  1927. while (isset($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1928. $index--;
  1929. if ($index < 0) {
  1930. return $this->index;
  1931. }
  1932. }
  1933. } else {
  1934. if ($this->debug > 2) {
  1935. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1936. }
  1937. // There is no previous item.
  1938. }
  1939. return $index;
  1940. }
  1941. /**
  1942. * Gets item_id for the next element
  1943. * @return integer Previous item (DB) ID
  1944. */
  1945. public function get_previous_item_id()
  1946. {
  1947. if ($this->debug > 0) {
  1948. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1949. }
  1950. $new_index = $this->get_previous_index();
  1951. return $this->ordered_items[$new_index];
  1952. }
  1953. /**
  1954. * Gets the progress value from the progress_db attribute
  1955. * @return integer Current progress value
  1956. */
  1957. public function get_progress()
  1958. {
  1959. if ($this->debug > 0) {
  1960. error_log('New LP - In learnpath::get_progress()', 0);
  1961. }
  1962. if (!empty ($this->progress_db)) {
  1963. return $this->progress_db;
  1964. }
  1965. return 0;
  1966. }
  1967. /**
  1968. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1969. * @param integer $lp_id Learnpath ID
  1970. * @param integer $user_id User ID
  1971. * @param string $mode Mode of display ('%','abs' or 'both')
  1972. * @param string $course_code Course code (optional, defaults to '')
  1973. * @param boolean $sincere Whether to return null if no record
  1974. * was found (true), or 0 (false) (optional, defaults to false)
  1975. * @param int $session_id
  1976. * @return integer Current progress value as found in the database
  1977. * @deprecated
  1978. */
  1979. public static function get_db_progress(
  1980. $lp_id,
  1981. $user_id,
  1982. $mode = '%',
  1983. $course_code = '',
  1984. $sincere = false,
  1985. $session_id = 0
  1986. ) {
  1987. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1988. $lp_id = intval($lp_id);
  1989. $session_id = intval($session_id);
  1990. $user_id = intval($user_id);
  1991. $course_info = api_get_course_info($course_code);
  1992. $session_condition = api_get_session_condition($session_id);
  1993. $course_id = $course_info['real_id'];
  1994. $sql = "SELECT * FROM $table
  1995. WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1996. $res = Database::query($sql);
  1997. $view_id = 0;
  1998. if (Database :: num_rows($res) > 0) {
  1999. $row = Database :: fetch_array($res);
  2000. $progress = $row['progress'];
  2001. $view_id = $row['id'];
  2002. } else {
  2003. if ($sincere) {
  2004. return null;
  2005. }
  2006. }
  2007. if (empty($progress)) {
  2008. $progress = '0';
  2009. }
  2010. if ($mode == '%') {
  2011. return $progress . '%';
  2012. } else {
  2013. // Get the number of items completed and the number of items total.
  2014. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2015. $sql = "SELECT count(*) FROM $tbl
  2016. WHERE
  2017. c_id = $course_id AND
  2018. c_id = ".$course_id." AND
  2019. lp_id = " . $lp_id . " AND
  2020. item_type NOT IN('dokeos_chapter','chapter','dir')";
  2021. $res = Database::query($sql);
  2022. $row = Database :: fetch_array($res);
  2023. $total = $row[0];
  2024. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2025. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2026. // 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
  2031. item.id = item_view.lp_item_id AND
  2032. item_type NOT IN('dokeos_chapter','chapter','dir')
  2033. WHERE
  2034. item_view.c_id = $course_id AND
  2035. item.c_id = $course_id AND
  2036. lp_view_id = " . $view_id . " AND
  2037. status IN ('passed','completed','succeeded','browsed','failed')";
  2038. $res = Database::query($sql);
  2039. $row = Database :: fetch_array($res);
  2040. $completed = $row[0];
  2041. if ($mode == 'abs') {
  2042. return $completed . '/' . $total;
  2043. } elseif ($mode == 'both') {
  2044. if ($progress < ($completed / ($total ? $total : 1))) {
  2045. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2046. }
  2047. return $progress . '% (' . $completed . '/' . $total . ')';
  2048. }
  2049. }
  2050. return $progress;
  2051. }
  2052. /**
  2053. * Returns the HTML necessary to print a mediaplayer block inside a page
  2054. * @return string The mediaplayer HTML
  2055. */
  2056. public function get_mediaplayer($autostart = 'true')
  2057. {
  2058. $course_id = api_get_course_int_id();
  2059. global $_course;
  2060. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2061. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2062. // Getting all the information about the item.
  2063. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2064. INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id
  2065. WHERE
  2066. lp.id = '".$_SESSION['oLP']->current."' AND
  2067. lp.c_id = $course_id AND
  2068. lp_view.c_id = $course_id";
  2069. $result = Database::query($sql);
  2070. $row = Database::fetch_assoc($result);
  2071. $output = '';
  2072. if (!empty ($row['audio'])) {
  2073. $list = $_SESSION['oLP']->get_toc();
  2074. $type_quiz = false;
  2075. foreach($list as $toc) {
  2076. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2077. $type_quiz = true;
  2078. }
  2079. }
  2080. if ($type_quiz) {
  2081. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2082. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2083. } else {
  2084. $autostart_audio = $autostart;
  2085. }
  2086. } else {
  2087. $autostart_audio = 'true';
  2088. }
  2089. $courseInfo = api_get_course_info();
  2090. $audio = $row['audio'];
  2091. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2092. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2093. if (!file_exists($file)) {
  2094. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2095. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2096. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2097. }
  2098. $player = Display::getMediaPlayer(
  2099. $file,
  2100. array(
  2101. 'id' => 'lp_audio_media_player',
  2102. 'url' => $url,
  2103. 'autoplay' => $autostart_audio,
  2104. 'width' => '100%'
  2105. )
  2106. );
  2107. // The mp3 player.
  2108. $output = '<div id="container">';
  2109. $output .= $player;
  2110. $output .= '</div>';
  2111. }
  2112. return $output;
  2113. }
  2114. /**
  2115. * Checks if the learning path is visible for student after the progress
  2116. * of its prerequisite is completed, considering the time availability and
  2117. * the LP visibility.
  2118. * @param int Learnpath id
  2119. * @param int Student id
  2120. * @param string Course code (optional)
  2121. * @param int $sessionId
  2122. * @return bool
  2123. */
  2124. public static function is_lp_visible_for_student(
  2125. $lp_id,
  2126. $student_id,
  2127. $courseCode = null,
  2128. $sessionId = null
  2129. ) {
  2130. $lp_id = (int)$lp_id;
  2131. $course = api_get_course_info($courseCode);
  2132. $sessionId = intval($sessionId);
  2133. if (empty($sessionId)) {
  2134. $sessionId = api_get_session_id();
  2135. }
  2136. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2137. // Get current prerequisite
  2138. $sql = "SELECT id, prerequisite, publicated_on, expired_on
  2139. FROM $tbl_learnpath
  2140. WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  2141. $itemInfo = api_get_item_property_info(
  2142. $course['real_id'],
  2143. TOOL_LEARNPATH,
  2144. $lp_id,
  2145. $sessionId
  2146. );
  2147. // If the item was deleted.
  2148. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2149. return false;
  2150. }
  2151. $rs = Database::query($sql);
  2152. $now = time();
  2153. if (Database::num_rows($rs)>0) {
  2154. $row = Database::fetch_array($rs, 'ASSOC');
  2155. $prerequisite = $row['prerequisite'];
  2156. $is_visible = true;
  2157. if (!empty($prerequisite)) {
  2158. $progress = self::getProgress(
  2159. $prerequisite,
  2160. $student_id,
  2161. $course['real_id'],
  2162. $sessionId
  2163. );
  2164. $progress = intval($progress);
  2165. if ($progress < 100) {
  2166. $is_visible = false;
  2167. }
  2168. }
  2169. // Also check the time availability of the LP
  2170. if ($is_visible) {
  2171. // Adding visibility restrictions
  2172. if (!empty($row['publicated_on']) &&
  2173. $row['publicated_on'] != '0000-00-00 00:00:00'
  2174. ) {
  2175. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2176. //api_not_allowed();
  2177. $is_visible = false;
  2178. }
  2179. }
  2180. // Blocking empty start times see BT#2800
  2181. global $_custom;
  2182. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2183. $_custom['lps_hidden_when_no_start_date']
  2184. ) {
  2185. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2186. //api_not_allowed();
  2187. $is_visible = false;
  2188. }
  2189. }
  2190. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2191. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2192. //api_not_allowed();
  2193. $is_visible = false;
  2194. }
  2195. }
  2196. }
  2197. return $is_visible;
  2198. }
  2199. return false;
  2200. }
  2201. /**
  2202. * @param int $lpId
  2203. * @param int $userId
  2204. * @param int $courseId
  2205. * @param int $sessionId
  2206. * @return int
  2207. */
  2208. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2209. {
  2210. $lpId = intval($lpId);
  2211. $userId = intval($userId);
  2212. $courseId = intval($courseId);
  2213. $sessionId = intval($sessionId);
  2214. $progress = 0;
  2215. $sessionCondition = api_get_session_condition($sessionId);
  2216. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2217. $sql = "SELECT * FROM $table
  2218. WHERE
  2219. c_id = ".$courseId." AND
  2220. lp_id = $lpId AND
  2221. user_id = $userId $sessionCondition";
  2222. $res = Database::query($sql);
  2223. if (Database :: num_rows($res) > 0) {
  2224. $row = Database:: fetch_array($res);
  2225. $progress = $row['progress'];
  2226. }
  2227. return $progress;
  2228. }
  2229. /**
  2230. * Displays a progress bar
  2231. * completed so far.
  2232. * @param integer $percentage Progress value to display
  2233. * @param string $text_add Text to display near the progress value
  2234. * @return string HTML string containing the progress bar
  2235. */
  2236. public static function get_progress_bar($percentage = -1, $text_add = '')
  2237. {
  2238. $text = $percentage . $text_add;
  2239. $output = '<div class="progress">
  2240. <div id="progress_bar_value" class="bar" style="width: '.$text.';"><span id="progress_text">'. $text .'</span></div>
  2241. </div>';
  2242. return $output;
  2243. }
  2244. /**
  2245. * @param string $mode can be '%' or 'abs'
  2246. * otherwise this value will be used $this->progress_bar_mode
  2247. * @return string
  2248. */
  2249. public function getProgressBar($mode = null)
  2250. {
  2251. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2252. return self::get_progress_bar($percentage, $text_add);
  2253. }
  2254. /**
  2255. * Gets the progress bar info to display inside the progress bar.
  2256. * Also used by scorm_api.php
  2257. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2258. * we display a number of completed elements per total elements
  2259. * @param integer $add Additional steps to fake as completed
  2260. * @return list array Percentage or number and symbol (% or /xx)
  2261. */
  2262. public function get_progress_bar_text($mode = '', $add = 0)
  2263. {
  2264. if ($this->debug > 0) {
  2265. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2266. }
  2267. if (empty($mode)) {
  2268. $mode = $this->progress_bar_mode;
  2269. }
  2270. $total_items = $this->get_total_items_count_without_chapters();
  2271. if ($this->debug > 2) {
  2272. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2273. }
  2274. $completeItems = $this->get_complete_items_count();
  2275. if ($this->debug > 2) {
  2276. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2277. }
  2278. if ($add != 0) {
  2279. $completeItems += $add;
  2280. if ($this->debug > 2) {
  2281. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2282. }
  2283. }
  2284. $text = '';
  2285. if ($completeItems > $total_items) {
  2286. $completeItems = $total_items;
  2287. }
  2288. $percentage = 0;
  2289. if ($mode == '%') {
  2290. if ($total_items > 0) {
  2291. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2292. } else {
  2293. $percentage = 0;
  2294. }
  2295. $percentage = number_format($percentage, 0);
  2296. $text = '%';
  2297. } elseif ($mode == 'abs') {
  2298. $percentage = $completeItems;
  2299. $text = '/' . $total_items;
  2300. }
  2301. return array(
  2302. $percentage,
  2303. $text
  2304. );
  2305. }
  2306. /**
  2307. * Gets the progress bar mode
  2308. * @return string The progress bar mode attribute
  2309. */
  2310. public function get_progress_bar_mode()
  2311. {
  2312. if ($this->debug > 0) {
  2313. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2314. }
  2315. if (!empty ($this->progress_bar_mode)) {
  2316. return $this->progress_bar_mode;
  2317. } else {
  2318. return '%';
  2319. }
  2320. }
  2321. /**
  2322. * Gets the learnpath proximity (remote or local)
  2323. * @return string Learnpath proximity
  2324. */
  2325. public function get_proximity()
  2326. {
  2327. if ($this->debug > 0) {
  2328. error_log('New LP - In learnpath::get_proximity()', 0);
  2329. }
  2330. if (!empty ($this->proximity)) {
  2331. return $this->proximity;
  2332. } else {
  2333. return '';
  2334. }
  2335. }
  2336. /**
  2337. * Gets the learnpath theme (remote or local)
  2338. * @return string Learnpath theme
  2339. */
  2340. public function get_theme()
  2341. {
  2342. if ($this->debug > 0) {
  2343. error_log('New LP - In learnpath::get_theme()', 0);
  2344. }
  2345. if (!empty ($this->theme)) {
  2346. return $this->theme;
  2347. } else {
  2348. return '';
  2349. }
  2350. }
  2351. /**
  2352. * Gets the learnpath session id
  2353. * @return string Learnpath theme
  2354. */
  2355. public function get_lp_session_id()
  2356. {
  2357. if ($this->debug > 0) {
  2358. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2359. }
  2360. if (!empty ($this->lp_session_id)) {
  2361. return $this->lp_session_id;
  2362. } else {
  2363. return 0;
  2364. }
  2365. }
  2366. /**
  2367. * Gets the learnpath image
  2368. * @return string Web URL of the LP image
  2369. */
  2370. public function get_preview_image()
  2371. {
  2372. if ($this->debug > 0) {
  2373. error_log('New LP - In learnpath::get_preview_image()', 0);
  2374. }
  2375. if (!empty($this->preview_image)) {
  2376. return $this->preview_image;
  2377. } else {
  2378. return '';
  2379. }
  2380. }
  2381. /**
  2382. * @param string $size
  2383. * @param string $path_type
  2384. * @return bool|string
  2385. */
  2386. public function get_preview_image_path($size = null, $path_type = 'web')
  2387. {
  2388. $preview_image = $this->get_preview_image();
  2389. if (isset($preview_image) && !empty($preview_image)) {
  2390. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2391. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2392. if (isset($size)) {
  2393. $info = pathinfo($preview_image);
  2394. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2395. if (file_exists($image_sys_path.$image_custom_size)) {
  2396. if ($path_type == 'web') {
  2397. return $image_path.$image_custom_size;
  2398. } else {
  2399. return $image_sys_path.$image_custom_size;
  2400. }
  2401. }
  2402. } else {
  2403. if ($path_type == 'web') {
  2404. return $image_path.$preview_image;
  2405. } else {
  2406. return $image_sys_path.$preview_image;
  2407. }
  2408. }
  2409. }
  2410. return false;
  2411. }
  2412. /**
  2413. * Gets the learnpath author
  2414. * @return string LP's author
  2415. */
  2416. public function get_author()
  2417. {
  2418. if ($this->debug > 0) {
  2419. error_log('New LP - In learnpath::get_author()', 0);
  2420. }
  2421. if (!empty ($this->author)) {
  2422. return $this->author;
  2423. } else {
  2424. return '';
  2425. }
  2426. }
  2427. /**
  2428. * Gets the learnpath author
  2429. * @return string LP's author
  2430. */
  2431. public function get_hide_toc_frame()
  2432. {
  2433. if ($this->debug > 0) {
  2434. error_log('New LP - In learnpath::get_author()', 0);
  2435. }
  2436. if (!empty ($this->hide_toc_frame)) {
  2437. return $this->hide_toc_frame;
  2438. } else {
  2439. return '';
  2440. }
  2441. }
  2442. /**
  2443. * Generate a new prerequisites string for a given item. If this item was a sco and
  2444. * its prerequisites were strings (instead of IDs), then transform those strings into
  2445. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2446. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2447. * same rule as the scorm_export() method
  2448. * @param integer Item ID
  2449. * @return string Prerequisites string ready for the export as SCORM
  2450. */
  2451. public function get_scorm_prereq_string($item_id)
  2452. {
  2453. if ($this->debug > 0) {
  2454. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2455. }
  2456. if (!is_object($this->items[$item_id])) {
  2457. return false;
  2458. }
  2459. $oItem = $this->items[$item_id];
  2460. $prereq = $oItem->get_prereq_string();
  2461. if (empty($prereq)) {
  2462. return '';
  2463. }
  2464. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2465. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2466. // then simply return it (with the ITEM_ prefix).
  2467. //return 'ITEM_' . $prereq;
  2468. return $this->items[$prereq]->ref;
  2469. } else {
  2470. if (isset($this->refs_list[$prereq])) {
  2471. // It's a simple string item from which the ID can be found in the refs list,
  2472. // so we can transform it directly to an ID for export.
  2473. return $this->items[$this->refs_list[$prereq]]->ref;
  2474. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2475. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2476. } else {
  2477. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2478. // and replace them, one by one, by the internal IDs (chamilo db)
  2479. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2480. // by a space as well.
  2481. $find = array (
  2482. '&',
  2483. '|',
  2484. '~',
  2485. '=',
  2486. '<>',
  2487. '{',
  2488. '}',
  2489. '*',
  2490. '(',
  2491. ')'
  2492. );
  2493. $replace = array (
  2494. ' ',
  2495. ' ',
  2496. ' ',
  2497. ' ',
  2498. ' ',
  2499. ' ',
  2500. ' ',
  2501. ' ',
  2502. ' ',
  2503. ' '
  2504. );
  2505. $prereq_mod = str_replace($find, $replace, $prereq);
  2506. $ids = explode(' ', $prereq_mod);
  2507. foreach ($ids as $id) {
  2508. $id = trim($id);
  2509. if (isset ($this->refs_list[$id])) {
  2510. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2511. }
  2512. }
  2513. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2514. return $prereq;
  2515. }
  2516. }
  2517. }
  2518. /**
  2519. * Returns the XML DOM document's node
  2520. * @param resource Reference to a list of objects to search for the given ITEM_*
  2521. * @param string The identifier to look for
  2522. * @return mixed The reference to the element found with that identifier. False if not found
  2523. */
  2524. public function get_scorm_xml_node(& $children, $id)
  2525. {
  2526. for ($i = 0; $i < $children->length; $i++) {
  2527. $item_temp = $children->item($i);
  2528. if ($item_temp->nodeName == 'item') {
  2529. if ($item_temp->getAttribute('identifier') == $id) {
  2530. return $item_temp;
  2531. }
  2532. }
  2533. $subchildren = $item_temp->childNodes;
  2534. if ($subchildren->length > 0) {
  2535. $val = $this->get_scorm_xml_node($subchildren, $id);
  2536. if (is_object($val)) {
  2537. return $val;
  2538. }
  2539. }
  2540. }
  2541. return false;
  2542. }
  2543. /**
  2544. * Returns a usable array of stats related to the current learnpath and user
  2545. * @return array Well-formatted array containing status for the current learnpath
  2546. */
  2547. public function get_stats()
  2548. {
  2549. if ($this->debug > 0) {
  2550. error_log('New LP - In learnpath::get_stats()', 0);
  2551. }
  2552. }
  2553. /**
  2554. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2555. * the given course (for all learnpaths and all users)
  2556. * @param string Course code
  2557. * @return array Well-formatted array containing status for the course's learnpaths
  2558. */
  2559. public function get_stats_course($course)
  2560. {
  2561. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2562. // TODO
  2563. }
  2564. /**
  2565. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2566. * the given course and learnpath (for all users)
  2567. * @param string Course code
  2568. * @param integer Learnpath ID
  2569. * @return array Well-formatted array containing status for the specified learnpath
  2570. */
  2571. public function get_stats_lp($course, $lp)
  2572. {
  2573. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2574. // TODO
  2575. }
  2576. /**
  2577. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2578. * the given course, learnpath and user.
  2579. * @param string Course code
  2580. * @param integer Learnpath ID
  2581. * @param integer User ID
  2582. * @return array Well-formatted array containing status for the specified learnpath and user
  2583. */
  2584. public function get_stats_lp_user($course, $lp, $user)
  2585. {
  2586. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2587. // TODO
  2588. }
  2589. /**
  2590. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2591. * the given course and learnpath (for all users)
  2592. * @param string Course code
  2593. * @param integer User ID
  2594. * @return array Well-formatted array containing status for the user's learnpaths
  2595. */
  2596. public function get_stats_user($course, $user) {
  2597. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2598. // TODO
  2599. }
  2600. /**
  2601. * Gets the status list for all LP's items
  2602. * @return array Array of [index] => [item ID => current status]
  2603. */
  2604. public function get_items_status_list()
  2605. {
  2606. if ($this->debug > 0) {
  2607. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2608. }
  2609. $list = array ();
  2610. foreach ($this->ordered_items as $item_id) {
  2611. $list[] = array (
  2612. $item_id => $this->items[$item_id]->get_status()
  2613. );
  2614. }
  2615. return $list;
  2616. }
  2617. /**
  2618. * Return the number of interactions for the given learnpath Item View ID.
  2619. * This method can be used as static.
  2620. * @param integer Item View ID
  2621. * @param integer course id
  2622. * @return integer Number of interactions
  2623. */
  2624. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2625. {
  2626. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2627. $lp_iv_id = intval($lp_iv_id);
  2628. $course_id = intval($course_id);
  2629. $sql = "SELECT count(*) FROM $table
  2630. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2631. $res = Database::query($sql);
  2632. $num = 0;
  2633. if (Database::num_rows($res)) {
  2634. $row = Database::fetch_array($res);
  2635. $num = $row[0];
  2636. }
  2637. return $num;
  2638. }
  2639. /**
  2640. * Return the interactions as an array for the given lp_iv_id.
  2641. * This method can be used as static.
  2642. * @param integer Learnpath Item View ID
  2643. * @return array
  2644. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2645. */
  2646. public static function get_iv_interactions_array($lp_iv_id = 0)
  2647. {
  2648. $course_id = api_get_course_int_id();
  2649. $list = array ();
  2650. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2651. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2652. $res = Database::query($sql);
  2653. $num = Database :: num_rows($res);
  2654. if ($num > 0) {
  2655. $list[] = array (
  2656. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2657. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2658. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2659. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2660. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2661. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2662. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2663. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2664. );
  2665. while ($row = Database :: fetch_array($res)) {
  2666. $list[] = array (
  2667. 'order_id' => ($row['order_id'] + 1),
  2668. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2669. 'type' => $row['interaction_type'],
  2670. 'time' => $row['completion_time'],
  2671. //'correct_responses' => $row['correct_responses'],
  2672. 'correct_responses' => '', // Hide correct responses from students.
  2673. 'student_response' => $row['student_response'],
  2674. 'result' => $row['result'],
  2675. 'latency' => $row['latency']
  2676. );
  2677. }
  2678. }
  2679. return $list;
  2680. }
  2681. /**
  2682. * Return the number of objectives for the given learnpath Item View ID.
  2683. * This method can be used as static.
  2684. * @param integer Item View ID
  2685. * @return integer Number of objectives
  2686. */
  2687. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2688. {
  2689. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2690. $course_id = intval($course_id);
  2691. $lp_iv_id = intval($lp_iv_id);
  2692. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2693. //@todo seems that this always returns 0
  2694. $res = Database::query($sql);
  2695. $num = 0;
  2696. if (Database::num_rows($res)) {
  2697. $row = Database :: fetch_array($res);
  2698. $num = $row[0];
  2699. }
  2700. return $num;
  2701. }
  2702. /**
  2703. * Return the objectives as an array for the given lp_iv_id.
  2704. * This method can be used as static.
  2705. * @param integer Learnpath Item View ID
  2706. * @return array
  2707. * @todo Translate labels
  2708. */
  2709. public static function get_iv_objectives_array($lp_iv_id = 0)
  2710. {
  2711. $course_id = api_get_course_int_id();
  2712. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2713. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2714. $res = Database::query($sql);
  2715. $num = Database :: num_rows($res);
  2716. $list = array();
  2717. if ($num > 0) {
  2718. $list[] = array(
  2719. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2720. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2721. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2722. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2723. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2724. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2725. );
  2726. while ($row = Database :: fetch_array($res)) {
  2727. $list[] = array (
  2728. 'order_id' => ($row['order_id'] + 1),
  2729. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2730. 'score_raw' => $row['score_raw'],
  2731. 'score_max' => $row['score_max'],
  2732. 'score_min' => $row['score_min'],
  2733. 'status' => $row['status']
  2734. );
  2735. }
  2736. }
  2737. return $list;
  2738. }
  2739. /**
  2740. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2741. * used by get_html_toc() to be ready to display
  2742. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2743. */
  2744. public function get_toc()
  2745. {
  2746. if ($this->debug > 0) {
  2747. error_log('learnpath::get_toc()', 0);
  2748. }
  2749. $toc = array();
  2750. foreach ($this->ordered_items as $item_id) {
  2751. if ($this->debug > 2) {
  2752. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2753. }
  2754. // TODO: Change this link generation and use new function instead.
  2755. $toc[] = array (
  2756. 'id' => $item_id,
  2757. 'title' => $this->items[$item_id]->get_title(),
  2758. 'status' => $this->items[$item_id]->get_status(),
  2759. 'level' => $this->items[$item_id]->get_level(),
  2760. 'type' => $this->items[$item_id]->get_type(),
  2761. 'description' => $this->items[$item_id]->get_description(),
  2762. 'path' => $this->items[$item_id]->get_path(),
  2763. );
  2764. }
  2765. if ($this->debug > 2) {
  2766. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2767. }
  2768. return $toc;
  2769. }
  2770. /**
  2771. * Generate and return the table of contents for this learnpath. The JS
  2772. * table returned is used inside of scorm_api.php
  2773. * @return string A JS array vairiable construction
  2774. */
  2775. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2776. {
  2777. if ($this->debug > 0) {
  2778. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2779. }
  2780. $toc = $varname.' = new Array();';
  2781. //echo "<pre>".print_r($this->items,true)."</pre>";
  2782. foreach ($this->ordered_items as $item_id) {
  2783. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2784. }
  2785. if ($this->debug > 2) {
  2786. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2787. }
  2788. return $toc;
  2789. }
  2790. /**
  2791. * Gets the learning path type
  2792. * @param boolean Return the name? If false, return the ID. Default is false.
  2793. * @return mixed Type ID or name, depending on the parameter
  2794. */
  2795. public function get_type($get_name = false)
  2796. {
  2797. $res = false;
  2798. if ($this->debug > 0) {
  2799. error_log('New LP - In learnpath::get_type()', 0);
  2800. }
  2801. if (!empty ($this->type)) {
  2802. if ($get_name) {
  2803. // Get it from the lp_type table in main db.
  2804. } else {
  2805. $res = $this->type;
  2806. }
  2807. }
  2808. if ($this->debug > 2) {
  2809. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2810. }
  2811. return $res;
  2812. }
  2813. /**
  2814. * Gets the learning path type as static method
  2815. * @param boolean Return the name? If false, return the ID. Default is false.
  2816. * @return mixed Type ID or name, depending on the parameter
  2817. */
  2818. public static function get_type_static($lp_id = 0)
  2819. {
  2820. $course_id = api_get_course_int_id();
  2821. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2822. $lp_id = intval($lp_id);
  2823. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2824. $res = Database::query($sql);
  2825. if ($res === false) {
  2826. return null;
  2827. }
  2828. if (Database :: num_rows($res) <= 0) {
  2829. return null;
  2830. }
  2831. $row = Database :: fetch_array($res);
  2832. return $row['lp_type'];
  2833. }
  2834. /**
  2835. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2836. * This method can be used as abstract and is recursive
  2837. * @param integer Learnpath ID
  2838. * @param integer Parent ID of the items to look for
  2839. * @return mixed Ordered list of item IDs or false on error
  2840. */
  2841. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2842. {
  2843. if (empty($course_id)) {
  2844. $course_id = api_get_course_int_id();
  2845. } else {
  2846. $course_id = intval($course_id);
  2847. }
  2848. $list = array();
  2849. if (empty($lp)) {
  2850. return false;
  2851. }
  2852. $lp = intval($lp);
  2853. $parent = intval($parent);
  2854. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2855. $sql = "SELECT id FROM $tbl_lp_item
  2856. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2857. ORDER BY display_order";
  2858. $res = Database::query($sql);
  2859. while ($row = Database :: fetch_array($res)) {
  2860. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2861. $list[] = $row['id'];
  2862. foreach ($sublist as $item) {
  2863. $list[] = $item;
  2864. }
  2865. }
  2866. return $list;
  2867. }
  2868. /**
  2869. * @return array
  2870. */
  2871. public static function getChapterTypes()
  2872. {
  2873. return array(
  2874. 'dokeos_chapter',
  2875. 'dokeos_module',
  2876. 'chapter',
  2877. 'dir'
  2878. );
  2879. }
  2880. /**
  2881. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2882. * @return string HTML TOC ready to display
  2883. */
  2884. public function get_html_toc($toc_list = null)
  2885. {
  2886. global $_configuration;
  2887. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2888. if ($this->debug > 0) {
  2889. error_log('In learnpath::get_html_toc()', 0);
  2890. }
  2891. if (empty($toc_list)) {
  2892. $toc_list = $this->get_toc();
  2893. }
  2894. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()) . '</div>';
  2895. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2896. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2897. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2898. if ($this->get_lp_session_id() == api_get_session_id()) {
  2899. $html .= '<div id="actions_lp" class="actions_lp">';
  2900. $html .= '<div class="btn-group">';
  2901. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2902. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;action=add_item&amp;type=step&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2903. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2904. $html .= '</div>';
  2905. $html .= '</div>';
  2906. }
  2907. }
  2908. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2909. require_once 'resourcelinker.inc.php';
  2910. // Temporary variables.
  2911. $mycurrentitemid = $this->get_current_item_id();
  2912. $color_counter = 0;
  2913. $i = 0;
  2914. foreach ($toc_list as $item) {
  2915. // TODO: Complete this
  2916. $icon_name = array (
  2917. 'not attempted' => '../img/notattempted.gif',
  2918. 'incomplete' => '../img/incomplete.png',
  2919. 'failed' => '../img/delete.png',
  2920. 'completed' => '../img/completed.png',
  2921. 'passed' => '../img/passed.png',
  2922. 'succeeded' => '../img/succeeded.png',
  2923. 'browsed' => '../img/completed.png',
  2924. );
  2925. // Style Status
  2926. $class_name = array (
  2927. 'not attempted' => 'scorm_not_attempted',
  2928. 'incomplete' => 'scorm_not_attempted',
  2929. 'failed' => 'scorm_failed',
  2930. 'completed' => 'scorm_completed',
  2931. 'passed' => 'scorm_completed',
  2932. 'succeeded' => 'scorm_completed',
  2933. 'browsed' => 'scorm_completed',
  2934. );
  2935. $scorm_color_background = 'scorm_item_2';
  2936. $style_item = '';
  2937. if ($color_counter % 2 == 0) {
  2938. $scorm_color_background = 'scorm_item_1';
  2939. }
  2940. $dirTypes = self::getChapterTypes();
  2941. if (in_array($item['type'], $dirTypes)) {
  2942. $scorm_color_background =' scorm_item_section ';
  2943. $style_item = '';
  2944. }
  2945. if ($item['id'] == $this->current) {
  2946. $scorm_color_background = 'scorm_item_normal scorm_highlight '.$scorm_color_background.' ';
  2947. } elseif (!in_array($item['type'], $dirTypes)) {
  2948. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2949. }
  2950. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2951. // Learning path title
  2952. $title = $item['title'];
  2953. if (empty ($title)) {
  2954. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2955. }
  2956. $title = Security::remove_XSS($title);
  2957. // Learning path personalization
  2958. // build the LP tree
  2959. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2960. $description = $item['description'];
  2961. if (empty($description)) {
  2962. $description = $title;
  2963. }
  2964. if (in_array($item['type'], $dirTypes)) {
  2965. // Chapters
  2966. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2967. } else {
  2968. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2969. $html .= '<a name="atoc_'.$item['id'].'" />';
  2970. }
  2971. if (in_array($item['type'], $dirTypes)) {
  2972. // Chapter
  2973. // if you want to put an image before, you should use css
  2974. $html .= stripslashes($title);
  2975. } else {
  2976. $this->get_link('http', $item['id'], $toc_list);
  2977. $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2978. }
  2979. $html .= "</div>";
  2980. if ($scorm_color_background != '') {
  2981. $html .= '</div>';
  2982. }
  2983. $color_counter++;
  2984. }
  2985. $html .= "</div>";
  2986. return $html;
  2987. }
  2988. /**
  2989. * Gets the learnpath maker name - generally the editor's name
  2990. * @return string Learnpath maker name
  2991. */
  2992. public function get_maker()
  2993. {
  2994. if ($this->debug > 0) {
  2995. error_log('New LP - In learnpath::get_maker()', 0);
  2996. }
  2997. if (!empty ($this->maker)) {
  2998. return $this->maker;
  2999. } else {
  3000. return '';
  3001. }
  3002. }
  3003. /**
  3004. * Gets the user-friendly message stored in $this->message
  3005. * @return string Message
  3006. */
  3007. public function get_message()
  3008. {
  3009. if ($this->debug > 0) {
  3010. error_log('New LP - In learnpath::get_message()', 0);
  3011. }
  3012. return $this->message;
  3013. }
  3014. /**
  3015. * Gets the learnpath name/title
  3016. * @return string Learnpath name/title
  3017. */
  3018. public function get_name()
  3019. {
  3020. if ($this->debug > 0) {
  3021. error_log('New LP - In learnpath::get_name()', 0);
  3022. }
  3023. if (!empty ($this->name)) {
  3024. return $this->name;
  3025. } else {
  3026. return 'N/A';
  3027. }
  3028. }
  3029. /**
  3030. * Gets a link to the resource from the present location, depending on item ID.
  3031. * @param string $type Type of link expected
  3032. * @param integer $item_id Learnpath item ID
  3033. * @return string $provided_toc Link to the lp_item resource
  3034. */
  3035. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3036. {
  3037. $course_id = $this->get_course_int_id();
  3038. if ($this->debug > 0) {
  3039. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3040. }
  3041. if (empty($item_id)) {
  3042. if ($this->debug > 2) {
  3043. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3044. }
  3045. $item_id = $this->get_current_item_id();
  3046. }
  3047. if (empty($item_id)) {
  3048. if ($this->debug > 2) {
  3049. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3050. }
  3051. //still empty, this means there was no item_id given and we are not in an object context or
  3052. //the object property is empty, return empty link
  3053. $item_id = $this->first();
  3054. return '';
  3055. }
  3056. $file = '';
  3057. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3058. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3059. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3060. $item_id = intval($item_id);
  3061. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3062. FROM $lp_table l
  3063. INNER JOIN $lp_item_table li
  3064. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3065. WHERE li.id = $item_id ";
  3066. if ($this->debug > 2) {
  3067. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3068. }
  3069. $res = Database::query($sql);
  3070. if (Database :: num_rows($res) > 0) {
  3071. $row = Database :: fetch_array($res);
  3072. $lp_type = $row['ltype'];
  3073. $lp_path = $row['lpath'];
  3074. $lp_item_type = $row['litype'];
  3075. $lp_item_path = $row['lipath'];
  3076. $lp_item_params = $row['liparams'];
  3077. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3078. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3079. }
  3080. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3081. if ($type == 'http') {
  3082. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3083. } else {
  3084. $course_path = $sys_course_path; //system path
  3085. }
  3086. // 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.
  3087. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3088. $lp_type = 1;
  3089. }
  3090. if ($this->debug > 2) {
  3091. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3092. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3093. }
  3094. // Now go through the specific cases to get the end of the path
  3095. // @todo Use constants instead of int values.
  3096. switch ($lp_type) {
  3097. case 1 :
  3098. if ($lp_item_type == 'dokeos_chapter') {
  3099. $file = 'lp_content.php?type=dir';
  3100. } else {
  3101. require_once 'resourcelinker.inc.php';
  3102. $file = rl_get_resource_link_for_learnpath(
  3103. $course_id,
  3104. $this->get_id(),
  3105. $item_id,
  3106. $this->get_view_id()
  3107. );
  3108. if ($this->debug > 0) {
  3109. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3110. }
  3111. if ($lp_item_type == 'link') {
  3112. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3113. if (is_youtube_link($file)) {
  3114. $src = get_youtube_video_id($file);
  3115. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&src='.$src;
  3116. } elseif (isVimeoLink($file)) {
  3117. $src = getVimeoLinkId($file);
  3118. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&src='.$src;
  3119. } else {
  3120. // If the current site is HTTPS and the link is
  3121. // HTTP, browsers will refuse opening the link
  3122. $urlId = api_get_current_access_url_id();
  3123. $url = api_get_access_url($urlId, false);
  3124. $protocol = substr($url['url'], 0, 5);
  3125. if ($protocol === 'https') {
  3126. $linkProtocol = substr($file, 0, 5);
  3127. if ($linkProtocol === 'http:') {
  3128. //this is the special intervention case
  3129. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&src=' . urlencode($file);
  3130. }
  3131. }
  3132. }
  3133. } else {
  3134. // Check how much attempts of a exercise exits in lp
  3135. $lp_item_id = $this->get_current_item_id();
  3136. $lp_view_id = $this->get_view_id();
  3137. $prevent_reinit = null;
  3138. if (isset($this->items[$this->current])) {
  3139. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3140. }
  3141. if (empty($provided_toc)) {
  3142. if ($this->debug > 0) {
  3143. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3144. }
  3145. $list = $this->get_toc();
  3146. } else {
  3147. if ($this->debug > 0) {
  3148. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3149. }
  3150. $list = $provided_toc;
  3151. }
  3152. $type_quiz = false;
  3153. foreach ($list as $toc) {
  3154. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3155. $type_quiz = true;
  3156. }
  3157. }
  3158. if ($type_quiz) {
  3159. $lp_item_id = intval($lp_item_id);
  3160. $lp_view_id = intval($lp_view_id);
  3161. $sql = "SELECT count(*) FROM $lp_item_view_table
  3162. WHERE
  3163. c_id = $course_id AND
  3164. lp_item_id='" . $lp_item_id . "' AND
  3165. lp_view_id ='" . $lp_view_id . "' AND
  3166. status='completed'";
  3167. $result = Database::query($sql);
  3168. $row_count = Database :: fetch_row($result);
  3169. $count_item_view = (int) $row_count[0];
  3170. $not_multiple_attempt = 0;
  3171. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3172. $not_multiple_attempt = 1;
  3173. }
  3174. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3175. }
  3176. $tmp_array = explode('/', $file);
  3177. $document_name = $tmp_array[count($tmp_array) - 1];
  3178. if (strpos($document_name, '_DELETED_')) {
  3179. $file = 'blank.php?error=document_deleted';
  3180. }
  3181. }
  3182. }
  3183. break;
  3184. case 2 :
  3185. if ($this->debug > 2) {
  3186. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3187. }
  3188. if ($lp_item_type != 'dir') {
  3189. // Quite complex here:
  3190. // We want to make sure 'http://' (and similar) links can
  3191. // be loaded as is (withouth the Chamilo path in front) but
  3192. // some contents use this form: resource.htm?resource=http://blablabla
  3193. // which means we have to find a protocol at the path's start, otherwise
  3194. // it should not be considered as an external URL.
  3195. //if ($this->prerequisites_match($item_id)) {
  3196. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3197. if ($this->debug > 2) {
  3198. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3199. }
  3200. // Distant url, return as is.
  3201. $file = $lp_item_path;
  3202. } else {
  3203. if ($this->debug > 2) {
  3204. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3205. }
  3206. // Prevent getting untranslatable urls.
  3207. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3208. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3209. // Prepare the path.
  3210. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3211. // TODO: Fix this for urls with protocol header.
  3212. $file = str_replace('//', '/', $file);
  3213. $file = str_replace(':/', '://', $file);
  3214. if (substr($lp_path, -1) == '/') {
  3215. $lp_path = substr($lp_path, 0, -1);
  3216. }
  3217. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3218. // if file not found.
  3219. $decoded = html_entity_decode($lp_item_path);
  3220. list ($decoded) = explode('?', $decoded);
  3221. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3222. require_once 'resourcelinker.inc.php';
  3223. $file = rl_get_resource_link_for_learnpath(
  3224. $course_id,
  3225. $this->get_id(),
  3226. $item_id,
  3227. $this->get_view_id()
  3228. );
  3229. if (empty($file)) {
  3230. $file = 'blank.php?error=document_not_found';
  3231. } else {
  3232. $tmp_array = explode('/', $file);
  3233. $document_name = $tmp_array[count($tmp_array) - 1];
  3234. if (strpos($document_name, '_DELETED_')) {
  3235. $file = 'blank.php?error=document_deleted';
  3236. } else {
  3237. $file = 'blank.php?error=document_not_found';
  3238. }
  3239. }
  3240. } else {
  3241. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3242. }
  3243. }
  3244. }
  3245. // We want to use parameters if they were defined in the imsmanifest
  3246. if (strpos($file, 'blank.php') === false) {
  3247. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3248. }
  3249. } else {
  3250. $file = 'lp_content.php?type=dir';
  3251. }
  3252. break;
  3253. case 3 :
  3254. if ($this->debug > 2) {
  3255. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3256. }
  3257. // Formatting AICC HACP append URL.
  3258. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3259. if (!empty($lp_item_params)) {
  3260. $aicc_append .= $lp_item_params . '&';
  3261. }
  3262. if ($lp_item_type != 'dir') {
  3263. // Quite complex here:
  3264. // We want to make sure 'http://' (and similar) links can
  3265. // be loaded as is (withouth the Chamilo path in front) but
  3266. // some contents use this form: resource.htm?resource=http://blablabla
  3267. // which means we have to find a protocol at the path's start, otherwise
  3268. // it should not be considered as an external URL.
  3269. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3270. if ($this->debug > 2) {
  3271. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3272. }
  3273. // Distant url, return as is.
  3274. $file = $lp_item_path;
  3275. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3276. /*
  3277. if (stristr($file,'<servername>') !== false) {
  3278. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3279. }
  3280. */
  3281. if (stripos($file, '<servername>') !== false) {
  3282. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3283. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3284. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3285. }
  3286. //
  3287. $file .= $aicc_append;
  3288. } else {
  3289. if ($this->debug > 2) {
  3290. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3291. }
  3292. // Prevent getting untranslatable urls.
  3293. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3294. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3295. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3296. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3297. // TODO: Fix this for urls with protocol header.
  3298. $file = str_replace('//', '/', $file);
  3299. $file = str_replace(':/', '://', $file);
  3300. $file .= $aicc_append;
  3301. }
  3302. } else {
  3303. $file = 'lp_content.php?type=dir';
  3304. }
  3305. break;
  3306. case 4 :
  3307. break;
  3308. default :
  3309. break;
  3310. }
  3311. // Replace &amp; by & because &amp; will break URL with params
  3312. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3313. }
  3314. if ($this->debug > 2) {
  3315. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3316. }
  3317. return $file;
  3318. }
  3319. /**
  3320. * Gets the latest usable view or generate a new one
  3321. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3322. * @return integer DB lp_view id
  3323. */
  3324. public function get_view($attempt_num = 0)
  3325. {
  3326. if ($this->debug > 0) {
  3327. error_log('New LP - In learnpath::get_view()', 0);
  3328. }
  3329. $search = '';
  3330. // Use $attempt_num to enable multi-views management (disabled so far).
  3331. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3332. $search = 'AND view_count = ' . $attempt_num;
  3333. }
  3334. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3335. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3336. $course_id = api_get_course_int_id();
  3337. $sessionId = api_get_session_id();
  3338. $sql = "SELECT id, view_count FROM $lp_view_table
  3339. WHERE
  3340. c_id = " . $course_id . " AND
  3341. lp_id = " . $this->get_id() . " AND
  3342. user_id = " . $this->get_user_id() . " AND
  3343. session_id = $sessionId
  3344. $search
  3345. ORDER BY view_count DESC";
  3346. $res = Database::query($sql);
  3347. if (Database :: num_rows($res) > 0) {
  3348. $row = Database :: fetch_array($res);
  3349. $this->lp_view_id = $row['id'];
  3350. } else {
  3351. // There is no database record, create one.
  3352. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3353. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3354. Database::query($sql);
  3355. $id = Database :: insert_id();
  3356. $this->lp_view_id = $id;
  3357. }
  3358. return $this->lp_view_id;
  3359. }
  3360. /**
  3361. * Gets the current view id
  3362. * @return integer View ID (from lp_view)
  3363. */
  3364. public function get_view_id()
  3365. {
  3366. if ($this->debug > 0) {
  3367. error_log('New LP - In learnpath::get_view_id()', 0);
  3368. }
  3369. if (!empty ($this->lp_view_id)) {
  3370. return $this->lp_view_id;
  3371. } else {
  3372. return 0;
  3373. }
  3374. }
  3375. /**
  3376. * Gets the update queue
  3377. * @return array Array containing IDs of items to be updated by JavaScript
  3378. */
  3379. public function get_update_queue()
  3380. {
  3381. if ($this->debug > 0) {
  3382. error_log('New LP - In learnpath::get_update_queue()', 0);
  3383. }
  3384. return $this->update_queue;
  3385. }
  3386. /**
  3387. * Gets the user ID
  3388. * @return integer User ID
  3389. */
  3390. public function get_user_id()
  3391. {
  3392. if ($this->debug > 0) {
  3393. error_log('New LP - In learnpath::get_user_id()', 0);
  3394. }
  3395. if (!empty ($this->user_id)) {
  3396. return $this->user_id;
  3397. } else {
  3398. return false;
  3399. }
  3400. }
  3401. /**
  3402. * Checks if any of the items has an audio element attached
  3403. * @return bool True or false
  3404. */
  3405. public function has_audio()
  3406. {
  3407. if ($this->debug > 1) {
  3408. error_log('New LP - In learnpath::has_audio()', 0);
  3409. }
  3410. $has = false;
  3411. foreach ($this->items as $i => $item) {
  3412. if (!empty ($this->items[$i]->audio)) {
  3413. $has = true;
  3414. break;
  3415. }
  3416. }
  3417. return $has;
  3418. }
  3419. /**
  3420. * Logs a message into a file
  3421. * @param string Message to log
  3422. * @return boolean True on success, false on error or if msg empty
  3423. */
  3424. public function log($msg)
  3425. {
  3426. if ($this->debug > 0) {
  3427. error_log('New LP - In learnpath::log()', 0);
  3428. }
  3429. // TODO
  3430. $this->error .= $msg;
  3431. return true;
  3432. }
  3433. /**
  3434. * Moves an item up and down at its level
  3435. * @param integer Item to move up and down
  3436. * @param string Direction 'up' or 'down'
  3437. * @return integer New display order, or false on error
  3438. */
  3439. public function move_item($id, $direction)
  3440. {
  3441. $course_id = api_get_course_int_id();
  3442. if ($this->debug > 0) {
  3443. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3444. }
  3445. if (empty ($id) or empty ($direction)) {
  3446. return false;
  3447. }
  3448. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3449. $sql_sel = "SELECT *
  3450. FROM " . $tbl_lp_item . "
  3451. WHERE c_id = ".$course_id." AND id = " . $id;
  3452. $res_sel = Database::query($sql_sel);
  3453. // Check if elem exists.
  3454. if (Database :: num_rows($res_sel) < 1) {
  3455. return false;
  3456. }
  3457. // Gather data.
  3458. $row = Database :: fetch_array($res_sel);
  3459. $previous = $row['previous_item_id'];
  3460. $next = $row['next_item_id'];
  3461. $display = $row['display_order'];
  3462. $parent = $row['parent_item_id'];
  3463. $lp = $row['lp_id'];
  3464. // Update the item (switch with previous/next one).
  3465. switch ($direction) {
  3466. case 'up' :
  3467. if ($this->debug > 2) {
  3468. error_log('Movement up detected', 0);
  3469. }
  3470. if ($display <= 1) { /*do nothing*/
  3471. } else {
  3472. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3473. WHERE c_id = ".$course_id." AND id = $previous";
  3474. if ($this->debug > 2) {
  3475. error_log('Selecting previous: ' . $sql_sel2, 0);
  3476. }
  3477. $res_sel2 = Database::query($sql_sel2);
  3478. if (Database :: num_rows($res_sel2) < 1) {
  3479. $previous_previous = 0;
  3480. }
  3481. // Gather data.
  3482. $row2 = Database :: fetch_array($res_sel2);
  3483. $previous_previous = $row2['previous_item_id'];
  3484. // Update previous_previous item (switch "next" with current).
  3485. if ($previous_previous != 0) {
  3486. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3487. next_item_id = $id
  3488. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3489. if ($this->debug > 2) {
  3490. error_log($sql_upd2, 0);
  3491. }
  3492. Database::query($sql_upd2);
  3493. }
  3494. // Update previous item (switch with current).
  3495. if ($previous != 0) {
  3496. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3497. next_item_id = $next,
  3498. previous_item_id = $id,
  3499. display_order = display_order +1
  3500. WHERE c_id = ".$course_id." AND id = $previous";
  3501. if ($this->debug > 2) {
  3502. error_log($sql_upd2, 0);
  3503. }
  3504. Database::query($sql_upd2);
  3505. }
  3506. // Update current item (switch with previous).
  3507. if ($id != 0) {
  3508. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3509. next_item_id = $previous,
  3510. previous_item_id = $previous_previous,
  3511. display_order = display_order-1
  3512. WHERE c_id = ".$course_id." AND id = $id";
  3513. if ($this->debug > 2) {
  3514. error_log($sql_upd2, 0);
  3515. }
  3516. Database::query($sql_upd2);
  3517. }
  3518. // Update next item (new previous item).
  3519. if ($next != 0) {
  3520. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3521. WHERE c_id = ".$course_id." AND id = $next";
  3522. if ($this->debug > 2) {
  3523. error_log($sql_upd2, 0);
  3524. }
  3525. Database::query($sql_upd2);
  3526. }
  3527. $display = $display -1;
  3528. }
  3529. break;
  3530. case 'down' :
  3531. if ($this->debug > 2) {
  3532. error_log('Movement down detected', 0);
  3533. }
  3534. if ($next == 0) { /* Do nothing. */
  3535. } else {
  3536. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3537. if ($this->debug > 2) {
  3538. error_log('Selecting next: ' . $sql_sel2, 0);
  3539. }
  3540. $res_sel2 = Database::query($sql_sel2);
  3541. if (Database :: num_rows($res_sel2) < 1) {
  3542. $next_next = 0;
  3543. }
  3544. // Gather data.
  3545. $row2 = Database :: fetch_array($res_sel2);
  3546. $next_next = $row2['next_item_id'];
  3547. // Update previous item (switch with current).
  3548. if ($previous != 0) {
  3549. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3550. WHERE c_id = ".$course_id." AND id = $previous";
  3551. Database::query($sql_upd2);
  3552. }
  3553. // Update current item (switch with previous).
  3554. if ($id != 0) {
  3555. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3556. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3557. WHERE c_id = ".$course_id." AND id = $id";
  3558. Database::query($sql_upd2);
  3559. }
  3560. // Update next item (new previous item).
  3561. if ($next != 0) {
  3562. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3563. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3564. WHERE c_id = ".$course_id." AND id = $next";
  3565. Database::query($sql_upd2);
  3566. }
  3567. // Update next_next item (switch "previous" with current).
  3568. if ($next_next != 0) {
  3569. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3570. previous_item_id = $id
  3571. WHERE c_id = ".$course_id." AND id = $next_next";
  3572. Database::query($sql_upd2);
  3573. }
  3574. $display = $display +1;
  3575. }
  3576. break;
  3577. default :
  3578. return false;
  3579. }
  3580. return $display;
  3581. }
  3582. /**
  3583. * Move a learnpath up (display_order)
  3584. * @param integer Learnpath ID
  3585. */
  3586. public function move_up($lp_id)
  3587. {
  3588. $course_id = api_get_course_int_id();
  3589. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3590. $sql = "SELECT * FROM $lp_table
  3591. WHERE c_id = ".$course_id."
  3592. ORDER BY display_order";
  3593. $res = Database::query($sql);
  3594. if ($res === false)
  3595. return false;
  3596. $lps = array ();
  3597. $lp_order = array ();
  3598. $num = Database :: num_rows($res);
  3599. // First check the order is correct, globally (might be wrong because
  3600. // of versions < 1.8.4)
  3601. if ($num > 0) {
  3602. $i = 1;
  3603. while ($row = Database :: fetch_array($res)) {
  3604. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3605. $need_fix = true;
  3606. $sql_u = "UPDATE $lp_table SET display_order = $i
  3607. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3608. Database::query($sql_u);
  3609. }
  3610. $row['display_order'] = $i;
  3611. $lps[$row['id']] = $row;
  3612. $lp_order[$i] = $row['id'];
  3613. $i++;
  3614. }
  3615. }
  3616. if ($num > 1) { // If there's only one element, no need to sort.
  3617. $order = $lps[$lp_id]['display_order'];
  3618. if ($order > 1) { // If it's the first element, no need to move up.
  3619. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3620. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3621. Database::query($sql_u1);
  3622. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3623. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3624. Database::query($sql_u2);
  3625. }
  3626. }
  3627. }
  3628. /**
  3629. * Move a learnpath down (display_order)
  3630. * @param integer Learnpath ID
  3631. */
  3632. public function move_down($lp_id)
  3633. {
  3634. $course_id = api_get_course_int_id();
  3635. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3636. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3637. $res = Database::query($sql);
  3638. if ($res === false)
  3639. return false;
  3640. $lps = array ();
  3641. $lp_order = array ();
  3642. $num = Database :: num_rows($res);
  3643. $max = 0;
  3644. // First check the order is correct, globally (might be wrong because
  3645. // of versions < 1.8.4).
  3646. if ($num > 0) {
  3647. $i = 1;
  3648. while ($row = Database :: fetch_array($res)) {
  3649. $max = $i;
  3650. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3651. $need_fix = true;
  3652. $sql_u = "UPDATE $lp_table SET display_order = $i
  3653. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3654. $res_u = Database::query($sql_u);
  3655. }
  3656. $row['display_order'] = $i;
  3657. $lps[$row['id']] = $row;
  3658. $lp_order[$i] = $row['id'];
  3659. $i++;
  3660. }
  3661. }
  3662. if ($num > 1) { // If there's only one element, no need to sort.
  3663. $order = $lps[$lp_id]['display_order'];
  3664. if ($order < $max) { // If it's the first element, no need to move up.
  3665. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3666. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3667. Database::query($sql_u1);
  3668. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3669. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3670. Database::query($sql_u2);
  3671. }
  3672. }
  3673. }
  3674. /**
  3675. * Updates learnpath attributes to point to the next element
  3676. * The last part is similar to set_current_item but processing the other way around
  3677. */
  3678. public function next()
  3679. {
  3680. if ($this->debug > 0) {
  3681. error_log('New LP - In learnpath::next()', 0);
  3682. }
  3683. $this->last = $this->get_current_item_id();
  3684. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3685. $this->autocomplete_parents($this->last);
  3686. $new_index = $this->get_next_index();
  3687. if ($this->debug > 2) {
  3688. error_log('New LP - New index: ' . $new_index, 0);
  3689. }
  3690. $this->index = $new_index;
  3691. if ($this->debug > 2) {
  3692. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3693. }
  3694. $this->current = $this->ordered_items[$new_index];
  3695. if ($this->debug > 2) {
  3696. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3697. }
  3698. }
  3699. /**
  3700. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3701. * class, this might be redefined to allow several behaviours depending on the document type.
  3702. * @param integer Resource ID
  3703. * @return boolean True on success, false otherwise
  3704. */
  3705. public function open($id)
  3706. {
  3707. if ($this->debug > 0) {
  3708. error_log('New LP - In learnpath::open()', 0);
  3709. }
  3710. // TODO:
  3711. // set the current resource attribute to this resource
  3712. // switch on element type (redefine in child class?)
  3713. // set status for this item to "opened"
  3714. // start timer
  3715. // initialise score
  3716. $this->index = 0; //or = the last item seen (see $this->last)
  3717. }
  3718. /**
  3719. * Check that all prerequisites are fulfilled. Returns true and an
  3720. * empty string on succes, returns false
  3721. * and the prerequisite string on error.
  3722. * This function is based on the rules for aicc_script language as
  3723. * described in the SCORM 1.2 CAM documentation page 108.
  3724. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3725. * @return boolean True if prerequisites are matched, false otherwise -
  3726. * Empty string if true returned, prerequisites string otherwise.
  3727. */
  3728. public function prerequisites_match($itemId = null)
  3729. {
  3730. $debug = $this->debug;
  3731. if ($debug > 0) {
  3732. error_log('In learnpath::prerequisites_match()', 0);
  3733. }
  3734. if (empty($itemId)) {
  3735. $itemId = $this->current;
  3736. }
  3737. $currentItem = $this->getItem($itemId);
  3738. if ($currentItem) {
  3739. if ($this->type == 2) {
  3740. // Getting prereq from scorm
  3741. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3742. } else {
  3743. $prereq_string = $currentItem->get_prereq_string();
  3744. }
  3745. if (empty($prereq_string)) {
  3746. if ($debug > 0) {
  3747. error_log('Found prereq_string is empty return true');
  3748. }
  3749. return true;
  3750. }
  3751. // Clean spaces.
  3752. $prereq_string = str_replace(' ', '', $prereq_string);
  3753. if ($debug > 0) {
  3754. error_log('Found prereq_string: ' . $prereq_string, 0);
  3755. }
  3756. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3757. $result = $currentItem->parse_prereq(
  3758. $prereq_string,
  3759. $this->items,
  3760. $this->refs_list,
  3761. $this->get_user_id()
  3762. );
  3763. if ($result === false) {
  3764. $this->set_error_msg($currentItem->prereq_alert);
  3765. }
  3766. } else {
  3767. $result = true;
  3768. if ($debug > 1) {
  3769. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3770. }
  3771. }
  3772. if ($debug > 1) {
  3773. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3774. }
  3775. return $result;
  3776. }
  3777. /**
  3778. * Updates learnpath attributes to point to the previous element
  3779. * The last part is similar to set_current_item but processing the other way around
  3780. */
  3781. public function previous()
  3782. {
  3783. if ($this->debug > 0) {
  3784. error_log('New LP - In learnpath::previous()', 0);
  3785. }
  3786. $this->last = $this->get_current_item_id();
  3787. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3788. $this->autocomplete_parents($this->last);
  3789. $new_index = $this->get_previous_index();
  3790. $this->index = $new_index;
  3791. $this->current = $this->ordered_items[$new_index];
  3792. }
  3793. /**
  3794. * Publishes a learnpath. This basically means show or hide the learnpath
  3795. * to normal users.
  3796. * Can be used as abstract
  3797. * @param integer Learnpath ID
  3798. * @param string New visibility
  3799. */
  3800. public function toggle_visibility($lp_id, $set_visibility = 1)
  3801. {
  3802. $action = 'visible';
  3803. if ($set_visibility != 1) {
  3804. $action = 'invisible';
  3805. self::toggle_publish($lp_id, 'i');
  3806. }
  3807. return api_item_property_update(
  3808. api_get_course_info(),
  3809. TOOL_LEARNPATH,
  3810. $lp_id,
  3811. $action,
  3812. api_get_user_id()
  3813. );
  3814. }
  3815. /**
  3816. * Publishes a learnpath. This basically means show or hide the learnpath
  3817. * on the course homepage
  3818. * Can be used as abstract
  3819. * @param integer $lp_id Learnpath id
  3820. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3821. * @return bool
  3822. */
  3823. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3824. {
  3825. $course_id = api_get_course_int_id();
  3826. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3827. $lp_id = intval($lp_id);
  3828. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3829. $result = Database::query($sql);
  3830. if (Database::num_rows($result)) {
  3831. $row = Database :: fetch_array($result);
  3832. $name = domesticate($row['name']);
  3833. if ($set_visibility == 'i') {
  3834. $s = $name . " " . get_lang('LearnpathNotPublished');
  3835. $dialogBox = $s;
  3836. $v = 0;
  3837. }
  3838. if ($set_visibility == 'v') {
  3839. $s = $name . " " . get_lang('LearnpathPublished');
  3840. $dialogBox = $s;
  3841. $v = 1;
  3842. }
  3843. } else {
  3844. return false;
  3845. }
  3846. $session_id = api_get_session_id();
  3847. $session_condition = api_get_session_condition($session_id);
  3848. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3849. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3850. $sql = "SELECT * FROM $tbl_tool
  3851. WHERE
  3852. c_id = ".$course_id." AND
  3853. link='$link' and
  3854. image='scormbuilder.gif' and
  3855. link LIKE '$link%'
  3856. $session_condition
  3857. ";
  3858. $result = Database::query($sql);
  3859. $num = Database :: num_rows($result);
  3860. //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
  3861. if (($set_visibility == 'i') && ($num > 0)) {
  3862. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3863. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3864. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3865. ($course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3866. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3867. $sql = "UPDATE $tbl_tool SET
  3868. c_id = $course_id,
  3869. name = '$name',
  3870. link = '$link',
  3871. image = 'scormbuilder.gif',
  3872. visibility = '$v',
  3873. admin = '0',
  3874. address = 'pastillegris.gif',
  3875. added_tool = 0,
  3876. session_id = $session_id
  3877. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3878. } else {
  3879. // Parameter and database incompatible, do nothing, exit.
  3880. return false;
  3881. }
  3882. Database::query($sql);
  3883. }
  3884. /**
  3885. * Restart the whole learnpath. Return the URL of the first element.
  3886. * Make sure the results are saved with anoter method. This method should probably be
  3887. * redefined in children classes.
  3888. * To use a similar method statically, use the create_new_attempt() method
  3889. * @return string URL to load in the viewer
  3890. */
  3891. public function restart()
  3892. {
  3893. if ($this->debug > 0) {
  3894. error_log('New LP - In learnpath::restart()', 0);
  3895. }
  3896. // TODO
  3897. // Call autosave method to save the current progress.
  3898. //$this->index = 0;
  3899. $session_id = api_get_session_id();
  3900. $course_id = api_get_course_int_id();
  3901. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3902. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
  3903. "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3904. if ($this->debug > 2) {
  3905. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3906. }
  3907. $res = Database::query($sql);
  3908. if ($view_id = Database :: insert_id($res)) {
  3909. $this->lp_view_id = $view_id;
  3910. $this->attempt = $this->attempt + 1;
  3911. } else {
  3912. $this->error = 'Could not insert into item_view table...';
  3913. return false;
  3914. }
  3915. $this->autocomplete_parents($this->current);
  3916. foreach ($this->items as $index => $dummy) {
  3917. $this->items[$index]->restart();
  3918. $this->items[$index]->set_lp_view($this->lp_view_id);
  3919. }
  3920. $this->first();
  3921. return true;
  3922. }
  3923. /**
  3924. * Saves the current item
  3925. * @return boolean
  3926. */
  3927. public function save_current()
  3928. {
  3929. if ($this->debug > 0) {
  3930. error_log('learnpath::save_current()', 0);
  3931. }
  3932. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3933. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3934. if ($this->debug > 2) {
  3935. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3936. }
  3937. if ($this->debug > 2) {
  3938. error_log('' . print_r($this->items, true), 0);
  3939. }
  3940. if (isset($this->items[$this->current]) &&
  3941. is_object($this->items[$this->current])
  3942. ) {
  3943. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3944. $this->autocomplete_parents($this->current);
  3945. $status = $this->items[$this->current]->get_status();
  3946. $this->append_message('new_item_status: ' . $status);
  3947. $this->update_queue[$this->current] = $status;
  3948. return $res;
  3949. }
  3950. return false;
  3951. }
  3952. /**
  3953. * Saves the given item
  3954. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3955. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3956. * @return boolean
  3957. */
  3958. public function save_item($item_id = null, $from_outside = true)
  3959. {
  3960. $debug = $this->debug;
  3961. if ($debug) {
  3962. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3963. }
  3964. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3965. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3966. if (empty($item_id)) {
  3967. $item_id = intval($_REQUEST['id']);
  3968. }
  3969. if (empty($item_id)) {
  3970. $item_id = $this->get_current_item_id();
  3971. }
  3972. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3973. if ($debug) {
  3974. error_log('Object exists');
  3975. }
  3976. // Saving the item.
  3977. $res = $this->items[$item_id]->save(
  3978. $from_outside,
  3979. $this->prerequisites_match($item_id)
  3980. );
  3981. if ($debug) {
  3982. error_log('update_queue before:');
  3983. error_log(print_r($this->update_queue,1));
  3984. }
  3985. $this->autocomplete_parents($item_id);
  3986. $status = $this->items[$item_id]->get_status();
  3987. $this->update_queue[$item_id] = $status;
  3988. if ($debug) {
  3989. error_log('get_status(): ' . $status);
  3990. error_log('update_queue after:');
  3991. error_log(print_r($this->update_queue,1));
  3992. }
  3993. return $res;
  3994. }
  3995. return false;
  3996. }
  3997. /**
  3998. * Saves the last item seen's ID only in case
  3999. */
  4000. public function save_last()
  4001. {
  4002. $course_id = api_get_course_int_id();
  4003. if ($this->debug > 0) {
  4004. error_log('New LP - In learnpath::save_last()', 0);
  4005. }
  4006. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4007. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4008. if (isset($this->current)) {
  4009. if ($this->debug > 2) {
  4010. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4011. }
  4012. $sql = "UPDATE $table SET
  4013. last_item = " . intval($this->get_current_item_id()). "
  4014. WHERE
  4015. c_id = $course_id AND
  4016. lp_id = " . $this->get_id() . " AND
  4017. user_id = " . $this->get_user_id()." ".$session_condition;
  4018. if ($this->debug > 2) {
  4019. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4020. }
  4021. Database::query($sql);
  4022. }
  4023. // Save progress.
  4024. list($progress, $text) = $this->get_progress_bar_text('%');
  4025. if ($progress >= 0 && $progress <= 100) {
  4026. $progress = (int) $progress;
  4027. $sql = "UPDATE $table SET
  4028. progress = $progress
  4029. WHERE
  4030. c_id = ".$course_id." AND
  4031. lp_id = " . $this->get_id() . " AND
  4032. user_id = " . $this->get_user_id()." ".$session_condition;
  4033. // Ignore errors as some tables might not have the progress field just yet.
  4034. Database::query($sql);
  4035. $this->progress_db = $progress;
  4036. }
  4037. }
  4038. /**
  4039. * Sets the current item ID (checks if valid and authorized first)
  4040. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4041. */
  4042. public function set_current_item($item_id = null)
  4043. {
  4044. if ($this->debug > 0) {
  4045. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4046. }
  4047. if (empty ($item_id)) {
  4048. if ($this->debug > 2) {
  4049. error_log('New LP - No new current item given, ignore...', 0);
  4050. }
  4051. // Do nothing.
  4052. } else {
  4053. if ($this->debug > 2) {
  4054. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4055. }
  4056. if (is_numeric($item_id)) {
  4057. $item_id = intval($item_id);
  4058. // TODO: Check in database here.
  4059. $this->last = $this->current;
  4060. $this->current = $item_id;
  4061. // TODO: Update $this->index as well.
  4062. foreach ($this->ordered_items as $index => $item) {
  4063. if ($item == $this->current) {
  4064. $this->index = $index;
  4065. break;
  4066. }
  4067. }
  4068. if ($this->debug > 2) {
  4069. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4070. }
  4071. } else {
  4072. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4073. }
  4074. }
  4075. }
  4076. /**
  4077. * Sets the encoding
  4078. * @param string New encoding
  4079. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4080. */
  4081. public function set_encoding($enc = 'UTF-8')
  4082. {
  4083. if ($this->debug > 0) {
  4084. error_log('New LP - In learnpath::set_encoding()', 0);
  4085. }
  4086. $course_id = api_get_course_int_id();
  4087. $enc = api_refine_encoding_id($enc);
  4088. if (empty($enc)) {
  4089. $enc = api_get_system_encoding();
  4090. }
  4091. if (api_is_encoding_supported($enc)) {
  4092. $lp = $this->get_id();
  4093. if ($lp != 0) {
  4094. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4095. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4096. $res = Database::query($sql);
  4097. return $res;
  4098. }
  4099. }
  4100. return false;
  4101. }
  4102. /**
  4103. * Sets the JS lib setting in the database directly.
  4104. * This is the JavaScript library file this lp needs to load on startup
  4105. * @param string Proximity setting
  4106. * @return boolean True on update success. False otherwise.
  4107. */
  4108. public function set_jslib($lib = '')
  4109. {
  4110. if ($this->debug > 0) {
  4111. error_log('New LP - In learnpath::set_jslib()', 0);
  4112. }
  4113. $lp = $this->get_id();
  4114. $course_id = api_get_course_int_id();
  4115. if ($lp != 0) {
  4116. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4117. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4118. $res = Database::query($sql);
  4119. return $res;
  4120. } else {
  4121. return false;
  4122. }
  4123. }
  4124. /**
  4125. * Sets the name of the LP maker (publisher) (and save)
  4126. * @param string Optional string giving the new content_maker of this learnpath
  4127. * @return boolean True
  4128. */
  4129. public function set_maker($name = '')
  4130. {
  4131. if ($this->debug > 0) {
  4132. error_log('New LP - In learnpath::set_maker()', 0);
  4133. }
  4134. if (empty ($name))
  4135. return false;
  4136. $this->maker = $name;
  4137. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4138. $course_id = api_get_course_int_id();
  4139. $lp_id = $this->get_id();
  4140. $sql = "UPDATE $lp_table SET content_maker = '" . Database::escape_string($this->maker) . "'
  4141. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4142. if ($this->debug > 2) {
  4143. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4144. }
  4145. Database::query($sql);
  4146. return true;
  4147. }
  4148. /**
  4149. * Sets the name of the current learnpath (and save)
  4150. * @param string $name Optional string giving the new name of this learnpath
  4151. * @return boolean True/False
  4152. */
  4153. public function set_name($name = null)
  4154. {
  4155. if ($this->debug > 0) {
  4156. error_log('New LP - In learnpath::set_name()', 0);
  4157. }
  4158. if (empty($name)) {
  4159. return false;
  4160. }
  4161. $this->name = $name;
  4162. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4163. $lp_id = $this->get_id();
  4164. $course_id = $this->course_info['real_id'];
  4165. $sql = "UPDATE $lp_table SET name = '" . Database::escape_string($this->name). "'
  4166. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4167. if ($this->debug > 2) {
  4168. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4169. }
  4170. Database::query($sql);
  4171. // If the lp is visible on the homepage, change his name there.
  4172. if (Database::affected_rows()) {
  4173. $session_id = api_get_session_id();
  4174. $session_condition = api_get_session_condition($session_id);
  4175. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4176. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4177. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4178. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4179. Database::query($sql);
  4180. return true;
  4181. } else {
  4182. return false;
  4183. }
  4184. }
  4185. /**
  4186. * Set index specified prefix terms for all items in this path
  4187. * @param string Comma-separated list of terms
  4188. * @param char Xapian term prefix
  4189. * @return boolean False on error, true otherwise
  4190. */
  4191. public function set_terms_by_prefix($terms_string, $prefix)
  4192. {
  4193. $course_id = api_get_course_int_id();
  4194. if (api_get_setting('search_enabled') !== 'true')
  4195. return false;
  4196. if (!extension_loaded('xapian')) {
  4197. return false;
  4198. }
  4199. $terms_string = trim($terms_string);
  4200. $terms = explode(',', $terms_string);
  4201. array_walk($terms, 'trim_value');
  4202. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4203. // Don't do anything if no change, verify only at DB, not the search engine.
  4204. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4205. return false;
  4206. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4207. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4208. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4209. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4210. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4211. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4212. $lp_id = intval($_POST['lp_id']);
  4213. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4214. $result = Database::query($sql);
  4215. $di = new ChamiloIndexer();
  4216. while ($lp_item = Database :: fetch_array($result)) {
  4217. // Get search_did.
  4218. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4219. $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';
  4220. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4221. //echo $sql; echo '<br>';
  4222. $res = Database::query($sql);
  4223. if (Database::num_rows($res) > 0) {
  4224. $se_ref = Database :: fetch_array($res);
  4225. // Compare terms.
  4226. $doc = $di->get_document($se_ref['search_did']);
  4227. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4228. $xterms = array();
  4229. foreach ($xapian_terms as $xapian_term) {
  4230. $xterms[] = substr($xapian_term['name'], 1);
  4231. }
  4232. $dterms = $terms;
  4233. $missing_terms = array_diff($dterms, $xterms);
  4234. $deprecated_terms = array_diff($xterms, $dterms);
  4235. // Save it to search engine.
  4236. foreach ($missing_terms as $term) {
  4237. $doc->add_term($prefix . $term, 1);
  4238. }
  4239. foreach ($deprecated_terms as $term) {
  4240. $doc->remove_term($prefix . $term);
  4241. }
  4242. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4243. $di->getDb()->flush();
  4244. } else {
  4245. //@todo What we should do here?
  4246. }
  4247. }
  4248. return true;
  4249. }
  4250. /**
  4251. * Sets the theme of the LP (local/remote) (and save)
  4252. * @param string Optional string giving the new theme of this learnpath
  4253. * @return bool Returns true if theme name is not empty
  4254. */
  4255. public function set_theme($name = '')
  4256. {
  4257. $course_id = api_get_course_int_id();
  4258. if ($this->debug > 0) {
  4259. error_log('New LP - In learnpath::set_theme()', 0);
  4260. }
  4261. $this->theme = $name;
  4262. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4263. $lp_id = $this->get_id();
  4264. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4265. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4266. if ($this->debug > 2) {
  4267. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4268. }
  4269. Database::query($sql);
  4270. return true;
  4271. }
  4272. /**
  4273. * Sets the image of an LP (and save)
  4274. * @param string Optional string giving the new image of this learnpath
  4275. * @return bool Returns true if theme name is not empty
  4276. */
  4277. public function set_preview_image($name = '')
  4278. {
  4279. $course_id = api_get_course_int_id();
  4280. if ($this->debug > 0) {
  4281. error_log('New LP - In learnpath::set_preview_image()', 0);
  4282. }
  4283. $this->preview_image = $name;
  4284. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4285. $lp_id = $this->get_id();
  4286. $sql = "UPDATE $lp_table SET
  4287. preview_image = '" . Database::escape_string($this->preview_image). "'
  4288. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4289. if ($this->debug > 2) {
  4290. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4291. }
  4292. Database::query($sql);
  4293. return true;
  4294. }
  4295. /**
  4296. * Sets the author of a LP (and save)
  4297. * @param string Optional string giving the new author of this learnpath
  4298. * @return bool Returns true if author's name is not empty
  4299. */
  4300. public function set_author($name = '') {
  4301. $course_id = api_get_course_int_id();
  4302. if ($this->debug > 0) {
  4303. error_log('New LP - In learnpath::set_author()', 0);
  4304. }
  4305. $this->author = $name;
  4306. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4307. $lp_id = $this->get_id();
  4308. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4309. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4310. if ($this->debug > 2) {
  4311. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4312. }
  4313. Database::query($sql);
  4314. return true;
  4315. }
  4316. /**
  4317. * Sets the hide_toc_frame parameter of a LP (and save)
  4318. * @param int 1 if frame is hidden 0 then else
  4319. * @return bool Returns true if author's name is not empty
  4320. */
  4321. public function set_hide_toc_frame($hide)
  4322. {
  4323. $course_id = api_get_course_int_id();
  4324. if ($this->debug > 0) {
  4325. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4326. }
  4327. if (intval($hide) == $hide){
  4328. $this->hide_toc_frame = $hide;
  4329. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4330. $lp_id = $this->get_id();
  4331. $sql = "UPDATE $lp_table SET hide_toc_frame = '" . $this->hide_toc_frame . "'
  4332. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4333. if ($this->debug > 2) {
  4334. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4335. }
  4336. Database::query($sql);
  4337. return true;
  4338. } else {
  4339. return false;
  4340. }
  4341. }
  4342. /**
  4343. * Sets the prerequisite of a LP (and save)
  4344. * @param int integer giving the new prerequisite of this learnpath
  4345. * @return bool returns true if prerequisite is not empty
  4346. */
  4347. public function set_prerequisite($prerequisite)
  4348. {
  4349. $course_id = api_get_course_int_id();
  4350. if ($this->debug > 0) {
  4351. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4352. }
  4353. $this->prerequisite = intval($prerequisite);
  4354. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4355. $lp_id = $this->get_id();
  4356. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4357. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4358. if ($this->debug > 2) {
  4359. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4360. }
  4361. Database::query($sql);
  4362. return true;
  4363. }
  4364. /**
  4365. * Sets the location/proximity of the LP (local/remote) (and save)
  4366. * @param string Optional string giving the new location of this learnpath
  4367. * @return boolean True on success / False on error
  4368. */
  4369. public function set_proximity($name = '')
  4370. {
  4371. $course_id = api_get_course_int_id();
  4372. if ($this->debug > 0) {
  4373. error_log('New LP - In learnpath::set_proximity()', 0);
  4374. }
  4375. if (empty ($name))
  4376. return false;
  4377. $this->proximity = $name;
  4378. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4379. $lp_id = $this->get_id();
  4380. $sql = "UPDATE $lp_table SET
  4381. content_local = '" . Database::escape_string($name) . "'
  4382. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4383. if ($this->debug > 2) {
  4384. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4385. }
  4386. Database::query($sql);
  4387. return true;
  4388. }
  4389. /**
  4390. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4391. * @param integer DB ID of the item
  4392. */
  4393. public function set_previous_item($id)
  4394. {
  4395. if ($this->debug > 0) {
  4396. error_log('New LP - In learnpath::set_previous_item()', 0);
  4397. }
  4398. $this->last = $id;
  4399. }
  4400. /**
  4401. * Sets use_max_score
  4402. * @param string $use_max_score Optional string giving the new location of this learnpath
  4403. * @return boolean True on success / False on error
  4404. */
  4405. public function set_use_max_score($use_max_score = 1)
  4406. {
  4407. $course_id = api_get_course_int_id();
  4408. if ($this->debug > 0) {
  4409. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4410. }
  4411. $use_max_score = intval($use_max_score);
  4412. $this->use_max_score = $use_max_score;
  4413. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4414. $lp_id = $this->get_id();
  4415. $sql = "UPDATE $lp_table SET
  4416. use_max_score = '" . $this->use_max_score . "'
  4417. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4418. if ($this->debug > 2) {
  4419. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4420. }
  4421. Database::query($sql);
  4422. return true;
  4423. }
  4424. /**
  4425. * Sets and saves the expired_on date
  4426. * @param string $expired_on Optional string giving the new author of this learnpath
  4427. * @return bool Returns true if author's name is not empty
  4428. */
  4429. public function set_expired_on($expired_on)
  4430. {
  4431. $course_id = api_get_course_int_id();
  4432. if ($this->debug > 0) {
  4433. error_log('New LP - In learnpath::set_expired_on()', 0);
  4434. }
  4435. if (!empty($expired_on)) {
  4436. $this->expired_on = api_get_utc_datetime($expired_on);
  4437. } else {
  4438. $this->expired_on = '';
  4439. }
  4440. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4441. $lp_id = $this->get_id();
  4442. $sql = "UPDATE $lp_table SET
  4443. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4444. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4445. if ($this->debug > 2) {
  4446. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4447. }
  4448. Database::query($sql);
  4449. return true;
  4450. }
  4451. /**
  4452. * Sets and saves the publicated_on date
  4453. * @param string $publicated_on Optional string giving the new author of this learnpath
  4454. * @return bool Returns true if author's name is not empty
  4455. */
  4456. public function set_publicated_on($publicated_on)
  4457. {
  4458. $course_id = api_get_course_int_id();
  4459. if ($this->debug > 0) {
  4460. error_log('New LP - In learnpath::set_expired_on()', 0);
  4461. }
  4462. if (!empty($publicated_on)) {
  4463. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4464. } else {
  4465. $this->publicated_on = '';
  4466. }
  4467. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4468. $lp_id = $this->get_id();
  4469. $sql = "UPDATE $lp_table SET
  4470. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4471. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4472. if ($this->debug > 2) {
  4473. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4474. }
  4475. Database::query($sql);
  4476. return true;
  4477. }
  4478. /**
  4479. * Sets and saves the expired_on date
  4480. * @return bool Returns true if author's name is not empty
  4481. */
  4482. public function set_modified_on()
  4483. {
  4484. $course_id = api_get_course_int_id();
  4485. if ($this->debug > 0) {
  4486. error_log('New LP - In learnpath::set_expired_on()', 0);
  4487. }
  4488. $this->modified_on = api_get_utc_datetime();
  4489. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4490. $lp_id = $this->get_id();
  4491. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4492. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4493. if ($this->debug > 2) {
  4494. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4495. }
  4496. Database::query($sql);
  4497. return true;
  4498. }
  4499. /**
  4500. * Sets the object's error message
  4501. * @param string Error message. If empty, reinits the error string
  4502. * @return void
  4503. */
  4504. public function set_error_msg($error = '')
  4505. {
  4506. if ($this->debug > 0) {
  4507. error_log('New LP - In learnpath::set_error_msg()', 0);
  4508. }
  4509. if (empty ($error)) {
  4510. $this->error = '';
  4511. } else {
  4512. $this->error .= $error;
  4513. }
  4514. }
  4515. /**
  4516. * Launches the current item if not 'sco'
  4517. * (starts timer and make sure there is a record ready in the DB)
  4518. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4519. * @return boolean
  4520. */
  4521. public function start_current_item($allow_new_attempt = false)
  4522. {
  4523. if ($this->debug > 0) {
  4524. error_log('New LP - In learnpath::start_current_item()', 0);
  4525. }
  4526. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4527. $type = $this->get_type();
  4528. $item_type = $this->items[$this->current]->get_type();
  4529. if (($type == 2 && $item_type != 'sco') || ($type == 3 && $item_type != 'au') ||
  4530. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4531. ) {
  4532. $this->items[$this->current]->open($allow_new_attempt);
  4533. $this->autocomplete_parents($this->current);
  4534. $prereq_check = $this->prerequisites_match($this->current);
  4535. $this->items[$this->current]->save(false, $prereq_check);
  4536. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4537. } else {
  4538. // If sco, then it is supposed to have been updated by some other call.
  4539. }
  4540. if ($item_type == 'sco') {
  4541. $this->items[$this->current]->restart();
  4542. }
  4543. }
  4544. if ($this->debug > 0) {
  4545. error_log('New LP - End of learnpath::start_current_item()', 0);
  4546. }
  4547. return true;
  4548. }
  4549. /**
  4550. * Stops the processing and counters for the old item (as held in $this->last)
  4551. * @return boolean True/False
  4552. */
  4553. public function stop_previous_item()
  4554. {
  4555. if ($this->debug > 0) {
  4556. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4557. }
  4558. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4559. if ($this->debug > 2) {
  4560. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4561. }
  4562. switch ($this->get_type()) {
  4563. case '3' :
  4564. if ($this->items[$this->last]->get_type() != 'au') {
  4565. if ($this->debug > 2) {
  4566. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4567. }
  4568. $this->items[$this->last]->close();
  4569. //$this->autocomplete_parents($this->last);
  4570. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4571. } else {
  4572. if ($this->debug > 2) {
  4573. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4574. }
  4575. }
  4576. case '2' :
  4577. if ($this->items[$this->last]->get_type() != 'sco') {
  4578. if ($this->debug > 2) {
  4579. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4580. }
  4581. $this->items[$this->last]->close();
  4582. //$this->autocomplete_parents($this->last);
  4583. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4584. } else {
  4585. if ($this->debug > 2) {
  4586. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4587. }
  4588. }
  4589. break;
  4590. case '1' :
  4591. default :
  4592. if ($this->debug > 2) {
  4593. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4594. }
  4595. $this->items[$this->last]->close();
  4596. break;
  4597. }
  4598. } else {
  4599. if ($this->debug > 2) {
  4600. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4601. }
  4602. return false;
  4603. }
  4604. return true;
  4605. }
  4606. /**
  4607. * Updates the default view mode from fullscreen to embedded and inversely
  4608. * @return string The current default view mode ('fullscreen' or 'embedded')
  4609. */
  4610. public function update_default_view_mode()
  4611. {
  4612. $course_id = api_get_course_int_id();
  4613. if ($this->debug > 0) {
  4614. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4615. }
  4616. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4617. $sql = "SELECT * FROM $lp_table
  4618. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4619. $res = Database::query($sql);
  4620. if (Database :: num_rows($res) > 0) {
  4621. $row = Database :: fetch_array($res);
  4622. $default_view_mode = $row['default_view_mod'];
  4623. $view_mode = $default_view_mode;
  4624. switch ($default_view_mode) {
  4625. case 'fullscreen':
  4626. $view_mode = 'embedded';
  4627. break;
  4628. case 'embedded':
  4629. $view_mode = 'embedframe';
  4630. break;
  4631. case 'embedframe':
  4632. $view_mode = 'impress';
  4633. break;
  4634. case 'impress':
  4635. $view_mode = 'fullscreen';
  4636. break;
  4637. }
  4638. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4639. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4640. Database::query($sql);
  4641. $this->mode = $view_mode;
  4642. return $view_mode;
  4643. } else {
  4644. if ($this->debug > 2) {
  4645. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4646. }
  4647. }
  4648. return -1;
  4649. }
  4650. /**
  4651. * Updates the default behaviour about auto-commiting SCORM updates
  4652. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4653. */
  4654. public function update_default_scorm_commit()
  4655. {
  4656. $course_id = api_get_course_int_id();
  4657. if ($this->debug > 0) {
  4658. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4659. }
  4660. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4661. $sql = "SELECT * FROM $lp_table
  4662. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4663. $res = Database::query($sql);
  4664. if (Database :: num_rows($res) > 0) {
  4665. $row = Database :: fetch_array($res);
  4666. $force = $row['force_commit'];
  4667. if ($force == 1) {
  4668. $force = 0;
  4669. $force_return = false;
  4670. } elseif ($force == 0) {
  4671. $force = 1;
  4672. $force_return = true;
  4673. }
  4674. $sql = "UPDATE $lp_table SET force_commit = $force
  4675. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4676. Database::query($sql);
  4677. $this->force_commit = $force_return;
  4678. return $force_return;
  4679. } else {
  4680. if ($this->debug > 2) {
  4681. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4682. }
  4683. }
  4684. return -1;
  4685. }
  4686. /**
  4687. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4688. * @return bool True on success, false on failure
  4689. */
  4690. public function update_display_order()
  4691. {
  4692. $course_id = api_get_course_int_id();
  4693. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4694. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4695. $res = Database::query($sql);
  4696. if ($res === false)
  4697. return false;
  4698. $num = Database :: num_rows($res);
  4699. // First check the order is correct, globally (might be wrong because
  4700. // of versions < 1.8.4).
  4701. if ($num > 0) {
  4702. $i = 1;
  4703. while ($row = Database :: fetch_array($res)) {
  4704. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4705. $need_fix = true;
  4706. $sql_u = "UPDATE $lp_table SET display_order = $i
  4707. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4708. Database::query($sql_u);
  4709. }
  4710. $i++;
  4711. }
  4712. }
  4713. return true;
  4714. }
  4715. /**
  4716. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4717. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4718. */
  4719. public function update_reinit()
  4720. {
  4721. $course_id = api_get_course_int_id();
  4722. if ($this->debug > 0) {
  4723. error_log('New LP - In learnpath::update_reinit()', 0);
  4724. }
  4725. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4726. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4727. $res = Database::query($sql);
  4728. if (Database :: num_rows($res) > 0) {
  4729. $row = Database :: fetch_array($res);
  4730. $force = $row['prevent_reinit'];
  4731. if ($force == 1) {
  4732. $force = 0;
  4733. } elseif ($force == 0) {
  4734. $force = 1;
  4735. }
  4736. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4737. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4738. Database::query($sql);
  4739. $this->prevent_reinit = $force;
  4740. return $force;
  4741. } else {
  4742. if ($this->debug > 2) {
  4743. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4744. }
  4745. }
  4746. return -1;
  4747. }
  4748. /**
  4749. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4750. *
  4751. * @return string 'single', 'multi' or 'seriousgame'
  4752. * @author ndiechburg <noel@cblue.be>
  4753. **/
  4754. public function get_attempt_mode()
  4755. {
  4756. //Set default value for seriousgame_mode
  4757. if (!isset($this->seriousgame_mode)) {
  4758. $this->seriousgame_mode=0;
  4759. }
  4760. // Set default value for prevent_reinit
  4761. if (!isset($this->prevent_reinit)) {
  4762. $this->prevent_reinit =1;
  4763. }
  4764. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4765. return 'seriousgame';
  4766. }
  4767. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4768. return 'single';
  4769. }
  4770. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4771. return 'multiple';
  4772. }
  4773. return 'single';
  4774. }
  4775. /**
  4776. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4777. *
  4778. * @param string 'seriousgame', 'single' or 'multiple'
  4779. * @return boolean
  4780. * @author ndiechburg <noel@cblue.be>
  4781. **/
  4782. public function set_attempt_mode($mode) {
  4783. $course_id = api_get_course_int_id();
  4784. switch ($mode) {
  4785. case 'seriousgame' :
  4786. $sg_mode = 1;
  4787. $prevent_reinit = 1;
  4788. break;
  4789. case 'single' :
  4790. $sg_mode = 0;
  4791. $prevent_reinit = 1;
  4792. break;
  4793. case 'multiple' :
  4794. $sg_mode = 0;
  4795. $prevent_reinit = 0;
  4796. break;
  4797. default :
  4798. $sg_mode = 0;
  4799. $prevent_reinit = 0;
  4800. break;
  4801. }
  4802. $this->prevent_reinit = $prevent_reinit;
  4803. $this->seriousgame_mode = $sg_mode;
  4804. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4805. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode
  4806. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4807. $res = Database::query($sql);
  4808. if ($res) {
  4809. return true;
  4810. } else {
  4811. return false;
  4812. }
  4813. }
  4814. /**
  4815. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4816. *
  4817. * @return boolean
  4818. * @author ndiechburg <noel@cblue.be>
  4819. **/
  4820. public function switch_attempt_mode() {
  4821. if ($this->debug > 0) {
  4822. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4823. }
  4824. $mode = $this->get_attempt_mode();
  4825. switch ($mode) {
  4826. case 'single' :
  4827. $next_mode = 'multiple';
  4828. break;
  4829. case 'multiple' :
  4830. $next_mode = 'seriousgame';
  4831. break;
  4832. case 'seriousgame' :
  4833. $next_mode = 'single';
  4834. break;
  4835. default :
  4836. $next_mode = 'single';
  4837. break;
  4838. }
  4839. $this->set_attempt_mode($next_mode);
  4840. }
  4841. /**
  4842. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4843. * but possibility to do again a completed item.
  4844. *
  4845. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4846. * @author ndiechburg <noel@cblue.be>
  4847. **/
  4848. public function set_seriousgame_mode() {
  4849. $course_id = api_get_course_int_id();
  4850. if ($this->debug > 0) {
  4851. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4852. }
  4853. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4854. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4855. $res = Database::query($sql);
  4856. if (Database :: num_rows($res) > 0) {
  4857. $row = Database :: fetch_array($res);
  4858. $force = $row['seriousgame_mode'];
  4859. if ($force == 1) {
  4860. $force = 0;
  4861. } elseif ($force == 0) {
  4862. $force = 1;
  4863. }
  4864. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4865. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4866. Database::query($sql);
  4867. $this->seriousgame_mode = $force;
  4868. return $force;
  4869. } else {
  4870. if ($this->debug > 2) {
  4871. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4872. }
  4873. }
  4874. return -1;
  4875. }
  4876. /**
  4877. * Updates the "scorm_debug" value that shows or hide the debug window
  4878. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4879. */
  4880. public function update_scorm_debug()
  4881. {
  4882. $course_id = api_get_course_int_id();
  4883. if ($this->debug > 0) {
  4884. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4885. }
  4886. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4887. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4888. $res = Database::query($sql);
  4889. if (Database :: num_rows($res) > 0) {
  4890. $row = Database :: fetch_array($res);
  4891. $force = $row['debug'];
  4892. if ($force == 1) {
  4893. $force = 0;
  4894. } elseif ($force == 0) {
  4895. $force = 1;
  4896. }
  4897. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4898. $res = Database::query($sql);
  4899. $this->scorm_debug = $force;
  4900. return $force;
  4901. } else {
  4902. if ($this->debug > 2) {
  4903. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4904. }
  4905. }
  4906. return -1;
  4907. }
  4908. /**
  4909. * Function that makes a call to the function sort_tree_array and create_tree_array
  4910. * @author Kevin Van Den Haute
  4911. * @param array
  4912. */
  4913. public function tree_array($array)
  4914. {
  4915. if ($this->debug > 1) {
  4916. error_log('New LP - In learnpath::tree_array()', 0);
  4917. }
  4918. $array = $this->sort_tree_array($array);
  4919. $this->create_tree_array($array);
  4920. }
  4921. /**
  4922. * Creates an array with the elements of the learning path tree in it
  4923. *
  4924. * @author Kevin Van Den Haute
  4925. * @param array $array
  4926. * @param int $parent
  4927. * @param int $depth
  4928. * @param array $tmp
  4929. */
  4930. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ()) {
  4931. if ($this->debug > 1) {
  4932. error_log('New LP - In learnpath::create_tree_array())', 0);
  4933. }
  4934. if (is_array($array)) {
  4935. for ($i = 0; $i < count($array); $i++) {
  4936. if ($array[$i]['parent_item_id'] == $parent) {
  4937. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4938. $tmp[] = $array[$i]['parent_item_id'];
  4939. $depth++;
  4940. }
  4941. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4942. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4943. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4944. $this->arrMenu[] = array(
  4945. 'id' => $array[$i]['id'],
  4946. 'item_type' => $array[$i]['item_type'],
  4947. 'title' => $array[$i]['title'],
  4948. 'path' => $path,
  4949. 'description' => $array[$i]['description'],
  4950. 'parent_item_id' => $array[$i]['parent_item_id'],
  4951. 'previous_item_id' => $array[$i]['previous_item_id'],
  4952. 'next_item_id' => $array[$i]['next_item_id'],
  4953. 'min_score' => $array[$i]['min_score'],
  4954. 'max_score' => $array[$i]['max_score'],
  4955. 'mastery_score' => $array[$i]['mastery_score'],
  4956. 'display_order' => $array[$i]['display_order'],
  4957. 'prerequisite' => $preq,
  4958. 'depth' => $depth,
  4959. 'audio' => $audio
  4960. );
  4961. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4962. }
  4963. }
  4964. }
  4965. }
  4966. /**
  4967. * Sorts a multi dimensional array by parent id and display order
  4968. * @author Kevin Van Den Haute
  4969. *
  4970. * @param array $array (array with al the learning path items in it)
  4971. *
  4972. * @return array
  4973. */
  4974. public function sort_tree_array($array) {
  4975. foreach ($array as $key => $row) {
  4976. $parent[$key] = $row['parent_item_id'];
  4977. $position[$key] = $row['display_order'];
  4978. }
  4979. if (count($array) > 0)
  4980. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4981. return $array;
  4982. }
  4983. /**
  4984. * Function that creates a html list of learning path items so that we can add audio files to them
  4985. * @author Kevin Van Den Haute
  4986. * @param int $lp_id
  4987. * @return string
  4988. */
  4989. public function overview()
  4990. {
  4991. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4992. if ($this->debug > 0) {
  4993. error_log('New LP - In learnpath::overview()', 0);
  4994. }
  4995. global $_course;
  4996. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4997. $return = '';
  4998. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  4999. // we need to start a form when we want to update all the mp3 files
  5000. if ($update_audio == 'true') {
  5001. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&amp;action=' . Security :: remove_XSS($_GET['action']) . '&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5002. }
  5003. $return .= '<div id="message"></div>';
  5004. if (count($this->items) == 0) {
  5005. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5006. } else {
  5007. $return_audio = '<table class="data_table">';
  5008. $return_audio .= '<tr>';
  5009. $return_audio .= '<th width="60%">' . get_lang('Title') . '</th>';
  5010. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5011. $return_audio .= '</tr>';
  5012. if ($update_audio != 'true') {
  5013. $return .= '<div class="span12">';
  5014. $return .= self::return_new_tree($update_audio);
  5015. $return .='</div>';
  5016. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5017. } else {
  5018. $return_audio .= self::return_new_tree($update_audio);
  5019. $return .= $return_audio.'</table>';
  5020. }
  5021. // We need to close the form when we are updating the mp3 files.
  5022. if ($update_audio == 'true') {
  5023. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5024. }
  5025. }
  5026. // We need to close the form when we are updating the mp3 files.
  5027. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5028. $return .= '</form>';
  5029. }
  5030. return $return;
  5031. }
  5032. /**
  5033. * @param string string $update_audio
  5034. * @param bool $drop_element_here
  5035. * @return string
  5036. */
  5037. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5038. {
  5039. $return = '';
  5040. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5041. $course_id = api_get_course_int_id();
  5042. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5043. $sql = "SELECT * FROM $tbl_lp_item
  5044. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5045. $result = Database::query($sql);
  5046. $arrLP = array();
  5047. while ($row = Database :: fetch_array($result)) {
  5048. $arrLP[] = array(
  5049. 'id' => $row['id'],
  5050. 'item_type' => $row['item_type'],
  5051. 'title' => Security :: remove_XSS($row['title']),
  5052. 'path' => $row['path'],
  5053. 'description' => Security::remove_XSS($row['description']),
  5054. 'parent_item_id' => $row['parent_item_id'],
  5055. 'previous_item_id' => $row['previous_item_id'],
  5056. 'next_item_id' => $row['next_item_id'],
  5057. 'max_score' => $row['max_score'],
  5058. 'min_score' => $row['min_score'],
  5059. 'mastery_score' => $row['mastery_score'],
  5060. 'prerequisite' => $row['prerequisite'],
  5061. 'display_order' => $row['display_order'],
  5062. 'audio' => $row['audio']
  5063. );
  5064. }
  5065. $this->tree_array($arrLP);
  5066. $arrLP = $this->arrMenu;
  5067. unset ($this->arrMenu);
  5068. $default_data = null;
  5069. $default_content = null;
  5070. $elements = array();
  5071. $return_audio = null;
  5072. for ($i = 0; $i < count($arrLP); $i++) {
  5073. $title = $arrLP[$i]['title'];
  5074. $title_cut = cut($arrLP[$i]['title'], 25);
  5075. //Link for the documents
  5076. if ($arrLP[$i]['item_type'] == 'document') {
  5077. $url = api_get_self() . '?'.api_get_cidreq().'&amp;action=view_item&amp;mode=preview_document&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id;
  5078. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5079. }
  5080. if (($i % 2) == 0) {
  5081. $oddClass = 'row_odd';
  5082. } else {
  5083. $oddClass = 'row_even';
  5084. }
  5085. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5086. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5087. $icon = '';
  5088. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5089. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5090. } else {
  5091. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5092. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5093. } else {
  5094. $icon = '<img src="../img/folder_document.gif" />';
  5095. }
  5096. }
  5097. // The audio column.
  5098. $return_audio .= '<td align="center">';
  5099. $audio = '';
  5100. if (!$update_audio OR $update_audio <> 'true') {
  5101. if (!empty($arrLP[$i]['audio'])) {
  5102. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5103. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5104. $audio .= '<script type="text/javascript">
  5105. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5106. s1.addParam("allowscriptaccess","always");
  5107. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5108. s1.write("container' . $i . '");
  5109. </script>';*/
  5110. } else {
  5111. $audio .= '';
  5112. }
  5113. } else {
  5114. $types = self::getChapterTypes();
  5115. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5116. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5117. if (!empty ($arrLP[$i]['audio'])) {
  5118. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5119. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5120. }
  5121. }
  5122. }
  5123. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5124. $return_audio .= '</td>';
  5125. $move_icon = '';
  5126. $move_item_icon = '';
  5127. $edit_icon = '';
  5128. $delete_icon = '';
  5129. $audio_icon = '';
  5130. $prerequisities_icon = '';
  5131. if ($is_allowed_to_edit) {
  5132. if (!$update_audio OR $update_audio <> 'true') {
  5133. $move_icon .= '<a class="moved" href="#">';
  5134. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5135. $move_icon .= '</a>';
  5136. }
  5137. // No edit for this item types
  5138. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5139. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5140. $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'] . '">';
  5141. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5142. $edit_icon .= '</a>';
  5143. } else {
  5144. $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'] . '">';
  5145. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5146. $edit_icon .= '</a>';
  5147. }
  5148. }
  5149. $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($title) . '\');">';
  5150. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5151. $delete_icon .= '</a>';
  5152. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5153. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5154. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5155. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5156. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5157. }
  5158. }
  5159. if ($update_audio != 'true') {
  5160. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
  5161. } else {
  5162. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5163. }
  5164. $parent_id = $arrLP[$i]['parent_item_id'];
  5165. $default_data[$arrLP[$i]['id']] = $row;
  5166. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5167. if (empty($parent_id)) {
  5168. $elements[$arrLP[$i]['id']]['data'] = $row;
  5169. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5170. } else {
  5171. $parent_arrays = array();
  5172. if ($arrLP[$i]['depth'] > 1) {
  5173. //Getting list of parents
  5174. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5175. foreach($arrLP as $item) {
  5176. if ($item['id'] == $parent_id) {
  5177. if ($item['parent_item_id'] == 0) {
  5178. $parent_id = $item['id'];
  5179. break;
  5180. } else {
  5181. $parent_id = $item['parent_item_id'];
  5182. if (empty($parent_arrays)) {
  5183. $parent_arrays[] = intval($item['id']);
  5184. }
  5185. $parent_arrays[] = $parent_id;
  5186. break;
  5187. }
  5188. }
  5189. }
  5190. }
  5191. }
  5192. if (!empty($parent_arrays)) {
  5193. $parent_arrays = array_reverse($parent_arrays);
  5194. $val = '$elements';
  5195. $x = 0;
  5196. foreach($parent_arrays as $item) {
  5197. if ($x != count($parent_arrays) -1) {
  5198. $val .= '["'.$item.'"]["children"]';
  5199. } else {
  5200. $val .= '["'.$item.'"]["children"]';
  5201. }
  5202. $x++;
  5203. }
  5204. $val .= "";
  5205. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5206. eval($code_str);
  5207. } else {
  5208. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5209. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5210. }
  5211. }
  5212. }
  5213. $return .= '<div class="lp_tree well">';
  5214. $return .= '<ul id="lp_item_list">';
  5215. $return .='<h4>'.$this->name.'</h4><br>';
  5216. $tree = self::print_recursive($elements, $default_data, $default_content);
  5217. if (!empty($tree)) {
  5218. $return .= $tree;
  5219. } else {
  5220. if ($drop_element_here) {
  5221. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5222. }
  5223. }
  5224. $return .= '</ul>';
  5225. if ($update_audio == 'true') {
  5226. $return = $return_audio;
  5227. } else {
  5228. $return .= '</div>';
  5229. }
  5230. return $return;
  5231. }
  5232. /**
  5233. * @param array $elements
  5234. * @param array $default_data
  5235. * @param array $default_content
  5236. * @return string
  5237. */
  5238. function print_recursive($elements, $default_data, $default_content)
  5239. {
  5240. $return = '';
  5241. foreach ($elements as $key => $item) {
  5242. if (isset($item['load_data']) || empty($item['data'])) {
  5243. $item['data'] = $default_data[$item['load_data']];
  5244. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5245. }
  5246. $sub_list = '';
  5247. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5248. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5249. }
  5250. if (empty($item['children'])) {
  5251. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5252. $active = null;
  5253. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5254. $active = 'active';
  5255. }
  5256. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5257. } else {
  5258. //sections
  5259. if (isset($item['children'])) {
  5260. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5261. }
  5262. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5263. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5264. }
  5265. }
  5266. return $return;
  5267. }
  5268. /**
  5269. * This function builds the action menu
  5270. * @param bool $returnContent
  5271. * @return void
  5272. */
  5273. public function build_action_menu($returnContent = false)
  5274. {
  5275. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5276. $return = '<div class="actions">';
  5277. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '&isStudentView=true">' . Display :: return_icon('preview_view.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  5278. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5279. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;action=edit&amp;lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5280. $buttons = array(
  5281. array(
  5282. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5283. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=set_previous_step_as_prerequisite&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  5284. ),
  5285. array(
  5286. 'title' => get_lang('ClearAllPrerequisites'),
  5287. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=clear_prerequisites&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  5288. ),
  5289. );
  5290. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5291. $return .= '</div>';
  5292. if ($returnContent) {
  5293. return $return;
  5294. }
  5295. echo $return;
  5296. }
  5297. /**
  5298. * This functions builds the LP tree based on data from the database.
  5299. * @return string
  5300. * @deprecated use the return_new_tree() function
  5301. * @uses dtree.js :: necessary javascript for building this tree
  5302. */
  5303. public function build_tree()
  5304. {
  5305. $course_id = api_get_course_int_id();
  5306. $return = "<script type=\"text/javascript\">\n";
  5307. $return .= "\tm = new dTree('m');\n\n";
  5308. $return .= "\tm.config.folderLinks = true;\n";
  5309. $return .= "\tm.config.useCookies = true;\n";
  5310. $return .= "\tm.config.useIcons = true;\n";
  5311. $return .= "\tm.config.useLines = true;\n";
  5312. $return .= "\tm.config.useSelection = true;\n";
  5313. $return .= "\tm.config.useStatustext = false;\n\n";
  5314. $menu = 0;
  5315. $parent = '';
  5316. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  5317. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5318. $sql = " SELECT
  5319. id,
  5320. title,
  5321. description,
  5322. item_type,
  5323. path,
  5324. parent_item_id,
  5325. previous_item_id,
  5326. next_item_id,
  5327. max_score,
  5328. min_score,
  5329. mastery_score,
  5330. display_order
  5331. FROM $tbl_lp_item
  5332. WHERE c_id = ".$course_id." AND lp_id = " . intval($this->lp_id);
  5333. $result = Database::query($sql);
  5334. $arrLP = array ();
  5335. while ($row = Database :: fetch_array($result)) {
  5336. $row['title'] = Security :: remove_XSS($row['title']);
  5337. $row['description'] = Security :: remove_XSS($row['description']);
  5338. $arrLP[] = array(
  5339. 'id' => $row['id'],
  5340. 'item_type' => $row['item_type'],
  5341. 'title' => $row['title'],
  5342. 'path' => $row['path'],
  5343. 'description' => $row['description'],
  5344. 'parent_item_id' => $row['parent_item_id'],
  5345. 'previous_item_id' => $row['previous_item_id'],
  5346. 'next_item_id' => $row['next_item_id'],
  5347. 'max_score' => $row['max_score'],
  5348. 'min_score' => $row['min_score'],
  5349. 'mastery_score' => $row['mastery_score'],
  5350. 'display_order' => $row['display_order']
  5351. );
  5352. }
  5353. $this->tree_array($arrLP);
  5354. $arrLP = $this->arrMenu;
  5355. unset ($this->arrMenu);
  5356. for ($i = 0; $i < count($arrLP); $i++) {
  5357. $title = addslashes($arrLP[$i]['title']);
  5358. $menu_page = api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=view_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $_SESSION['oLP']->lp_id;
  5359. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5360. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5361. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/lp_" . $icon_name . ".png', '../img/lp_" . $icon_name . ".png');\n";
  5362. } else
  5363. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5364. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/lp_" . $icon_name . ".gif', '../img/lp_" . $icon_name . ".gif');\n";
  5365. } else {
  5366. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5367. }
  5368. if ($menu < $arrLP[$i]['id'])
  5369. $menu = $arrLP[$i]['id'];
  5370. }
  5371. $return .= "\n\tdocument.write(m);\n";
  5372. $return .= "\t if(!m.selectedNode) m.s(1);";
  5373. $return .= "</script>\n";
  5374. return $return;
  5375. }
  5376. /**
  5377. * Creates the default learning path folder
  5378. * @param array $course
  5379. * @return bool
  5380. */
  5381. public static function generate_learning_path_folder($course) {
  5382. //Creating learning_path folder
  5383. $dir = '/learning_path';
  5384. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5385. $folder = false;
  5386. if (!is_dir($filepath.'/'.$dir)) {
  5387. $folderData = create_unexisting_directory(
  5388. $course,
  5389. api_get_user_id(),
  5390. api_get_session_id(),
  5391. 0,
  5392. 0,
  5393. $filepath,
  5394. $dir,
  5395. get_lang('LearningPaths'),
  5396. 0
  5397. );
  5398. if (!empty($folderData)) {
  5399. $folder = true;
  5400. }
  5401. } else {
  5402. $folder = true;
  5403. }
  5404. return $folder;
  5405. }
  5406. /**
  5407. * @param array $course
  5408. * @param string $lp_name
  5409. * @return array
  5410. */
  5411. public function generate_lp_folder($course, $lp_name = null)
  5412. {
  5413. $filepath = '';
  5414. $dir = '/learning_path/';
  5415. if (empty($lp_name)) {
  5416. $lp_name = $this->name;
  5417. }
  5418. $folder = self::generate_learning_path_folder($course);
  5419. // Creating LP folder
  5420. if ($folder) {
  5421. //Limits title size
  5422. $title = api_substr(replace_dangerous_char($lp_name), 0 , 80);
  5423. $dir = $dir.$title;
  5424. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5425. if (!is_dir($filepath.'/'.$dir)) {
  5426. $folderData = create_unexisting_directory(
  5427. $course,
  5428. api_get_user_id(),
  5429. 0,
  5430. 0,
  5431. 0,
  5432. $filepath,
  5433. $dir,
  5434. $lp_name
  5435. );
  5436. if (!empty($folderData)) {
  5437. $folder = true;
  5438. }
  5439. } else {
  5440. $folder = true;
  5441. }
  5442. $dir = $dir.'/';
  5443. if ($folder) {
  5444. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5445. }
  5446. }
  5447. $array = array(
  5448. 'dir' => $dir,
  5449. 'filepath' => $filepath,
  5450. 'folder' => $folder
  5451. );
  5452. return $array;
  5453. }
  5454. /**
  5455. * Create a new document //still needs some finetuning
  5456. * @param array $courseInfo
  5457. * @param string $content
  5458. * @param string $title
  5459. * @param string $extension
  5460. *
  5461. * @return string
  5462. */
  5463. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html')
  5464. {
  5465. if (!empty($courseInfo)) {
  5466. $course_id = $courseInfo['real_id'];
  5467. } else {
  5468. $course_id = api_get_course_int_id();
  5469. }
  5470. global $charset;
  5471. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5472. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5473. if (strstr($dir, '..'))
  5474. $dir = '/';
  5475. if ($dir[0] == '.')
  5476. $dir = substr($dir, 1);
  5477. if ($dir[0] != '/')
  5478. $dir = '/' . $dir;
  5479. if ($dir[strlen($dir) - 1] != '/')
  5480. $dir .= '/';
  5481. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document' . $dir;
  5482. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5483. //Generates folder
  5484. $result = $this->generate_lp_folder($courseInfo);
  5485. $dir = $result['dir'];
  5486. $filepath = $result['filepath'];
  5487. }
  5488. if (!is_dir($filepath)) {
  5489. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/';
  5490. $dir = '/';
  5491. }
  5492. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  5493. // is already escaped twice when it gets here.
  5494. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5495. if (!empty($title)) {
  5496. $title = replace_dangerous_char(stripslashes($title));
  5497. } else {
  5498. $title = replace_dangerous_char(stripslashes($_POST['title']));
  5499. }
  5500. $title = disable_dangerous_file($title);
  5501. $filename = $title;
  5502. $content = isset($content) ? $content : $_POST['content_lp'];
  5503. $tmp_filename = $filename;
  5504. $i = 0;
  5505. while (file_exists($filepath . $tmp_filename . '.'.$extension))
  5506. $tmp_filename = $filename . '_' . ++ $i;
  5507. $filename = $tmp_filename . '.'.$extension;
  5508. if ($extension == 'html') {
  5509. $content = stripslashes($content);
  5510. $content = str_replace(
  5511. api_get_path(WEB_COURSE_PATH),
  5512. api_get_path(REL_PATH).'courses/',
  5513. $content
  5514. );
  5515. // Change the path of mp3 to absolute.
  5516. // The first regexp deals with :// urls.
  5517. $content = preg_replace(
  5518. "|(flashvars=\"file=)([^:/]+)/|",
  5519. "$1".api_get_path(
  5520. REL_COURSE_PATH
  5521. ).$courseInfo['path'].'/document/',
  5522. $content
  5523. );
  5524. // The second regexp deals with audio/ urls.
  5525. $content = preg_replace(
  5526. "|(flashvars=\"file=)([^/]+)/|",
  5527. "$1".api_get_path(
  5528. REL_COURSE_PATH
  5529. ).$courseInfo['path'].'/document/$2/',
  5530. $content
  5531. );
  5532. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5533. $content = str_replace(
  5534. '</body>',
  5535. '<style type="text/css">body{}</style></body>',
  5536. $content
  5537. );
  5538. }
  5539. if (!file_exists($filepath . $filename)) {
  5540. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5541. fputs($fp, $content);
  5542. fclose($fp);
  5543. $file_size = filesize($filepath . $filename);
  5544. $save_file_path = $dir.$filename;
  5545. $document_id = add_document(
  5546. $courseInfo,
  5547. $save_file_path,
  5548. 'file',
  5549. $file_size,
  5550. $tmp_filename
  5551. );
  5552. if ($document_id) {
  5553. api_item_property_update(
  5554. $courseInfo,
  5555. TOOL_DOCUMENT,
  5556. $document_id,
  5557. 'DocumentAdded',
  5558. api_get_user_id(),
  5559. null,
  5560. null,
  5561. null,
  5562. null,
  5563. api_get_session_id()
  5564. );
  5565. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5566. $new_title = $originalTitle;
  5567. if ($new_comment || $new_title) {
  5568. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5569. $ct = '';
  5570. if ($new_comment)
  5571. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5572. if ($new_title)
  5573. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5574. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5575. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5576. Database::query($sql);
  5577. }
  5578. }
  5579. return $document_id;
  5580. }
  5581. }
  5582. }
  5583. /**
  5584. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5585. * @param array $_course array
  5586. * @return void
  5587. */
  5588. public function edit_document($_course)
  5589. {
  5590. $course_id = api_get_course_int_id();
  5591. global $_configuration;
  5592. // Please, do not modify this dirname formatting.
  5593. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5594. if (strstr($dir, '..'))
  5595. $dir = '/';
  5596. if ($dir[0] == '.')
  5597. $dir = substr($dir, 1);
  5598. if ($dir[0] != '/')
  5599. $dir = '/' . $dir;
  5600. if ($dir[strlen($dir) - 1] != '/')
  5601. $dir .= '/';
  5602. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5603. if (!is_dir($filepath)) {
  5604. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5605. $dir = '/';
  5606. }
  5607. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5608. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5609. $document_id = intval($_POST['path']);
  5610. $sql = "SELECT path FROM " . $table_doc . "
  5611. WHERE c_id = $course_id AND id = " . $document_id;
  5612. $res = Database::query($sql);
  5613. $row = Database :: fetch_array($res);
  5614. $content = stripslashes($_POST['content_lp']);
  5615. $file = $filepath . $row['path'];
  5616. if ($fp = @ fopen($file, 'w')) {
  5617. $content = text_filter($content);
  5618. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5619. // Change the path of mp3 to absolute.
  5620. // The first regexp deals with :// urls.
  5621. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5622. // The second regexp deals with audio/ urls.
  5623. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5624. fputs($fp, $content);
  5625. fclose($fp);
  5626. $sql = "UPDATE " . $table_doc ." SET
  5627. title='".Database::escape_string($_POST['title'])."'
  5628. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5629. Database::query($sql);
  5630. }
  5631. }
  5632. }
  5633. /**
  5634. * Displays the selected item, with a panel for manipulating the item
  5635. * @param int $item_id
  5636. * @param string $msg
  5637. * @return string
  5638. */
  5639. public function display_item($item_id, $msg = null, $show_actions = true)
  5640. {
  5641. $course_id = api_get_course_int_id();
  5642. $return = '';
  5643. if (is_numeric($item_id)) {
  5644. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5645. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5646. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5647. $result = Database::query($sql);
  5648. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5649. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5650. // Prevents wrong parent selection for document, see Bug#1251.
  5651. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5652. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5653. }
  5654. if ($show_actions) {
  5655. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5656. }
  5657. $return .= '<div style="padding:10px;">';
  5658. if ($msg != '')
  5659. $return .= $msg;
  5660. $return .= '<h3>'.$row['title'].'</h3>';
  5661. switch ($row['item_type']) {
  5662. case TOOL_QUIZ:
  5663. if (!empty($row['path'])) {
  5664. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5665. $exercise = new Exercise();
  5666. $exercise->read($row['path']);
  5667. $return .= $exercise->description.'<br />';
  5668. }
  5669. break;
  5670. case TOOL_DOCUMENT:
  5671. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5672. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5673. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5674. $result = Database::query($sql_doc);
  5675. $path_file = Database::result($result, 0, 0);
  5676. $path_parts = pathinfo($path_file);
  5677. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5678. if (in_array($path_parts['extension'], array(
  5679. 'html',
  5680. 'txt',
  5681. 'png',
  5682. 'jpg',
  5683. 'JPG',
  5684. 'jpeg',
  5685. 'JPEG',
  5686. 'gif',
  5687. 'swf'
  5688. ))) {
  5689. $return .= $this->display_document($row['path'], true, true);
  5690. }
  5691. break;
  5692. }
  5693. $return .= '</div>';
  5694. }
  5695. }
  5696. return $return;
  5697. }
  5698. /**
  5699. * Shows the needed forms for editing a specific item
  5700. * @param int $item_id
  5701. * @return string
  5702. */
  5703. public function display_edit_item($item_id)
  5704. {
  5705. $course_id = api_get_course_int_id();
  5706. $return = '';
  5707. if (is_numeric($item_id)) {
  5708. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5709. $sql = "SELECT * FROM $tbl_lp_item
  5710. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5711. $res = Database::query($sql);
  5712. $row = Database::fetch_array($res);
  5713. switch ($row['item_type']) {
  5714. case 'dokeos_chapter' :
  5715. case 'dir' :
  5716. case 'asset' :
  5717. case 'sco' :
  5718. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5719. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5720. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5721. } else {
  5722. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5723. }
  5724. break;
  5725. case TOOL_DOCUMENT :
  5726. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5727. $sql_step = " SELECT lp.*, doc.path as dir
  5728. FROM " . $tbl_lp_item . " as lp
  5729. LEFT JOIN " . $tbl_doc . " as doc ON doc.id = lp.path
  5730. WHERE
  5731. lp.c_id = $course_id AND
  5732. doc.c_id = $course_id AND
  5733. lp.id = " . intval($item_id);
  5734. $res_step = Database::query($sql_step);
  5735. $row_step = Database :: fetch_array($res_step);
  5736. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5737. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5738. break;
  5739. case TOOL_LINK :
  5740. $link_id = (string) $row['path'];
  5741. if (ctype_digit($link_id)) {
  5742. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5743. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5744. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5745. $res_link = Database::query($sql_select);
  5746. $row_link = Database :: fetch_array($res_link);
  5747. if (is_array($row_link)) {
  5748. $row['url'] = $row_link['url'];
  5749. }
  5750. }
  5751. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5752. $return .= $this->display_link_form('edit', $item_id, $row);
  5753. break;
  5754. case 'dokeos_module' :
  5755. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5756. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5757. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5758. } else {
  5759. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5760. }
  5761. break;
  5762. case TOOL_QUIZ :
  5763. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5764. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5765. break;
  5766. case TOOL_HOTPOTATOES :
  5767. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5768. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5769. break;
  5770. case TOOL_STUDENTPUBLICATION :
  5771. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5772. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5773. break;
  5774. case TOOL_FORUM :
  5775. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5776. $return .= $this->display_forum_form('edit', $item_id, $row);
  5777. break;
  5778. case TOOL_THREAD :
  5779. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5780. $return .= $this->display_thread_form('edit', $item_id, $row);
  5781. break;
  5782. }
  5783. }
  5784. return $return;
  5785. }
  5786. /**
  5787. * Function that displays a list with al the resources that
  5788. * could be added to the learning path
  5789. * @return string
  5790. */
  5791. public function display_resources()
  5792. {
  5793. $course_code = api_get_course_id();
  5794. // Get all the docs.
  5795. $documents = $this->get_documents(true);
  5796. // Get all the exercises.
  5797. $exercises = $this->get_exercises();
  5798. // Get all the links.
  5799. $links = $this->get_links();
  5800. // Get al the student publications.
  5801. $works = $this->get_student_publications();
  5802. // Get al the forums.
  5803. $forums = $this->get_forums(null, $course_code);
  5804. $headers = array(
  5805. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5806. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5807. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5808. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5809. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5810. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5811. );
  5812. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5813. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5814. echo Display::tabs(
  5815. $headers,
  5816. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5817. );
  5818. return true;
  5819. }
  5820. /**
  5821. * Returns the extension of a document
  5822. * @param string filename
  5823. * @return string Extension (part after the last dot)
  5824. */
  5825. public function get_extension($filename)
  5826. {
  5827. $explode = explode('.', $filename);
  5828. return $explode[count($explode) - 1];
  5829. }
  5830. /**
  5831. * Displays a document by id
  5832. *
  5833. * @param int $id
  5834. * @return string
  5835. */
  5836. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5837. {
  5838. global $_course; // It is temporary.
  5839. $course_id = api_get_course_int_id();
  5840. $return = '';
  5841. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5842. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5843. WHERE c_id = ".$course_id." AND id = " . $id;
  5844. $res_doc = Database::query($sql_doc);
  5845. $row_doc = Database :: fetch_array($res_doc);
  5846. // TODO: Add a path filter.
  5847. if ($iframe) {
  5848. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document' . str_replace('%2F', '/', urlencode($row_doc['path'])) . '?' . api_get_cidreq() . '"></iframe>';
  5849. } else {
  5850. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5851. }
  5852. return $return;
  5853. }
  5854. /**
  5855. * Return HTML form to add/edit a quiz
  5856. * @param string Action (add/edit)
  5857. * @param integer Item ID if already exists
  5858. * @param mixed Extra information (quiz ID if integer)
  5859. * @return string HTML form
  5860. */
  5861. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5862. {
  5863. $course_id = api_get_course_int_id();
  5864. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5865. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5866. if ($id != 0 && is_array($extra_info)) {
  5867. $item_title = $extra_info['title'];
  5868. $item_description = $extra_info['description'];
  5869. } elseif (is_numeric($extra_info)) {
  5870. $sql_quiz = "SELECT title, description FROM " . $tbl_quiz . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5871. $result = Database::query($sql_quiz);
  5872. $row = Database :: fetch_array($result);
  5873. $item_title = $row['title'];
  5874. $item_description = $row['description'];
  5875. } else {
  5876. $item_title = '';
  5877. $item_description = '';
  5878. }
  5879. $item_title = Security::remove_XSS($item_title);
  5880. $item_description = Security::remove_XSS($item_description);
  5881. $legend = '<legend>';
  5882. if ($id != 0 && is_array($extra_info))
  5883. $parent = $extra_info['parent_item_id'];
  5884. else
  5885. $parent = 0;
  5886. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5887. $result = Database::query($sql);
  5888. $arrLP = array ();
  5889. while ($row = Database :: fetch_array($result)) {
  5890. $arrLP[] = array (
  5891. 'id' => $row['id'],
  5892. 'item_type' => $row['item_type'],
  5893. 'title' => $row['title'],
  5894. 'path' => $row['path'],
  5895. 'description' => $row['description'],
  5896. 'parent_item_id' => $row['parent_item_id'],
  5897. 'previous_item_id' => $row['previous_item_id'],
  5898. 'next_item_id' => $row['next_item_id'],
  5899. 'display_order' => $row['display_order'],
  5900. 'max_score' => $row['max_score'],
  5901. 'min_score' => $row['min_score'],
  5902. 'mastery_score' => $row['mastery_score'],
  5903. 'prerequisite' => $row['prerequisite'],
  5904. 'max_time_allowed' => $row['max_time_allowed']
  5905. );
  5906. }
  5907. $this->tree_array($arrLP);
  5908. $arrLP = $this->arrMenu;
  5909. unset ($this->arrMenu);
  5910. if ($action == 'add') {
  5911. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5912. } elseif ($action == 'move') {
  5913. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5914. } else {
  5915. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5916. }
  5917. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5918. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5919. }
  5920. $legend .= '</legend>';
  5921. $return = '';
  5922. $return .= '<div class="sectioncomment">';
  5923. $return .= '<form method="POST">';
  5924. $return .= $legend;
  5925. $return .= '<table class="lp_form">';
  5926. if ($action != 'move') {
  5927. $return .= '<tr>';
  5928. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5929. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5930. $return .= '</tr>';
  5931. }
  5932. $return .= '<tr>';
  5933. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5934. $return .= '<td class="input">';
  5935. // Select for Parent item, root or chapter
  5936. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5937. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5938. $arrHide = array (
  5939. $id
  5940. );
  5941. //$parent_item_id = $_SESSION['parent_item_id'];
  5942. for ($i = 0; $i < count($arrLP); $i++) {
  5943. if ($action != 'add') {
  5944. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5945. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5946. } else {
  5947. $arrHide[] = $arrLP[$i]['id'];
  5948. }
  5949. } else {
  5950. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5951. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5952. }
  5953. }
  5954. if (is_array($arrLP)) {
  5955. reset($arrLP);
  5956. }
  5957. $return .= '</select>';
  5958. $return .= '</td>';
  5959. $return .= '</tr>';
  5960. $return .= '<tr>';
  5961. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5962. $return .= '<td class="input">';
  5963. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5964. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5965. for ($i = 0; $i < count($arrLP); $i++) {
  5966. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5967. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5968. $selected = 'selected="selected" ';
  5969. elseif ($action == 'add') $selected = 'selected="selected" ';
  5970. else
  5971. $selected = '';
  5972. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5973. }
  5974. }
  5975. $return .= '</select>';
  5976. $return .= '</td>';
  5977. $return .= '</tr>';
  5978. if ($action != 'move') {
  5979. $id_prerequisite = 0;
  5980. if (is_array($arrLP)) {
  5981. foreach ($arrLP as $key => $value) {
  5982. if ($value['id'] == $id) {
  5983. $id_prerequisite = $value['prerequisite'];
  5984. break;
  5985. }
  5986. }
  5987. }
  5988. $arrHide = array ();
  5989. for ($i = 0; $i < count($arrLP); $i++) {
  5990. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5991. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5992. $s_selected_position = $arrLP[$i]['id'];
  5993. elseif ($action == 'add') $s_selected_position = 0;
  5994. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5995. }
  5996. }
  5997. /*// Commented the prerequisites, only visible in edit (exercise).
  5998. $return .= '<tr>';
  5999. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6000. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6001. foreach($arrHide as $key => $value){
  6002. if($key==$s_selected_position && $action == 'add'){
  6003. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6004. }
  6005. elseif($key==$id_prerequisite && $action == 'edit'){
  6006. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6007. }
  6008. else{
  6009. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6010. }
  6011. }
  6012. $return .= "</select></td>";
  6013. */
  6014. $return .= '</tr>';
  6015. /*$return .= '<tr>';
  6016. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6017. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6018. // Remove temporarily the test description.
  6019. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6020. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6021. $return .= '</tr>'; */
  6022. }
  6023. $return .= '<tr>';
  6024. if ($action == 'add') {
  6025. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  6026. } else {
  6027. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  6028. }
  6029. $return .= '</tr>';
  6030. $return .= '</table>';
  6031. if ($action == 'move') {
  6032. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6033. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6034. }
  6035. if (is_numeric($extra_info)) {
  6036. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6037. } elseif (is_array($extra_info)) {
  6038. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6039. }
  6040. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  6041. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6042. $return .= '</form>';
  6043. $return .= '</div>';
  6044. return $return;
  6045. }
  6046. /**
  6047. * Addition of Hotpotatoes tests
  6048. * @param string Action
  6049. * @param integer Internal ID of the item
  6050. * @param mixed Extra information - can be an array with title and description indexes
  6051. * @return string HTML structure to display the hotpotatoes addition formular
  6052. */
  6053. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') {
  6054. $course_id = api_get_course_int_id();
  6055. global $charset;
  6056. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6057. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6058. if ($id != 0 && is_array($extra_info)) {
  6059. $item_title = stripslashes($extra_info['title']);
  6060. $item_description = stripslashes($extra_info['description']);
  6061. } elseif (is_numeric($extra_info)) {
  6062. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6063. $sql_hot = "SELECT * FROM " . $TBL_DOCUMENT . "
  6064. WHERE c_id = ".$course_id." AND
  6065. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6066. id = " . (int) $extra_info . "
  6067. ORDER BY id ASC";
  6068. $res_hot = Database::query($sql_hot);
  6069. $row = Database::fetch_array($res_hot);
  6070. $item_title = $row['title'];
  6071. $item_description = $row['description'];
  6072. if (!empty ($row['comment'])) {
  6073. $item_title = $row['comment'];
  6074. }
  6075. } else {
  6076. $item_title = '';
  6077. $item_description = '';
  6078. }
  6079. if ($id != 0 && is_array($extra_info))
  6080. $parent = $extra_info['parent_item_id'];
  6081. else
  6082. $parent = 0;
  6083. $sql = "SELECT * FROM $tbl_lp_item
  6084. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6085. $result = Database::query($sql);
  6086. $arrLP = array ();
  6087. while ($row = Database :: fetch_array($result)) {
  6088. $arrLP[] = array (
  6089. 'id' => $row['id'],
  6090. 'item_type' => $row['item_type'],
  6091. 'title' => $row['title'],
  6092. 'path' => $row['path'],
  6093. 'description' => $row['description'],
  6094. 'parent_item_id' => $row['parent_item_id'],
  6095. 'previous_item_id' => $row['previous_item_id'],
  6096. 'next_item_id' => $row['next_item_id'],
  6097. 'display_order' => $row['display_order'],
  6098. 'max_score' => $row['max_score'],
  6099. 'min_score' => $row['min_score'],
  6100. 'mastery_score' => $row['mastery_score'],
  6101. 'prerequisite' => $row['prerequisite'],
  6102. 'max_time_allowed' => $row['max_time_allowed']
  6103. );
  6104. }
  6105. $legend = '<legend>';
  6106. if ($action == 'add')
  6107. $legend .= get_lang('CreateTheExercise');
  6108. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6109. else
  6110. $legend .= get_lang('EditCurrentExecice');
  6111. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6112. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6113. }
  6114. $legend .= '</legend>';
  6115. $return = '<form method="POST">';
  6116. $return .= $legend;
  6117. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6118. $return .= '<tr>';
  6119. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6120. $return .= '<td class="input">';
  6121. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6122. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6123. $arrHide = array (
  6124. $id
  6125. );
  6126. if (count($arrLP) > 0) {
  6127. for ($i = 0; $i < count($arrLP); $i++) {
  6128. if ($action != 'add') {
  6129. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6130. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6131. } else {
  6132. $arrHide[] = $arrLP[$i]['id'];
  6133. }
  6134. } else {
  6135. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6136. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6137. }
  6138. }
  6139. reset($arrLP);
  6140. }
  6141. $return .= '</select>';
  6142. $return .= '</td>';
  6143. $return .= '</tr>';
  6144. $return .= '<tr>';
  6145. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6146. $return .= '<td class="input">';
  6147. $return .= '<select id="previous" name="previous" size="1">';
  6148. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6149. for ($i = 0; $i < count($arrLP); $i++) {
  6150. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6151. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6152. $selected = 'selected="selected" ';
  6153. elseif ($action == 'add') $selected = 'selected="selected" ';
  6154. else
  6155. $selected = '';
  6156. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6157. }
  6158. }
  6159. $return .= '</select>';
  6160. $return .= '</td>';
  6161. $return .= '</tr>';
  6162. if ($action != 'move') {
  6163. $return .= '<tr>';
  6164. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6165. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6166. $return .= '</tr>';
  6167. $id_prerequisite = 0;
  6168. if (is_array($arrLP) && count($arrLP) > 0) {
  6169. foreach ($arrLP as $key => $value) {
  6170. if ($value['id'] == $id) {
  6171. $id_prerequisite = $value['prerequisite'];
  6172. break;
  6173. }
  6174. }
  6175. $arrHide = array ();
  6176. for ($i = 0; $i < count($arrLP); $i++) {
  6177. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6178. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6179. $s_selected_position = $arrLP[$i]['id'];
  6180. elseif ($action == 'add') $s_selected_position = 0;
  6181. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6182. }
  6183. }
  6184. }
  6185. }
  6186. $return .= '<tr>';
  6187. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6188. $return .= '</tr>';
  6189. $return .= '</table>';
  6190. if ($action == 'move') {
  6191. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6192. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6193. }
  6194. if (is_numeric($extra_info)) {
  6195. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6196. } elseif (is_array($extra_info)) {
  6197. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6198. }
  6199. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6200. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6201. $return .= '</form>';
  6202. return $return;
  6203. }
  6204. /**
  6205. * Return the form to display the forum edit/add option
  6206. * @param string Action (add/edit)
  6207. * @param integer ID of the lp_item if already exists
  6208. * @param mixed Forum ID or title
  6209. * @return string HTML form
  6210. */
  6211. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6212. {
  6213. $course_id = api_get_course_int_id();
  6214. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6215. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6216. if ($id != 0 && is_array($extra_info)) {
  6217. $item_title = stripslashes($extra_info['title']);
  6218. } elseif (is_numeric($extra_info)) {
  6219. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6220. FROM " . $tbl_forum . "
  6221. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6222. $result = Database::query($sql_forum);
  6223. $row = Database :: fetch_array($result);
  6224. $item_title = $row['title'];
  6225. $item_description = $row['comment'];
  6226. } else {
  6227. $item_title = '';
  6228. $item_description = '';
  6229. }
  6230. $legend = '<legend>';
  6231. if ($id != 0 && is_array($extra_info))
  6232. $parent = $extra_info['parent_item_id'];
  6233. else
  6234. $parent = 0;
  6235. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6236. WHERE c_id = ".$course_id." AND
  6237. lp_id = " . $this->lp_id;
  6238. $result = Database::query($sql);
  6239. $arrLP = array();
  6240. while ($row = Database :: fetch_array($result)) {
  6241. $arrLP[] = array (
  6242. 'id' => $row['id'],
  6243. 'item_type' => $row['item_type'],
  6244. 'title' => $row['title'],
  6245. 'path' => $row['path'],
  6246. 'description' => $row['description'],
  6247. 'parent_item_id' => $row['parent_item_id'],
  6248. 'previous_item_id' => $row['previous_item_id'],
  6249. 'next_item_id' => $row['next_item_id'],
  6250. 'display_order' => $row['display_order'],
  6251. 'max_score' => $row['max_score'],
  6252. 'min_score' => $row['min_score'],
  6253. 'mastery_score' => $row['mastery_score'],
  6254. 'prerequisite' => $row['prerequisite']
  6255. );
  6256. }
  6257. $this->tree_array($arrLP);
  6258. $arrLP = $this->arrMenu;
  6259. unset ($this->arrMenu);
  6260. if ($action == 'add')
  6261. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6262. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6263. else
  6264. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6265. $legend .= '</legend>';
  6266. $return = '<div class="sectioncomment">';
  6267. $return .= '<form method="POST">';
  6268. $return .= $legend;
  6269. $return .= '<table class="lp_form">';
  6270. if ($action != 'move') {
  6271. $return .= '<tr>';
  6272. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6273. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6274. $return .= '</tr>';
  6275. }
  6276. $return .= '<tr>';
  6277. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6278. $return .= '<td class="input">';
  6279. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6280. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6281. $arrHide = array (
  6282. $id
  6283. );
  6284. //$parent_item_id = $_SESSION['parent_item_id'];
  6285. for ($i = 0; $i < count($arrLP); $i++) {
  6286. if ($action != 'add') {
  6287. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6288. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6289. } else {
  6290. $arrHide[] = $arrLP[$i]['id'];
  6291. }
  6292. } else {
  6293. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6294. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6295. }
  6296. }
  6297. if (is_array($arrLP)) {
  6298. reset($arrLP);
  6299. }
  6300. $return .= "\t\t\t\t" . '</select>';
  6301. $return .= '</td>';
  6302. $return .= '</tr>';
  6303. $return .= '<tr>';
  6304. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6305. $return .= '<td class="input">';
  6306. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6307. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6308. for ($i = 0; $i < count($arrLP); $i++) {
  6309. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6310. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6311. $selected = 'selected="selected" ';
  6312. elseif ($action == 'add') $selected = 'selected="selected" ';
  6313. else
  6314. $selected = '';
  6315. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6316. }
  6317. }
  6318. $return .= "\t\t\t\t" . '</select>';
  6319. $return .= '</td>';
  6320. $return .= '</tr>';
  6321. if ($action != 'move') {
  6322. $return .= '<tr>';
  6323. $return .= '</tr>';
  6324. $id_prerequisite = 0;
  6325. if (is_array($arrLP)) {
  6326. foreach ($arrLP as $key => $value) {
  6327. if ($value['id'] == $id) {
  6328. $id_prerequisite = $value['prerequisite'];
  6329. break;
  6330. }
  6331. }
  6332. }
  6333. $arrHide = array ();
  6334. for ($i = 0; $i < count($arrLP); $i++) {
  6335. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6336. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6337. $s_selected_position = $arrLP[$i]['id'];
  6338. elseif ($action == 'add') $s_selected_position = 0;
  6339. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6340. }
  6341. }
  6342. $return .= '</tr>';
  6343. }
  6344. $return .= '<tr>';
  6345. if ($action == 'add') {
  6346. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6347. } else {
  6348. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6349. }
  6350. $return .= '</tr>';
  6351. $return .= '</table>';
  6352. if ($action == 'move') {
  6353. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6354. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6355. }
  6356. if (is_numeric($extra_info)) {
  6357. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6358. }
  6359. elseif (is_array($extra_info)) {
  6360. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6361. }
  6362. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6363. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6364. $return .= '</form>';
  6365. $return .= '</div>';
  6366. return $return;
  6367. }
  6368. /**
  6369. * Return HTML form to add/edit forum threads
  6370. * @param string Action (add/edit)
  6371. * @param integer Item ID if already exists in learning path
  6372. * @param mixed Extra information (thread ID if integer)
  6373. * @return string HTML form
  6374. */
  6375. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6376. {
  6377. $course_id = api_get_course_int_id();
  6378. if (empty($course_id)) {
  6379. return null;
  6380. }
  6381. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6382. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6383. if ($id != 0 && is_array($extra_info)) {
  6384. $item_title = stripslashes($extra_info['title']);
  6385. } elseif (is_numeric($extra_info)) {
  6386. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6387. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6388. $result = Database::query($sql_forum);
  6389. $row = Database :: fetch_array($result);
  6390. $item_title = $row['title'];
  6391. $item_description = '';
  6392. } else {
  6393. $item_title = '';
  6394. $item_description = '';
  6395. }
  6396. $return = null;
  6397. if ($id != 0 && is_array($extra_info))
  6398. $parent = $extra_info['parent_item_id'];
  6399. else
  6400. $parent = 0;
  6401. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6402. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6403. $result = Database::query($sql);
  6404. $arrLP = array ();
  6405. while ($row = Database :: fetch_array($result)) {
  6406. $arrLP[] = array (
  6407. 'id' => $row['id'],
  6408. 'item_type' => $row['item_type'],
  6409. 'title' => $row['title'],
  6410. 'path' => $row['path'],
  6411. 'description' => $row['description'],
  6412. 'parent_item_id' => $row['parent_item_id'],
  6413. 'previous_item_id' => $row['previous_item_id'],
  6414. 'next_item_id' => $row['next_item_id'],
  6415. 'display_order' => $row['display_order'],
  6416. 'max_score' => $row['max_score'],
  6417. 'min_score' => $row['min_score'],
  6418. 'mastery_score' => $row['mastery_score'],
  6419. 'prerequisite' => $row['prerequisite']
  6420. );
  6421. }
  6422. $this->tree_array($arrLP);
  6423. $arrLP = $this->arrMenu;
  6424. unset ($this->arrMenu);
  6425. $return .= '<form method="POST">';
  6426. if ($action == 'add')
  6427. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6428. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6429. else
  6430. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6431. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6432. $return .= '<tr>';
  6433. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6434. $return .= '<td class="input">';
  6435. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6436. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6437. $arrHide = array (
  6438. $id
  6439. );
  6440. for ($i = 0; $i < count($arrLP); $i++) {
  6441. if ($action != 'add') {
  6442. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6443. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6444. } else {
  6445. $arrHide[] = $arrLP[$i]['id'];
  6446. }
  6447. } else {
  6448. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6449. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6450. }
  6451. }
  6452. if ($arrLP != null) {
  6453. reset($arrLP);
  6454. }
  6455. $return .= '</select>';
  6456. $return .= '</td>';
  6457. $return .= '</tr>';
  6458. $return .= '<tr>';
  6459. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6460. $return .= '<td class="input">';
  6461. $return .= '<select id="previous" name="previous" size="1">';
  6462. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6463. for ($i = 0; $i < count($arrLP); $i++) {
  6464. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6465. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6466. $selected = 'selected="selected" ';
  6467. elseif ($action == 'add') $selected = 'selected="selected" ';
  6468. else
  6469. $selected = '';
  6470. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6471. }
  6472. }
  6473. $return .= "\t\t\t\t" . '</select>';
  6474. $return .= '</td>';
  6475. $return .= '</tr>';
  6476. if ($action != 'move') {
  6477. $return .= '<tr>';
  6478. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6479. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6480. $return .= '</tr>';
  6481. $return .= '<tr>';
  6482. $return .= '</tr>';
  6483. $id_prerequisite = 0;
  6484. if ($arrLP != null) {
  6485. foreach ($arrLP as $key => $value) {
  6486. if ($value['id'] == $id) {
  6487. $id_prerequisite = $value['prerequisite'];
  6488. break;
  6489. }
  6490. }
  6491. }
  6492. $arrHide = array();
  6493. for ($i = 0; $i < count($arrLP); $i++) {
  6494. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6495. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6496. $s_selected_position = $arrLP[$i]['id'];
  6497. elseif ($action == 'add') $s_selected_position = 0;
  6498. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6499. }
  6500. }
  6501. $return .= '<tr>';
  6502. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6503. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6504. foreach ($arrHide as $key => $value) {
  6505. if ($key == $s_selected_position && $action == 'add') {
  6506. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6507. }
  6508. elseif ($key == $id_prerequisite && $action == 'edit') {
  6509. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6510. } else {
  6511. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6512. }
  6513. }
  6514. $return .= "</select></td>";
  6515. $return .= '</tr>';
  6516. }
  6517. $return .= '<tr>';
  6518. $return .= '<td></td><td>
  6519. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6520. $return .= '</tr>';
  6521. $return .= '</table>';
  6522. if ($action == 'move') {
  6523. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6524. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6525. }
  6526. if (is_numeric($extra_info)) {
  6527. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6528. }
  6529. elseif (is_array($extra_info)) {
  6530. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6531. }
  6532. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6533. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6534. $return .= '</form>';
  6535. $return .= '</div>';
  6536. return $return;
  6537. }
  6538. /**
  6539. * Return the HTML form to display an item (generally a section/module item)
  6540. * @param string Item type (module/dokeos_module)
  6541. * @param string Title (optional, only when creating)
  6542. * @param string Action ('add'/'edit')
  6543. * @param integer lp_item ID
  6544. * @param mixed Extra info
  6545. * @return string HTML form
  6546. */
  6547. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6548. {
  6549. $course_id = api_get_course_int_id();
  6550. global $_course;
  6551. global $charset;
  6552. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6553. if ($id != 0 && is_array($extra_info)) {
  6554. $item_title = $extra_info['title'];
  6555. $item_description = $extra_info['description'];
  6556. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6557. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6558. } else {
  6559. $item_title = '';
  6560. $item_description = '';
  6561. $item_path_fck = '';
  6562. }
  6563. if ($id != 0 && is_array($extra_info))
  6564. $parent = $extra_info['parent_item_id'];
  6565. else
  6566. $parent = 0;
  6567. $id = intval($id);
  6568. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6569. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND id != $id";
  6570. if ($item_type == 'module')
  6571. $sql .= " AND parent_item_id = 0";
  6572. $result = Database::query($sql);
  6573. $arrLP = array ();
  6574. while ($row = Database :: fetch_array($result)) {
  6575. $arrLP[] = array (
  6576. 'id' => $row['id'],
  6577. 'item_type' => $row['item_type'],
  6578. 'title' => $row['title'],
  6579. 'path' => $row['path'],
  6580. 'description' => $row['description'],
  6581. 'parent_item_id' => $row['parent_item_id'],
  6582. 'previous_item_id' => $row['previous_item_id'],
  6583. 'next_item_id' => $row['next_item_id'],
  6584. 'max_score' => $row['max_score'],
  6585. 'min_score' => $row['min_score'],
  6586. 'mastery_score' => $row['mastery_score'],
  6587. 'prerequisite' => $row['prerequisite'],
  6588. 'display_order' => $row['display_order']
  6589. );
  6590. }
  6591. $this->tree_array($arrLP);
  6592. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6593. unset ($this->arrMenu);
  6594. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6595. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6596. $form = new FormValidator('form', 'POST', $url);
  6597. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6598. $defaults['description'] = $item_description;
  6599. $form->addElement('header', $title);
  6600. //$arrHide = array($id);
  6601. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6602. $arrHide[0]['padding'] = 3;
  6603. $charset = api_get_system_encoding();
  6604. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6605. for ($i = 0; $i < count($arrLP); $i++) {
  6606. if ($action != 'add') {
  6607. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6608. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6609. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6610. if ($parent == $arrLP[$i]['id']) {
  6611. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6612. }
  6613. }
  6614. } else {
  6615. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6616. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6617. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6618. if ($parent == $arrLP[$i]['id']) {
  6619. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6620. }
  6621. }
  6622. }
  6623. }
  6624. if ($action != 'move') {
  6625. $form->addElement('text', 'title', get_lang('Title'), 'id="idTitle" class="learnpath_chapter_form" size="40%"');
  6626. $form->applyFilter('title', 'html_filter');
  6627. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6628. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6629. } else {
  6630. $form->addElement('hidden', 'title');
  6631. }
  6632. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"');
  6633. foreach ($arrHide as $key => $value) {
  6634. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6635. }
  6636. if (!empty($s_selected_parent)) {
  6637. $parent_select->setSelected($s_selected_parent);
  6638. }
  6639. }
  6640. if (is_array($arrLP)) {
  6641. reset($arrLP);
  6642. }
  6643. $arrHide = array();
  6644. // POSITION
  6645. for ($i = 0; $i < count($arrLP); $i++) {
  6646. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6647. //this is the same!
  6648. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6649. $s_selected_position = $arrLP[$i]['id'];
  6650. } elseif ($action == 'add') {
  6651. $s_selected_position = $arrLP[$i]['id'];
  6652. }
  6653. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6654. }
  6655. }
  6656. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_chapter_form" style="width:37%;"');
  6657. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6658. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6659. foreach ($arrHide as $key => $value) {
  6660. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6661. }
  6662. if (!empty ($s_selected_position)) {
  6663. $position->setSelected($s_selected_position);
  6664. }
  6665. if (is_array($arrLP)) {
  6666. reset($arrLP);
  6667. }
  6668. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6669. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6670. $form->addElement('hidden', 'parent', '0');
  6671. }
  6672. //fix in order to use the tab
  6673. if ($item_type == 'chapter') {
  6674. $form->addElement('hidden', 'type', 'chapter');
  6675. }
  6676. $extension = null;
  6677. if (!empty($item_path)) {
  6678. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6679. }
  6680. //assets can't be modified
  6681. //$item_type == 'asset' ||
  6682. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6683. if ($item_type == 'sco') {
  6684. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6685. }
  6686. $renderer = $form->defaultRenderer();
  6687. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6688. $relative_prefix = '';
  6689. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6690. 'Width' => '100%',
  6691. 'Height' => '500',
  6692. 'FullPage' => true,
  6693. 'CreateDocumentDir' => $relative_prefix,
  6694. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6695. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6696. );
  6697. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6698. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6699. //$defaults['content_lp'] = file_get_contents($item_path);
  6700. $defaults['content_lp'] = file_get_contents($content_path);
  6701. }
  6702. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6703. $form->addElement('hidden', 'post_time', time());
  6704. $form->setDefaults($defaults);
  6705. return $form->return_form();
  6706. }
  6707. /**
  6708. * Returns the form to update or create a document
  6709. * @param string Action (add/edit)
  6710. * @param integer ID of the lp_item (if already exists)
  6711. * @param mixed Integer if document ID, string if info ('new')
  6712. * @return string HTML form
  6713. */
  6714. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6715. {
  6716. $course_id = api_get_course_int_id();
  6717. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6718. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6719. $no_display_edit_textarea = false;
  6720. //If action==edit document
  6721. //We don't display the document form if it's not an editable document (html or txt file)
  6722. if ($action == "edit") {
  6723. if (is_array($extra_info)) {
  6724. $path_parts = pathinfo($extra_info['dir']);
  6725. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6726. $no_display_edit_textarea = true;
  6727. }
  6728. }
  6729. }
  6730. $no_display_add = false;
  6731. // If action==add an existing document
  6732. // We don't display the document form if it's not an editable document (html or txt file).
  6733. if ($action == "add") {
  6734. if (is_numeric($extra_info)) {
  6735. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6736. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6737. $result = Database::query($sql_doc);
  6738. $path_file = Database :: result($result, 0, 0);
  6739. $path_parts = pathinfo($path_file);
  6740. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6741. $no_display_add = true;
  6742. }
  6743. }
  6744. }
  6745. if ($id != 0 && is_array($extra_info)) {
  6746. $item_title = stripslashes($extra_info['title']);
  6747. $item_description = stripslashes($extra_info['description']);
  6748. $item_terms = stripslashes($extra_info['terms']);
  6749. if (empty ($item_title)) {
  6750. $path_parts = pathinfo($extra_info['path']);
  6751. $item_title = stripslashes($path_parts['filename']);
  6752. }
  6753. } elseif (is_numeric($extra_info)) {
  6754. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6755. WHERE
  6756. c_id = ".$course_id." AND
  6757. id = " . intval($extra_info);
  6758. $result = Database::query($sql_doc);
  6759. $row = Database::fetch_array($result);
  6760. $item_title = $row['title'];
  6761. $item_title = str_replace('_', ' ', $item_title);
  6762. if (empty ($item_title)) {
  6763. $path_parts = pathinfo($row['path']);
  6764. $item_title = stripslashes($path_parts['filename']);
  6765. }
  6766. } else {
  6767. $item_title = '';
  6768. $item_description = '';
  6769. }
  6770. $return = '<legend>';
  6771. if ($id != 0 && is_array($extra_info))
  6772. $parent = $extra_info['parent_item_id'];
  6773. else
  6774. $parent = 0;
  6775. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6776. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6777. $result = Database::query($sql);
  6778. $arrLP = array ();
  6779. while ($row = Database :: fetch_array($result)) {
  6780. $arrLP[] = array (
  6781. 'id' => $row['id'],
  6782. 'item_type' => $row['item_type'],
  6783. 'title' => $row['title'],
  6784. 'path' => $row['path'],
  6785. 'description' => $row['description'],
  6786. 'parent_item_id' => $row['parent_item_id'],
  6787. 'previous_item_id' => $row['previous_item_id'],
  6788. 'next_item_id' => $row['next_item_id'],
  6789. 'display_order' => $row['display_order'],
  6790. 'max_score' => $row['max_score'],
  6791. 'min_score' => $row['min_score'],
  6792. 'mastery_score' => $row['mastery_score'],
  6793. 'prerequisite' => $row['prerequisite']
  6794. );
  6795. }
  6796. $this->tree_array($arrLP);
  6797. $arrLP = $this->arrMenu;
  6798. unset ($this->arrMenu);
  6799. if ($action == 'add') {
  6800. $return .= get_lang('CreateTheDocument');
  6801. } elseif ($action == 'move') {
  6802. $return .= get_lang('MoveTheCurrentDocument');
  6803. } else {
  6804. $return .= get_lang('EditTheCurrentDocument');
  6805. }
  6806. $return .= '</legend>';
  6807. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6808. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6809. }
  6810. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6811. $defaults['title'] = Security :: remove_XSS($item_title);
  6812. if (empty($item_title)) {
  6813. $defaults['title'] = Security::remove_XSS($item_title);
  6814. }
  6815. $defaults['description'] = $item_description;
  6816. $form->addElement('html', $return);
  6817. if ($action != 'move') {
  6818. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6819. $form->applyFilter('title', 'html_filter');
  6820. }
  6821. $arrHide[0]['value'] = $this->name;
  6822. $arrHide[0]['padding'] = 3;
  6823. for ($i = 0; $i < count($arrLP); $i++) {
  6824. if ($action != 'add') {
  6825. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6826. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6827. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6828. if ($parent == $arrLP[$i]['id']) {
  6829. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6830. }
  6831. }
  6832. } else {
  6833. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6834. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6835. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6836. if ($parent == $arrLP[$i]['id']) {
  6837. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6838. }
  6839. }
  6840. }
  6841. }
  6842. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6843. $my_count=0;
  6844. foreach ($arrHide as $key => $value) {
  6845. if ($my_count!=0) {
  6846. // The LP name is also the first section and is not in the same charset like the other sections.
  6847. $value['value'] = Security :: remove_XSS($value['value']);
  6848. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6849. } else {
  6850. $value['value'] = Security :: remove_XSS($value['value']);
  6851. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6852. }
  6853. $my_count++;
  6854. }
  6855. if (!empty($id)) {
  6856. $parent_select->setSelected($parent);
  6857. } else {
  6858. $parent_item_id = $_SESSION['parent_item_id'];
  6859. $parent_select->setSelected($parent_item_id);
  6860. }
  6861. if (is_array($arrLP)) {
  6862. reset($arrLP);
  6863. }
  6864. $arrHide = array ();
  6865. //POSITION
  6866. for ($i = 0; $i < count($arrLP); $i++) {
  6867. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6868. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6869. $s_selected_position = $arrLP[$i]['id'];
  6870. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6871. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6872. }
  6873. }
  6874. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6875. $position->addOption(get_lang('FirstPosition'), 0);
  6876. foreach ($arrHide as $key => $value) {
  6877. $padding = isset($value['padding']) ? $value['padding']: 0;
  6878. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6879. }
  6880. $position->setSelected($s_selected_position);
  6881. if (is_array($arrLP)) {
  6882. reset($arrLP);
  6883. }
  6884. if ($action != 'move') {
  6885. $id_prerequisite = 0;
  6886. if (is_array($arrLP)) {
  6887. foreach ($arrLP as $key => $value) {
  6888. if ($value['id'] == $id) {
  6889. $id_prerequisite = $value['prerequisite'];
  6890. break;
  6891. }
  6892. }
  6893. }
  6894. $arrHide = array ();
  6895. for ($i = 0; $i < count($arrLP); $i++) {
  6896. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6897. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6898. $s_selected_position = $arrLP[$i]['id'];
  6899. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6900. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6901. }
  6902. }
  6903. if (!$no_display_add) {
  6904. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6905. if (isset ($_POST['content']))
  6906. $content = stripslashes($_POST['content']);
  6907. elseif (is_array($extra_info)) {
  6908. //If it's an html document or a text file
  6909. if (!$no_display_edit_textarea) {
  6910. $content = $this->display_document($extra_info['path'], false, false);
  6911. }
  6912. } elseif (is_numeric($extra_info))
  6913. $content = $this->display_document($extra_info, false, false);
  6914. else
  6915. $content = '';
  6916. if (!$no_display_edit_textarea) {
  6917. // We need to calculate here some specific settings for the online editor.
  6918. // The calculated settings work for documents in the Documents tool
  6919. // (on the root or in subfolders).
  6920. // For documents in native scorm packages it is unclear whether the
  6921. // online editor should be activated or not.
  6922. // A new document, it is in the root of the repository.
  6923. $relative_path = '';
  6924. $relative_prefix = '';
  6925. if (is_array($extra_info) && $extra_info != 'new') {
  6926. // The document already exists. Whe have to determine its relative path towards the repository root.
  6927. $relative_path = explode('/', $extra_info['dir']);
  6928. $cnt = count($relative_path) - 2;
  6929. if ($cnt < 0) {
  6930. $cnt = 0;
  6931. }
  6932. $relative_prefix = str_repeat('../', $cnt);
  6933. $relative_path = array_slice($relative_path, 1, $cnt);
  6934. $relative_path = implode('/', $relative_path);
  6935. if (strlen($relative_path) > 0) {
  6936. $relative_path = $relative_path . '/';
  6937. }
  6938. } else {
  6939. global $_course;
  6940. $result = $this->generate_lp_folder($_course);
  6941. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6942. $relative_prefix = '../../';
  6943. }
  6944. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6945. 'Width' => '100%',
  6946. 'Height' => '500',
  6947. 'FullPage' => true,
  6948. 'CreateDocumentDir' => $relative_prefix,
  6949. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6950. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6951. );
  6952. if ($_GET['action'] == 'add_item') {
  6953. $class = 'add';
  6954. $text = get_lang('LPCreateDocument');
  6955. } else
  6956. if ($_GET['action'] == 'edit_item') {
  6957. $class = 'save';
  6958. $text = get_lang('SaveDocument');
  6959. }
  6960. $form->addElement('style_submit_button', 'submit_button', $text, 'class="' . $class . '"');
  6961. $renderer = $form->defaultRenderer();
  6962. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6963. $form->addElement('html', '<div>');
  6964. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6965. $form->addElement('html', '</div>');
  6966. $defaults['content_lp'] = $content;
  6967. }
  6968. } elseif (is_numeric($extra_info)) {
  6969. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6970. $return = $this->display_document($extra_info, true, true, true);
  6971. $form->addElement('html', $return);
  6972. }
  6973. }
  6974. }
  6975. if ($action == 'move') {
  6976. $form->addElement('hidden', 'title', $item_title);
  6977. $form->addElement('hidden', 'description', $item_description);
  6978. }
  6979. if (is_numeric($extra_info)) {
  6980. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'value="submit_button", class="save"');
  6981. $form->addElement('hidden', 'path', $extra_info);
  6982. } elseif (is_array($extra_info)) {
  6983. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6984. $form->addElement('hidden', 'path', $extra_info['path']);
  6985. }
  6986. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6987. $form->addElement('hidden', 'post_time', time());
  6988. $form->setDefaults($defaults);
  6989. return $form->return_form();
  6990. }
  6991. /**
  6992. * Return HTML form to add/edit a link item
  6993. * @param string $action (add/edit)
  6994. * @param integer $id Item ID if exists
  6995. * @param mixed $extra_info
  6996. * @return string HTML form
  6997. */
  6998. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6999. {
  7000. $course_id = api_get_course_int_id();
  7001. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7002. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7003. if ($id != 0 && is_array($extra_info)) {
  7004. $item_title = stripslashes($extra_info['title']);
  7005. $item_description = stripslashes($extra_info['description']);
  7006. $item_url = stripslashes($extra_info['url']);
  7007. } elseif (is_numeric($extra_info)) {
  7008. $extra_info = intval($extra_info);
  7009. $sql_link = "SELECT title, description, url FROM " . $tbl_link . "
  7010. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7011. $result = Database::query($sql_link);
  7012. $row = Database :: fetch_array($result);
  7013. $item_title = $row['title'];
  7014. $item_description = $row['description'];
  7015. $item_url = $row['url'];
  7016. } else {
  7017. $item_title = '';
  7018. $item_description = '';
  7019. $item_url = '';
  7020. }
  7021. $legend = '<legend>';
  7022. if ($id != 0 && is_array($extra_info)) {
  7023. $parent = $extra_info['parent_item_id'];
  7024. } else {
  7025. $parent = 0;
  7026. }
  7027. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7028. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7029. $result = Database::query($sql);
  7030. $arrLP = array ();
  7031. while ($row = Database :: fetch_array($result)) {
  7032. $arrLP[] = array (
  7033. 'id' => $row['id'],
  7034. 'item_type' => $row['item_type'],
  7035. 'title' => $row['title'],
  7036. 'path' => $row['path'],
  7037. 'description' => $row['description'],
  7038. 'parent_item_id' => $row['parent_item_id'],
  7039. 'previous_item_id' => $row['previous_item_id'],
  7040. 'next_item_id' => $row['next_item_id'],
  7041. 'display_order' => $row['display_order'],
  7042. 'max_score' => $row['max_score'],
  7043. 'min_score' => $row['min_score'],
  7044. 'mastery_score' => $row['mastery_score'],
  7045. 'prerequisite' => $row['prerequisite']
  7046. );
  7047. }
  7048. $this->tree_array($arrLP);
  7049. $arrLP = $this->arrMenu;
  7050. unset ($this->arrMenu);
  7051. if ($action == 'add')
  7052. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  7053. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  7054. else
  7055. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7056. $legend .= '</legend>';
  7057. $return = '<div class="sectioncomment">';
  7058. $return .= '<form method="POST">';
  7059. $return .= $legend;
  7060. $return .= '<table>';
  7061. if ($action != 'move') {
  7062. $return .= '<tr>';
  7063. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7064. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7065. $return .= '</tr>';
  7066. }
  7067. $return .= '<tr>';
  7068. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7069. $return .= '<td class="input">';
  7070. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7071. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7072. $arrHide = array (
  7073. $id
  7074. );
  7075. $parent_item_id = $_SESSION['parent_item_id'];
  7076. for ($i = 0; $i < count($arrLP); $i++) {
  7077. if ($action != 'add') {
  7078. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7079. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7080. } else {
  7081. $arrHide[] = $arrLP[$i]['id'];
  7082. }
  7083. } else {
  7084. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7085. $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>';
  7086. }
  7087. }
  7088. if (is_array($arrLP)) {
  7089. reset($arrLP);
  7090. }
  7091. $return .= '</select>';
  7092. $return .= '</td>';
  7093. $return .= '</tr>';
  7094. $return .= '<tr>';
  7095. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7096. $return .= '<td class="input">';
  7097. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7098. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7099. for ($i = 0; $i < count($arrLP); $i++) {
  7100. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7101. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7102. $selected = 'selected="selected" ';
  7103. elseif ($action == 'add')
  7104. $selected = 'selected="selected" ';
  7105. else
  7106. $selected = '';
  7107. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7108. }
  7109. }
  7110. $return .= '</select>';
  7111. $return .= '</td>';
  7112. $return .= '</tr>';
  7113. if ($action != 'move') {
  7114. $return .= '<tr>';
  7115. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7116. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  7117. $return .= '</tr>';
  7118. $id_prerequisite = 0;
  7119. if (is_array($arrLP)) {
  7120. foreach ($arrLP as $key => $value) {
  7121. if ($value['id'] == $id) {
  7122. $id_prerequisite = $value['prerequisite'];
  7123. break;
  7124. }
  7125. }
  7126. }
  7127. $arrHide = array();
  7128. for ($i = 0; $i < count($arrLP); $i++) {
  7129. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7130. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7131. $s_selected_position = $arrLP[$i]['id'];
  7132. elseif ($action == 'add') $s_selected_position = 0;
  7133. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7134. }
  7135. }
  7136. $return .= '</tr>';
  7137. }
  7138. $return .= '<tr>';
  7139. if ($action == 'add') {
  7140. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7141. } else {
  7142. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7143. }
  7144. $return .= '</tr>';
  7145. $return .= '</table>';
  7146. if ($action == 'move') {
  7147. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7148. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7149. }
  7150. if (is_numeric($extra_info)) {
  7151. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7152. } elseif (is_array($extra_info)) {
  7153. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7154. }
  7155. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7156. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7157. $return .= '</form>';
  7158. $return .= '</div>';
  7159. return $return;
  7160. }
  7161. /**
  7162. * Return HTML form to add/edit a student publication (work)
  7163. * @param string Action (add/edit)
  7164. * @param integer Item ID if already exists
  7165. * @param mixed Extra info (work ID if integer)
  7166. * @return string HTML form
  7167. */
  7168. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7169. {
  7170. $course_id = api_get_course_int_id();
  7171. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7172. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7173. if ($id != 0 && is_array($extra_info)) {
  7174. $item_title = stripslashes($extra_info['title']);
  7175. $item_description = stripslashes($extra_info['description']);
  7176. } elseif (is_numeric($extra_info)) {
  7177. $extra_info = intval($extra_info);
  7178. $sql_publication = "SELECT title, description FROM " . $tbl_publication . "
  7179. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7180. $result = Database::query($sql_publication);
  7181. $row = Database :: fetch_array($result);
  7182. $item_title = $row['title'];
  7183. } else {
  7184. $item_title = get_lang('Student_publication');
  7185. }
  7186. $legend = '<legend>';
  7187. if ($id != 0 && is_array($extra_info))
  7188. $parent = $extra_info['parent_item_id'];
  7189. else
  7190. $parent = 0;
  7191. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7192. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7193. $result = Database::query($sql);
  7194. $arrLP = array();
  7195. while ($row = Database :: fetch_array($result)) {
  7196. $arrLP[] = array (
  7197. 'id' => $row['id'],
  7198. 'item_type' => $row['item_type'],
  7199. 'title' => $row['title'],
  7200. 'path' => $row['path'],
  7201. 'description' => $row['description'],
  7202. 'parent_item_id' => $row['parent_item_id'],
  7203. 'previous_item_id' => $row['previous_item_id'],
  7204. 'next_item_id' => $row['next_item_id'],
  7205. 'display_order' => $row['display_order'],
  7206. 'max_score' => $row['max_score'],
  7207. 'min_score' => $row['min_score'],
  7208. 'mastery_score' => $row['mastery_score'],
  7209. 'prerequisite' => $row['prerequisite']
  7210. );
  7211. }
  7212. $this->tree_array($arrLP);
  7213. $arrLP = $this->arrMenu;
  7214. unset ($this->arrMenu);
  7215. if ($action == 'add') {
  7216. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7217. } elseif ($action == 'move') {
  7218. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7219. } else {
  7220. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7221. }
  7222. $legend .= '</legend>';
  7223. $return = '<div class="sectioncomment">';
  7224. $return .= '<form method="POST">';
  7225. $return .= $legend;
  7226. $return .= '<table class="lp_form">';
  7227. if ($action != 'move') {
  7228. $return .= '<tr>';
  7229. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7230. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7231. $return .= '</tr>';
  7232. }
  7233. $return .= '<tr>';
  7234. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7235. $return .= '<td class="input">';
  7236. $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">';
  7237. //$parent_item_id = $_SESSION['parent_item_id'];
  7238. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7239. $arrHide = array (
  7240. $id
  7241. );
  7242. for ($i = 0; $i < count($arrLP); $i++) {
  7243. if ($action != 'add') {
  7244. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7245. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7246. } else {
  7247. $arrHide[] = $arrLP[$i]['id'];
  7248. }
  7249. } else {
  7250. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7251. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7252. }
  7253. }
  7254. if (is_array($arrLP)) {
  7255. reset($arrLP);
  7256. }
  7257. $return .= "\t\t\t\t" . '</select>';
  7258. $return .= '</td>';
  7259. $return .= '</tr>';
  7260. $return .= '<tr>';
  7261. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7262. $return .= '<td class="input">';
  7263. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7264. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7265. for ($i = 0; $i < count($arrLP); $i++) {
  7266. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7267. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7268. $selected = 'selected="selected" ';
  7269. elseif ($action == 'add') $selected = 'selected="selected" ';
  7270. else
  7271. $selected = '';
  7272. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7273. }
  7274. }
  7275. $return .= "\t\t\t\t" . '</select>';
  7276. $return .= '</td>';
  7277. $return .= '</tr>';
  7278. if ($action != 'move') {
  7279. $id_prerequisite = 0;
  7280. if (is_array($arrLP)) {
  7281. foreach ($arrLP as $key => $value) {
  7282. if ($value['id'] == $id) {
  7283. $id_prerequisite = $value['prerequisite'];
  7284. break;
  7285. }
  7286. }
  7287. }
  7288. $arrHide = array ();
  7289. for ($i = 0; $i < count($arrLP); $i++) {
  7290. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7291. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7292. $s_selected_position = $arrLP[$i]['id'];
  7293. elseif ($action == 'add') $s_selected_position = 0;
  7294. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7295. }
  7296. }
  7297. // Commented the prerequisites, only visible in edit (work).
  7298. /*
  7299. $return .= '<tr>';
  7300. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7301. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7302. foreach($arrHide as $key => $value) {
  7303. if ($key == $s_selected_position && $action == 'add') {
  7304. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7305. }
  7306. elseif ($key == $id_prerequisite && $action == 'edit') {
  7307. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7308. }
  7309. else {
  7310. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7311. }
  7312. }
  7313. $return .= "</select></td>";
  7314. */
  7315. $return .= '</tr>';
  7316. }
  7317. $return .= '<tr>';
  7318. if ($action == 'add') {
  7319. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7320. } else {
  7321. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7322. }
  7323. $return .= '</tr>';
  7324. $return .= '</table>';
  7325. if ($action == 'move') {
  7326. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7327. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7328. }
  7329. if (is_numeric($extra_info)) {
  7330. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7331. } elseif (is_array($extra_info)) {
  7332. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7333. }
  7334. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7335. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7336. $return .= '</form>';
  7337. $return .= '</div>';
  7338. return $return;
  7339. }
  7340. /**
  7341. * Displays the menu for manipulating a step
  7342. *
  7343. * @param $item_id
  7344. * @param string $item_type
  7345. * @return string
  7346. */
  7347. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7348. {
  7349. $_course = api_get_course_info();
  7350. $course_id = api_get_course_int_id();
  7351. $course_code = api_get_course_id();
  7352. $return = '<div class="actions">';
  7353. switch ($item_type) {
  7354. case 'dokeos_chapter' :
  7355. case 'chapter' :
  7356. // Commented the message cause should not show it.
  7357. //$lang = get_lang('TitleManipulateChapter');
  7358. break;
  7359. case 'dokeos_module' :
  7360. case 'module' :
  7361. // Commented the message cause should not show it.
  7362. //$lang = get_lang('TitleManipulateModule');
  7363. break;
  7364. case TOOL_DOCUMENT :
  7365. // Commented the message cause should not show it.
  7366. //$lang = get_lang('TitleManipulateDocument');
  7367. break;
  7368. case TOOL_LINK :
  7369. case 'link' :
  7370. // Commented the message cause should not show it.
  7371. //$lang = get_lang('TitleManipulateLink');
  7372. break;
  7373. case TOOL_QUIZ :
  7374. // Commented the message cause should not show it.
  7375. //$lang = get_lang('TitleManipulateQuiz');
  7376. break;
  7377. case TOOL_STUDENTPUBLICATION :
  7378. // Commented the message cause should not show it.
  7379. //$lang = get_lang('TitleManipulateStudentPublication');
  7380. break;
  7381. }
  7382. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7383. $item_id = intval($item_id);
  7384. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7385. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7386. $result = Database::query($sql);
  7387. $row = Database::fetch_assoc($result);
  7388. $audio_player = null;
  7389. // We display an audio player if needed.
  7390. if (!empty($row['audio'])) {
  7391. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7392. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7393. </div>';
  7394. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7395. $audio_player .= '<script>
  7396. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7397. s1.addParam("allowscriptaccess","always");
  7398. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7399. s1.write("container");
  7400. </script>';
  7401. }
  7402. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7403. $return .= Display::url(
  7404. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7405. $url.'&action=edit_item&path_item=' . $row['path']
  7406. );
  7407. $return .= Display::url(
  7408. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7409. $url.'&action=move_item'
  7410. );
  7411. // Commented for now as prerequisites cannot be added to chapters.
  7412. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7413. $return .= Display::url(
  7414. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7415. $url.'&action=edit_item_prereq'
  7416. );
  7417. }
  7418. $return .= Display::url(
  7419. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7420. $url.'&action=delete_item'
  7421. );
  7422. if ($item_type == TOOL_HOTPOTATOES ) {
  7423. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7424. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7425. }
  7426. if ($item_type == TOOL_DOCUMENT ) {
  7427. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7428. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7429. }
  7430. $return .= '</div>';
  7431. if (!empty($audio_player)) {
  7432. $return .= '<br />'.$audio_player;
  7433. }
  7434. return $return;
  7435. }
  7436. /**
  7437. * Creates the javascript needed for filling up the checkboxes without page reload
  7438. * @return string
  7439. */
  7440. public function get_js_dropdown_array()
  7441. {
  7442. $course_id = api_get_course_int_id();
  7443. $return = 'var child_name = new Array();' . "\n";
  7444. $return .= 'var child_value = new Array();' . "\n\n";
  7445. $return .= 'child_name[0] = new Array();' . "\n";
  7446. $return .= 'child_value[0] = new Array();' . "\n\n";
  7447. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7448. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7449. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7450. ORDER BY display_order ASC";
  7451. $res_zero = Database::query($sql_zero);
  7452. $i = 0;
  7453. while ($row_zero = Database :: fetch_array($res_zero)) {
  7454. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7455. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7456. }
  7457. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7458. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7459. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7460. }
  7461. $return .= "\n";
  7462. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7463. $res = Database::query($sql);
  7464. while ($row = Database :: fetch_array($res)) {
  7465. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7466. WHERE
  7467. c_id = ".$course_id." AND
  7468. parent_item_id = " . $row['id'] . "
  7469. ORDER BY display_order ASC";
  7470. $res_parent = Database::query($sql_parent);
  7471. $i = 0;
  7472. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7473. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7474. while ($row_parent = Database :: fetch_array($res_parent)) {
  7475. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7476. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7477. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7478. }
  7479. $return .= "\n";
  7480. }
  7481. return $return;
  7482. }
  7483. /**
  7484. * Display the form to allow moving an item
  7485. * @param integer Item ID
  7486. * @return string HTML form
  7487. */
  7488. public function display_move_item($item_id)
  7489. {
  7490. $course_id = api_get_course_int_id();
  7491. global $_course; //will disappear
  7492. global $charset;
  7493. $return = '';
  7494. if (is_numeric($item_id)) {
  7495. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7496. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7497. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7498. $res = Database::query($sql);
  7499. $row = Database :: fetch_array($res);
  7500. switch ($row['item_type']) {
  7501. case 'dokeos_chapter' :
  7502. case 'dir' :
  7503. case 'asset' :
  7504. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7505. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7506. break;
  7507. case 'dokeos_module' :
  7508. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7509. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7510. break;
  7511. case TOOL_DOCUMENT :
  7512. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7513. $return .= $this->display_document_form('move', $item_id, $row);
  7514. break;
  7515. case TOOL_LINK :
  7516. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7517. $return .= $this->display_link_form('move', $item_id, $row);
  7518. break;
  7519. case TOOL_HOTPOTATOES :
  7520. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7521. $return .= $this->display_link_form('move', $item_id, $row);
  7522. break;
  7523. case TOOL_QUIZ :
  7524. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7525. $return .= $this->display_quiz_form('move', $item_id, $row);
  7526. break;
  7527. case TOOL_STUDENTPUBLICATION :
  7528. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7529. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7530. break;
  7531. case TOOL_FORUM :
  7532. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7533. $return .= $this->display_forum_form('move', $item_id, $row);
  7534. break;
  7535. case TOOL_THREAD :
  7536. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7537. $return .= $this->display_forum_form('move', $item_id, $row);
  7538. break;
  7539. }
  7540. }
  7541. return $return;
  7542. }
  7543. /**
  7544. * Displays a basic form on the overview page for changing the item title and the item description.
  7545. * @param string $item_type
  7546. * @param string $title
  7547. * @param array $data
  7548. * @return string
  7549. */
  7550. public function display_item_small_form($item_type, $title = '', $data = array())
  7551. {
  7552. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7553. $form = new FormValidator('small_form', 'post', $url);
  7554. $form->addElement('header', $title);
  7555. $form->addElement('text', 'title', get_lang('Title'));
  7556. $form->addElement('button', 'submit_button', get_lang('Save'));
  7557. $form->addElement('hidden', 'id', $data['id']);
  7558. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7559. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7560. $form->setDefaults(array('title' => $data['title']));
  7561. return $form->toHtml();
  7562. }
  7563. /**
  7564. * Return HTML form to allow prerequisites selection
  7565. * @param integer Item ID
  7566. * @return string HTML form
  7567. */
  7568. public function display_item_prerequisites_form($item_id)
  7569. {
  7570. $course_id = api_get_course_int_id();
  7571. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7572. $item_id = intval($item_id);
  7573. /* Current prerequisite */
  7574. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = " . $item_id;
  7575. $result = Database::query($sql);
  7576. $row = Database::fetch_array($result);
  7577. $preq_id = $row['prerequisite'];
  7578. $return = '<legend>';
  7579. $return .= get_lang('AddEditPrerequisites');
  7580. $return .= '</legend>';
  7581. $return .= '<form method="POST">';
  7582. $return .= '<table class="data_table">';
  7583. $return .= '<tr>';
  7584. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7585. $return .= '<th width="70" height="24">' . get_lang('Minimum') . '</th>';
  7586. $return .= '<th width="70" height="24">' . get_lang('Maximum') . '</th>';
  7587. $return .= '</tr>';
  7588. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7589. $return .= '<tr >';
  7590. $return .= '<td colspan="3" class="radio">';
  7591. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7592. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7593. $return .= '</tr>';
  7594. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7595. $result = Database::query($sql);
  7596. $arrLP = array ();
  7597. while ($row = Database :: fetch_array($result)) {
  7598. $arrLP[] = array(
  7599. 'id' => $row['id'],
  7600. 'item_type' => $row['item_type'],
  7601. 'title' => $row['title'],
  7602. 'ref' => $row['ref'],
  7603. 'description' => $row['description'],
  7604. 'parent_item_id' => $row['parent_item_id'],
  7605. 'previous_item_id' => $row['previous_item_id'],
  7606. 'next_item_id' => $row['next_item_id'],
  7607. 'max_score' => $row['max_score'],
  7608. 'min_score' => $row['min_score'],
  7609. 'mastery_score' => $row['mastery_score'],
  7610. 'prerequisite' => $row['prerequisite'],
  7611. 'next_item_id' => $row['next_item_id'],
  7612. 'display_order' => $row['display_order']
  7613. );
  7614. if ($row['ref'] == $preq_id) {
  7615. $preq_mastery = $row['mastery_score'];
  7616. $preq_max = $row['max_score'];
  7617. }
  7618. }
  7619. $this->tree_array($arrLP);
  7620. $arrLP = $this->arrMenu;
  7621. unset($this->arrMenu);
  7622. for ($i = 0; $i < count($arrLP); $i++) {
  7623. if ($arrLP[$i]['id'] == $item_id)
  7624. break;
  7625. $return .= '<tr>';
  7626. $return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7627. $return .= '<label for="id' . $arrLP[$i]['id'] . '">';
  7628. $return .= '<input' . (($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '') . (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $arrLP[$i]['id'] . '" name="prerequisites" style="margin-left:' . $arrLP[$i]['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $arrLP[$i]['id'] . '" />';
  7629. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  7630. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7631. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7632. } else {
  7633. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7634. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7635. } else {
  7636. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7637. }
  7638. }
  7639. $return .= $arrLP[$i]['title'] . '</label>';
  7640. $return .= '</td>';
  7641. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  7642. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7643. require_once api_get_path(LIBRARY_PATH)."lp_item.lib.php";
  7644. $tmp_obj_lp_item = new LpItem($course_id, $arrLP[$i]['id']);
  7645. $tmp_obj_exercice = new Exercise();
  7646. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7647. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7648. $tmp_obj_lp_item->update_in_bdd();
  7649. $arrLP[$i]['max_score'] = $tmp_obj_lp_item->max_score;
  7650. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7651. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7652. $return .= '</td>';
  7653. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7654. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7655. $return .= '</td>';
  7656. }
  7657. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  7658. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7659. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7660. $return .= '</td>';
  7661. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7662. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7663. $return .= '</td>';
  7664. }
  7665. $return .= '</tr>';
  7666. }
  7667. $return .= '<tr>';
  7668. $return .= '</tr>';
  7669. $return .= '</table>';
  7670. $return .= '<div style="padding-top:3px;">';
  7671. $return .= '<button class="save" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7672. $return .= '</form>';
  7673. return $return;
  7674. }
  7675. /**
  7676. * Return HTML list to allow prerequisites selection for lp
  7677. * @param integer Item ID
  7678. * @return string HTML form
  7679. */
  7680. public function display_lp_prerequisites_list()
  7681. {
  7682. $course_id = api_get_course_int_id();
  7683. $lp_id = $this->lp_id;
  7684. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7685. // get current prerequisite
  7686. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7687. $result = Database::query($sql);
  7688. $row = Database :: fetch_array($result);
  7689. $preq_id = $row['prerequisite'];
  7690. $session_id = api_get_session_id();
  7691. $session_condition = api_get_session_condition($session_id);
  7692. $sql = "SELECT * FROM $tbl_lp
  7693. WHERE c_id = $course_id $session_condition
  7694. ORDER BY display_order ";
  7695. $rs = Database::query($sql);
  7696. $return = '';
  7697. $return .= '<select name="prerequisites" >';
  7698. $return .= '<option value="0">'.get_lang('None').'</option>';
  7699. if (Database::num_rows($rs) > 0) {
  7700. while ($row = Database::fetch_array($rs)) {
  7701. if ($row['id'] == $lp_id) {
  7702. continue;
  7703. }
  7704. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$preq_id)?' selected ' : '').'>'.$row['name'].'</option>';
  7705. }
  7706. }
  7707. $return .= '</select>';
  7708. return $return;
  7709. }
  7710. /**
  7711. * Creates a list with all the documents in it
  7712. * @param bool $showInvisibleFiles
  7713. * @return string
  7714. */
  7715. public function get_documents($showInvisibleFiles = false)
  7716. {
  7717. $course_info = api_get_course_info();
  7718. $sessionId = api_get_session_id();
  7719. $document_tree = DocumentManager::get_document_preview(
  7720. $course_info,
  7721. $this->lp_id,
  7722. null,
  7723. $sessionId,
  7724. true,
  7725. null,
  7726. null,
  7727. $showInvisibleFiles,
  7728. true
  7729. );
  7730. return $document_tree;
  7731. }
  7732. /**
  7733. * Creates a list with all the exercises (quiz) in it
  7734. * @return string
  7735. */
  7736. public function get_exercises() {
  7737. $course_id = api_get_course_int_id();
  7738. // New for hotpotatoes.
  7739. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7740. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7741. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7742. $session_id = api_get_session_id();
  7743. $condition_session = api_get_session_condition($session_id);
  7744. $sql_quiz = "SELECT * FROM $tbl_quiz
  7745. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7746. ORDER BY title ASC";
  7747. $sql_hot = "SELECT * FROM $tbl_doc
  7748. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7749. ORDER BY id ASC";
  7750. $res_quiz = Database::query($sql_quiz);
  7751. $res_hot = Database::query($sql_hot);
  7752. $return = '<ul class="lp_resource">';
  7753. $return .= '<li class="lp_resource_element">';
  7754. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7755. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?lp_id=' . $this->lp_id . '">' . get_lang('NewExercise') . '</a>';
  7756. $return .= '</li>';
  7757. // Display hotpotatoes
  7758. while ($row_hot = Database :: fetch_array($res_hot)) {
  7759. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7760. $return .= '<a class="moved" href="#">';
  7761. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7762. $return .= '</a> ';
  7763. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7764. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_HOTPOTATOES . '&amp;file=' . $row_hot['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7765. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7766. $return .= '</li>';
  7767. }
  7768. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7769. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7770. $return .= '<a class="moved" href="#">';
  7771. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7772. $return .= '</a> ';
  7773. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7774. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_QUIZ . '&amp;file=' . $row_quiz['id'] . '&amp;lp_id=' . $this->lp_id . '">' .
  7775. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7776. '</a>';
  7777. $return .= '</li>';
  7778. }
  7779. $return .= '</ul>';
  7780. return $return;
  7781. }
  7782. /**
  7783. * Creates a list with all the links in it
  7784. * @return string
  7785. */
  7786. public function get_links()
  7787. {
  7788. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  7789. $course_id = api_get_course_int_id();
  7790. $tbl_link = Database::get_course_table(TABLE_LINK);
  7791. $session_id = api_get_session_id();
  7792. $condition_session = api_get_session_condition($session_id);
  7793. $sql = "SELECT id, title, category_id FROM $tbl_link
  7794. WHERE c_id = ".$course_id." $condition_session
  7795. ORDER BY title ASC";
  7796. $res_link = Database::query($sql);
  7797. $return = '<ul class="lp_resource">';
  7798. $return .= '<li class="lp_resource_element">';
  7799. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  7800. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'link/link.php?' . api_get_cidreq() . '&action=addlink&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('LinkAdd') . '">' . get_lang('LinkAdd') . '</a>';
  7801. $return .= '</li>';
  7802. $course_info = api_get_course_info();
  7803. $linkCategories = getLinkCategories($course_id, $session_id);
  7804. $categoryIdList = array();
  7805. if (!empty($linkCategories)) {
  7806. foreach ($linkCategories as $categoryInfo) {
  7807. $categoryIdList[] = $categoryInfo['id'];
  7808. }
  7809. }
  7810. while ($row_link = Database :: fetch_array($res_link)) {
  7811. // Check if category exists if not then consider as deleted.
  7812. if (!empty($row_link['category_id'])) {
  7813. $categoryId = $row_link['category_id'];
  7814. if (!in_array($categoryId, $categoryIdList)) {
  7815. continue;
  7816. }
  7817. }
  7818. $item_visibility = api_get_item_visibility(
  7819. $course_info,
  7820. TOOL_LINK,
  7821. $row_link['id'],
  7822. $session_id
  7823. );
  7824. if ($item_visibility != 2) {
  7825. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  7826. $return .= '<a class="moved" href="#">';
  7827. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7828. $return .= '</a> ';
  7829. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  7830. $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 . '">'.
  7831. $row_link['title'].
  7832. '</a>';
  7833. $return .= '</li>';
  7834. }
  7835. }
  7836. $return .= '</ul>';
  7837. return $return;
  7838. }
  7839. /**
  7840. * Creates a list with all the student publications in it
  7841. * @return unknown
  7842. */
  7843. public function get_student_publications()
  7844. {
  7845. $return = '<div class="lp_resource" >';
  7846. $return .= '<div class="lp_resource_element">';
  7847. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7848. $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>';
  7849. $return .= '</div>';
  7850. $return .= '</div>';
  7851. return $return;
  7852. }
  7853. /**
  7854. * Creates a list with all the forums in it
  7855. * @return string
  7856. */
  7857. public function get_forums()
  7858. {
  7859. require_once '../forum/forumfunction.inc.php';
  7860. require_once '../forum/forumconfig.inc.php';
  7861. $a_forums = get_forums();
  7862. $return = '<ul class="lp_resource">';
  7863. //First add link
  7864. $return .= '<li class="lp_resource_element">';
  7865. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7866. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7867. $return .= '</li>';
  7868. $return .= '<script>
  7869. function toggle_forum(forum_id){
  7870. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7871. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7872. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7873. } else {
  7874. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7875. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7876. }
  7877. }
  7878. </script>';
  7879. foreach ($a_forums as $forum) {
  7880. if (!empty($forum['forum_id'])) {
  7881. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7882. $return .= '<a class="moved" href="#">';
  7883. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7884. $return .= ' </a>';
  7885. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7886. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7887. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7888. </a>
  7889. <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">' .
  7890. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7891. $return .= '</li>';
  7892. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7893. $a_threads = get_threads($forum['forum_id']);
  7894. if (is_array($a_threads)) {
  7895. foreach ($a_threads as $thread) {
  7896. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7897. $return .= '&nbsp;<a class="moved" href="#">';
  7898. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7899. $return .= ' </a>';
  7900. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7901. $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 . '">' .
  7902. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7903. $return .= '</li>';
  7904. }
  7905. }
  7906. $return .= '</div>';
  7907. }
  7908. }
  7909. $return .= '</ul>';
  7910. return $return;
  7911. }
  7912. /**
  7913. * // TODO: The output encoding should be equal to the system encoding.
  7914. *
  7915. * Exports the learning path as a SCORM package. This is the main function that
  7916. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7917. * whole thing and returns the zip.
  7918. *
  7919. * This method needs to be called in PHP5, as it will fail with non-adequate
  7920. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7921. * you need to call it on a learnpath object.
  7922. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7923. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7924. * path has been modified, it should use the generic method here below.
  7925. * @TODO link this function with the export_lp() function in the same class
  7926. * @param string Optional name of zip file. If none, title of learnpath is
  7927. * domesticated and trailed with ".zip"
  7928. * @return string Returns the zip package string, or null if error
  7929. */
  7930. public function scorm_export()
  7931. {
  7932. $_course = api_get_course_info();
  7933. $course_id = api_get_course_int_id();
  7934. // Remove memory and time limits as much as possible as this might be a long process...
  7935. if (function_exists('ini_set')) {
  7936. api_set_memory_limit('128M');
  7937. ini_set('max_execution_time', 600);
  7938. }
  7939. // Create the zip handler (this will remain available throughout the method).
  7940. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7941. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7942. $temp_dir_short = uniqid();
  7943. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7944. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7945. $zip_folder = new PclZip($temp_zip_file);
  7946. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7947. $root_path = $main_path = api_get_path(SYS_PATH);
  7948. $files_cleanup = array();
  7949. // Place to temporarily stash the zip file.
  7950. // create the temp dir if it doesn't exist
  7951. // or do a cleanup before creating the zip file.
  7952. if (!is_dir($temp_zip_dir)) {
  7953. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7954. } else {
  7955. // Cleanup: Check the temp dir for old files and delete them.
  7956. $handle = opendir($temp_zip_dir);
  7957. while (false !== ($file = readdir($handle))) {
  7958. if ($file != '.' && $file != '..') {
  7959. unlink("$temp_zip_dir/$file");
  7960. }
  7961. }
  7962. closedir($handle);
  7963. }
  7964. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7965. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7966. // Remove the possible . at the end of the path.
  7967. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7968. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7969. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7970. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7971. }
  7972. // Build a dummy imsmanifest structure.
  7973. // Do not add to the zip yet (we still need it).
  7974. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7975. // Aggregation Model official document, section "2.3 Content Packaging".
  7976. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7977. $xmldoc = new DOMDocument('1.0');
  7978. $root = $xmldoc->createElement('manifest');
  7979. $root->setAttribute('identifier', 'SingleCourseManifest');
  7980. $root->setAttribute('version', '1.1');
  7981. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7982. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7983. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7984. $root->setAttribute('xsi:schemaLocation', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd');
  7985. // Build mandatory sub-root container elements.
  7986. $metadata = $xmldoc->createElement('metadata');
  7987. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7988. $metadata->appendChild($md_schema);
  7989. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7990. $metadata->appendChild($md_schemaversion);
  7991. $root->appendChild($metadata);
  7992. $organizations = $xmldoc->createElement('organizations');
  7993. $resources = $xmldoc->createElement('resources');
  7994. // Build the only organization we will use in building our learnpaths.
  7995. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7996. $organization = $xmldoc->createElement('organization');
  7997. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7998. // To set the title of the SCORM entity (=organization), we take the name given
  7999. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8000. // learning path charset) as it is the encoding that defines how it is stored
  8001. // in the database. Then we convert it to HTML entities again as the "&" character
  8002. // alone is not authorized in XML (must be &amp;).
  8003. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8004. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8005. $organization->appendChild($org_title);
  8006. $folder_name = 'document';
  8007. // Removes the learning_path/scorm_folder path when exporting see #4841
  8008. $path_to_remove = null;
  8009. $result = $this->generate_lp_folder($_course);
  8010. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8011. $path_to_remove = 'document'.$result['dir'];
  8012. $path_to_replace = $folder_name.'/';
  8013. }
  8014. //Fixes chamilo scorm exports
  8015. if ($this->ref == 'chamilo_scorm_export') {
  8016. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8017. }
  8018. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8019. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8020. $link_updates = array();
  8021. $links_to_create = array();
  8022. foreach ($this->items as $index => $item) {
  8023. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8024. // Get included documents from this item.
  8025. if ($item->type == 'sco') {
  8026. $inc_docs = $item->get_resources_from_source(
  8027. null,
  8028. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8029. );
  8030. } else {
  8031. $inc_docs = $item->get_resources_from_source();
  8032. }
  8033. // Give a child element <item> to the <organization> element.
  8034. $my_item_id = $item->get_id();
  8035. $my_item = $xmldoc->createElement('item');
  8036. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8037. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8038. $my_item->setAttribute('isvisible', 'true');
  8039. // Give a child element <title> to the <item> element.
  8040. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8041. $my_item->appendChild($my_title);
  8042. // Give a child element <adlcp:prerequisites> to the <item> element.
  8043. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8044. $my_prereqs->setAttribute('type', 'aicc_script');
  8045. $my_item->appendChild($my_prereqs);
  8046. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8047. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8048. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8049. //$xmldoc->createElement('adlcp:timelimitaction','');
  8050. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8051. //$xmldoc->createElement('adlcp:datafromlms','');
  8052. // Give a child element <adlcp:masteryscore> to the <item> element.
  8053. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8054. $my_item->appendChild($my_masteryscore);
  8055. // Attach this item to the organization element or hits parent if there is one.
  8056. if (!empty($item->parent) && $item->parent != 0) {
  8057. $children = $organization->childNodes;
  8058. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8059. if (is_object($possible_parent)) {
  8060. $possible_parent->appendChild($my_item);
  8061. } else {
  8062. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8063. }
  8064. } else {
  8065. if ($this->debug > 0) { error_log('No parent'); }
  8066. $organization->appendChild($my_item);
  8067. }
  8068. // Get the path of the file(s) from the course directory root.
  8069. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8070. if (!empty($path_to_remove)) {
  8071. //From docs
  8072. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8073. //From quiz
  8074. if ($this->ref == 'chamilo_scorm_export') {
  8075. $path_to_remove = 'scorm/'.$this->path.'/';
  8076. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8077. }
  8078. } else {
  8079. $my_xml_file_path = $my_file_path;
  8080. }
  8081. $my_sub_dir = dirname($my_file_path);
  8082. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8083. $my_xml_sub_dir = $my_sub_dir;
  8084. // Give a <resource> child to the <resources> element
  8085. $my_resource = $xmldoc->createElement('resource');
  8086. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8087. $my_resource->setAttribute('type', 'webcontent');
  8088. $my_resource->setAttribute('href', $my_xml_file_path);
  8089. // adlcp:scormtype can be either 'sco' or 'asset'.
  8090. if ($item->type == 'sco') {
  8091. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8092. } else {
  8093. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8094. }
  8095. // xml:base is the base directory to find the files declared in this resource.
  8096. $my_resource->setAttribute('xml:base', '');
  8097. // Give a <file> child to the <resource> element.
  8098. $my_file = $xmldoc->createElement('file');
  8099. $my_file->setAttribute('href', $my_xml_file_path);
  8100. $my_resource->appendChild($my_file);
  8101. // Dependency to other files - not yet supported.
  8102. $i = 1;
  8103. foreach ($inc_docs as $doc_info) {
  8104. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8105. $my_dep = $xmldoc->createElement('resource');
  8106. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8107. $my_dep->setAttribute('identifier', $res_id);
  8108. $my_dep->setAttribute('type', 'webcontent');
  8109. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8110. $my_dep_file = $xmldoc->createElement('file');
  8111. // Check type of URL.
  8112. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8113. if ($doc_info[1] == 'remote') {
  8114. // Remote file. Save url as is.
  8115. $my_dep_file->setAttribute('href', $doc_info[0]);
  8116. $my_dep->setAttribute('xml:base', '');
  8117. } elseif ($doc_info[1] == 'local') {
  8118. switch ($doc_info[2]) {
  8119. case 'url': // Local URL - save path as url for now, don't zip file.
  8120. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8121. $current_dir = dirname($abs_path);
  8122. $current_dir = str_replace('\\', '/', $current_dir);
  8123. $file_path = realpath($abs_path);
  8124. $file_path = str_replace('\\', '/', $file_path);
  8125. $my_dep_file->setAttribute('href', $file_path);
  8126. $my_dep->setAttribute('xml:base', '');
  8127. if (strstr($file_path, $main_path) !== false) {
  8128. // The calculated real path is really inside Chamilo's root path.
  8129. // Reduce file path to what's under the DocumentRoot.
  8130. $file_path = substr($file_path, strlen($root_path) - 1);
  8131. //echo $file_path;echo '<br /><br />';
  8132. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8133. $zip_files_abs[] = $file_path;
  8134. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8135. $my_dep_file->setAttribute('href', $file_path);
  8136. $my_dep->setAttribute('xml:base', '');
  8137. } elseif (empty($file_path)) {
  8138. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8139. if (strpos($document_root, -1) == '/') {
  8140. $document_root = substr(0, -1, $document_root);
  8141. }*/
  8142. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8143. $file_path = str_replace('//', '/', $file_path);
  8144. if (file_exists($file_path)) {
  8145. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8146. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8147. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8148. $my_dep_file->setAttribute('href', $file_path);
  8149. $my_dep->setAttribute('xml:base', '');
  8150. }
  8151. }
  8152. break;
  8153. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8154. $my_dep_file->setAttribute('href', $doc_info[0]);
  8155. $my_dep->setAttribute('xml:base', '');
  8156. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8157. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8158. $abs_img_path_without_subdir = $doc_info[0];
  8159. $relp = api_get_path(REL_PATH); // The url-append config param.
  8160. $pos = strpos($abs_img_path_without_subdir, $relp);
  8161. if ($pos === 0) {
  8162. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8163. }
  8164. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8165. $file_path = str_replace('\\', '/', $file_path);
  8166. $file_path = str_replace('//', '/', $file_path);
  8167. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8168. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8169. // Check if the current document is in that path.
  8170. if (strstr($file_path, $cur_path) !== false) {
  8171. // The document is in that path, now get the relative path
  8172. // to the containing document.
  8173. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8174. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8175. $relative_path = '';
  8176. if (strstr($file_path, $cur_path) !== false) {
  8177. //$relative_path = substr($file_path, strlen($orig_file_path));
  8178. $relative_path = str_replace($cur_path, '', $file_path);
  8179. $file_path = substr($file_path, strlen($cur_path));
  8180. } else {
  8181. // This case is still a problem as it's difficult to calculate a relative path easily
  8182. // might still generate wrong links.
  8183. //$file_path = substr($file_path,strlen($cur_path));
  8184. // Calculate the directory path to the current file (without trailing slash).
  8185. $my_relative_path = dirname($file_path);
  8186. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8187. $my_relative_file = basename($file_path);
  8188. // Calculate the directory path to the containing file (without trailing slash).
  8189. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8190. $dotdot = '';
  8191. $subdir = '';
  8192. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8193. $my_relative_path2 = dirname($my_relative_path);
  8194. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8195. $my_orig_file_path = dirname($my_orig_file_path);
  8196. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8197. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8198. $dotdot += '../';
  8199. $my_relative_path = $my_relative_path2;
  8200. }
  8201. $relative_path = $dotdot.$subdir.$my_relative_file;
  8202. }
  8203. // Put the current document in the zip (this array is the array
  8204. // that will manage documents already in the course folder - relative).
  8205. $zip_files[] = $file_path;
  8206. // Update the links to the current document in the containing document (make them relative).
  8207. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8208. $my_dep_file->setAttribute('href', $file_path);
  8209. $my_dep->setAttribute('xml:base', '');
  8210. } elseif (strstr($file_path, $main_path) !== false) {
  8211. // The calculated real path is really inside Chamilo's root path.
  8212. // Reduce file path to what's under the DocumentRoot.
  8213. $file_path = substr($file_path, strlen($root_path));
  8214. //echo $file_path;echo '<br /><br />';
  8215. //error_log('Reduced path: '.$file_path, 0);
  8216. $zip_files_abs[] = $file_path;
  8217. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8218. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8219. $my_dep->setAttribute('xml:base', '');
  8220. } elseif (empty($file_path)) {
  8221. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8222. if(strpos($document_root,-1) == '/') {
  8223. $document_root = substr(0, -1, $document_root);
  8224. }*/
  8225. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8226. $file_path = str_replace('//', '/', $file_path);
  8227. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8228. $current_dir = dirname($abs_path);
  8229. $current_dir = str_replace('\\', '/', $current_dir);
  8230. if (file_exists($file_path)) {
  8231. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8232. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8233. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8234. $my_dep_file->setAttribute('href','document/'.$file_path);
  8235. $my_dep->setAttribute('xml:base', '');
  8236. }
  8237. }
  8238. break;
  8239. case 'rel':
  8240. // Path relative to the current document.
  8241. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8242. if (substr($doc_info[0], 0, 2) == '..') {
  8243. // Relative path going up.
  8244. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8245. $current_dir = str_replace('\\', '/', $current_dir);
  8246. $file_path = realpath($current_dir.$doc_info[0]);
  8247. $file_path = str_replace('\\', '/', $file_path);
  8248. //error_log($file_path.' <-> '.$main_path,0);
  8249. if (strstr($file_path, $main_path) !== false) {
  8250. // The calculated real path is really inside Chamilo's root path.
  8251. // Reduce file path to what's under the DocumentRoot.
  8252. $file_path = substr($file_path, strlen($root_path));
  8253. //error_log('Reduced path: '.$file_path, 0);
  8254. $zip_files_abs[] = $file_path;
  8255. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8256. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8257. $my_dep->setAttribute('xml:base', '');
  8258. }
  8259. } else {
  8260. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8261. $my_dep_file->setAttribute('href', $doc_info[0]);
  8262. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8263. }
  8264. break;
  8265. default:
  8266. $my_dep_file->setAttribute('href', $doc_info[0]);
  8267. $my_dep->setAttribute('xml:base', '');
  8268. break;
  8269. }
  8270. }
  8271. $my_dep->appendChild($my_dep_file);
  8272. $resources->appendChild($my_dep);
  8273. $dependency = $xmldoc->createElement('dependency');
  8274. $dependency->setAttribute('identifierref', $res_id);
  8275. $my_resource->appendChild($dependency);
  8276. $i++;
  8277. }
  8278. $resources->appendChild($my_resource);
  8279. $zip_files[] = $my_file_path;
  8280. } else {
  8281. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8282. switch ($item->type) {
  8283. case TOOL_LINK:
  8284. $my_item = $xmldoc->createElement('item');
  8285. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8286. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8287. $my_item->setAttribute('isvisible', 'true');
  8288. // Give a child element <title> to the <item> element.
  8289. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8290. $my_item->appendChild($my_title);
  8291. // Give a child element <adlcp:prerequisites> to the <item> element.
  8292. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8293. $my_prereqs->setAttribute('type', 'aicc_script');
  8294. $my_item->appendChild($my_prereqs);
  8295. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8296. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8297. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8298. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8299. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8300. //$xmldoc->createElement('adlcp:datafromlms', '');
  8301. // Give a child element <adlcp:masteryscore> to the <item> element.
  8302. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8303. $my_item->appendChild($my_masteryscore);
  8304. // Attach this item to the organization element or its parent if there is one.
  8305. if (!empty($item->parent) && $item->parent != 0) {
  8306. $children = $organization->childNodes;
  8307. for ($i = 0; $i < $children->length; $i++) {
  8308. $item_temp = $children->item($i);
  8309. if ($item_temp -> nodeName == 'item') {
  8310. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8311. $item_temp -> appendChild($my_item);
  8312. }
  8313. }
  8314. }
  8315. } else {
  8316. $organization->appendChild($my_item);
  8317. }
  8318. $my_file_path = 'link_'.$item->get_id().'.html';
  8319. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8320. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8321. $rs = Database::query($sql);
  8322. if ($link = Database :: fetch_array($rs)) {
  8323. $url = $link['url'];
  8324. $title = stripslashes($link['title']);
  8325. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8326. $my_xml_file_path = $my_file_path;
  8327. $my_sub_dir = dirname($my_file_path);
  8328. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8329. $my_xml_sub_dir = $my_sub_dir;
  8330. // Give a <resource> child to the <resources> element.
  8331. $my_resource = $xmldoc->createElement('resource');
  8332. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8333. $my_resource->setAttribute('type', 'webcontent');
  8334. $my_resource->setAttribute('href', $my_xml_file_path);
  8335. // adlcp:scormtype can be either 'sco' or 'asset'.
  8336. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8337. // xml:base is the base directory to find the files declared in this resource.
  8338. $my_resource->setAttribute('xml:base', '');
  8339. // give a <file> child to the <resource> element.
  8340. $my_file = $xmldoc->createElement('file');
  8341. $my_file->setAttribute('href', $my_xml_file_path);
  8342. $my_resource->appendChild($my_file);
  8343. $resources->appendChild($my_resource);
  8344. }
  8345. break;
  8346. case TOOL_QUIZ:
  8347. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8348. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8349. $exe = new Exercise();
  8350. $exe->read($exe_id);
  8351. $my_item = $xmldoc->createElement('item');
  8352. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8353. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8354. $my_item->setAttribute('isvisible', 'true');
  8355. // Give a child element <title> to the <item> element.
  8356. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8357. $my_item->appendChild($my_title);
  8358. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8359. //$my_item->appendChild($my_max_score);
  8360. // Give a child element <adlcp:prerequisites> to the <item> element.
  8361. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8362. $my_prereqs->setAttribute('type','aicc_script');
  8363. $my_item->appendChild($my_prereqs);
  8364. // Give a child element <adlcp:masteryscore> to the <item> element.
  8365. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8366. $my_item->appendChild($my_masteryscore);
  8367. // Attach this item to the organization element or hits parent if there is one.
  8368. if (!empty($item->parent) && $item->parent != 0) {
  8369. $children = $organization->childNodes;
  8370. for ($i = 0; $i < $children->length; $i++) {
  8371. $item_temp = $children->item($i);
  8372. if ($item_temp -> nodeName == 'item') {
  8373. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8374. $item_temp -> appendChild($my_item);
  8375. }
  8376. }
  8377. }
  8378. } else {
  8379. $organization->appendChild($my_item);
  8380. }
  8381. // Include export scripts.
  8382. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8383. // Get the path of the file(s) from the course directory root
  8384. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8385. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8386. // Write the contents of the exported exercise into a (big) html file
  8387. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8388. $contents = export_exercise($exe_id, true);
  8389. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8390. $res = file_put_contents($tmp_file_path, $contents);
  8391. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8392. $files_cleanup[] = $tmp_file_path;
  8393. //error_log($tmp_path); die();
  8394. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8395. $my_xml_file_path = $my_file_path;
  8396. $my_sub_dir = dirname($my_file_path);
  8397. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8398. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8399. $my_xml_sub_dir = $my_sub_dir;
  8400. // Give a <resource> child to the <resources> element.
  8401. $my_resource = $xmldoc->createElement('resource');
  8402. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8403. $my_resource->setAttribute('type', 'webcontent');
  8404. $my_resource->setAttribute('href', $my_xml_file_path);
  8405. // adlcp:scormtype can be either 'sco' or 'asset'.
  8406. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8407. // xml:base is the base directory to find the files declared in this resource.
  8408. $my_resource->setAttribute('xml:base', '');
  8409. // Give a <file> child to the <resource> element.
  8410. $my_file = $xmldoc->createElement('file');
  8411. $my_file->setAttribute('href', $my_xml_file_path);
  8412. $my_resource->appendChild($my_file);
  8413. // Get included docs.
  8414. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8415. // Dependency to other files - not yet supported.
  8416. $i = 1;
  8417. foreach ($inc_docs as $doc_info) {
  8418. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8419. $my_dep = $xmldoc->createElement('resource');
  8420. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8421. $my_dep->setAttribute('identifier', $res_id);
  8422. $my_dep->setAttribute('type', 'webcontent');
  8423. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8424. $my_dep_file = $xmldoc->createElement('file');
  8425. // Check type of URL.
  8426. if ($doc_info[1] == 'remote') {
  8427. // Remote file. Save url as is.
  8428. $my_dep_file->setAttribute('href', $doc_info[0]);
  8429. $my_dep->setAttribute('xml:base', '');
  8430. } elseif ($doc_info[1] == 'local') {
  8431. switch ($doc_info[2]) {
  8432. case 'url': // Local URL - save path as url for now, don't zip file.
  8433. // Save file but as local file (retrieve from URL).
  8434. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8435. $current_dir = dirname($abs_path);
  8436. $current_dir = str_replace('\\', '/', $current_dir);
  8437. $file_path = realpath($abs_path);
  8438. $file_path = str_replace('\\', '/', $file_path);
  8439. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8440. $my_dep->setAttribute('xml:base', '');
  8441. if (strstr($file_path, $main_path) !== false) {
  8442. // The calculated real path is really inside the chamilo root path.
  8443. // Reduce file path to what's under the DocumentRoot.
  8444. $file_path = substr($file_path, strlen($root_path));
  8445. //echo $file_path;echo '<br /><br />';
  8446. //error_log('Reduced path: '.$file_path, 0);
  8447. $zip_files_abs[] = $file_path;
  8448. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8449. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8450. $my_dep->setAttribute('xml:base', '');
  8451. } elseif (empty($file_path)) {
  8452. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8453. if (strpos($document_root,-1) == '/') {
  8454. $document_root = substr(0, -1, $document_root);
  8455. }*/
  8456. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8457. $file_path = str_replace('//', '/', $file_path);
  8458. if (file_exists($file_path)) {
  8459. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8460. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8461. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8462. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8463. $my_dep->setAttribute('xml:base', '');
  8464. }
  8465. }
  8466. break;
  8467. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8468. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8469. $current_dir = str_replace('\\', '/', $current_dir);
  8470. $file_path = realpath($doc_info[0]);
  8471. $file_path = str_replace('\\', '/', $file_path);
  8472. $my_dep_file->setAttribute('href', $file_path);
  8473. $my_dep->setAttribute('xml:base', '');
  8474. if (strstr($file_path,$main_path) !== false) {
  8475. // The calculated real path is really inside the chamilo root path.
  8476. // Reduce file path to what's under the DocumentRoot.
  8477. $file_path = substr($file_path, strlen($root_path));
  8478. //echo $file_path;echo '<br /><br />';
  8479. //error_log('Reduced path: '.$file_path, 0);
  8480. $zip_files_abs[] = $file_path;
  8481. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8482. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8483. $my_dep->setAttribute('xml:base', '');
  8484. } elseif (empty($file_path)) {
  8485. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8486. if (strpos($document_root,-1) == '/') {
  8487. $document_root = substr(0, -1, $document_root);
  8488. }*/
  8489. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8490. $file_path = str_replace('//', '/', $file_path);
  8491. if (file_exists($file_path)) {
  8492. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8493. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8494. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8495. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8496. $my_dep->setAttribute('xml:base', '');
  8497. }
  8498. }
  8499. break;
  8500. 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
  8501. if (substr($doc_info[0], 0, 2) == '..') {
  8502. // Relative path going up.
  8503. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8504. $current_dir = str_replace('\\', '/', $current_dir);
  8505. $file_path = realpath($current_dir.$doc_info[0]);
  8506. $file_path = str_replace('\\', '/', $file_path);
  8507. //error_log($file_path.' <-> '.$main_path, 0);
  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));
  8512. $file_path_dest = $file_path;
  8513. // File path is courses/CHAMILO/document/....
  8514. $info_file_path = explode('/', $file_path);
  8515. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8516. $file_path_dest = 'document/'.$file_path;
  8517. }
  8518. //error_log('Reduced path: '.$file_path, 0);
  8519. $zip_files_abs[] = $file_path;
  8520. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8521. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8522. $my_dep->setAttribute('xml:base', '');
  8523. }
  8524. } else {
  8525. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8526. $my_dep_file->setAttribute('href', $doc_info[0]);
  8527. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8528. }
  8529. break;
  8530. default:
  8531. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8532. $my_dep->setAttribute('xml:base', '');
  8533. break;
  8534. }
  8535. }
  8536. $my_dep->appendChild($my_dep_file);
  8537. $resources->appendChild($my_dep);
  8538. $dependency = $xmldoc->createElement('dependency');
  8539. $dependency->setAttribute('identifierref', $res_id);
  8540. $my_resource->appendChild($dependency);
  8541. $i++;
  8542. }
  8543. $resources->appendChild($my_resource);
  8544. $zip_files[] = $my_file_path;
  8545. break;
  8546. default:
  8547. // Get the path of the file(s) from the course directory root
  8548. $my_file_path = 'non_exportable.html';
  8549. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8550. $my_xml_file_path = $my_file_path;
  8551. $my_sub_dir = dirname($my_file_path);
  8552. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8553. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8554. $my_xml_sub_dir = $my_sub_dir;
  8555. // Give a <resource> child to the <resources> element.
  8556. $my_resource = $xmldoc->createElement('resource');
  8557. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8558. $my_resource->setAttribute('type', 'webcontent');
  8559. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8560. // adlcp:scormtype can be either 'sco' or 'asset'.
  8561. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8562. // xml:base is the base directory to find the files declared in this resource.
  8563. $my_resource->setAttribute('xml:base', '');
  8564. // Give a <file> child to the <resource> element.
  8565. $my_file = $xmldoc->createElement('file');
  8566. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8567. $my_resource->appendChild($my_file);
  8568. $resources->appendChild($my_resource);
  8569. break;
  8570. }
  8571. }
  8572. }
  8573. $organizations->appendChild($organization);
  8574. $root->appendChild($organizations);
  8575. $root->appendChild($resources);
  8576. $xmldoc->appendChild($root);
  8577. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8578. // then add the file to the zip, then destroy the file (this is done automatically).
  8579. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8580. global $_configuration;
  8581. $append = $_configuration['url_append'];
  8582. foreach ($zip_files as $file_path) {
  8583. if (empty($file_path)) {
  8584. continue;
  8585. }
  8586. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8587. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8588. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8589. }
  8590. $this->create_path($dest_file);
  8591. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8592. // Check if the file needs a link update.
  8593. if (in_array($file_path, array_keys($link_updates))) {
  8594. $string = file_get_contents($dest_file);
  8595. unlink($dest_file);
  8596. foreach ($link_updates[$file_path] as $old_new) {
  8597. // This is an ugly hack that allows .flv files to be found by the flv player that
  8598. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8599. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8600. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8601. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8602. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8603. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8604. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8605. }
  8606. //Fix to avoid problems with default_course_document
  8607. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8608. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8609. $newDestination = $old_new['dest'];
  8610. } else {
  8611. $newDestination = str_replace('document/', '', $old_new['dest']);
  8612. }
  8613. $string = str_replace($old_new['orig'], $newDestination, $string);
  8614. //Add files inside the HTMLs
  8615. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8616. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8617. if (file_exists($sys_course_path.$new_path)) {
  8618. copy($sys_course_path.$new_path, $destinationFile);
  8619. }
  8620. }
  8621. file_put_contents($dest_file, $string);
  8622. }
  8623. }
  8624. foreach ($zip_files_abs as $file_path) {
  8625. if (empty($file_path)) {
  8626. continue;
  8627. }
  8628. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8629. continue;
  8630. }
  8631. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8632. $this->create_path($dest_file);
  8633. copy($main_path.$file_path, $dest_file);
  8634. // Check if the file needs a link update.
  8635. if (in_array($file_path, array_keys($link_updates))) {
  8636. $string = file_get_contents($dest_file);
  8637. unlink($dest_file);
  8638. foreach ($link_updates[$file_path] as $old_new) {
  8639. // This is an ugly hack that allows .flv files to be found by the flv player that
  8640. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8641. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8642. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8643. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8644. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8645. }
  8646. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8647. }
  8648. file_put_contents($dest_file, $string);
  8649. }
  8650. }
  8651. if (is_array($links_to_create)) {
  8652. foreach ($links_to_create as $file => $link) {
  8653. $file_content = '<!DOCTYPE html><head>
  8654. <meta charset="'.api_get_language_isocode().'" />
  8655. <title>'.$link['title'].'</title>
  8656. </head>
  8657. <body dir="'.api_get_text_direction().'">
  8658. <div style="text-align:center">
  8659. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8660. </body>
  8661. </html>';
  8662. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8663. }
  8664. }
  8665. // Add non exportable message explanation.
  8666. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8667. $file_content = '<!DOCTYPE html><head>
  8668. <meta charset="'.api_get_language_isocode().'" />
  8669. <title>'.$lang_not_exportable.'</title>
  8670. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8671. </head>
  8672. <body dir="'.api_get_text_direction().'">';
  8673. $file_content .=
  8674. <<<EOD
  8675. <style>
  8676. .error-message {
  8677. font-family: arial, verdana, helvetica, sans-serif;
  8678. border-width: 1px;
  8679. border-style: solid;
  8680. left: 50%;
  8681. margin: 10px auto;
  8682. min-height: 30px;
  8683. padding: 5px;
  8684. right: 50%;
  8685. width: 500px;
  8686. background-color: #FFD1D1;
  8687. border-color: #FF0000;
  8688. color: #000;
  8689. }
  8690. </style>
  8691. <body>
  8692. <div class="error-message">
  8693. $lang_not_exportable
  8694. </div>
  8695. </body>
  8696. </html>
  8697. EOD;
  8698. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8699. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8700. }
  8701. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8702. // Add the extra files that go along with a SCORM package.
  8703. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8704. $extra_files = scandir($main_code_path);
  8705. foreach ($extra_files as $extra_file) {
  8706. if (strpos($extra_file, '.') === 0)
  8707. continue;
  8708. else {
  8709. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8710. $this->create_path($dest_file);
  8711. copy($main_code_path.$extra_file, $dest_file);
  8712. }
  8713. }
  8714. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8715. $manifest = @$xmldoc->saveXML();
  8716. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8717. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8718. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8719. // Clean possible temporary files.
  8720. foreach ($files_cleanup as $file) {
  8721. $res = unlink($file);
  8722. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8723. }
  8724. // Send file to client.
  8725. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  8726. $name = replace_dangerous_char($this->get_name()).'.zip';
  8727. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8728. }
  8729. /**
  8730. * @param int $lp_id
  8731. * @return bool
  8732. */
  8733. public function scorm_export_to_pdf($lp_id)
  8734. {
  8735. $lp_id = intval($lp_id);
  8736. $files_to_export = array();
  8737. $course_data = api_get_course_info($this->cc);
  8738. $sessionId = api_get_session_id();
  8739. if (!empty($course_data)) {
  8740. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8741. $list = self::get_flat_ordered_items_list($lp_id);
  8742. if (!empty($list)) {
  8743. foreach ($list as $item_id) {
  8744. $item = $this->items[$item_id];
  8745. switch ($item->type) {
  8746. case 'document':
  8747. //Getting documents from a LP with chamilo documents
  8748. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8749. // Try loading document from the base course.
  8750. if (empty($file_data) && !empty($sessionId)) {
  8751. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8752. }
  8753. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8754. if (file_exists($file_path)) {
  8755. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8756. }
  8757. break;
  8758. case 'asset': //commes from a scorm package generated by chamilo
  8759. case 'sco':
  8760. $file_path = $scorm_path.'/'.$item->path;
  8761. if (file_exists($file_path)) {
  8762. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8763. }
  8764. break;
  8765. case 'dokeos_chapter':
  8766. case 'dir':
  8767. case 'chapter':
  8768. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8769. break;
  8770. }
  8771. }
  8772. }
  8773. $pdf = new PDF();
  8774. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8775. return $result;
  8776. }
  8777. return false;
  8778. }
  8779. /**
  8780. * Temp function to be moved in main_api or the best place around for this.
  8781. * Creates a file path if it doesn't exist
  8782. * @param string $path
  8783. */
  8784. public function create_path($path)
  8785. {
  8786. $path_bits = explode('/', dirname($path));
  8787. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8788. $path_built = IS_WINDOWS_OS ? '' : '/';
  8789. foreach ($path_bits as $bit) {
  8790. if (!empty ($bit)) {
  8791. $new_path = $path_built . $bit;
  8792. if (is_dir($new_path)) {
  8793. $path_built = $new_path . '/';
  8794. } else {
  8795. mkdir($new_path, api_get_permissions_for_new_directories());
  8796. $path_built = $new_path . '/';
  8797. }
  8798. }
  8799. }
  8800. }
  8801. /**
  8802. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8803. * @return boolean The results of the unlink function, or false if there was no image to start with
  8804. */
  8805. public function delete_lp_image()
  8806. {
  8807. $img = $this->get_preview_image();
  8808. if ($img != '') {
  8809. $del_file = $this->get_preview_image_path(null, 'sys');
  8810. if (isset($del_file) && file_exists($del_file)) {
  8811. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8812. if (file_exists($del_file_2)) {
  8813. unlink($del_file_2);
  8814. }
  8815. $this->set_preview_image('');
  8816. return @unlink($del_file);
  8817. }
  8818. }
  8819. return false;
  8820. }
  8821. /**
  8822. * Uploads an author image to the upload/learning_path/images directory
  8823. * @param array The image array, coming from the $_FILES superglobal
  8824. * @return boolean True on success, false on error
  8825. */
  8826. public function upload_image($image_array)
  8827. {
  8828. $image_moved = false;
  8829. if (!empty ($image_array['name'])) {
  8830. $upload_ok = process_uploaded_file($image_array);
  8831. $has_attachment = true;
  8832. } else {
  8833. $image_moved = true;
  8834. }
  8835. if ($upload_ok) {
  8836. if ($has_attachment) {
  8837. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8838. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8839. $updir = $sys_course_path . $courseDir;
  8840. // Try to add an extension to the file if it hasn't one.
  8841. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8842. if (!filter_extension($new_file_name)) {
  8843. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8844. $image_moved = false;
  8845. } else {
  8846. $file_extension = explode('.', $image_array['name']);
  8847. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8848. $filename = uniqid('');
  8849. $new_file_name = $filename.'.'.$file_extension;
  8850. $new_path = $updir.'/'.$new_file_name;
  8851. // Resize the image.
  8852. $temp = new Image($image_array['tmp_name']);
  8853. $picture_infos = $temp->get_image_info();
  8854. if ($picture_infos['width'] > 104) {
  8855. $thumbwidth = 104;
  8856. } else {
  8857. $thumbwidth = $picture_infos['width'];
  8858. }
  8859. if ($picture_infos['height'] > 96) {
  8860. $new_height = 96;
  8861. } else {
  8862. $new_height = $picture_infos['height'];
  8863. }
  8864. $temp->resize($thumbwidth, $new_height, 0);
  8865. $result = $temp->send_image($new_path);
  8866. // Storing the image filename.
  8867. if ($result) {
  8868. $image_moved = true;
  8869. $this->set_preview_image($new_file_name);
  8870. //Resize to 64px to use on course homepage
  8871. $temp->resize(64, 64, 0);
  8872. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8873. return true;
  8874. }
  8875. }
  8876. }
  8877. }
  8878. return false;
  8879. }
  8880. /**
  8881. * @param int $lp_id
  8882. * @param string $status
  8883. */
  8884. public function set_autolunch($lp_id, $status)
  8885. {
  8886. $course_id = api_get_course_int_id();
  8887. $lp_id = intval($lp_id);
  8888. $status = intval($status);
  8889. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8890. // Setting everything to autolunch = 0
  8891. $attributes['autolunch'] = 0;
  8892. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8893. Database::update($lp_table, $attributes, $where);
  8894. if ($status == 1) {
  8895. //Setting my lp_id to autolunch = 1
  8896. $attributes['autolunch'] = 1;
  8897. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8898. Database::update($lp_table, $attributes, $where );
  8899. }
  8900. }
  8901. /**
  8902. * Gets previous_item_id for the next element of the lp_item table
  8903. * @author Isaac flores paz
  8904. * @return integer Previous item ID
  8905. */
  8906. function select_previous_item_id()
  8907. {
  8908. $course_id = api_get_course_int_id();
  8909. if ($this->debug > 0) {
  8910. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8911. }
  8912. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8913. // Get the max order of the items
  8914. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8915. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8916. $rs_max_order = Database::query($sql_max_order);
  8917. $row_max_order = Database::fetch_object($rs_max_order);
  8918. $max_order = $row_max_order->display_order;
  8919. // Get the previous item ID
  8920. $sql = "SELECT id as previous FROM $table_lp_item
  8921. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8922. $rs_max = Database::query($sql);
  8923. $row_max = Database::fetch_object($rs_max);
  8924. // Return the previous item ID
  8925. return $row_max->previous;
  8926. }
  8927. /**
  8928. * Copies an LP
  8929. */
  8930. public function copy()
  8931. {
  8932. $main_path = api_get_path(SYS_CODE_PATH);
  8933. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8934. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8935. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8936. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8937. //Course builder
  8938. $cb = new CourseBuilder();
  8939. //Setting tools that will be copied
  8940. $cb->set_tools_to_build(array('learnpaths'));
  8941. //Setting elements that will be copied
  8942. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  8943. $course = $cb->build();
  8944. //Course restorer
  8945. $course_restorer = new CourseRestorer($course);
  8946. $course_restorer->set_add_text_in_items(true);
  8947. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8948. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8949. }
  8950. function verify_document_size($s)
  8951. {
  8952. $post_max = ini_get('post_max_size');
  8953. if (substr($post_max, -1, 1) == 'M') {
  8954. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  8955. } elseif (substr($post_max, -1, 1) == 'G') {
  8956. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  8957. }
  8958. $upl_max = ini_get('upload_max_filesize');
  8959. if (substr($upl_max, -1, 1) == 'M') {
  8960. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  8961. } elseif (substr($upl_max, -1, 1) == 'G') {
  8962. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  8963. }
  8964. $documents_total_space = DocumentManager::documents_total_space();
  8965. $course_max_space = DocumentManager::get_course_quota();
  8966. $total_size = filesize($s) + $documents_total_space;
  8967. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8968. return true;
  8969. } else{
  8970. return false;
  8971. }
  8972. }
  8973. /**
  8974. * Clear LP prerequisites
  8975. */
  8976. function clear_prerequisites()
  8977. {
  8978. $course_id = $this->get_course_int_id();
  8979. if ($this->debug > 0) {
  8980. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8981. }
  8982. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8983. $lp_id = $this->get_id();
  8984. //Cleaning prerequisites
  8985. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8986. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8987. Database::query($sql);
  8988. //Cleaning mastery score for exercises
  8989. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8990. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8991. Database::query($sql);
  8992. }
  8993. function set_previous_step_as_prerequisite_for_all_items()
  8994. {
  8995. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8996. $course_id = $this->get_course_int_id();
  8997. $lp_id = $this->get_id();
  8998. if (!empty($this->items)) {
  8999. $previous_item_id = null;
  9000. $previous_item_max = 0;
  9001. $previous_item_type = null;
  9002. $last_item_not_chapter = null;
  9003. $last_item_not_chapter_type = null;
  9004. $last_item_not_chapter_max = null;
  9005. foreach ($this->items as $item) {
  9006. // if there was a previous item... (otherwise jump to set it)
  9007. if (!empty($previous_item_id)) {
  9008. $current_item_id = $item->get_id(); //save current id
  9009. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9010. // Current item is not a folder, so it qualifies to get a prerequisites
  9011. if ($last_item_not_chapter_type == 'quiz') {
  9012. // if previous is quiz, mark its max score as default score to be achieved
  9013. $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'";
  9014. Database::query($sql);
  9015. }
  9016. // now simply update the prerequisite to set it to the last non-chapter item
  9017. $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'";
  9018. Database::query($sql);
  9019. // record item as 'non-chapter' reference
  9020. $last_item_not_chapter = $item->get_id();
  9021. $last_item_not_chapter_type = $item->get_type();
  9022. $last_item_not_chapter_max = $item->get_max();
  9023. }
  9024. } else {
  9025. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9026. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9027. $last_item_not_chapter = $item->get_id();
  9028. $last_item_not_chapter_type = $item->get_type();
  9029. $last_item_not_chapter_max = $item->get_max();
  9030. }
  9031. }
  9032. // Saving the item as "previous item" for the next loop
  9033. $previous_item_id = $item->get_id();
  9034. $previous_item_max = $item->get_max();
  9035. $previous_item_type = $item->get_type();
  9036. }
  9037. }
  9038. }
  9039. /**
  9040. * Return the scorm item type object with spaces replaced with _
  9041. * The return result is use to build a css classname like scorm_type_$return
  9042. * @param $in_type
  9043. * @return mixed
  9044. */
  9045. private static function format_scorm_type_item($in_type)
  9046. {
  9047. return str_replace(' ', '_', $in_type);
  9048. }
  9049. /**
  9050. * @return \learnpath
  9051. */
  9052. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9053. {
  9054. $lpObject = Session::read('lpobject');
  9055. $learnPath = null;
  9056. if (isset($lpObject)) {
  9057. $learnPath = unserialize($lpObject);
  9058. }
  9059. if (!is_object($learnPath)) {
  9060. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9061. }
  9062. return $learnPath;
  9063. }
  9064. /**
  9065. * @param int $itemId
  9066. * @return learnpathItem|false
  9067. */
  9068. public function getItem($itemId)
  9069. {
  9070. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9071. return $this->items[$itemId];
  9072. }
  9073. return false;
  9074. }
  9075. /**
  9076. * Check if URL is not allowed to be show in a iframe
  9077. * @param string $src
  9078. *
  9079. * @return string
  9080. */
  9081. public function fixBlockedLinks($src)
  9082. {
  9083. $urlInfo = parse_url($src);
  9084. //$platformProtocol = api_get_protocol();
  9085. $platformProtocol = 'https';
  9086. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9087. $platformProtocol = 'http';
  9088. }
  9089. $protocolFixApplied = false;
  9090. if ($platformProtocol != $urlInfo['scheme']) {
  9091. $_SESSION['x_frame_source'] = $src;
  9092. $src = 'blank.php?error=x_frames_options';
  9093. $protocolFixApplied = true;
  9094. }
  9095. if ($protocolFixApplied == false) {
  9096. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9097. // Check X-Frame-Options
  9098. $ch = curl_init();
  9099. $options = array(
  9100. CURLOPT_URL => $src,
  9101. CURLOPT_RETURNTRANSFER => true,
  9102. CURLOPT_HEADER => true,
  9103. CURLOPT_FOLLOWLOCATION => true,
  9104. CURLOPT_ENCODING => "",
  9105. CURLOPT_AUTOREFERER => true,
  9106. CURLOPT_CONNECTTIMEOUT => 120,
  9107. CURLOPT_TIMEOUT => 120,
  9108. CURLOPT_MAXREDIRS => 10,
  9109. );
  9110. curl_setopt_array($ch, $options);
  9111. $response = curl_exec($ch);
  9112. $httpCode = curl_getinfo($ch);
  9113. $headers = substr($response, 0, $httpCode['header_size']);
  9114. $error = false;
  9115. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9116. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9117. ) {
  9118. $error = true;
  9119. }
  9120. if ($error) {
  9121. $_SESSION['x_frame_source'] = $src;
  9122. $src = 'blank.php?error=x_frames_options';
  9123. }
  9124. }
  9125. }
  9126. return $src;
  9127. }
  9128. }
  9129. if (!function_exists('trim_value')) {
  9130. function trim_value(& $value) {
  9131. $value = trim($value);
  9132. }
  9133. }