learnpath.class.php 442 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  202. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  203. Database::query($sql);
  204. $this->lp_view_id = Database::insert_id();
  205. if ($this->debug > 2) {
  206. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  207. }
  208. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  209. Database::query($sql);
  210. }
  211. // Initialise items.
  212. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  213. $sql = "SELECT * FROM $lp_item_table
  214. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  215. ORDER BY parent_item_id, display_order";
  216. $res = Database::query($sql);
  217. if ($this->debug > 2) {
  218. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  219. error_log('-- Start while--', 0);
  220. }
  221. $lp_item_id_list = array();
  222. while ($row = Database::fetch_array($res)) {
  223. $lp_item_id_list[] = $row['id'];
  224. switch ($this->type) {
  225. case 3: //aicc
  226. $oItem = new aiccItem('db', $row['id'], $course_id);
  227. if (is_object($oItem)) {
  228. $my_item_id = $oItem->get_id();
  229. $oItem->set_lp_view($this->lp_view_id, $course_id);
  230. $oItem->set_prevent_reinit($this->prevent_reinit);
  231. // Don't use reference here as the next loop will make the pointed object change.
  232. $this->items[$my_item_id] = $oItem;
  233. $this->refs_list[$oItem->ref] = $my_item_id;
  234. if ($this->debug > 2) {
  235. error_log(
  236. 'New LP - learnpath::__construct() - ' .
  237. 'aicc object with id ' . $my_item_id .
  238. ' set in items[]', 0);
  239. }
  240. }
  241. break;
  242. case 2:
  243. $oItem = new scormItem('db', $row['id'], $course_id);
  244. if (is_object($oItem)) {
  245. $my_item_id = $oItem->get_id();
  246. $oItem->set_lp_view($this->lp_view_id, $course_id);
  247. $oItem->set_prevent_reinit($this->prevent_reinit);
  248. // Don't use reference here as the next loop will make the pointed object change.
  249. $this->items[$my_item_id] = $oItem;
  250. $this->refs_list[$oItem->ref] = $my_item_id;
  251. if ($this->debug > 2) {
  252. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  253. }
  254. }
  255. break;
  256. case 1:
  257. default:
  258. if ($this->debug > 2) {
  259. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  260. }
  261. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  262. if ($this->debug > 2) {
  263. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  264. }
  265. if (is_object($oItem)) {
  266. $my_item_id = $oItem->get_id();
  267. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  268. $oItem->set_prevent_reinit($this->prevent_reinit);
  269. // Don't use reference here as the next loop will make the pointed object change.
  270. $this->items[$my_item_id] = $oItem;
  271. $this->refs_list[$my_item_id] = $my_item_id;
  272. if ($this->debug > 2) {
  273. error_log(
  274. 'New LP - learnpath::__construct() ' . __LINE__ .
  275. ' - object with id ' . $my_item_id . ' set in items[]',
  276. 0);
  277. }
  278. }
  279. break;
  280. }
  281. // Setting the object level with variable $this->items[$i][parent]
  282. foreach ($this->items as $itemLPObject) {
  283. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  284. $itemLPObject->level = $level;
  285. }
  286. // Setting the view in the item object.
  287. if (is_object($this->items[$row['id']])) {
  288. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  289. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  290. $this->items[$row['id']]->current_start_time = 0;
  291. $this->items[$row['id']]->current_stop_time = 0;
  292. }
  293. }
  294. }
  295. if ($this->debug > 2) {
  296. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  297. }
  298. if (!empty($lp_item_id_list)) {
  299. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  300. if (!empty($lp_item_id_list_to_string)) {
  301. // Get last viewing vars.
  302. $lp_item_view_table = Database:: get_course_table(
  303. TABLE_LP_ITEM_VIEW
  304. );
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $lp_item_view_table
  308. WHERE
  309. c_id = $course_id AND
  310. lp_view_id = ".$this->lp_view_id." AND
  311. lp_item_id IN ('".$lp_item_id_list_to_string."')
  312. ORDER BY view_count DESC ";
  313. if ($this->debug > 2) {
  314. error_log(
  315. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  316. 0
  317. );
  318. }
  319. $status_list = array();
  320. $res = Database::query($sql);
  321. while ($row = Database:: fetch_array($res)) {
  322. $status_list[$row['lp_item_id']] = $row['status'];
  323. }
  324. foreach ($lp_item_id_list as $item_id) {
  325. if (isset($status_list[$item_id])) {
  326. $status = $status_list[$item_id];
  327. if (is_object($this->items[$item_id])) {
  328. $this->items[$item_id]->set_status($status);
  329. if (empty($status)) {
  330. $this->items[$item_id]->set_status(
  331. $this->default_status
  332. );
  333. }
  334. }
  335. } else {
  336. if (!api_is_invitee()) {
  337. if (is_object($this->items[$item_id])) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  343. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  344. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  345. if ($this->debug > 2) {
  346. error_log(
  347. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  348. 0
  349. );
  350. }
  351. $this->items[$item_id]->set_lp_view(
  352. $this->lp_view_id,
  353. $course_id
  354. );
  355. Database::query($sql);
  356. $insertId = Database::insert_id();
  357. $sql = "UPDATE $lp_item_view_table SET id = iid
  358. WHERE iid = $insertId";
  359. Database::query($sql);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. $this->ordered_items = self::get_flat_ordered_items_list(
  366. $this->get_id(),
  367. 0,
  368. $course_id
  369. );
  370. $this->max_ordered_items = 0;
  371. foreach ($this->ordered_items as $index => $dummy) {
  372. if ($index > $this->max_ordered_items && !empty($dummy)) {
  373. $this->max_ordered_items = $index;
  374. }
  375. }
  376. // TODO: Define the current item better.
  377. $this->first();
  378. if ($this->debug > 2) {
  379. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  380. }
  381. return true;
  382. }
  383. /**
  384. * @return string
  385. */
  386. public function getCourseCode()
  387. {
  388. return $this->cc;
  389. }
  390. /**
  391. * @return int
  392. */
  393. public function get_course_int_id()
  394. {
  395. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  396. }
  397. /**
  398. * @param $course_id
  399. * @return int
  400. */
  401. public function set_course_int_id($course_id)
  402. {
  403. return $this->course_int_id = intval($course_id);
  404. }
  405. /**
  406. * Get the depth level of LP item
  407. * @param $in_tab_items
  408. * @param $in_current_item_id
  409. * @return int
  410. */
  411. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  412. {
  413. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  414. if ($parent_item_id == 0) {
  415. return 0;
  416. } else {
  417. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  418. }
  419. }
  420. /**
  421. * Function rewritten based on old_add_item() from Yannick Warnier.
  422. * Due the fact that users can decide where the item should come, I had to overlook this function and
  423. * I found it better to rewrite it. Old function is still available.
  424. * Added also the possibility to add a description.
  425. *
  426. * @param int $parent
  427. * @param int $previous
  428. * @param string $type
  429. * @param int resource ID (ref)
  430. * @param string $title
  431. * @param string $description
  432. * @param int $prerequisites
  433. * @param int $max_time_allowed
  434. * @return int
  435. */
  436. public function add_item(
  437. $parent,
  438. $previous,
  439. $type = 'dokeos_chapter',
  440. $id,
  441. $title,
  442. $description,
  443. $prerequisites = 0,
  444. $max_time_allowed = 0
  445. ) {
  446. $course_id = api_get_course_int_id();
  447. if ($this->debug > 0) {
  448. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  449. }
  450. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  451. $_course = api_get_course_info();
  452. $parent = intval($parent);
  453. $previous = intval($previous);
  454. $id = intval($id);
  455. $max_time_allowed = htmlentities($max_time_allowed);
  456. if (empty($max_time_allowed)) {
  457. $max_time_allowed = 0;
  458. }
  459. $sql = "SELECT COUNT(id) AS num
  460. FROM $tbl_lp_item
  461. WHERE
  462. c_id = $course_id AND
  463. lp_id = " . $this->get_id() . " AND
  464. parent_item_id = " . $parent;
  465. $res_count = Database::query($sql);
  466. $row = Database :: fetch_array($res_count);
  467. $num = $row['num'];
  468. if ($num > 0) {
  469. if ($previous == 0) {
  470. $sql = "SELECT id, next_item_id, display_order
  471. FROM " . $tbl_lp_item . "
  472. WHERE
  473. c_id = $course_id AND
  474. lp_id = " . $this->get_id() . " AND
  475. parent_item_id = " . $parent . " AND
  476. previous_item_id = 0 OR
  477. previous_item_id=" . $parent;
  478. $result = Database::query($sql);
  479. $row = Database :: fetch_array($result);
  480. $tmp_previous = 0;
  481. $next = $row['id'];
  482. $display_order = 0;
  483. } else {
  484. $previous = (int) $previous;
  485. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  486. FROM $tbl_lp_item
  487. WHERE
  488. c_id = $course_id AND
  489. lp_id = " . $this->get_id() . " AND
  490. id = " . $previous;
  491. $result = Database::query($sql);
  492. $row = Database:: fetch_array($result);
  493. $tmp_previous = $row['id'];
  494. $next = $row['next_item_id'];
  495. $display_order = $row['display_order'];
  496. }
  497. } else {
  498. $tmp_previous = 0;
  499. $next = 0;
  500. $display_order = 0;
  501. }
  502. $id = intval($id);
  503. $typeCleaned = Database::escape_string($type);
  504. if ($type == 'quiz') {
  505. $sql = 'SELECT SUM(ponderation)
  506. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  507. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  508. ON
  509. quiz_question.id = quiz_rel_question.question_id AND
  510. quiz_question.c_id = quiz_rel_question.c_id
  511. WHERE
  512. quiz_rel_question.exercice_id = '.$id." AND
  513. quiz_question.c_id = $course_id AND
  514. quiz_rel_question.c_id = $course_id ";
  515. $rsQuiz = Database::query($sql);
  516. $max_score = Database :: result($rsQuiz, 0, 0);
  517. // Disabling the exercise if we add it inside a LP
  518. $exercise = new Exercise();
  519. $exercise->read($id);
  520. $exercise->disable();
  521. $exercise->save();
  522. } else {
  523. $max_score = 100;
  524. }
  525. $params = array(
  526. "c_id" => $course_id,
  527. "lp_id" => $this->get_id(),
  528. "item_type" =>$typeCleaned ,
  529. "ref" => '',
  530. "title" =>$title ,
  531. "description" => $description,
  532. "path" => $id,
  533. "max_score" => $max_score,
  534. "parent_item_id" => $parent,
  535. "previous_item_id" => $previous,
  536. "next_item_id" => $next,
  537. "display_order" => $display_order +1,
  538. "prerequisite" => $prerequisites,
  539. "max_time_allowed" => $max_time_allowed
  540. );
  541. if ($prerequisites != 0) {
  542. $params['prerequisite'] = $prerequisites;
  543. }
  544. $new_item_id = Database::insert($tbl_lp_item, $params);
  545. if ($this->debug > 2) {
  546. error_log('New LP - Inserting chapter: ' . $new_item_id, 0);
  547. }
  548. if ($new_item_id) {
  549. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  550. Database::query($sql);
  551. // Update the item that should come after the new item.
  552. $sql = " UPDATE $tbl_lp_item SET
  553. previous_item_id = $new_item_id,
  554. next_item_id = $new_item_id,
  555. id = $new_item_id
  556. WHERE iid = $new_item_id";
  557. Database::query($sql);
  558. // Update all the items after the new item.
  559. $sql = "UPDATE " . $tbl_lp_item . "
  560. SET display_order = display_order + 1
  561. WHERE
  562. c_id = $course_id AND
  563. lp_id = " . $this->get_id() . " AND
  564. id <> " . $new_item_id . " AND
  565. parent_item_id = " . $parent . " AND
  566. display_order > " . $display_order;
  567. Database::query($sql);
  568. // Update the item that should come after the new item.
  569. $sql = "UPDATE " . $tbl_lp_item . "
  570. SET ref = " . $new_item_id . "
  571. WHERE c_id = $course_id AND id = " . $new_item_id;
  572. Database::query($sql);
  573. // Upload audio.
  574. if (!empty($_FILES['mp3']['name'])) {
  575. // Create the audio folder if it does not exist yet.
  576. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  577. if (!is_dir($filepath . 'audio')) {
  578. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  579. $audio_id = add_document(
  580. $_course,
  581. '/audio',
  582. 'folder',
  583. 0,
  584. 'audio'
  585. );
  586. api_item_property_update(
  587. $_course,
  588. TOOL_DOCUMENT,
  589. $audio_id,
  590. 'FolderCreated',
  591. api_get_user_id(),
  592. null,
  593. null,
  594. null,
  595. null,
  596. api_get_session_id()
  597. );
  598. api_item_property_update(
  599. $_course,
  600. TOOL_DOCUMENT,
  601. $audio_id,
  602. 'invisible',
  603. api_get_user_id(),
  604. null,
  605. null,
  606. null,
  607. null,
  608. api_get_session_id()
  609. );
  610. }
  611. $file_path = handle_uploaded_document(
  612. $_course,
  613. $_FILES['mp3'],
  614. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  615. '/audio',
  616. api_get_user_id(),
  617. '',
  618. '',
  619. '',
  620. '',
  621. false
  622. );
  623. // Getting the filename only.
  624. $file_components = explode('/', $file_path);
  625. $file = $file_components[count($file_components) - 1];
  626. // Store the mp3 file in the lp_item table.
  627. $sql = "UPDATE $tbl_lp_item SET
  628. audio = '" . Database::escape_string($file) . "'
  629. WHERE id = '" . intval($new_item_id) . "'";
  630. Database::query($sql);
  631. }
  632. }
  633. return $new_item_id;
  634. }
  635. /**
  636. * Static admin function allowing addition of a learnpath to a course.
  637. * @param string Course code
  638. * @param string Learnpath name
  639. * @param string Learnpath description string, if provided
  640. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  641. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  642. * @param string Zip file containing the learnpath or directory containing the learnpath
  643. * @return integer The new learnpath ID on success, 0 on failure
  644. */
  645. public static function add_lp(
  646. $course,
  647. $name,
  648. $description = '',
  649. $learnpath = 'guess',
  650. $origin = 'zip',
  651. $zipname = '',
  652. $publicated_on = '',
  653. $expired_on = '',
  654. $categoryId = 0
  655. ) {
  656. global $charset;
  657. $course_id = api_get_course_int_id();
  658. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  659. // Check course code exists.
  660. // Check lp_name doesn't exist, otherwise append something.
  661. $i = 0;
  662. $name = Database::escape_string($name);
  663. $categoryId = intval($categoryId);
  664. // Session id.
  665. $session_id = api_get_session_id();
  666. $check_name = "SELECT * FROM $tbl_lp
  667. WHERE c_id = $course_id AND name = '$name'";
  668. $res_name = Database::query($check_name);
  669. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  670. //by default the publication date is the same that the creation date
  671. //The behaviour above was changed due BT#2800
  672. global $_custom;
  673. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  674. $publicated_on = '';
  675. } else {
  676. $publicated_on = api_get_utc_datetime();
  677. }
  678. } else {
  679. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  680. }
  681. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  682. $expired_on = '';
  683. } else {
  684. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  685. }
  686. while (Database :: num_rows($res_name)) {
  687. // There is already one such name, update the current one a bit.
  688. $i++;
  689. $name = $name . ' - ' . $i;
  690. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  691. $res_name = Database::query($check_name);
  692. }
  693. // New name does not exist yet; keep it.
  694. // Escape description.
  695. // Kevin: added htmlentities().
  696. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  697. $type = 1;
  698. switch ($learnpath) {
  699. case 'guess':
  700. break;
  701. case 'dokeos':
  702. case 'chamilo':
  703. $type = 1;
  704. break;
  705. case 'aicc':
  706. break;
  707. }
  708. switch ($origin) {
  709. case 'zip':
  710. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  711. break;
  712. case 'manual':
  713. default:
  714. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  715. $res_max = Database::query($get_max);
  716. if (Database :: num_rows($res_max) < 1) {
  717. $dsp = 1;
  718. } else {
  719. $row = Database :: fetch_array($res_max);
  720. $dsp = $row[0] + 1;
  721. }
  722. $params = [
  723. 'c_id' => $course_id,
  724. 'lp_type' => $type,
  725. 'name' => $name,
  726. 'description' => $description,
  727. 'path' => '',
  728. 'default_view_mod' => 'embedded',
  729. 'default_encoding' => 'UTF-8',
  730. 'display_order' => $dsp,
  731. 'content_maker' => 'Chamilo',
  732. 'content_local' => 'local',
  733. 'js_lib' => '',
  734. 'session_id' => $session_id,
  735. 'created_on' => api_get_utc_datetime(),
  736. 'publicated_on' => $publicated_on,
  737. 'expired_on' => $expired_on,
  738. 'category_id' => $categoryId
  739. ];
  740. $id = Database::insert($tbl_lp, $params);
  741. if ($id > 0) {
  742. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  743. Database::query($sql);
  744. $course_info = api_get_course_info();
  745. // Insert into item_property.
  746. api_item_property_update(
  747. $course_info,
  748. TOOL_LEARNPATH,
  749. $id,
  750. 'LearnpathAdded',
  751. api_get_user_id()
  752. );
  753. api_set_default_visibility($id, TOOL_LEARNPATH);
  754. return $id;
  755. }
  756. break;
  757. }
  758. }
  759. /**
  760. * Auto completes the parents of an item in case it's been completed or passed
  761. * @param integer $item Optional ID of the item from which to look for parents
  762. */
  763. public function autocomplete_parents($item)
  764. {
  765. $debug = $this->debug;
  766. if ($debug) {
  767. error_log('Learnpath::autocomplete_parents()', 0);
  768. }
  769. if (empty($item)) {
  770. $item = $this->current;
  771. }
  772. $currentItem = $this->getItem($item);
  773. if ($currentItem) {
  774. $parent_id = $currentItem->get_parent();
  775. $parent = $this->getItem($parent_id);
  776. if ($parent) {
  777. // if $item points to an object and there is a parent.
  778. if ($debug) {
  779. error_log(
  780. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  781. 0
  782. );
  783. }
  784. // New experiment including failed and browsed in completed status.
  785. //$current_status = $currentItem->get_status();
  786. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  787. // Fixes chapter auto complete
  788. if (true) {
  789. // If the current item is completed or passes or succeeded.
  790. $updateParentStatus = true;
  791. if ($debug) {
  792. error_log('Status of current item is alright', 0);
  793. }
  794. foreach ($parent->get_children() as $childItemId) {
  795. $childItem = $this->getItem($childItemId);
  796. // If children was not set try to get the info
  797. if (empty($childItem->db_item_view_id)) {
  798. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  799. }
  800. // Check all his brothers (parent's children) for completion status.
  801. if ($childItemId != $item) {
  802. if ($debug) {
  803. error_log(
  804. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  805. 0
  806. );
  807. }
  808. // Trying completing parents of failed and browsed items as well.
  809. if ($childItem->status_is(
  810. array(
  811. 'completed',
  812. 'passed',
  813. 'succeeded',
  814. 'browsed',
  815. 'failed'
  816. )
  817. )
  818. ) {
  819. // Keep completion status to true.
  820. continue;
  821. } else {
  822. if ($debug > 2) {
  823. error_log(
  824. '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,
  825. 0
  826. );
  827. }
  828. $updateParentStatus = false;
  829. break;
  830. }
  831. }
  832. }
  833. if ($updateParentStatus) {
  834. // If all the children were completed:
  835. $parent->set_status('completed');
  836. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  837. // Force the status to "completed"
  838. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  839. $this->update_queue[$parent->get_id()] = 'completed';
  840. if ($debug) {
  841. error_log(
  842. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  843. print_r($this->update_queue, 1),
  844. 0
  845. );
  846. }
  847. // Recursive call.
  848. $this->autocomplete_parents($parent->get_id());
  849. }
  850. }
  851. } else {
  852. if ($debug) {
  853. error_log("Parent #$parent_id does not exists");
  854. }
  855. }
  856. } else {
  857. if ($debug) {
  858. error_log("#$item is an item that doesn't have parents");
  859. }
  860. }
  861. }
  862. /**
  863. * Auto saves the current results into the database for the whole learnpath
  864. */
  865. public function autosave()
  866. {
  867. if ($this->debug > 0) {
  868. error_log('New LP - In learnpath::autosave()', 0);
  869. }
  870. // TODO: Add save operations for the learnpath itself.
  871. }
  872. /**
  873. * Closes the current resource
  874. *
  875. * Stops the timer
  876. * Saves into the database if required
  877. * Clears the current resource data from this object
  878. * @return boolean True on success, false on failure
  879. */
  880. public function close()
  881. {
  882. if ($this->debug > 0) {
  883. error_log('New LP - In learnpath::close()', 0);
  884. }
  885. if (empty ($this->lp_id)) {
  886. $this->error = 'Trying to close this learnpath but no ID is set';
  887. return false;
  888. }
  889. $this->current_time_stop = time();
  890. $this->ordered_items = array();
  891. $this->index = 0;
  892. unset ($this->lp_id);
  893. //unset other stuff
  894. return true;
  895. }
  896. /**
  897. * Static admin function allowing removal of a learnpath
  898. * @param string Course code
  899. * @param integer Learnpath ID
  900. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  901. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  902. */
  903. public function delete($course = null, $id = null, $delete = 'keep')
  904. {
  905. $course_id = api_get_course_int_id();
  906. // TODO: Implement a way of getting this to work when the current object is not set.
  907. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  908. // If an ID is specifically given and the current LP is not the same, prevent delete.
  909. if (!empty ($id) && ($id != $this->lp_id)) {
  910. return false;
  911. }
  912. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  913. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  914. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  915. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  916. // Delete lp item id.
  917. foreach ($this->items as $id => $dummy) {
  918. $sql = "DELETE FROM $lp_item_view
  919. 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. Database::query($sql);
  927. self::toggle_publish($this->lp_id, 'i');
  928. if ($this->type == 2 || $this->type == 3) {
  929. // This is a scorm learning path, delete the files as well.
  930. $sql = "SELECT path FROM $lp
  931. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  932. $res = Database::query($sql);
  933. if (Database :: num_rows($res) > 0) {
  934. $row = Database :: fetch_array($res);
  935. $path = $row['path'];
  936. $sql = "SELECT id FROM $lp
  937. 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
  965. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  966. Database::query($sql);
  967. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  968. Database::query($sql);
  969. // Updates the display order of all lps.
  970. $this->update_display_order();
  971. api_item_property_update(
  972. api_get_course_info(),
  973. TOOL_LEARNPATH,
  974. $this->lp_id,
  975. 'delete',
  976. api_get_user_id()
  977. );
  978. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  979. if ($link_info !== false) {
  980. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  981. }
  982. if (api_get_setting('search_enabled') == 'true') {
  983. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  984. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  985. }
  986. }
  987. /**
  988. * Removes all the children of one item - dangerous!
  989. * @param integer $id Element ID of which children have to be removed
  990. * @return integer Total number of children removed
  991. */
  992. public function delete_children_items($id)
  993. {
  994. $course_id = api_get_course_int_id();
  995. if ($this->debug > 0) {
  996. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  997. }
  998. $num = 0;
  999. if (empty ($id) || $id != strval(intval($id))) {
  1000. return false;
  1001. }
  1002. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1003. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1004. $res = Database::query($sql);
  1005. while ($row = Database :: fetch_array($res)) {
  1006. $num += $this->delete_children_items($row['id']);
  1007. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1008. Database::query($sql_del);
  1009. $num++;
  1010. }
  1011. return $num;
  1012. }
  1013. /**
  1014. * Removes an item from the current learnpath
  1015. * @param integer $id Elem ID (0 if first)
  1016. * @param integer $remove Whether to remove the resource/data from the
  1017. * system or leave it (default: 'keep', others 'remove')
  1018. * @return integer Number of elements moved
  1019. * @todo implement resource removal
  1020. */
  1021. public function delete_item($id, $remove = 'keep')
  1022. {
  1023. $course_id = api_get_course_int_id();
  1024. if ($this->debug > 0) {
  1025. error_log('New LP - In learnpath::delete_item()', 0);
  1026. }
  1027. // TODO: Implement the resource removal.
  1028. if (empty ($id) || $id != strval(intval($id))) {
  1029. return false;
  1030. }
  1031. // First select item to get previous, next, and display order.
  1032. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1033. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1034. $res_sel = Database::query($sql_sel);
  1035. if (Database :: num_rows($res_sel) < 1) {
  1036. return false;
  1037. }
  1038. $row = Database :: fetch_array($res_sel);
  1039. $previous = $row['previous_item_id'];
  1040. $next = $row['next_item_id'];
  1041. $display = $row['display_order'];
  1042. $parent = $row['parent_item_id'];
  1043. $lp = $row['lp_id'];
  1044. // Delete children items.
  1045. $num = $this->delete_children_items($id);
  1046. if ($this->debug > 2) {
  1047. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1048. }
  1049. // Now delete the item.
  1050. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1051. if ($this->debug > 2) {
  1052. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1053. }
  1054. Database::query($sql_del);
  1055. // Now update surrounding items.
  1056. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1057. WHERE c_id = ".$course_id." AND id = $previous";
  1058. Database::query($sql_upd);
  1059. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1060. WHERE c_id = ".$course_id." AND id = $next";
  1061. Database::query($sql_upd);
  1062. // Now update all following items with new display order.
  1063. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1064. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1065. Database::query($sql_all);
  1066. //Removing prerequisites since the item will not longer exist
  1067. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1068. Database::query($sql_all);
  1069. // Remove from search engine if enabled.
  1070. if (api_get_setting('search_enabled') == 'true') {
  1071. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1072. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1073. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1074. $res = Database::query($sql);
  1075. if (Database :: num_rows($res) > 0) {
  1076. $row2 = Database :: fetch_array($res);
  1077. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1078. $di = new ChamiloIndexer();
  1079. $di->remove_document((int) $row2['search_did']);
  1080. }
  1081. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1082. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1083. Database::query($sql);
  1084. }
  1085. }
  1086. /**
  1087. * Updates an item's content in place
  1088. * @param integer $id Element ID
  1089. * @param integer $parent Parent item ID
  1090. * @param integer $previous Previous item ID
  1091. * @param string $title Item title
  1092. * @param string $description Item description
  1093. * @param string $prerequisites Prerequisites (optional)
  1094. * @param array $audio The array resulting of the $_FILES[mp3] element
  1095. * @param int $max_time_allowed
  1096. * @param string $url
  1097. * @return boolean True on success, false on error
  1098. */
  1099. public function edit_item(
  1100. $id,
  1101. $parent,
  1102. $previous,
  1103. $title,
  1104. $description,
  1105. $prerequisites = '0',
  1106. $audio = array(),
  1107. $max_time_allowed = 0,
  1108. $url = ''
  1109. ) {
  1110. $course_id = api_get_course_int_id();
  1111. $_course = api_get_course_info();
  1112. if ($this->debug > 0) {
  1113. error_log('New LP - In learnpath::edit_item()', 0);
  1114. }
  1115. if (empty ($max_time_allowed)) {
  1116. $max_time_allowed = 0;
  1117. }
  1118. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1119. return false;
  1120. }
  1121. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1122. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1123. $res_select = Database::query($sql_select);
  1124. $row_select = Database :: fetch_array($res_select);
  1125. $audio_update_sql = '';
  1126. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1127. // Create the audio folder if it does not exist yet.
  1128. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1129. if (!is_dir($filepath . 'audio')) {
  1130. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1131. $audio_id = add_document(
  1132. $_course,
  1133. '/audio',
  1134. 'folder',
  1135. 0,
  1136. 'audio'
  1137. );
  1138. api_item_property_update(
  1139. $_course,
  1140. TOOL_DOCUMENT,
  1141. $audio_id,
  1142. 'FolderCreated',
  1143. api_get_user_id(),
  1144. null,
  1145. null,
  1146. null,
  1147. null,
  1148. api_get_session_id()
  1149. );
  1150. api_item_property_update(
  1151. $_course,
  1152. TOOL_DOCUMENT,
  1153. $audio_id,
  1154. 'invisible',
  1155. api_get_user_id(),
  1156. null,
  1157. null,
  1158. null,
  1159. null,
  1160. api_get_session_id()
  1161. );
  1162. }
  1163. // Upload file in documents.
  1164. $pi = pathinfo($audio['name']);
  1165. if ($pi['extension'] == 'mp3') {
  1166. $c_det = api_get_course_info($this->cc);
  1167. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1168. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1169. $path = substr($path, 7);
  1170. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1171. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1172. }
  1173. }
  1174. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1175. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1176. // TODO: htmlspecialchars to be checked for encoding related problems.
  1177. if ($same_parent && $same_previous) {
  1178. // Only update title and description.
  1179. $sql = "UPDATE " . $tbl_lp_item . "
  1180. SET title = '" . Database::escape_string($title) . "',
  1181. prerequisite = '" . $prerequisites . "',
  1182. description = '" . Database::escape_string($description) . "'
  1183. " . $audio_update_sql . ",
  1184. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1185. WHERE c_id = ".$course_id." AND id = " . $id;
  1186. Database::query($sql);
  1187. } else {
  1188. $old_parent = $row_select['parent_item_id'];
  1189. $old_previous = $row_select['previous_item_id'];
  1190. $old_next = $row_select['next_item_id'];
  1191. $old_order = $row_select['display_order'];
  1192. $old_prerequisite = $row_select['prerequisite'];
  1193. $old_max_time_allowed = $row_select['max_time_allowed'];
  1194. /* BEGIN -- virtually remove the current item id */
  1195. /* for the next and previous item it is like the current item doesn't exist anymore */
  1196. if ($old_previous != 0) {
  1197. // Next
  1198. $sql = "UPDATE " . $tbl_lp_item . "
  1199. SET next_item_id = " . $old_next . "
  1200. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1201. Database::query($sql);
  1202. }
  1203. if ($old_next != 0) {
  1204. // Previous
  1205. $sql = "UPDATE " . $tbl_lp_item . "
  1206. SET previous_item_id = " . $old_previous . "
  1207. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1208. Database::query($sql);
  1209. }
  1210. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1211. $sql = "UPDATE " . $tbl_lp_item . "
  1212. SET display_order = display_order - 1
  1213. WHERE
  1214. c_id = ".$course_id." AND
  1215. display_order > " . $old_order . " AND
  1216. lp_id = " . $this->lp_id . " AND
  1217. parent_item_id = " . $old_parent;
  1218. Database::query($sql);
  1219. /* END -- virtually remove the current item id */
  1220. /* BEGIN -- update the current item id to his new location */
  1221. if ($previous == 0) {
  1222. // Select the data of the item that should come after the current item.
  1223. $sql = "SELECT id, display_order
  1224. FROM " . $tbl_lp_item . "
  1225. WHERE
  1226. c_id = ".$course_id." AND
  1227. lp_id = " . $this->lp_id . " AND
  1228. parent_item_id = " . $parent . " AND
  1229. previous_item_id = " . $previous;
  1230. $res_select_old = Database::query($sql);
  1231. $row_select_old = Database::fetch_array($res_select_old);
  1232. // If the new parent didn't have children before.
  1233. if (Database :: num_rows($res_select_old) == 0) {
  1234. $new_next = 0;
  1235. $new_order = 1;
  1236. } else {
  1237. $new_next = $row_select_old['id'];
  1238. $new_order = $row_select_old['display_order'];
  1239. }
  1240. } else {
  1241. // Select the data of the item that should come before the current item.
  1242. $sql = "SELECT next_item_id, display_order
  1243. FROM " . $tbl_lp_item . "
  1244. WHERE c_id = ".$course_id." AND id = " . $previous;
  1245. $res_select_old = Database::query($sql);
  1246. $row_select_old = Database :: fetch_array($res_select_old);
  1247. $new_next = $row_select_old['next_item_id'];
  1248. $new_order = $row_select_old['display_order'] + 1;
  1249. }
  1250. // TODO: htmlspecialchars to be checked for encoding related problems.
  1251. // Update the current item with the new data.
  1252. $sql = "UPDATE " . $tbl_lp_item . "
  1253. SET
  1254. title = '" . Database::escape_string($title) . "',
  1255. description = '" . Database::escape_string($description) . "',
  1256. parent_item_id = " . $parent . ",
  1257. previous_item_id = " . $previous . ",
  1258. next_item_id = " . $new_next . ",
  1259. display_order = " . $new_order . "
  1260. " . $audio_update_sql . "
  1261. WHERE c_id = ".$course_id." AND id = " . $id;
  1262. Database::query($sql);
  1263. if ($previous != 0) {
  1264. // Update the previous item's next_item_id.
  1265. $sql = "UPDATE " . $tbl_lp_item . "
  1266. SET next_item_id = " . $id . "
  1267. WHERE c_id = ".$course_id." AND id = " . $previous;
  1268. Database::query($sql);
  1269. }
  1270. if ($new_next != 0) {
  1271. // Update the next item's previous_item_id.
  1272. $sql = "UPDATE " . $tbl_lp_item . "
  1273. SET previous_item_id = " . $id . "
  1274. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1275. Database::query($sql);
  1276. }
  1277. if ($old_prerequisite != $prerequisites) {
  1278. $sql = "UPDATE " . $tbl_lp_item . "
  1279. SET prerequisite = '" . $prerequisites . "'
  1280. WHERE c_id = ".$course_id." AND id = " . $id;
  1281. Database::query($sql);
  1282. }
  1283. if ($old_max_time_allowed != $max_time_allowed) {
  1284. // update max time allowed
  1285. $sql = "UPDATE " . $tbl_lp_item . "
  1286. SET max_time_allowed = " . $max_time_allowed . "
  1287. WHERE c_id = ".$course_id." AND id = " . $id;
  1288. Database::query($sql);
  1289. }
  1290. // Update all the items with the same or a bigger display_order than the current item.
  1291. $sql = "UPDATE " . $tbl_lp_item . "
  1292. SET display_order = display_order + 1
  1293. WHERE
  1294. c_id = ".$course_id." AND
  1295. lp_id = " . $this->get_id() . " AND
  1296. id <> " . $id . " AND
  1297. parent_item_id = " . $parent . " AND
  1298. display_order >= " . $new_order;
  1299. Database::query($sql);
  1300. }
  1301. if ($row_select['item_type'] == 'link') {
  1302. $link = new Link();
  1303. $linkId = $row_select['path'];
  1304. $link->updateLink($linkId, $url);
  1305. }
  1306. }
  1307. /**
  1308. * Updates an item's prereq in place
  1309. * @param integer $id Element ID
  1310. * @param string $prerequisite_id Prerequisite Element ID
  1311. * @param int $mastery_score Prerequisite min score
  1312. * @param int $max_score Prerequisite max score
  1313. *
  1314. * @return boolean True on success, false on error
  1315. */
  1316. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1317. {
  1318. $course_id = api_get_course_int_id();
  1319. if ($this->debug > 0) {
  1320. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1321. }
  1322. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1323. return false;
  1324. }
  1325. $prerequisite_id = intval($prerequisite_id);
  1326. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1327. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1328. $mastery_score = 0;
  1329. }
  1330. if (!is_numeric($max_score) || $max_score < 0) {
  1331. $max_score = 100;
  1332. }
  1333. /*if ($mastery_score > $max_score) {
  1334. $max_score = $mastery_score;
  1335. }*/
  1336. if (!is_numeric($prerequisite_id)) {
  1337. $prerequisite_id = 'NULL';
  1338. }
  1339. $mastery_score = floatval($mastery_score);
  1340. $max_score = floatval($max_score);
  1341. $sql = " UPDATE $tbl_lp_item
  1342. SET
  1343. prerequisite = $prerequisite_id ,
  1344. prerequisite_min_score = $mastery_score ,
  1345. prerequisite_max_score = $max_score
  1346. WHERE c_id = $course_id AND id = $id";
  1347. Database::query($sql);
  1348. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1349. // Will this be enough to ensure unicity?
  1350. /*$sql = " UPDATE $tbl_lp_item
  1351. SET mastery_score = $mastery_score
  1352. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1353. Database::query($sql);*/
  1354. }
  1355. // TODO: Update the item object (can be ignored for now because refreshed).
  1356. return true;
  1357. }
  1358. /**
  1359. * Escapes a string with the available database escape function
  1360. * @param string String to escape
  1361. * @return string String escaped
  1362. * @deprecated use Database::escape_string
  1363. */
  1364. public function escape_string($string)
  1365. {
  1366. return Database::escape_string($string);
  1367. }
  1368. /**
  1369. * Static admin function exporting a learnpath into a zip file
  1370. * @param string Export type (scorm, zip, cd)
  1371. * @param string Course code
  1372. * @param integer Learnpath ID
  1373. * @param string Zip file name
  1374. * @return string Zip file path (or false on error)
  1375. */
  1376. public function export_lp($type, $course, $id, $zipname)
  1377. {
  1378. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1379. return false;
  1380. }
  1381. $url = '';
  1382. switch ($type) {
  1383. case 'scorm':
  1384. break;
  1385. case 'zip':
  1386. break;
  1387. case 'cdrom':
  1388. break;
  1389. }
  1390. return $url;
  1391. }
  1392. /**
  1393. * Gets all the chapters belonging to the same parent as the item/chapter given
  1394. * Can also be called as abstract method
  1395. * @param integer Item ID
  1396. * @return array A list of all the "brother items" (or an empty array on failure)
  1397. */
  1398. public function get_brother_chapters($id)
  1399. {
  1400. $course_id = api_get_course_int_id();
  1401. if ($this->debug > 0) {
  1402. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1403. }
  1404. if (empty($id)|| $id != strval(intval($id))) {
  1405. return array ();
  1406. }
  1407. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1408. $sql_parent = "SELECT * FROM $lp_item
  1409. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1410. $res_parent = Database::query($sql_parent);
  1411. if (Database :: num_rows($res_parent) > 0) {
  1412. $row_parent = Database :: fetch_array($res_parent);
  1413. $parent = $row_parent['parent_item_id'];
  1414. $sql_bros = "SELECT * FROM $lp_item
  1415. WHERE
  1416. c_id = ".$course_id." AND
  1417. parent_item_id = $parent AND
  1418. id = $id AND
  1419. item_type='dokeos_chapter'
  1420. ORDER BY display_order";
  1421. $res_bros = Database::query($sql_bros);
  1422. $list = array ();
  1423. while ($row_bro = Database :: fetch_array($res_bros)) {
  1424. $list[] = $row_bro;
  1425. }
  1426. return $list;
  1427. }
  1428. return array ();
  1429. }
  1430. /**
  1431. * Gets all the items belonging to the same parent as the item given
  1432. * Can also be called as abstract method
  1433. * @param integer Item ID
  1434. * @return array A list of all the "brother items" (or an empty array on failure)
  1435. */
  1436. public function get_brother_items($id)
  1437. {
  1438. $course_id = api_get_course_int_id();
  1439. if ($this->debug > 0) {
  1440. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1441. }
  1442. if (empty ($id) || $id != strval(intval($id))) {
  1443. return array ();
  1444. }
  1445. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1446. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1447. $res_parent = Database::query($sql_parent);
  1448. if (Database :: num_rows($res_parent) > 0) {
  1449. $row_parent = Database :: fetch_array($res_parent);
  1450. $parent = $row_parent['parent_item_id'];
  1451. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1452. ORDER BY display_order";
  1453. $res_bros = Database::query($sql_bros);
  1454. $list = array ();
  1455. while ($row_bro = Database :: fetch_array($res_bros)) {
  1456. $list[] = $row_bro;
  1457. }
  1458. return $list;
  1459. }
  1460. return array ();
  1461. }
  1462. /**
  1463. * Get the specific prefix index terms of this learning path
  1464. * @param string $prefix
  1465. * @return array Array of terms
  1466. */
  1467. public function get_common_index_terms_by_prefix($prefix)
  1468. {
  1469. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1470. $terms = get_specific_field_values_list_by_prefix(
  1471. $prefix,
  1472. $this->cc,
  1473. TOOL_LEARNPATH,
  1474. $this->lp_id
  1475. );
  1476. $prefix_terms = array();
  1477. if (!empty($terms)) {
  1478. foreach ($terms as $term) {
  1479. $prefix_terms[] = $term['value'];
  1480. }
  1481. }
  1482. return $prefix_terms;
  1483. }
  1484. /**
  1485. * Gets the number of items currently completed
  1486. * @return integer The number of items currently completed
  1487. */
  1488. public function get_complete_items_count()
  1489. {
  1490. if ($this->debug > 0) {
  1491. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1492. }
  1493. $i = 0;
  1494. $completedStatusList = array(
  1495. 'completed',
  1496. 'passed',
  1497. 'succeeded',
  1498. 'browsed',
  1499. 'failed'
  1500. );
  1501. foreach ($this->items as $id => $dummy) {
  1502. // Trying failed and browsed considered "progressed" as well.
  1503. if ($this->items[$id]->status_is($completedStatusList) &&
  1504. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1505. $this->items[$id]->get_type() != 'dir'
  1506. ) {
  1507. $i++;
  1508. }
  1509. }
  1510. return $i;
  1511. }
  1512. /**
  1513. * Gets the current item ID
  1514. * @return integer The current learnpath item id
  1515. */
  1516. public function get_current_item_id()
  1517. {
  1518. $current = 0;
  1519. if ($this->debug > 0) {
  1520. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1521. }
  1522. if (!empty($this->current)) {
  1523. $current = $this->current;
  1524. }
  1525. if ($this->debug > 2) {
  1526. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1527. }
  1528. return $current;
  1529. }
  1530. /**
  1531. * Force to get the first learnpath item id
  1532. * @return integer The current learnpath item id
  1533. */
  1534. public function get_first_item_id()
  1535. {
  1536. $current = 0;
  1537. if (is_array($this->ordered_items)) {
  1538. $current = $this->ordered_items[0];
  1539. }
  1540. return $current;
  1541. }
  1542. /**
  1543. * Gets the total number of items available for viewing in this SCORM
  1544. * @return integer The total number of items
  1545. */
  1546. public function get_total_items_count()
  1547. {
  1548. if ($this->debug > 0) {
  1549. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1550. }
  1551. return count($this->items);
  1552. }
  1553. /**
  1554. * Gets the total number of items available for viewing in this SCORM but without chapters
  1555. * @return integer The total no-chapters number of items
  1556. */
  1557. public function get_total_items_count_without_chapters()
  1558. {
  1559. if ($this->debug > 0) {
  1560. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1561. }
  1562. $total = 0;
  1563. $typeListNotToCount = self::getChapterTypes();
  1564. foreach ($this->items as $temp2) {
  1565. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1566. $total++;
  1567. }
  1568. }
  1569. return $total;
  1570. }
  1571. /**
  1572. * Gets the first element URL.
  1573. * @return string URL to load into the viewer
  1574. */
  1575. public function first()
  1576. {
  1577. if ($this->debug > 0) {
  1578. error_log('New LP - In learnpath::first()', 0);
  1579. error_log('$this->last_item_seen '.$this->last_item_seen);
  1580. }
  1581. // Test if the last_item_seen exists and is not a dir.
  1582. if (count($this->ordered_items) == 0) {
  1583. $this->index = 0;
  1584. }
  1585. if ($this->debug > 0) {
  1586. if (isset($this->items[$this->last_item_seen])) {
  1587. $status = $this->items[$this->last_item_seen]->get_status();
  1588. }
  1589. error_log('status '.$status);
  1590. }
  1591. if (!empty($this->last_item_seen) &&
  1592. !empty($this->items[$this->last_item_seen]) &&
  1593. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1594. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1595. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1596. //&& !$this->items[$this->last_item_seen]->is_done()
  1597. ) {
  1598. if ($this->debug > 2) {
  1599. 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);
  1600. }
  1601. $index = -1;
  1602. foreach ($this->ordered_items as $myindex => $item_id) {
  1603. if ($item_id == $this->last_item_seen) {
  1604. $index = $myindex;
  1605. break;
  1606. }
  1607. }
  1608. if ($index == -1) {
  1609. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1610. if ($this->debug > 2) {
  1611. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1612. }
  1613. return false;
  1614. } else {
  1615. $this->last = $this->last_item_seen;
  1616. $this->current = $this->last_item_seen;
  1617. $this->index = $index;
  1618. }
  1619. } else {
  1620. if ($this->debug > 2) {
  1621. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1622. }
  1623. $index = 0;
  1624. // Loop through all ordered items and stop at the first item that is
  1625. // not a directory *and* that has not been completed yet.
  1626. while ( !empty($this->ordered_items[$index]) AND
  1627. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1628. (
  1629. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1630. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1631. $this->items[$this->ordered_items[$index]]->is_done() === true
  1632. ) AND $index < $this->max_ordered_items) {
  1633. $index++;
  1634. }
  1635. $this->last = $this->current;
  1636. // current is
  1637. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1638. $this->index = $index;
  1639. if ($this->debug > 2) {
  1640. error_log('$index ' . $index);
  1641. }
  1642. if ($this->debug > 2) {
  1643. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1644. }
  1645. }
  1646. if ($this->debug > 2) {
  1647. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1648. }
  1649. }
  1650. /**
  1651. * Gets the information about an item in a format usable as JavaScript to update
  1652. * the JS API by just printing this content into the <head> section of the message frame
  1653. * @param int $item_id
  1654. * @return string
  1655. */
  1656. public function get_js_info($item_id = '')
  1657. {
  1658. if ($this->debug > 0) {
  1659. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1660. }
  1661. $info = '';
  1662. $item_id = intval($item_id);
  1663. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1664. //if item is defined, return values from DB
  1665. $oItem = $this->items[$item_id];
  1666. $info .= '<script language="javascript">';
  1667. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1668. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1669. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1670. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1671. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1672. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1673. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1674. $info .= "top.set_flag_synchronized();";
  1675. $info .= '</script>';
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1678. }
  1679. return $info;
  1680. } else {
  1681. // If item_id is empty, just update to default SCORM data.
  1682. $info .= '<script language="javascript">';
  1683. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1684. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1685. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1686. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1687. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1688. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1689. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1690. $info .= "top.set_flag_synchronized();";
  1691. $info .= '</script>';
  1692. if ($this->debug > 2) {
  1693. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1694. }
  1695. return $info;
  1696. }
  1697. }
  1698. /**
  1699. * Gets the js library from the database
  1700. * @return string The name of the javascript library to be used
  1701. */
  1702. public function get_js_lib()
  1703. {
  1704. $lib = '';
  1705. if (!empty ($this->js_lib)) {
  1706. $lib = $this->js_lib;
  1707. }
  1708. return $lib;
  1709. }
  1710. /**
  1711. * Gets the learnpath database ID
  1712. * @return integer Learnpath ID in the lp table
  1713. */
  1714. public function get_id()
  1715. {
  1716. if (!empty ($this->lp_id)) {
  1717. return $this->lp_id;
  1718. } else {
  1719. return 0;
  1720. }
  1721. }
  1722. /**
  1723. * Gets the last element URL.
  1724. * @return string URL to load into the viewer
  1725. */
  1726. public function get_last()
  1727. {
  1728. if ($this->debug > 0) {
  1729. error_log('New LP - In learnpath::get_last()', 0);
  1730. }
  1731. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1732. if ($this->index > 0) {
  1733. $this->index = count($this->ordered_items) - 1;
  1734. return $this->ordered_items[$this->index];
  1735. }
  1736. return false;
  1737. }
  1738. /**
  1739. * Gets the navigation bar for the learnpath display screen
  1740. * @return string The HTML string to use as a navigation bar
  1741. */
  1742. public function get_navigation_bar($idBar = null, $display=null) {
  1743. if ($this->debug > 0) {
  1744. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1745. }
  1746. if(empty($idBar)){
  1747. $idBar='control-top';
  1748. }
  1749. if(empty($display)){
  1750. $display='display:block';
  1751. }
  1752. $navbar = null;
  1753. $lp_id = $this->lp_id;
  1754. $mycurrentitemid = $this->get_current_item_id();
  1755. if ($this->mode == 'fullscreen') {
  1756. $navbar = '
  1757. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1758. <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>
  1759. <a id="scorm-previous" href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1760. <a id="scorm-next" href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1761. <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>
  1762. </div>';
  1763. } else {
  1764. $navbar = '
  1765. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1766. <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>
  1767. <a id="scorm-previous" href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1768. <a id="scorm-next" href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>
  1769. </div>';
  1770. }
  1771. return $navbar;
  1772. }
  1773. /**
  1774. * Gets the next resource in queue (url).
  1775. * @return string URL to load into the viewer
  1776. */
  1777. public function get_next_index()
  1778. {
  1779. if ($this->debug > 0) {
  1780. error_log('New LP - In learnpath::get_next_index()', 0);
  1781. }
  1782. // TODO
  1783. $index = $this->index;
  1784. $index++;
  1785. if ($this->debug > 2) {
  1786. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1787. }
  1788. 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) {
  1789. $index++;
  1790. if ($index == $this->max_ordered_items){
  1791. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1792. return $this->index;
  1793. } else {
  1794. return $index;
  1795. }
  1796. }
  1797. }
  1798. if (empty ($this->ordered_items[$index])) {
  1799. return $this->index;
  1800. }
  1801. if ($this->debug > 2) {
  1802. error_log('New LP - index is now ' . $index, 0);
  1803. }
  1804. return $index;
  1805. }
  1806. /**
  1807. * Gets item_id for the next element
  1808. * @return integer Next item (DB) ID
  1809. */
  1810. public function get_next_item_id()
  1811. {
  1812. if ($this->debug > 0) {
  1813. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1814. }
  1815. $new_index = $this->get_next_index();
  1816. if (!empty ($new_index)) {
  1817. if (isset ($this->ordered_items[$new_index])) {
  1818. if ($this->debug > 2) {
  1819. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1820. }
  1821. return $this->ordered_items[$new_index];
  1822. }
  1823. }
  1824. if ($this->debug > 2) {
  1825. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1826. }
  1827. return 0;
  1828. }
  1829. /**
  1830. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1831. *
  1832. * Generally, the package provided is in the form of a zip file, so the function
  1833. * has been written to test a zip file. If not a zip, the function will return the
  1834. * default return value: ''
  1835. * @param string the path to the file
  1836. * @param string the original name of the file
  1837. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1838. */
  1839. public static function get_package_type($file_path, $file_name)
  1840. {
  1841. // Get name of the zip file without the extension.
  1842. $file_info = pathinfo($file_name);
  1843. $filename = $file_info['basename']; // Name including extension.
  1844. $extension = $file_info['extension']; // Extension only.
  1845. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1846. 'dll',
  1847. 'exe'
  1848. ))) {
  1849. return 'oogie';
  1850. }
  1851. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1852. 'dll',
  1853. 'exe'
  1854. ))) {
  1855. return 'woogie';
  1856. }
  1857. // Filename without its extension.
  1858. $file_base_name = str_replace('.' . $extension, '', $filename);
  1859. $zipFile = new PclZip($file_path);
  1860. // Check the zip content (real size and file extension).
  1861. $zipContentArray = $zipFile->listContent();
  1862. $package_type = '';
  1863. $at_root = false;
  1864. $manifest = '';
  1865. $aicc_match_crs = 0;
  1866. $aicc_match_au = 0;
  1867. $aicc_match_des = 0;
  1868. $aicc_match_cst = 0;
  1869. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1870. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1871. foreach ($zipContentArray as $thisContent) {
  1872. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1873. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1874. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1875. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1876. $package_type = 'scorm';
  1877. break; // Exit the foreach loop.
  1878. } elseif (
  1879. preg_match('/aicc\//i', $thisContent['filename']) ||
  1880. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1881. ) {
  1882. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1883. switch ($ext) {
  1884. case 'crs':
  1885. $aicc_match_crs = 1;
  1886. break;
  1887. case 'au':
  1888. $aicc_match_au = 1;
  1889. break;
  1890. case 'des':
  1891. $aicc_match_des = 1;
  1892. break;
  1893. case 'cst':
  1894. $aicc_match_cst = 1;
  1895. break;
  1896. default:
  1897. break;
  1898. }
  1899. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1900. } else {
  1901. $package_type = '';
  1902. }
  1903. }
  1904. }
  1905. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1906. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1907. $package_type = 'aicc';
  1908. }
  1909. return $package_type;
  1910. }
  1911. /**
  1912. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1913. * @return string URL to load into the viewer
  1914. */
  1915. public function get_previous_index()
  1916. {
  1917. if ($this->debug > 0) {
  1918. error_log('New LP - In learnpath::get_previous_index()', 0);
  1919. }
  1920. $index = $this->index;
  1921. if (isset ($this->ordered_items[$index -1])) {
  1922. $index--;
  1923. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1924. $index--;
  1925. if ($index < 0) {
  1926. return $this->index;
  1927. }
  1928. }
  1929. } else {
  1930. if ($this->debug > 2) {
  1931. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1932. }
  1933. // There is no previous item.
  1934. }
  1935. return $index;
  1936. }
  1937. /**
  1938. * Gets item_id for the next element
  1939. * @return integer Previous item (DB) ID
  1940. */
  1941. public function get_previous_item_id()
  1942. {
  1943. if ($this->debug > 0) {
  1944. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1945. }
  1946. $new_index = $this->get_previous_index();
  1947. return $this->ordered_items[$new_index];
  1948. }
  1949. /**
  1950. * Gets the progress value from the progress_db attribute
  1951. * @return integer Current progress value
  1952. */
  1953. public function get_progress()
  1954. {
  1955. if ($this->debug > 0) {
  1956. error_log('New LP - In learnpath::get_progress()', 0);
  1957. }
  1958. if (!empty ($this->progress_db)) {
  1959. return $this->progress_db;
  1960. }
  1961. return 0;
  1962. }
  1963. /**
  1964. * Returns the HTML necessary to print a mediaplayer block inside a page
  1965. * @return string The mediaplayer HTML
  1966. */
  1967. public function get_mediaplayer($autostart = 'true')
  1968. {
  1969. $course_id = api_get_course_int_id();
  1970. $_course = api_get_course_info();
  1971. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1972. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1973. // Getting all the information about the item.
  1974. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  1975. INNER JOIN ".$tbl_lp_item_view." as lp_view
  1976. ON lp.id = lp_view.lp_item_id
  1977. WHERE
  1978. lp.id = '".$_SESSION['oLP']->current."' AND
  1979. lp.c_id = $course_id AND
  1980. lp_view.c_id = $course_id";
  1981. $result = Database::query($sql);
  1982. $row = Database::fetch_assoc($result);
  1983. $output = '';
  1984. if (!empty ($row['audio'])) {
  1985. $list = $_SESSION['oLP']->get_toc();
  1986. $type_quiz = false;
  1987. foreach($list as $toc) {
  1988. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  1989. $type_quiz = true;
  1990. }
  1991. }
  1992. if ($type_quiz) {
  1993. if ($_SESSION['oLP']->prevent_reinit == 1) {
  1994. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  1995. } else {
  1996. $autostart_audio = $autostart;
  1997. }
  1998. } else {
  1999. $autostart_audio = 'true';
  2000. }
  2001. $courseInfo = api_get_course_info();
  2002. $audio = $row['audio'];
  2003. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2004. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2005. if (!file_exists($file)) {
  2006. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2007. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2008. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2009. }
  2010. $player = Display::getMediaPlayer(
  2011. $file,
  2012. array(
  2013. 'id' => 'lp_audio_media_player',
  2014. 'url' => $url,
  2015. 'autoplay' => $autostart_audio,
  2016. 'width' => '100%'
  2017. )
  2018. );
  2019. // The mp3 player.
  2020. $output = '<div id="container">';
  2021. $output .= $player;
  2022. $output .= '</div>';
  2023. }
  2024. return $output;
  2025. }
  2026. /**
  2027. * Checks if the learning path is visible for student after the progress
  2028. * of its prerequisite is completed, considering the time availability and
  2029. * the LP visibility.
  2030. * @param int $lp_id
  2031. * @param int $student_id
  2032. * @param string Course code (optional)
  2033. * @param int $sessionId
  2034. * @return bool
  2035. */
  2036. public static function is_lp_visible_for_student(
  2037. $lp_id,
  2038. $student_id,
  2039. $courseCode = null,
  2040. $sessionId = null
  2041. ) {
  2042. $lp_id = (int)$lp_id;
  2043. $courseInfo = api_get_course_info($courseCode);
  2044. $sessionId = intval($sessionId);
  2045. if (empty($sessionId)) {
  2046. $sessionId = api_get_session_id();
  2047. }
  2048. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2049. // Get current prerequisite
  2050. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2051. FROM $tbl_learnpath
  2052. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2053. $itemInfo = api_get_item_property_info(
  2054. $courseInfo['real_id'],
  2055. TOOL_LEARNPATH,
  2056. $lp_id,
  2057. $sessionId
  2058. );
  2059. // If the item was deleted.
  2060. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2061. return false;
  2062. }
  2063. $rs = Database::query($sql);
  2064. $now = time();
  2065. if (Database::num_rows($rs) > 0) {
  2066. $row = Database::fetch_array($rs, 'ASSOC');
  2067. $prerequisite = $row['prerequisite'];
  2068. $is_visible = true;
  2069. if (!empty($prerequisite)) {
  2070. $progress = self::getProgress(
  2071. $prerequisite,
  2072. $student_id,
  2073. $courseInfo['real_id'],
  2074. $sessionId
  2075. );
  2076. $progress = intval($progress);
  2077. if ($progress < 100) {
  2078. $is_visible = false;
  2079. }
  2080. }
  2081. // Also check the time availability of the LP
  2082. if ($is_visible) {
  2083. // Adding visibility restrictions
  2084. if (!empty($row['publicated_on']) &&
  2085. $row['publicated_on'] != '0000-00-00 00:00:00'
  2086. ) {
  2087. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2088. //api_not_allowed();
  2089. $is_visible = false;
  2090. }
  2091. }
  2092. // Blocking empty start times see BT#2800
  2093. global $_custom;
  2094. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2095. $_custom['lps_hidden_when_no_start_date']
  2096. ) {
  2097. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2098. //api_not_allowed();
  2099. $is_visible = false;
  2100. }
  2101. }
  2102. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2103. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2104. //api_not_allowed();
  2105. $is_visible = false;
  2106. }
  2107. }
  2108. }
  2109. // Check if the subscription users/group to a LP is ON
  2110. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2111. // Try group
  2112. $is_visible = false;
  2113. // Checking only the user visibility
  2114. $userVisibility = api_get_item_visibility(
  2115. $courseInfo,
  2116. 'learnpath',
  2117. $row['id'],
  2118. $sessionId,
  2119. $student_id,
  2120. 'LearnpathSubscription'
  2121. );
  2122. if ($userVisibility == 1) {
  2123. $is_visible = true;
  2124. } else {
  2125. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2126. if (!empty($userGroups)) {
  2127. foreach ($userGroups as $groupInfo) {
  2128. $groupId = $groupInfo['iid'];
  2129. $userVisibility = api_get_item_visibility(
  2130. $courseInfo,
  2131. 'learnpath',
  2132. $row['id'],
  2133. $sessionId,
  2134. null,
  2135. 'LearnpathSubscription',
  2136. $groupId
  2137. );
  2138. if ($userVisibility == 1) {
  2139. $is_visible = true;
  2140. break;
  2141. }
  2142. }
  2143. }
  2144. }
  2145. }
  2146. return $is_visible;
  2147. }
  2148. return false;
  2149. }
  2150. /**
  2151. * @param int $lpId
  2152. * @param int $userId
  2153. * @param int $courseId
  2154. * @param int $sessionId
  2155. * @return int
  2156. */
  2157. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2158. {
  2159. $lpId = intval($lpId);
  2160. $userId = intval($userId);
  2161. $courseId = intval($courseId);
  2162. $sessionId = intval($sessionId);
  2163. $progress = 0;
  2164. $sessionCondition = api_get_session_condition($sessionId);
  2165. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2166. $sql = "SELECT * FROM $table
  2167. WHERE
  2168. c_id = ".$courseId." AND
  2169. lp_id = $lpId AND
  2170. user_id = $userId $sessionCondition";
  2171. $res = Database::query($sql);
  2172. if (Database :: num_rows($res) > 0) {
  2173. $row = Database:: fetch_array($res);
  2174. $progress = $row['progress'];
  2175. }
  2176. return $progress;
  2177. }
  2178. /**
  2179. * Displays a progress bar
  2180. * completed so far.
  2181. * @param integer $percentage Progress value to display
  2182. * @param string $text_add Text to display near the progress value
  2183. * @return string HTML string containing the progress bar
  2184. */
  2185. public static function get_progress_bar($percentage = -1, $text_add = '')
  2186. {
  2187. $text = $percentage . $text_add;
  2188. $output = '<div class="progress">
  2189. <div id="progress_bar_value" class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2190. '. $text .'
  2191. </div>
  2192. </div>';
  2193. return $output;
  2194. }
  2195. /**
  2196. * @param string $mode can be '%' or 'abs'
  2197. * otherwise this value will be used $this->progress_bar_mode
  2198. * @return string
  2199. */
  2200. public function getProgressBar($mode = null)
  2201. {
  2202. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2203. return self::get_progress_bar($percentage, $text_add);
  2204. }
  2205. /**
  2206. * Gets the progress bar info to display inside the progress bar.
  2207. * Also used by scorm_api.php
  2208. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2209. * we display a number of completed elements per total elements
  2210. * @param integer $add Additional steps to fake as completed
  2211. * @return list array Percentage or number and symbol (% or /xx)
  2212. */
  2213. public function get_progress_bar_text($mode = '', $add = 0)
  2214. {
  2215. if ($this->debug > 0) {
  2216. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2217. }
  2218. if (empty($mode)) {
  2219. $mode = $this->progress_bar_mode;
  2220. }
  2221. $total_items = $this->get_total_items_count_without_chapters();
  2222. if ($this->debug > 2) {
  2223. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2224. }
  2225. $completeItems = $this->get_complete_items_count();
  2226. if ($this->debug > 2) {
  2227. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2228. }
  2229. if ($add != 0) {
  2230. $completeItems += $add;
  2231. if ($this->debug > 2) {
  2232. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2233. }
  2234. }
  2235. $text = '';
  2236. if ($completeItems > $total_items) {
  2237. $completeItems = $total_items;
  2238. }
  2239. $percentage = 0;
  2240. if ($mode == '%') {
  2241. if ($total_items > 0) {
  2242. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2243. } else {
  2244. $percentage = 0;
  2245. }
  2246. $percentage = number_format($percentage, 0);
  2247. $text = '%';
  2248. } elseif ($mode == 'abs') {
  2249. $percentage = $completeItems;
  2250. $text = '/' . $total_items;
  2251. }
  2252. return array(
  2253. $percentage,
  2254. $text
  2255. );
  2256. }
  2257. /**
  2258. * Gets the progress bar mode
  2259. * @return string The progress bar mode attribute
  2260. */
  2261. public function get_progress_bar_mode()
  2262. {
  2263. if ($this->debug > 0) {
  2264. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2265. }
  2266. if (!empty ($this->progress_bar_mode)) {
  2267. return $this->progress_bar_mode;
  2268. } else {
  2269. return '%';
  2270. }
  2271. }
  2272. /**
  2273. * Gets the learnpath proximity (remote or local)
  2274. * @return string Learnpath proximity
  2275. */
  2276. public function get_proximity()
  2277. {
  2278. if ($this->debug > 0) {
  2279. error_log('New LP - In learnpath::get_proximity()', 0);
  2280. }
  2281. if (!empty ($this->proximity)) {
  2282. return $this->proximity;
  2283. } else {
  2284. return '';
  2285. }
  2286. }
  2287. /**
  2288. * Gets the learnpath theme (remote or local)
  2289. * @return string Learnpath theme
  2290. */
  2291. public function get_theme()
  2292. {
  2293. if ($this->debug > 0) {
  2294. error_log('New LP - In learnpath::get_theme()', 0);
  2295. }
  2296. if (!empty ($this->theme)) {
  2297. return $this->theme;
  2298. } else {
  2299. return '';
  2300. }
  2301. }
  2302. /**
  2303. * Gets the learnpath session id
  2304. * @return string Learnpath theme
  2305. */
  2306. public function get_lp_session_id()
  2307. {
  2308. if ($this->debug > 0) {
  2309. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2310. }
  2311. if (!empty ($this->lp_session_id)) {
  2312. return $this->lp_session_id;
  2313. } else {
  2314. return 0;
  2315. }
  2316. }
  2317. /**
  2318. * Gets the learnpath image
  2319. * @return string Web URL of the LP image
  2320. */
  2321. public function get_preview_image()
  2322. {
  2323. if ($this->debug > 0) {
  2324. error_log('New LP - In learnpath::get_preview_image()', 0);
  2325. }
  2326. if (!empty($this->preview_image)) {
  2327. return $this->preview_image;
  2328. } else {
  2329. return '';
  2330. }
  2331. }
  2332. /**
  2333. * @param string $size
  2334. * @param string $path_type
  2335. * @return bool|string
  2336. */
  2337. public function get_preview_image_path($size = null, $path_type = 'web')
  2338. {
  2339. $preview_image = $this->get_preview_image();
  2340. if (isset($preview_image) && !empty($preview_image)) {
  2341. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2342. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2343. if (isset($size)) {
  2344. $info = pathinfo($preview_image);
  2345. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2346. if (file_exists($image_sys_path.$image_custom_size)) {
  2347. if ($path_type == 'web') {
  2348. return $image_path.$image_custom_size;
  2349. } else {
  2350. return $image_sys_path.$image_custom_size;
  2351. }
  2352. }
  2353. } else {
  2354. if ($path_type == 'web') {
  2355. return $image_path.$preview_image;
  2356. } else {
  2357. return $image_sys_path.$preview_image;
  2358. }
  2359. }
  2360. }
  2361. return false;
  2362. }
  2363. /**
  2364. * Gets the learnpath author
  2365. * @return string LP's author
  2366. */
  2367. public function get_author()
  2368. {
  2369. if ($this->debug > 0) {
  2370. error_log('New LP - In learnpath::get_author()', 0);
  2371. }
  2372. if (!empty ($this->author)) {
  2373. return $this->author;
  2374. } else {
  2375. return '';
  2376. }
  2377. }
  2378. /**
  2379. * Gets the learnpath author
  2380. * @return string LP's author
  2381. */
  2382. public function get_hide_toc_frame()
  2383. {
  2384. if ($this->debug > 0) {
  2385. error_log('New LP - In learnpath::get_author()', 0);
  2386. }
  2387. if (!empty ($this->hide_toc_frame)) {
  2388. return $this->hide_toc_frame;
  2389. } else {
  2390. return '';
  2391. }
  2392. }
  2393. /**
  2394. * Generate a new prerequisites string for a given item. If this item was a sco and
  2395. * its prerequisites were strings (instead of IDs), then transform those strings into
  2396. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2397. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2398. * same rule as the scorm_export() method
  2399. * @param integer Item ID
  2400. * @return string Prerequisites string ready for the export as SCORM
  2401. */
  2402. public function get_scorm_prereq_string($item_id)
  2403. {
  2404. if ($this->debug > 0) {
  2405. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2406. }
  2407. if (!is_object($this->items[$item_id])) {
  2408. return false;
  2409. }
  2410. /** @var learnpathItem $oItem */
  2411. $oItem = $this->items[$item_id];
  2412. $prereq = $oItem->get_prereq_string();
  2413. if (empty($prereq)) {
  2414. return '';
  2415. }
  2416. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2417. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2418. // then simply return it (with the ITEM_ prefix).
  2419. //return 'ITEM_' . $prereq;
  2420. return $this->items[$prereq]->ref;
  2421. } else {
  2422. if (isset($this->refs_list[$prereq])) {
  2423. // It's a simple string item from which the ID can be found in the refs list,
  2424. // so we can transform it directly to an ID for export.
  2425. return $this->items[$this->refs_list[$prereq]]->ref;
  2426. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2427. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2428. } else {
  2429. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2430. // and replace them, one by one, by the internal IDs (chamilo db)
  2431. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2432. // by a space as well.
  2433. $find = array (
  2434. '&',
  2435. '|',
  2436. '~',
  2437. '=',
  2438. '<>',
  2439. '{',
  2440. '}',
  2441. '*',
  2442. '(',
  2443. ')'
  2444. );
  2445. $replace = array (
  2446. ' ',
  2447. ' ',
  2448. ' ',
  2449. ' ',
  2450. ' ',
  2451. ' ',
  2452. ' ',
  2453. ' ',
  2454. ' ',
  2455. ' '
  2456. );
  2457. $prereq_mod = str_replace($find, $replace, $prereq);
  2458. $ids = explode(' ', $prereq_mod);
  2459. foreach ($ids as $id) {
  2460. $id = trim($id);
  2461. if (isset ($this->refs_list[$id])) {
  2462. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2463. }
  2464. }
  2465. return $prereq;
  2466. }
  2467. }
  2468. }
  2469. /**
  2470. * Returns the XML DOM document's node
  2471. * @param resource Reference to a list of objects to search for the given ITEM_*
  2472. * @param string The identifier to look for
  2473. * @return mixed The reference to the element found with that identifier. False if not found
  2474. */
  2475. public function get_scorm_xml_node(& $children, $id)
  2476. {
  2477. for ($i = 0; $i < $children->length; $i++) {
  2478. $item_temp = $children->item($i);
  2479. if ($item_temp->nodeName == 'item') {
  2480. if ($item_temp->getAttribute('identifier') == $id) {
  2481. return $item_temp;
  2482. }
  2483. }
  2484. $subchildren = $item_temp->childNodes;
  2485. if ($subchildren->length > 0) {
  2486. $val = $this->get_scorm_xml_node($subchildren, $id);
  2487. if (is_object($val)) {
  2488. return $val;
  2489. }
  2490. }
  2491. }
  2492. return false;
  2493. }
  2494. /**
  2495. * Returns a usable array of stats related to the current learnpath and user
  2496. * @return array Well-formatted array containing status for the current learnpath
  2497. */
  2498. public function get_stats()
  2499. {
  2500. if ($this->debug > 0) {
  2501. error_log('New LP - In learnpath::get_stats()', 0);
  2502. }
  2503. }
  2504. /**
  2505. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2506. * the given course (for all learnpaths and all users)
  2507. * @param string Course code
  2508. * @return array Well-formatted array containing status for the course's learnpaths
  2509. */
  2510. public function get_stats_course($course)
  2511. {
  2512. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2513. // TODO
  2514. }
  2515. /**
  2516. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2517. * the given course and learnpath (for all users)
  2518. * @param string Course code
  2519. * @param integer Learnpath ID
  2520. * @return array Well-formatted array containing status for the specified learnpath
  2521. */
  2522. public function get_stats_lp($course, $lp)
  2523. {
  2524. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2525. // TODO
  2526. }
  2527. /**
  2528. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2529. * the given course, learnpath and user.
  2530. * @param string Course code
  2531. * @param integer Learnpath ID
  2532. * @param integer User ID
  2533. * @return array Well-formatted array containing status for the specified learnpath and user
  2534. */
  2535. public function get_stats_lp_user($course, $lp, $user)
  2536. {
  2537. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2538. // TODO
  2539. }
  2540. /**
  2541. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2542. * the given course and learnpath (for all users)
  2543. * @param string Course code
  2544. * @param integer User ID
  2545. * @return array Well-formatted array containing status for the user's learnpaths
  2546. */
  2547. public function get_stats_user($course, $user) {
  2548. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2549. // TODO
  2550. }
  2551. /**
  2552. * Gets the status list for all LP's items
  2553. * @return array Array of [index] => [item ID => current status]
  2554. */
  2555. public function get_items_status_list()
  2556. {
  2557. if ($this->debug > 0) {
  2558. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2559. }
  2560. $list = array ();
  2561. foreach ($this->ordered_items as $item_id) {
  2562. $list[] = array (
  2563. $item_id => $this->items[$item_id]->get_status()
  2564. );
  2565. }
  2566. return $list;
  2567. }
  2568. /**
  2569. * Return the number of interactions for the given learnpath Item View ID.
  2570. * This method can be used as static.
  2571. * @param integer Item View ID
  2572. * @param integer course id
  2573. * @return integer Number of interactions
  2574. */
  2575. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2576. {
  2577. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2578. $lp_iv_id = intval($lp_iv_id);
  2579. $course_id = intval($course_id);
  2580. $sql = "SELECT count(*) FROM $table
  2581. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2582. $res = Database::query($sql);
  2583. $num = 0;
  2584. if (Database::num_rows($res)) {
  2585. $row = Database::fetch_array($res);
  2586. $num = $row[0];
  2587. }
  2588. return $num;
  2589. }
  2590. /**
  2591. * Return the interactions as an array for the given lp_iv_id.
  2592. * This method can be used as static.
  2593. * @param integer Learnpath Item View ID
  2594. * @return array
  2595. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2596. */
  2597. public static function get_iv_interactions_array($lp_iv_id)
  2598. {
  2599. $course_id = api_get_course_int_id();
  2600. $list = array();
  2601. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2602. if (empty($lp_iv_id)) {
  2603. return array();
  2604. }
  2605. $sql = "SELECT * FROM $table
  2606. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2607. ORDER BY order_id ASC";
  2608. $res = Database::query($sql);
  2609. $num = Database :: num_rows($res);
  2610. if ($num > 0) {
  2611. $list[] = array (
  2612. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2613. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2614. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2615. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2616. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2617. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2618. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2619. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2620. );
  2621. while ($row = Database :: fetch_array($res)) {
  2622. $list[] = array (
  2623. 'order_id' => ($row['order_id'] + 1),
  2624. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2625. 'type' => $row['interaction_type'],
  2626. 'time' => $row['completion_time'],
  2627. //'correct_responses' => $row['correct_responses'],
  2628. 'correct_responses' => '', // Hide correct responses from students.
  2629. 'student_response' => $row['student_response'],
  2630. 'result' => $row['result'],
  2631. 'latency' => $row['latency']
  2632. );
  2633. }
  2634. }
  2635. return $list;
  2636. }
  2637. /**
  2638. * Return the number of objectives for the given learnpath Item View ID.
  2639. * This method can be used as static.
  2640. * @param integer Item View ID
  2641. * @return integer Number of objectives
  2642. */
  2643. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2644. {
  2645. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2646. $course_id = intval($course_id);
  2647. $lp_iv_id = intval($lp_iv_id);
  2648. $sql = "SELECT count(*) FROM $table
  2649. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2650. //@todo seems that this always returns 0
  2651. $res = Database::query($sql);
  2652. $num = 0;
  2653. if (Database::num_rows($res)) {
  2654. $row = Database :: fetch_array($res);
  2655. $num = $row[0];
  2656. }
  2657. return $num;
  2658. }
  2659. /**
  2660. * Return the objectives as an array for the given lp_iv_id.
  2661. * This method can be used as static.
  2662. * @param integer Learnpath Item View ID
  2663. * @return array
  2664. * @todo Translate labels
  2665. */
  2666. public static function get_iv_objectives_array($lp_iv_id = 0)
  2667. {
  2668. $course_id = api_get_course_int_id();
  2669. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2670. $sql = "SELECT * FROM $table
  2671. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2672. ORDER BY order_id ASC";
  2673. $res = Database::query($sql);
  2674. $num = Database :: num_rows($res);
  2675. $list = array();
  2676. if ($num > 0) {
  2677. $list[] = array(
  2678. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2679. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2680. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2681. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2682. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2683. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2684. );
  2685. while ($row = Database :: fetch_array($res)) {
  2686. $list[] = array (
  2687. 'order_id' => ($row['order_id'] + 1),
  2688. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2689. 'score_raw' => $row['score_raw'],
  2690. 'score_max' => $row['score_max'],
  2691. 'score_min' => $row['score_min'],
  2692. 'status' => $row['status']
  2693. );
  2694. }
  2695. }
  2696. return $list;
  2697. }
  2698. /**
  2699. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2700. * used by get_html_toc() to be ready to display
  2701. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2702. */
  2703. public function get_toc()
  2704. {
  2705. if ($this->debug > 0) {
  2706. error_log('learnpath::get_toc()', 0);
  2707. }
  2708. $toc = array();
  2709. foreach ($this->ordered_items as $item_id) {
  2710. if ($this->debug > 2) {
  2711. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2712. }
  2713. // TODO: Change this link generation and use new function instead.
  2714. $toc[] = array (
  2715. 'id' => $item_id,
  2716. 'title' => $this->items[$item_id]->get_title(),
  2717. 'status' => $this->items[$item_id]->get_status(),
  2718. 'level' => $this->items[$item_id]->get_level(),
  2719. 'type' => $this->items[$item_id]->get_type(),
  2720. 'description' => $this->items[$item_id]->get_description(),
  2721. 'path' => $this->items[$item_id]->get_path(),
  2722. );
  2723. }
  2724. if ($this->debug > 2) {
  2725. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2726. }
  2727. return $toc;
  2728. }
  2729. /**
  2730. * Generate and return the table of contents for this learnpath. The JS
  2731. * table returned is used inside of scorm_api.php
  2732. * @return string A JS array vairiable construction
  2733. */
  2734. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2735. {
  2736. if ($this->debug > 0) {
  2737. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2738. }
  2739. $toc = $varname.' = new Array();';
  2740. foreach ($this->ordered_items as $item_id) {
  2741. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2742. }
  2743. if ($this->debug > 2) {
  2744. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2745. }
  2746. return $toc;
  2747. }
  2748. /**
  2749. * Gets the learning path type
  2750. * @param boolean Return the name? If false, return the ID. Default is false.
  2751. * @return mixed Type ID or name, depending on the parameter
  2752. */
  2753. public function get_type($get_name = false)
  2754. {
  2755. $res = false;
  2756. if ($this->debug > 0) {
  2757. error_log('New LP - In learnpath::get_type()', 0);
  2758. }
  2759. if (!empty ($this->type)) {
  2760. if ($get_name) {
  2761. // Get it from the lp_type table in main db.
  2762. } else {
  2763. $res = $this->type;
  2764. }
  2765. }
  2766. if ($this->debug > 2) {
  2767. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2768. }
  2769. return $res;
  2770. }
  2771. /**
  2772. * Gets the learning path type as static method
  2773. * @param boolean Return the name? If false, return the ID. Default is false.
  2774. * @return mixed Type ID or name, depending on the parameter
  2775. */
  2776. public static function get_type_static($lp_id = 0)
  2777. {
  2778. $course_id = api_get_course_int_id();
  2779. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2780. $lp_id = intval($lp_id);
  2781. $sql = "SELECT lp_type FROM $tbl_lp
  2782. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2783. $res = Database::query($sql);
  2784. if ($res === false) {
  2785. return null;
  2786. }
  2787. if (Database :: num_rows($res) <= 0) {
  2788. return null;
  2789. }
  2790. $row = Database :: fetch_array($res);
  2791. return $row['lp_type'];
  2792. }
  2793. /**
  2794. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2795. * This method can be used as abstract and is recursive
  2796. * @param integer Learnpath ID
  2797. * @param integer Parent ID of the items to look for
  2798. * @return mixed Ordered list of item IDs or false on error
  2799. */
  2800. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2801. {
  2802. if (empty($course_id)) {
  2803. $course_id = api_get_course_int_id();
  2804. } else {
  2805. $course_id = intval($course_id);
  2806. }
  2807. $list = array();
  2808. if (empty($lp)) {
  2809. return false;
  2810. }
  2811. $lp = intval($lp);
  2812. $parent = intval($parent);
  2813. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2814. $sql = "SELECT id FROM $tbl_lp_item
  2815. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2816. ORDER BY display_order";
  2817. $res = Database::query($sql);
  2818. while ($row = Database :: fetch_array($res)) {
  2819. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2820. $list[] = $row['id'];
  2821. foreach ($sublist as $item) {
  2822. $list[] = $item;
  2823. }
  2824. }
  2825. return $list;
  2826. }
  2827. /**
  2828. * @return array
  2829. */
  2830. public static function getChapterTypes()
  2831. {
  2832. return array(
  2833. 'dokeos_chapter',
  2834. 'dokeos_module',
  2835. 'chapter',
  2836. 'dir'
  2837. );
  2838. }
  2839. /**
  2840. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2841. * @return string HTML TOC ready to display
  2842. */
  2843. public function get_html_toc($toc_list = null)
  2844. {
  2845. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2846. if ($this->debug > 0) {
  2847. error_log('In learnpath::get_html_toc()', 0);
  2848. }
  2849. if (empty($toc_list)) {
  2850. $toc_list = $this->get_toc();
  2851. }
  2852. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2853. $html = '<div class="scorm-body">';
  2854. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2855. require_once 'resourcelinker.inc.php';
  2856. // Temporary variables.
  2857. $mycurrentitemid = $this->get_current_item_id();
  2858. $color_counter = 0;
  2859. $i = 0;
  2860. foreach ($toc_list as $item) {
  2861. // TODO: Complete this
  2862. $icon_name = array (
  2863. 'not attempted' => '../img/notattempted.gif',
  2864. 'incomplete' => '../img/incomplete.png',
  2865. 'failed' => '../img/delete.png',
  2866. 'completed' => '../img/completed.png',
  2867. 'passed' => '../img/passed.png',
  2868. 'succeeded' => '../img/succeeded.png',
  2869. 'browsed' => '../img/completed.png',
  2870. );
  2871. // Style Status
  2872. $class_name = array (
  2873. 'not attempted' => 'scorm_not_attempted',
  2874. 'incomplete' => 'scorm_not_attempted',
  2875. 'failed' => 'scorm_failed',
  2876. 'completed' => 'scorm_completed',
  2877. 'passed' => 'scorm_completed',
  2878. 'succeeded' => 'scorm_completed',
  2879. 'browsed' => 'scorm_completed',
  2880. );
  2881. $scorm_color_background = 'row_odd';
  2882. $style_item = '';
  2883. if ($color_counter % 2 == 0) {
  2884. $scorm_color_background = 'row_even';
  2885. }
  2886. $dirTypes = self::getChapterTypes();
  2887. if (in_array($item['type'], $dirTypes)) {
  2888. $scorm_color_background ='scorm_item_section ';
  2889. $style_item = '';
  2890. }
  2891. if ($item['id'] == $this->current) {
  2892. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2893. } elseif (!in_array($item['type'], $dirTypes)) {
  2894. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2895. }
  2896. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2897. // Learning path title
  2898. $title = $item['title'];
  2899. if (empty ($title)) {
  2900. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2901. }
  2902. $title = Security::remove_XSS($title);
  2903. // Learning path personalization
  2904. // build the LP tree
  2905. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2906. $description = $item['description'];
  2907. if (empty($description)) {
  2908. $description = $title;
  2909. }
  2910. if (in_array($item['type'], $dirTypes)) {
  2911. // Chapters
  2912. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2913. } else {
  2914. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2915. $html .= '<a name="atoc_'.$item['id'].'" />';
  2916. }
  2917. if (in_array($item['type'], $dirTypes)) {
  2918. // Chapter
  2919. // if you want to put an image before, you should use css
  2920. $html .= stripslashes($title);
  2921. } else {
  2922. $this->get_link('http', $item['id'], $toc_list);
  2923. $html .= '<a class="items-list" href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2924. }
  2925. $html .= "</div>";
  2926. if ($scorm_color_background != '') {
  2927. $html .= '</div>';
  2928. }
  2929. $color_counter++;
  2930. }
  2931. $html .= "</div>";
  2932. $html .= "</div>";
  2933. return $html;
  2934. }
  2935. /**
  2936. * Returns an HTML-formatted string ready to display with teacher buttons
  2937. * in LP view menu
  2938. * @return string HTML TOC ready to display
  2939. */
  2940. public function get_teacher_toc_buttons()
  2941. {
  2942. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2943. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2944. $html = '';
  2945. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2946. $gradebook = '';
  2947. if (!empty($_GET['gradebook'])) {
  2948. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2949. }
  2950. if ($this->get_lp_session_id() == api_get_session_id()) {
  2951. $html .= '<div id="actions_lp" class="actions_lp">';
  2952. $html .= '<div class="btn-group">';
  2953. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" . Display::returnFontAswesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2954. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "' target='_parent'>" . Display::returnFontAswesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2955. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'">' . Display::returnFontAswesomeIcon('cog') . get_lang('Settings').'</a>';
  2956. $html .= '</div>';
  2957. $html .= '</div>';
  2958. }
  2959. }
  2960. return $html;
  2961. }
  2962. /**
  2963. * Gets the learnpath maker name - generally the editor's name
  2964. * @return string Learnpath maker name
  2965. */
  2966. public function get_maker()
  2967. {
  2968. if ($this->debug > 0) {
  2969. error_log('New LP - In learnpath::get_maker()', 0);
  2970. }
  2971. if (!empty ($this->maker)) {
  2972. return $this->maker;
  2973. } else {
  2974. return '';
  2975. }
  2976. }
  2977. /**
  2978. * Gets the learnpath name/title
  2979. * @return string Learnpath name/title
  2980. */
  2981. public function get_name()
  2982. {
  2983. if ($this->debug > 0) {
  2984. error_log('New LP - In learnpath::get_name()', 0);
  2985. }
  2986. if (!empty ($this->name)) {
  2987. return $this->name;
  2988. } else {
  2989. return 'N/A';
  2990. }
  2991. }
  2992. /**
  2993. * Gets a link to the resource from the present location, depending on item ID.
  2994. * @param string $type Type of link expected
  2995. * @param integer $item_id Learnpath item ID
  2996. * @return string $provided_toc Link to the lp_item resource
  2997. */
  2998. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  2999. {
  3000. $course_id = $this->get_course_int_id();
  3001. if ($this->debug > 0) {
  3002. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3003. }
  3004. if (empty($item_id)) {
  3005. if ($this->debug > 2) {
  3006. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3007. }
  3008. $item_id = $this->get_current_item_id();
  3009. }
  3010. if (empty($item_id)) {
  3011. if ($this->debug > 2) {
  3012. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3013. }
  3014. //still empty, this means there was no item_id given and we are not in an object context or
  3015. //the object property is empty, return empty link
  3016. $item_id = $this->first();
  3017. return '';
  3018. }
  3019. $file = '';
  3020. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3021. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3022. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3023. $item_id = intval($item_id);
  3024. $sql = "SELECT
  3025. l.lp_type as ltype,
  3026. l.path as lpath,
  3027. li.item_type as litype,
  3028. li.path as lipath,
  3029. li.parameters as liparams
  3030. FROM $lp_table l
  3031. INNER JOIN $lp_item_table li
  3032. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3033. WHERE li.id = $item_id ";
  3034. if ($this->debug > 2) {
  3035. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3036. }
  3037. $res = Database::query($sql);
  3038. if (Database :: num_rows($res) > 0) {
  3039. $row = Database :: fetch_array($res);
  3040. $lp_type = $row['ltype'];
  3041. $lp_path = $row['lpath'];
  3042. $lp_item_type = $row['litype'];
  3043. $lp_item_path = $row['lipath'];
  3044. $lp_item_params = $row['liparams'];
  3045. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3046. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3047. }
  3048. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3049. if ($type == 'http') {
  3050. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3051. } else {
  3052. $course_path = $sys_course_path; //system path
  3053. }
  3054. // 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.
  3055. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3056. $lp_type = 1;
  3057. }
  3058. if ($this->debug > 2) {
  3059. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3060. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3061. }
  3062. // Now go through the specific cases to get the end of the path
  3063. // @todo Use constants instead of int values.
  3064. switch ($lp_type) {
  3065. case 1 :
  3066. if ($lp_item_type == 'dokeos_chapter') {
  3067. $file = 'lp_content.php?type=dir';
  3068. } else {
  3069. require_once 'resourcelinker.inc.php';
  3070. $file = rl_get_resource_link_for_learnpath(
  3071. $course_id,
  3072. $this->get_id(),
  3073. $item_id,
  3074. $this->get_view_id()
  3075. );
  3076. if ($this->debug > 0) {
  3077. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3078. }
  3079. if ($lp_item_type == 'link') {
  3080. if (Link::is_youtube_link($file)) {
  3081. $src = Link::get_youtube_video_id($file);
  3082. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3083. } elseif (Link::isVimeoLink($file)) {
  3084. $src = Link::getVimeoLinkId($file);
  3085. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3086. } else {
  3087. // If the current site is HTTPS and the link is
  3088. // HTTP, browsers will refuse opening the link
  3089. $urlId = api_get_current_access_url_id();
  3090. $url = api_get_access_url($urlId, false);
  3091. $protocol = substr($url['url'], 0, 5);
  3092. if ($protocol === 'https') {
  3093. $linkProtocol = substr($file, 0, 5);
  3094. if ($linkProtocol === 'http:') {
  3095. //this is the special intervention case
  3096. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=nonhttps&source=' . urlencode($file);
  3097. }
  3098. }
  3099. }
  3100. } else {
  3101. // Check how much attempts of a exercise exits in lp
  3102. $lp_item_id = $this->get_current_item_id();
  3103. $lp_view_id = $this->get_view_id();
  3104. $prevent_reinit = null;
  3105. if (isset($this->items[$this->current])) {
  3106. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3107. }
  3108. if (empty($provided_toc)) {
  3109. if ($this->debug > 0) {
  3110. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3111. }
  3112. $list = $this->get_toc();
  3113. } else {
  3114. if ($this->debug > 0) {
  3115. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3116. }
  3117. $list = $provided_toc;
  3118. }
  3119. $type_quiz = false;
  3120. foreach ($list as $toc) {
  3121. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3122. $type_quiz = true;
  3123. }
  3124. }
  3125. if ($type_quiz) {
  3126. $lp_item_id = intval($lp_item_id);
  3127. $lp_view_id = intval($lp_view_id);
  3128. $sql = "SELECT count(*) FROM $lp_item_view_table
  3129. WHERE
  3130. c_id = $course_id AND
  3131. lp_item_id='" . $lp_item_id . "' AND
  3132. lp_view_id ='" . $lp_view_id . "' AND
  3133. status='completed'";
  3134. $result = Database::query($sql);
  3135. $row_count = Database :: fetch_row($result);
  3136. $count_item_view = (int) $row_count[0];
  3137. $not_multiple_attempt = 0;
  3138. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3139. $not_multiple_attempt = 1;
  3140. }
  3141. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3142. }
  3143. $tmp_array = explode('/', $file);
  3144. $document_name = $tmp_array[count($tmp_array) - 1];
  3145. if (strpos($document_name, '_DELETED_')) {
  3146. $file = 'blank.php?error=document_deleted';
  3147. }
  3148. }
  3149. }
  3150. break;
  3151. case 2 :
  3152. if ($this->debug > 2) {
  3153. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3154. }
  3155. if ($lp_item_type != 'dir') {
  3156. // Quite complex here:
  3157. // We want to make sure 'http://' (and similar) links can
  3158. // be loaded as is (withouth the Chamilo path in front) but
  3159. // some contents use this form: resource.htm?resource=http://blablabla
  3160. // which means we have to find a protocol at the path's start, otherwise
  3161. // it should not be considered as an external URL.
  3162. //if ($this->prerequisites_match($item_id)) {
  3163. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3164. if ($this->debug > 2) {
  3165. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3166. }
  3167. // Distant url, return as is.
  3168. $file = $lp_item_path;
  3169. } else {
  3170. if ($this->debug > 2) {
  3171. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3172. }
  3173. // Prevent getting untranslatable urls.
  3174. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3175. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3176. // Prepare the path.
  3177. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3178. // TODO: Fix this for urls with protocol header.
  3179. $file = str_replace('//', '/', $file);
  3180. $file = str_replace(':/', '://', $file);
  3181. if (substr($lp_path, -1) == '/') {
  3182. $lp_path = substr($lp_path, 0, -1);
  3183. }
  3184. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3185. // if file not found.
  3186. $decoded = html_entity_decode($lp_item_path);
  3187. list ($decoded) = explode('?', $decoded);
  3188. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3189. require_once 'resourcelinker.inc.php';
  3190. $file = rl_get_resource_link_for_learnpath(
  3191. $course_id,
  3192. $this->get_id(),
  3193. $item_id,
  3194. $this->get_view_id()
  3195. );
  3196. if (empty($file)) {
  3197. $file = 'blank.php?error=document_not_found';
  3198. } else {
  3199. $tmp_array = explode('/', $file);
  3200. $document_name = $tmp_array[count($tmp_array) - 1];
  3201. if (strpos($document_name, '_DELETED_')) {
  3202. $file = 'blank.php?error=document_deleted';
  3203. } else {
  3204. $file = 'blank.php?error=document_not_found';
  3205. }
  3206. }
  3207. } else {
  3208. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3209. }
  3210. }
  3211. }
  3212. // We want to use parameters if they were defined in the imsmanifest
  3213. if (strpos($file, 'blank.php') === false) {
  3214. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3215. }
  3216. } else {
  3217. $file = 'lp_content.php?type=dir';
  3218. }
  3219. break;
  3220. case 3 :
  3221. if ($this->debug > 2) {
  3222. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3223. }
  3224. // Formatting AICC HACP append URL.
  3225. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3226. if (!empty($lp_item_params)) {
  3227. $aicc_append .= $lp_item_params . '&';
  3228. }
  3229. if ($lp_item_type != 'dir') {
  3230. // Quite complex here:
  3231. // We want to make sure 'http://' (and similar) links can
  3232. // be loaded as is (withouth the Chamilo path in front) but
  3233. // some contents use this form: resource.htm?resource=http://blablabla
  3234. // which means we have to find a protocol at the path's start, otherwise
  3235. // it should not be considered as an external URL.
  3236. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3237. if ($this->debug > 2) {
  3238. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3239. }
  3240. // Distant url, return as is.
  3241. $file = $lp_item_path;
  3242. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3243. /*
  3244. if (stristr($file,'<servername>') !== false) {
  3245. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3246. }
  3247. */
  3248. if (stripos($file, '<servername>') !== false) {
  3249. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3250. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3251. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3252. }
  3253. //
  3254. $file .= $aicc_append;
  3255. } else {
  3256. if ($this->debug > 2) {
  3257. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3258. }
  3259. // Prevent getting untranslatable urls.
  3260. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3261. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3262. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3263. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3264. // TODO: Fix this for urls with protocol header.
  3265. $file = str_replace('//', '/', $file);
  3266. $file = str_replace(':/', '://', $file);
  3267. $file .= $aicc_append;
  3268. }
  3269. } else {
  3270. $file = 'lp_content.php?type=dir';
  3271. }
  3272. break;
  3273. case 4 :
  3274. break;
  3275. default :
  3276. break;
  3277. }
  3278. // Replace &amp; by & because &amp; will break URL with params
  3279. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3280. }
  3281. if ($this->debug > 2) {
  3282. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3283. }
  3284. return $file;
  3285. }
  3286. /**
  3287. * Gets the latest usable view or generate a new one
  3288. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3289. * @return integer DB lp_view id
  3290. */
  3291. public function get_view($attempt_num = 0)
  3292. {
  3293. if ($this->debug > 0) {
  3294. error_log('New LP - In learnpath::get_view()', 0);
  3295. }
  3296. $search = '';
  3297. // Use $attempt_num to enable multi-views management (disabled so far).
  3298. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3299. $search = 'AND view_count = ' . $attempt_num;
  3300. }
  3301. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3302. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3303. $course_id = api_get_course_int_id();
  3304. $sessionId = api_get_session_id();
  3305. $sql = "SELECT id, view_count FROM $lp_view_table
  3306. WHERE
  3307. c_id = " . $course_id . " AND
  3308. lp_id = " . $this->get_id() . " AND
  3309. user_id = " . $this->get_user_id() . " AND
  3310. session_id = $sessionId
  3311. $search
  3312. ORDER BY view_count DESC";
  3313. $res = Database::query($sql);
  3314. if (Database :: num_rows($res) > 0) {
  3315. $row = Database :: fetch_array($res);
  3316. $this->lp_view_id = $row['id'];
  3317. } else if (!api_is_invitee()) {
  3318. // There is no database record, create one.
  3319. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3320. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3321. Database::query($sql);
  3322. $id = Database :: insert_id();
  3323. $this->lp_view_id = $id;
  3324. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3325. Database::query($sql);
  3326. }
  3327. return $this->lp_view_id;
  3328. }
  3329. /**
  3330. * Gets the current view id
  3331. * @return integer View ID (from lp_view)
  3332. */
  3333. public function get_view_id()
  3334. {
  3335. if ($this->debug > 0) {
  3336. error_log('New LP - In learnpath::get_view_id()', 0);
  3337. }
  3338. if (!empty ($this->lp_view_id)) {
  3339. return $this->lp_view_id;
  3340. } else {
  3341. return 0;
  3342. }
  3343. }
  3344. /**
  3345. * Gets the update queue
  3346. * @return array Array containing IDs of items to be updated by JavaScript
  3347. */
  3348. public function get_update_queue()
  3349. {
  3350. if ($this->debug > 0) {
  3351. error_log('New LP - In learnpath::get_update_queue()', 0);
  3352. }
  3353. return $this->update_queue;
  3354. }
  3355. /**
  3356. * Gets the user ID
  3357. * @return integer User ID
  3358. */
  3359. public function get_user_id()
  3360. {
  3361. if ($this->debug > 0) {
  3362. error_log('New LP - In learnpath::get_user_id()', 0);
  3363. }
  3364. if (!empty ($this->user_id)) {
  3365. return $this->user_id;
  3366. } else {
  3367. return false;
  3368. }
  3369. }
  3370. /**
  3371. * Checks if any of the items has an audio element attached
  3372. * @return bool True or false
  3373. */
  3374. public function has_audio()
  3375. {
  3376. if ($this->debug > 1) {
  3377. error_log('New LP - In learnpath::has_audio()', 0);
  3378. }
  3379. $has = false;
  3380. foreach ($this->items as $i => $item) {
  3381. if (!empty ($this->items[$i]->audio)) {
  3382. $has = true;
  3383. break;
  3384. }
  3385. }
  3386. return $has;
  3387. }
  3388. /**
  3389. * Logs a message into a file
  3390. * @param string Message to log
  3391. * @return boolean True on success, false on error or if msg empty
  3392. */
  3393. public function log($msg)
  3394. {
  3395. if ($this->debug > 0) {
  3396. error_log('New LP - In learnpath::log()', 0);
  3397. }
  3398. // TODO
  3399. $this->error .= $msg;
  3400. return true;
  3401. }
  3402. /**
  3403. * Moves an item up and down at its level
  3404. * @param integer Item to move up and down
  3405. * @param string Direction 'up' or 'down'
  3406. * @return integer New display order, or false on error
  3407. */
  3408. public function move_item($id, $direction)
  3409. {
  3410. $course_id = api_get_course_int_id();
  3411. if ($this->debug > 0) {
  3412. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3413. }
  3414. if (empty($id) || empty($direction)) {
  3415. return false;
  3416. }
  3417. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3418. $sql_sel = "SELECT *
  3419. FROM " . $tbl_lp_item . "
  3420. WHERE c_id = ".$course_id." AND id = " . $id;
  3421. $res_sel = Database::query($sql_sel);
  3422. // Check if elem exists.
  3423. if (Database :: num_rows($res_sel) < 1) {
  3424. return false;
  3425. }
  3426. // Gather data.
  3427. $row = Database :: fetch_array($res_sel);
  3428. $previous = $row['previous_item_id'];
  3429. $next = $row['next_item_id'];
  3430. $display = $row['display_order'];
  3431. $parent = $row['parent_item_id'];
  3432. $lp = $row['lp_id'];
  3433. // Update the item (switch with previous/next one).
  3434. switch ($direction) {
  3435. case 'up':
  3436. if ($this->debug > 2) {
  3437. error_log('Movement up detected', 0);
  3438. }
  3439. if ($display <= 1) { /*do nothing*/
  3440. } else {
  3441. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3442. WHERE c_id = ".$course_id." AND id = $previous";
  3443. if ($this->debug > 2) {
  3444. error_log('Selecting previous: ' . $sql_sel2, 0);
  3445. }
  3446. $res_sel2 = Database::query($sql_sel2);
  3447. if (Database :: num_rows($res_sel2) < 1) {
  3448. $previous_previous = 0;
  3449. }
  3450. // Gather data.
  3451. $row2 = Database :: fetch_array($res_sel2);
  3452. $previous_previous = $row2['previous_item_id'];
  3453. // Update previous_previous item (switch "next" with current).
  3454. if ($previous_previous != 0) {
  3455. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3456. next_item_id = $id
  3457. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3458. if ($this->debug > 2) {
  3459. error_log($sql_upd2, 0);
  3460. }
  3461. Database::query($sql_upd2);
  3462. }
  3463. // Update previous item (switch with current).
  3464. if ($previous != 0) {
  3465. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3466. next_item_id = $next,
  3467. previous_item_id = $id,
  3468. display_order = display_order +1
  3469. WHERE c_id = ".$course_id." AND id = $previous";
  3470. if ($this->debug > 2) {
  3471. error_log($sql_upd2, 0);
  3472. }
  3473. Database::query($sql_upd2);
  3474. }
  3475. // Update current item (switch with previous).
  3476. if ($id != 0) {
  3477. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3478. next_item_id = $previous,
  3479. previous_item_id = $previous_previous,
  3480. display_order = display_order-1
  3481. WHERE c_id = ".$course_id." AND id = $id";
  3482. if ($this->debug > 2) {
  3483. error_log($sql_upd2, 0);
  3484. }
  3485. Database::query($sql_upd2);
  3486. }
  3487. // Update next item (new previous item).
  3488. if ($next != 0) {
  3489. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3490. WHERE c_id = ".$course_id." AND id = $next";
  3491. if ($this->debug > 2) {
  3492. error_log($sql_upd2, 0);
  3493. }
  3494. Database::query($sql_upd2);
  3495. }
  3496. $display = $display -1;
  3497. }
  3498. break;
  3499. case 'down':
  3500. if ($this->debug > 2) {
  3501. error_log('Movement down detected', 0);
  3502. }
  3503. if ($next == 0) { /* Do nothing. */
  3504. } else {
  3505. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3506. if ($this->debug > 2) {
  3507. error_log('Selecting next: ' . $sql_sel2, 0);
  3508. }
  3509. $res_sel2 = Database::query($sql_sel2);
  3510. if (Database :: num_rows($res_sel2) < 1) {
  3511. $next_next = 0;
  3512. }
  3513. // Gather data.
  3514. $row2 = Database :: fetch_array($res_sel2);
  3515. $next_next = $row2['next_item_id'];
  3516. // Update previous item (switch with current).
  3517. if ($previous != 0) {
  3518. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3519. WHERE c_id = ".$course_id." AND id = $previous";
  3520. Database::query($sql_upd2);
  3521. }
  3522. // Update current item (switch with previous).
  3523. if ($id != 0) {
  3524. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3525. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3526. WHERE c_id = ".$course_id." AND id = $id";
  3527. Database::query($sql_upd2);
  3528. }
  3529. // Update next item (new previous item).
  3530. if ($next != 0) {
  3531. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3532. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3533. WHERE c_id = ".$course_id." AND id = $next";
  3534. Database::query($sql_upd2);
  3535. }
  3536. // Update next_next item (switch "previous" with current).
  3537. if ($next_next != 0) {
  3538. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3539. previous_item_id = $id
  3540. WHERE c_id = ".$course_id." AND id = $next_next";
  3541. Database::query($sql_upd2);
  3542. }
  3543. $display = $display +1;
  3544. }
  3545. break;
  3546. default :
  3547. return false;
  3548. }
  3549. return $display;
  3550. }
  3551. /**
  3552. * Move a learnpath up (display_order)
  3553. * @param integer $lp_id Learnpath ID
  3554. */
  3555. public static function move_up($lp_id)
  3556. {
  3557. $course_id = api_get_course_int_id();
  3558. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3559. $sql = "SELECT * FROM $lp_table
  3560. WHERE c_id = ".$course_id."
  3561. ORDER BY display_order";
  3562. $res = Database::query($sql);
  3563. if ($res === false)
  3564. return false;
  3565. $lps = array ();
  3566. $lp_order = array ();
  3567. $num = Database :: num_rows($res);
  3568. // First check the order is correct, globally (might be wrong because
  3569. // of versions < 1.8.4)
  3570. if ($num > 0) {
  3571. $i = 1;
  3572. while ($row = Database :: fetch_array($res)) {
  3573. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3574. $need_fix = true;
  3575. $sql_u = "UPDATE $lp_table SET display_order = $i
  3576. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3577. Database::query($sql_u);
  3578. }
  3579. $row['display_order'] = $i;
  3580. $lps[$row['id']] = $row;
  3581. $lp_order[$i] = $row['id'];
  3582. $i++;
  3583. }
  3584. }
  3585. if ($num > 1) { // If there's only one element, no need to sort.
  3586. $order = $lps[$lp_id]['display_order'];
  3587. if ($order > 1) { // If it's the first element, no need to move up.
  3588. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3589. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3590. Database::query($sql_u1);
  3591. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3592. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3593. Database::query($sql_u2);
  3594. }
  3595. }
  3596. }
  3597. /**
  3598. * Move a learnpath down (display_order)
  3599. * @param integer $lp_id Learnpath ID
  3600. */
  3601. public static function move_down($lp_id)
  3602. {
  3603. $course_id = api_get_course_int_id();
  3604. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3605. $sql = "SELECT * FROM $lp_table
  3606. WHERE c_id = ".$course_id."
  3607. ORDER BY display_order";
  3608. $res = Database::query($sql);
  3609. if ($res === false) {
  3610. return false;
  3611. }
  3612. $lps = array ();
  3613. $lp_order = array ();
  3614. $num = Database :: num_rows($res);
  3615. $max = 0;
  3616. // First check the order is correct, globally (might be wrong because
  3617. // of versions < 1.8.4).
  3618. if ($num > 0) {
  3619. $i = 1;
  3620. while ($row = Database :: fetch_array($res)) {
  3621. $max = $i;
  3622. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3623. $need_fix = true;
  3624. $sql_u = "UPDATE $lp_table SET display_order = $i
  3625. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3626. Database::query($sql_u);
  3627. }
  3628. $row['display_order'] = $i;
  3629. $lps[$row['id']] = $row;
  3630. $lp_order[$i] = $row['id'];
  3631. $i++;
  3632. }
  3633. }
  3634. if ($num > 1) { // If there's only one element, no need to sort.
  3635. $order = $lps[$lp_id]['display_order'];
  3636. if ($order < $max) { // If it's the first element, no need to move up.
  3637. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3638. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3639. Database::query($sql_u1);
  3640. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3641. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3642. Database::query($sql_u2);
  3643. }
  3644. }
  3645. }
  3646. /**
  3647. * Updates learnpath attributes to point to the next element
  3648. * The last part is similar to set_current_item but processing the other way around
  3649. */
  3650. public function next()
  3651. {
  3652. if ($this->debug > 0) {
  3653. error_log('New LP - In learnpath::next()', 0);
  3654. }
  3655. $this->last = $this->get_current_item_id();
  3656. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3657. $this->autocomplete_parents($this->last);
  3658. $new_index = $this->get_next_index();
  3659. if ($this->debug > 2) {
  3660. error_log('New LP - New index: ' . $new_index, 0);
  3661. }
  3662. $this->index = $new_index;
  3663. if ($this->debug > 2) {
  3664. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3665. }
  3666. $this->current = $this->ordered_items[$new_index];
  3667. if ($this->debug > 2) {
  3668. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3669. }
  3670. }
  3671. /**
  3672. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3673. * class, this might be redefined to allow several behaviours depending on the document type.
  3674. * @param integer Resource ID
  3675. * @return boolean True on success, false otherwise
  3676. */
  3677. public function open($id)
  3678. {
  3679. if ($this->debug > 0) {
  3680. error_log('New LP - In learnpath::open()', 0);
  3681. }
  3682. // TODO:
  3683. // set the current resource attribute to this resource
  3684. // switch on element type (redefine in child class?)
  3685. // set status for this item to "opened"
  3686. // start timer
  3687. // initialise score
  3688. $this->index = 0; //or = the last item seen (see $this->last)
  3689. }
  3690. /**
  3691. * Check that all prerequisites are fulfilled. Returns true and an
  3692. * empty string on succes, returns false
  3693. * and the prerequisite string on error.
  3694. * This function is based on the rules for aicc_script language as
  3695. * described in the SCORM 1.2 CAM documentation page 108.
  3696. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3697. * @return boolean True if prerequisites are matched, false otherwise -
  3698. * Empty string if true returned, prerequisites string otherwise.
  3699. */
  3700. public function prerequisites_match($itemId = null)
  3701. {
  3702. $debug = $this->debug;
  3703. if ($debug > 0) {
  3704. error_log('In learnpath::prerequisites_match()', 0);
  3705. }
  3706. if (empty($itemId)) {
  3707. $itemId = $this->current;
  3708. }
  3709. $currentItem = $this->getItem($itemId);
  3710. if ($currentItem) {
  3711. if ($this->type == 2) {
  3712. // Getting prereq from scorm
  3713. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3714. } else {
  3715. $prereq_string = $currentItem->get_prereq_string();
  3716. }
  3717. if (empty($prereq_string)) {
  3718. if ($debug > 0) {
  3719. error_log('Found prereq_string is empty return true');
  3720. }
  3721. return true;
  3722. }
  3723. // Clean spaces.
  3724. $prereq_string = str_replace(' ', '', $prereq_string);
  3725. if ($debug > 0) {
  3726. error_log('Found prereq_string: ' . $prereq_string, 0);
  3727. }
  3728. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3729. $result = $currentItem->parse_prereq(
  3730. $prereq_string,
  3731. $this->items,
  3732. $this->refs_list,
  3733. $this->get_user_id()
  3734. );
  3735. if ($result === false) {
  3736. $this->set_error_msg($currentItem->prereq_alert);
  3737. }
  3738. } else {
  3739. $result = true;
  3740. if ($debug > 1) {
  3741. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3742. }
  3743. }
  3744. if ($debug > 1) {
  3745. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3746. }
  3747. return $result;
  3748. }
  3749. /**
  3750. * Updates learnpath attributes to point to the previous element
  3751. * The last part is similar to set_current_item but processing the other way around
  3752. */
  3753. public function previous()
  3754. {
  3755. if ($this->debug > 0) {
  3756. error_log('New LP - In learnpath::previous()', 0);
  3757. }
  3758. $this->last = $this->get_current_item_id();
  3759. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3760. $this->autocomplete_parents($this->last);
  3761. $new_index = $this->get_previous_index();
  3762. $this->index = $new_index;
  3763. $this->current = $this->ordered_items[$new_index];
  3764. }
  3765. /**
  3766. * Publishes a learnpath. This basically means show or hide the learnpath
  3767. * to normal users.
  3768. * Can be used as abstract
  3769. * @param integer Learnpath ID
  3770. * @param string New visibility
  3771. */
  3772. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3773. {
  3774. $action = 'visible';
  3775. if ($set_visibility != 1) {
  3776. $action = 'invisible';
  3777. self::toggle_publish($lp_id, 'i');
  3778. }
  3779. return api_item_property_update(
  3780. api_get_course_info(),
  3781. TOOL_LEARNPATH,
  3782. $lp_id,
  3783. $action,
  3784. api_get_user_id()
  3785. );
  3786. }
  3787. /**
  3788. * Publishes a learnpath. This basically means show or hide the learnpath
  3789. * on the course homepage
  3790. * Can be used as abstract
  3791. * @param integer $lp_id Learnpath id
  3792. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3793. * @return bool
  3794. */
  3795. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3796. {
  3797. $course_id = api_get_course_int_id();
  3798. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3799. $lp_id = intval($lp_id);
  3800. $sql = "SELECT * FROM $tbl_lp
  3801. WHERE c_id = ".$course_id." AND id = $lp_id";
  3802. $result = Database::query($sql);
  3803. if (Database::num_rows($result)) {
  3804. $row = Database :: fetch_array($result);
  3805. $name = domesticate($row['name']);
  3806. if ($set_visibility == 'i') {
  3807. $s = $name . " " . get_lang('LearnpathNotPublished');
  3808. $dialogBox = $s;
  3809. $v = 0;
  3810. }
  3811. if ($set_visibility == 'v') {
  3812. $s = $name . " " . get_lang('LearnpathPublished');
  3813. $dialogBox = $s;
  3814. $v = 1;
  3815. }
  3816. } else {
  3817. return false;
  3818. }
  3819. $session_id = api_get_session_id();
  3820. $session_condition = api_get_session_condition($session_id);
  3821. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3822. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3823. $sql = "SELECT * FROM $tbl_tool
  3824. WHERE
  3825. c_id = ".$course_id." AND
  3826. link='$link' and
  3827. image='scormbuilder.gif' and
  3828. link LIKE '$link%'
  3829. $session_condition
  3830. ";
  3831. $result = Database::query($sql);
  3832. $num = Database :: num_rows($result);
  3833. if ($set_visibility == 'i' && $num > 0) {
  3834. $sql = "DELETE FROM $tbl_tool
  3835. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3836. Database::query($sql);
  3837. } elseif ($set_visibility == 'v' && $num == 0) {
  3838. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3839. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3840. Database::query($sql);
  3841. $insertId = Database::insert_id();
  3842. if ($insertId) {
  3843. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3844. Database::query($sql);
  3845. }
  3846. } elseif ($set_visibility == 'v' && $num > 0) {
  3847. $sql = "UPDATE $tbl_tool SET
  3848. c_id = $course_id,
  3849. name = '$name',
  3850. link = '$link',
  3851. image = 'scormbuilder.gif',
  3852. visibility = '$v',
  3853. admin = '0',
  3854. address = 'pastillegris.gif',
  3855. added_tool = 0,
  3856. session_id = $session_id
  3857. WHERE
  3858. c_id = ".$course_id." AND
  3859. (link='$link' and image='scormbuilder.gif' $session_condition)
  3860. ";
  3861. Database::query($sql);
  3862. } else {
  3863. // Parameter and database incompatible, do nothing, exit.
  3864. return false;
  3865. }
  3866. }
  3867. /**
  3868. * Restart the whole learnpath. Return the URL of the first element.
  3869. * Make sure the results are saved with anoter method. This method should probably be
  3870. * redefined in children classes.
  3871. * To use a similar method statically, use the create_new_attempt() method
  3872. * @return string URL to load in the viewer
  3873. */
  3874. public function restart()
  3875. {
  3876. if ($this->debug > 0) {
  3877. error_log('New LP - In learnpath::restart()', 0);
  3878. }
  3879. // TODO
  3880. // Call autosave method to save the current progress.
  3881. //$this->index = 0;
  3882. if (api_is_invitee()) {
  3883. return false;
  3884. }
  3885. $session_id = api_get_session_id();
  3886. $course_id = api_get_course_int_id();
  3887. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3888. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3889. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3890. if ($this->debug > 2) {
  3891. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3892. }
  3893. $res = Database::query($sql);
  3894. $view_id = Database::insert_id();
  3895. if ($view_id) {
  3896. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3897. Database::query($sql);
  3898. $this->lp_view_id = $view_id;
  3899. $this->attempt = $this->attempt + 1;
  3900. } else {
  3901. $this->error = 'Could not insert into item_view table...';
  3902. return false;
  3903. }
  3904. $this->autocomplete_parents($this->current);
  3905. foreach ($this->items as $index => $dummy) {
  3906. $this->items[$index]->restart();
  3907. $this->items[$index]->set_lp_view($this->lp_view_id);
  3908. }
  3909. $this->first();
  3910. return true;
  3911. }
  3912. /**
  3913. * Saves the current item
  3914. * @return boolean
  3915. */
  3916. public function save_current()
  3917. {
  3918. if ($this->debug > 0) {
  3919. error_log('learnpath::save_current()', 0);
  3920. }
  3921. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3922. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3923. if ($this->debug > 2) {
  3924. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3925. }
  3926. if ($this->debug > 2) {
  3927. error_log('' . print_r($this->items, true), 0);
  3928. }
  3929. if (isset($this->items[$this->current]) &&
  3930. is_object($this->items[$this->current])
  3931. ) {
  3932. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3933. $this->autocomplete_parents($this->current);
  3934. $status = $this->items[$this->current]->get_status();
  3935. $this->update_queue[$this->current] = $status;
  3936. return $res;
  3937. }
  3938. return false;
  3939. }
  3940. /**
  3941. * Saves the given item
  3942. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3943. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3944. * @return boolean
  3945. */
  3946. public function save_item($item_id = null, $from_outside = true)
  3947. {
  3948. $debug = $this->debug;
  3949. if ($debug) {
  3950. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3951. }
  3952. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3953. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3954. if (empty($item_id)) {
  3955. $item_id = intval($_REQUEST['id']);
  3956. }
  3957. if (empty($item_id)) {
  3958. $item_id = $this->get_current_item_id();
  3959. }
  3960. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3961. if ($debug) {
  3962. error_log('Object exists');
  3963. }
  3964. // Saving the item.
  3965. $res = $this->items[$item_id]->save(
  3966. $from_outside,
  3967. $this->prerequisites_match($item_id)
  3968. );
  3969. if ($debug) {
  3970. error_log('update_queue before:');
  3971. error_log(print_r($this->update_queue,1));
  3972. }
  3973. $this->autocomplete_parents($item_id);
  3974. $status = $this->items[$item_id]->get_status();
  3975. $this->update_queue[$item_id] = $status;
  3976. if ($debug) {
  3977. error_log('get_status(): ' . $status);
  3978. error_log('update_queue after:');
  3979. error_log(print_r($this->update_queue,1));
  3980. }
  3981. return $res;
  3982. }
  3983. return false;
  3984. }
  3985. /**
  3986. * Saves the last item seen's ID only in case
  3987. */
  3988. public function save_last()
  3989. {
  3990. $course_id = api_get_course_int_id();
  3991. if ($this->debug > 0) {
  3992. error_log('New LP - In learnpath::save_last()', 0);
  3993. }
  3994. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3995. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3996. if (isset($this->current) && !api_is_invitee()) {
  3997. if ($this->debug > 2) {
  3998. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3999. }
  4000. $sql = "UPDATE $table SET
  4001. last_item = " . intval($this->get_current_item_id()). "
  4002. WHERE
  4003. c_id = $course_id AND
  4004. lp_id = " . $this->get_id() . " AND
  4005. user_id = " . $this->get_user_id()." ".$session_condition;
  4006. if ($this->debug > 2) {
  4007. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4008. }
  4009. Database::query($sql);
  4010. }
  4011. if (!api_is_invitee()) {
  4012. // Save progress.
  4013. list($progress, $text) = $this->get_progress_bar_text('%');
  4014. if ($progress >= 0 && $progress <= 100) {
  4015. $progress = (int) $progress;
  4016. $sql = "UPDATE $table SET
  4017. progress = $progress
  4018. WHERE
  4019. c_id = ".$course_id." AND
  4020. lp_id = " . $this->get_id() . " AND
  4021. user_id = " . $this->get_user_id()." ".$session_condition;
  4022. // Ignore errors as some tables might not have the progress field just yet.
  4023. Database::query($sql);
  4024. $this->progress_db = $progress;
  4025. }
  4026. }
  4027. }
  4028. /**
  4029. * Sets the current item ID (checks if valid and authorized first)
  4030. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4031. */
  4032. public function set_current_item($item_id = null)
  4033. {
  4034. if ($this->debug > 0) {
  4035. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4036. }
  4037. if (empty ($item_id)) {
  4038. if ($this->debug > 2) {
  4039. error_log('New LP - No new current item given, ignore...', 0);
  4040. }
  4041. // Do nothing.
  4042. } else {
  4043. if ($this->debug > 2) {
  4044. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4045. }
  4046. if (is_numeric($item_id)) {
  4047. $item_id = intval($item_id);
  4048. // TODO: Check in database here.
  4049. $this->last = $this->current;
  4050. $this->current = $item_id;
  4051. // TODO: Update $this->index as well.
  4052. foreach ($this->ordered_items as $index => $item) {
  4053. if ($item == $this->current) {
  4054. $this->index = $index;
  4055. break;
  4056. }
  4057. }
  4058. if ($this->debug > 2) {
  4059. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4060. }
  4061. } else {
  4062. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4063. }
  4064. }
  4065. }
  4066. /**
  4067. * Sets the encoding
  4068. * @param string New encoding
  4069. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4070. */
  4071. public function set_encoding($enc = 'UTF-8')
  4072. {
  4073. if ($this->debug > 0) {
  4074. error_log('New LP - In learnpath::set_encoding()', 0);
  4075. }
  4076. $course_id = api_get_course_int_id();
  4077. $enc = api_refine_encoding_id($enc);
  4078. if (empty($enc)) {
  4079. $enc = api_get_system_encoding();
  4080. }
  4081. if (api_is_encoding_supported($enc)) {
  4082. $lp = $this->get_id();
  4083. if ($lp != 0) {
  4084. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4085. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4086. $res = Database::query($sql);
  4087. return $res;
  4088. }
  4089. }
  4090. return false;
  4091. }
  4092. /**
  4093. * Sets the JS lib setting in the database directly.
  4094. * This is the JavaScript library file this lp needs to load on startup
  4095. * @param string Proximity setting
  4096. * @return boolean True on update success. False otherwise.
  4097. */
  4098. public function set_jslib($lib = '')
  4099. {
  4100. if ($this->debug > 0) {
  4101. error_log('New LP - In learnpath::set_jslib()', 0);
  4102. }
  4103. $lp = $this->get_id();
  4104. $course_id = api_get_course_int_id();
  4105. if ($lp != 0) {
  4106. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4107. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4108. $res = Database::query($sql);
  4109. return $res;
  4110. } else {
  4111. return false;
  4112. }
  4113. }
  4114. /**
  4115. * Sets the name of the LP maker (publisher) (and save)
  4116. * @param string Optional string giving the new content_maker of this learnpath
  4117. * @return boolean True
  4118. */
  4119. public function set_maker($name = '')
  4120. {
  4121. if ($this->debug > 0) {
  4122. error_log('New LP - In learnpath::set_maker()', 0);
  4123. }
  4124. if (empty ($name))
  4125. return false;
  4126. $this->maker = $name;
  4127. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4128. $course_id = api_get_course_int_id();
  4129. $lp_id = $this->get_id();
  4130. $sql = "UPDATE $lp_table SET
  4131. content_maker = '" . Database::escape_string($this->maker) . "'
  4132. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4133. if ($this->debug > 2) {
  4134. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4135. }
  4136. Database::query($sql);
  4137. return true;
  4138. }
  4139. /**
  4140. * Sets the name of the current learnpath (and save)
  4141. * @param string $name Optional string giving the new name of this learnpath
  4142. * @return boolean True/False
  4143. */
  4144. public function set_name($name = null)
  4145. {
  4146. if ($this->debug > 0) {
  4147. error_log('New LP - In learnpath::set_name()', 0);
  4148. }
  4149. if (empty($name)) {
  4150. return false;
  4151. }
  4152. $this->name = $name;
  4153. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4154. $lp_id = $this->get_id();
  4155. $course_id = api_get_course_int_id();
  4156. $sql = "UPDATE $lp_table SET
  4157. name = '" . Database::escape_string($this->name). "'
  4158. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4159. if ($this->debug > 2) {
  4160. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4161. }
  4162. $result = Database::query($sql);
  4163. // If the lp is visible on the homepage, change his name there.
  4164. if (Database::affected_rows($result)) {
  4165. $session_id = api_get_session_id();
  4166. $session_condition = api_get_session_condition($session_id);
  4167. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4168. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4169. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4170. WHERE
  4171. c_id = $course_id AND
  4172. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4173. Database::query($sql);
  4174. return true;
  4175. } else {
  4176. return false;
  4177. }
  4178. }
  4179. /**
  4180. * Set index specified prefix terms for all items in this path
  4181. * @param string Comma-separated list of terms
  4182. * @param char Xapian term prefix
  4183. * @return boolean False on error, true otherwise
  4184. */
  4185. public function set_terms_by_prefix($terms_string, $prefix)
  4186. {
  4187. $course_id = api_get_course_int_id();
  4188. if (api_get_setting('search_enabled') !== 'true')
  4189. return false;
  4190. if (!extension_loaded('xapian')) {
  4191. return false;
  4192. }
  4193. $terms_string = trim($terms_string);
  4194. $terms = explode(',', $terms_string);
  4195. array_walk($terms, 'trim_value');
  4196. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4197. // Don't do anything if no change, verify only at DB, not the search engine.
  4198. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4199. return false;
  4200. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4201. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4202. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4203. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4204. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4205. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4206. $lp_id = intval($_POST['lp_id']);
  4207. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4208. $result = Database::query($sql);
  4209. $di = new ChamiloIndexer();
  4210. while ($lp_item = Database :: fetch_array($result)) {
  4211. // Get search_did.
  4212. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4213. $sql = 'SELECT * FROM %s
  4214. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4215. LIMIT 1';
  4216. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4217. //echo $sql; echo '<br>';
  4218. $res = Database::query($sql);
  4219. if (Database::num_rows($res) > 0) {
  4220. $se_ref = Database :: fetch_array($res);
  4221. // Compare terms.
  4222. $doc = $di->get_document($se_ref['search_did']);
  4223. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4224. $xterms = array();
  4225. foreach ($xapian_terms as $xapian_term) {
  4226. $xterms[] = substr($xapian_term['name'], 1);
  4227. }
  4228. $dterms = $terms;
  4229. $missing_terms = array_diff($dterms, $xterms);
  4230. $deprecated_terms = array_diff($xterms, $dterms);
  4231. // Save it to search engine.
  4232. foreach ($missing_terms as $term) {
  4233. $doc->add_term($prefix . $term, 1);
  4234. }
  4235. foreach ($deprecated_terms as $term) {
  4236. $doc->remove_term($prefix . $term);
  4237. }
  4238. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4239. $di->getDb()->flush();
  4240. } else {
  4241. //@todo What we should do here?
  4242. }
  4243. }
  4244. return true;
  4245. }
  4246. /**
  4247. * Sets the theme of the LP (local/remote) (and save)
  4248. * @param string Optional string giving the new theme of this learnpath
  4249. * @return bool Returns true if theme name is not empty
  4250. */
  4251. public function set_theme($name = '')
  4252. {
  4253. $course_id = api_get_course_int_id();
  4254. if ($this->debug > 0) {
  4255. error_log('New LP - In learnpath::set_theme()', 0);
  4256. }
  4257. $this->theme = $name;
  4258. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4259. $lp_id = $this->get_id();
  4260. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4261. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4262. if ($this->debug > 2) {
  4263. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4264. }
  4265. Database::query($sql);
  4266. return true;
  4267. }
  4268. /**
  4269. * Sets the image of an LP (and save)
  4270. * @param string Optional string giving the new image of this learnpath
  4271. * @return bool Returns true if theme name is not empty
  4272. */
  4273. public function set_preview_image($name = '')
  4274. {
  4275. $course_id = api_get_course_int_id();
  4276. if ($this->debug > 0) {
  4277. error_log('New LP - In learnpath::set_preview_image()', 0);
  4278. }
  4279. $this->preview_image = $name;
  4280. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4281. $lp_id = $this->get_id();
  4282. $sql = "UPDATE $lp_table SET
  4283. preview_image = '" . Database::escape_string($this->preview_image). "'
  4284. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4285. if ($this->debug > 2) {
  4286. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4287. }
  4288. Database::query($sql);
  4289. return true;
  4290. }
  4291. /**
  4292. * Sets the author of a LP (and save)
  4293. * @param string Optional string giving the new author of this learnpath
  4294. * @return bool Returns true if author's name is not empty
  4295. */
  4296. public function set_author($name = '')
  4297. {
  4298. $course_id = api_get_course_int_id();
  4299. if ($this->debug > 0) {
  4300. error_log('New LP - In learnpath::set_author()', 0);
  4301. }
  4302. $this->author = $name;
  4303. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4304. $lp_id = $this->get_id();
  4305. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4306. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4307. if ($this->debug > 2) {
  4308. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4309. }
  4310. Database::query($sql);
  4311. return true;
  4312. }
  4313. /**
  4314. * Sets the hide_toc_frame parameter of a LP (and save)
  4315. * @param int 1 if frame is hidden 0 then else
  4316. * @return bool Returns true if author's name is not empty
  4317. */
  4318. public function set_hide_toc_frame($hide)
  4319. {
  4320. $course_id = api_get_course_int_id();
  4321. if ($this->debug > 0) {
  4322. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4323. }
  4324. if (intval($hide) == $hide){
  4325. $this->hide_toc_frame = $hide;
  4326. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4327. $lp_id = $this->get_id();
  4328. $sql = "UPDATE $lp_table SET
  4329. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4330. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4331. if ($this->debug > 2) {
  4332. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4333. }
  4334. Database::query($sql);
  4335. return true;
  4336. } else {
  4337. return false;
  4338. }
  4339. }
  4340. /**
  4341. * Sets the prerequisite of a LP (and save)
  4342. * @param int integer giving the new prerequisite of this learnpath
  4343. * @return bool returns true if prerequisite is not empty
  4344. */
  4345. public function set_prerequisite($prerequisite)
  4346. {
  4347. $course_id = api_get_course_int_id();
  4348. if ($this->debug > 0) {
  4349. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4350. }
  4351. $this->prerequisite = intval($prerequisite);
  4352. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4353. $lp_id = $this->get_id();
  4354. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4355. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4356. if ($this->debug > 2) {
  4357. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4358. }
  4359. Database::query($sql);
  4360. return true;
  4361. }
  4362. /**
  4363. * Sets the location/proximity of the LP (local/remote) (and save)
  4364. * @param string Optional string giving the new location of this learnpath
  4365. * @return boolean True on success / False on error
  4366. */
  4367. public function set_proximity($name = '')
  4368. {
  4369. $course_id = api_get_course_int_id();
  4370. if ($this->debug > 0) {
  4371. error_log('New LP - In learnpath::set_proximity()', 0);
  4372. }
  4373. if (empty ($name))
  4374. return false;
  4375. $this->proximity = $name;
  4376. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4377. $lp_id = $this->get_id();
  4378. $sql = "UPDATE $lp_table SET
  4379. content_local = '" . Database::escape_string($name) . "'
  4380. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4381. if ($this->debug > 2) {
  4382. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4383. }
  4384. Database::query($sql);
  4385. return true;
  4386. }
  4387. /**
  4388. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4389. * @param integer DB ID of the item
  4390. */
  4391. public function set_previous_item($id)
  4392. {
  4393. if ($this->debug > 0) {
  4394. error_log('New LP - In learnpath::set_previous_item()', 0);
  4395. }
  4396. $this->last = $id;
  4397. }
  4398. /**
  4399. * Sets use_max_score
  4400. * @param string $use_max_score Optional string giving the new location of this learnpath
  4401. * @return boolean True on success / False on error
  4402. */
  4403. public function set_use_max_score($use_max_score = 1)
  4404. {
  4405. $course_id = api_get_course_int_id();
  4406. if ($this->debug > 0) {
  4407. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4408. }
  4409. $use_max_score = intval($use_max_score);
  4410. $this->use_max_score = $use_max_score;
  4411. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4412. $lp_id = $this->get_id();
  4413. $sql = "UPDATE $lp_table SET
  4414. use_max_score = '" . $this->use_max_score . "'
  4415. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4416. if ($this->debug > 2) {
  4417. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4418. }
  4419. Database::query($sql);
  4420. return true;
  4421. }
  4422. /**
  4423. * Sets and saves the expired_on date
  4424. * @param string $expired_on Optional string giving the new author of this learnpath
  4425. * @return bool Returns true if author's name is not empty
  4426. */
  4427. public function set_expired_on($expired_on)
  4428. {
  4429. $course_id = api_get_course_int_id();
  4430. if ($this->debug > 0) {
  4431. error_log('New LP - In learnpath::set_expired_on()', 0);
  4432. }
  4433. if (!empty($expired_on)) {
  4434. $this->expired_on = api_get_utc_datetime($expired_on);
  4435. } else {
  4436. $this->expired_on = '';
  4437. }
  4438. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4439. $lp_id = $this->get_id();
  4440. $sql = "UPDATE $lp_table SET
  4441. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4442. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4443. if ($this->debug > 2) {
  4444. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4445. }
  4446. Database::query($sql);
  4447. return true;
  4448. }
  4449. /**
  4450. * Sets and saves the publicated_on date
  4451. * @param string $publicated_on Optional string giving the new author of this learnpath
  4452. * @return bool Returns true if author's name is not empty
  4453. */
  4454. public function set_publicated_on($publicated_on)
  4455. {
  4456. $course_id = api_get_course_int_id();
  4457. if ($this->debug > 0) {
  4458. error_log('New LP - In learnpath::set_expired_on()', 0);
  4459. }
  4460. if (!empty($publicated_on)) {
  4461. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4462. } else {
  4463. $this->publicated_on = '';
  4464. }
  4465. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4466. $lp_id = $this->get_id();
  4467. $sql = "UPDATE $lp_table SET
  4468. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4469. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4470. if ($this->debug > 2) {
  4471. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4472. }
  4473. Database::query($sql);
  4474. return true;
  4475. }
  4476. /**
  4477. * Sets and saves the expired_on date
  4478. * @return bool Returns true if author's name is not empty
  4479. */
  4480. public function set_modified_on()
  4481. {
  4482. $course_id = api_get_course_int_id();
  4483. if ($this->debug > 0) {
  4484. error_log('New LP - In learnpath::set_expired_on()', 0);
  4485. }
  4486. $this->modified_on = api_get_utc_datetime();
  4487. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4488. $lp_id = $this->get_id();
  4489. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4490. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4491. if ($this->debug > 2) {
  4492. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4493. }
  4494. Database::query($sql);
  4495. return true;
  4496. }
  4497. /**
  4498. * Sets the object's error message
  4499. * @param string Error message. If empty, reinits the error string
  4500. * @return void
  4501. */
  4502. public function set_error_msg($error = '')
  4503. {
  4504. if ($this->debug > 0) {
  4505. error_log('New LP - In learnpath::set_error_msg()', 0);
  4506. }
  4507. if (empty ($error)) {
  4508. $this->error = '';
  4509. } else {
  4510. $this->error .= $error;
  4511. }
  4512. }
  4513. /**
  4514. * Launches the current item if not 'sco'
  4515. * (starts timer and make sure there is a record ready in the DB)
  4516. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4517. * @return boolean
  4518. */
  4519. public function start_current_item($allow_new_attempt = false)
  4520. {
  4521. if ($this->debug > 0) {
  4522. error_log('New LP - In learnpath::start_current_item()', 0);
  4523. }
  4524. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4525. $type = $this->get_type();
  4526. $item_type = $this->items[$this->current]->get_type();
  4527. if (($type == 2 && $item_type != 'sco') ||
  4528. ($type == 3 && $item_type != 'au') ||
  4529. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4530. ) {
  4531. $this->items[$this->current]->open($allow_new_attempt);
  4532. $this->autocomplete_parents($this->current);
  4533. $prereq_check = $this->prerequisites_match($this->current);
  4534. $this->items[$this->current]->save(false, $prereq_check);
  4535. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4536. } else {
  4537. // If sco, then it is supposed to have been updated by some other call.
  4538. }
  4539. if ($item_type == 'sco') {
  4540. $this->items[$this->current]->restart();
  4541. }
  4542. }
  4543. if ($this->debug > 0) {
  4544. error_log('New LP - End of learnpath::start_current_item()', 0);
  4545. }
  4546. return true;
  4547. }
  4548. /**
  4549. * Stops the processing and counters for the old item (as held in $this->last)
  4550. * @return boolean True/False
  4551. */
  4552. public function stop_previous_item()
  4553. {
  4554. if ($this->debug > 0) {
  4555. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4556. }
  4557. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4558. if ($this->debug > 2) {
  4559. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4560. }
  4561. switch ($this->get_type()) {
  4562. case '3' :
  4563. if ($this->items[$this->last]->get_type() != 'au') {
  4564. if ($this->debug > 2) {
  4565. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4566. }
  4567. $this->items[$this->last]->close();
  4568. //$this->autocomplete_parents($this->last);
  4569. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4570. } else {
  4571. if ($this->debug > 2) {
  4572. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4573. }
  4574. }
  4575. case '2' :
  4576. if ($this->items[$this->last]->get_type() != 'sco') {
  4577. if ($this->debug > 2) {
  4578. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4579. }
  4580. $this->items[$this->last]->close();
  4581. //$this->autocomplete_parents($this->last);
  4582. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4583. } else {
  4584. if ($this->debug > 2) {
  4585. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4586. }
  4587. }
  4588. break;
  4589. case '1' :
  4590. default :
  4591. if ($this->debug > 2) {
  4592. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4593. }
  4594. $this->items[$this->last]->close();
  4595. break;
  4596. }
  4597. } else {
  4598. if ($this->debug > 2) {
  4599. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4600. }
  4601. return false;
  4602. }
  4603. return true;
  4604. }
  4605. /**
  4606. * Updates the default view mode from fullscreen to embedded and inversely
  4607. * @return string The current default view mode ('fullscreen' or 'embedded')
  4608. */
  4609. public function update_default_view_mode()
  4610. {
  4611. $course_id = api_get_course_int_id();
  4612. if ($this->debug > 0) {
  4613. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4614. }
  4615. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4616. $sql = "SELECT * FROM $lp_table
  4617. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4618. $res = Database::query($sql);
  4619. if (Database :: num_rows($res) > 0) {
  4620. $row = Database :: fetch_array($res);
  4621. $default_view_mode = $row['default_view_mod'];
  4622. $view_mode = $default_view_mode;
  4623. switch ($default_view_mode) {
  4624. case 'fullscreen':
  4625. $view_mode = 'embedded';
  4626. break;
  4627. case 'embedded':
  4628. $view_mode = 'embedframe';
  4629. break;
  4630. case 'embedframe':
  4631. $view_mode = 'impress';
  4632. break;
  4633. case 'impress':
  4634. $view_mode = 'fullscreen';
  4635. break;
  4636. }
  4637. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4638. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4639. Database::query($sql);
  4640. $this->mode = $view_mode;
  4641. return $view_mode;
  4642. } else {
  4643. if ($this->debug > 2) {
  4644. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4645. }
  4646. }
  4647. return -1;
  4648. }
  4649. /**
  4650. * Updates the default behaviour about auto-commiting SCORM updates
  4651. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4652. */
  4653. public function update_default_scorm_commit()
  4654. {
  4655. $course_id = api_get_course_int_id();
  4656. if ($this->debug > 0) {
  4657. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4658. }
  4659. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4660. $sql = "SELECT * FROM $lp_table
  4661. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4662. $res = Database::query($sql);
  4663. if (Database :: num_rows($res) > 0) {
  4664. $row = Database :: fetch_array($res);
  4665. $force = $row['force_commit'];
  4666. if ($force == 1) {
  4667. $force = 0;
  4668. $force_return = false;
  4669. } elseif ($force == 0) {
  4670. $force = 1;
  4671. $force_return = true;
  4672. }
  4673. $sql = "UPDATE $lp_table SET force_commit = $force
  4674. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4675. Database::query($sql);
  4676. $this->force_commit = $force_return;
  4677. return $force_return;
  4678. } else {
  4679. if ($this->debug > 2) {
  4680. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4681. }
  4682. }
  4683. return -1;
  4684. }
  4685. /**
  4686. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4687. * @return bool True on success, false on failure
  4688. */
  4689. public function update_display_order()
  4690. {
  4691. $course_id = api_get_course_int_id();
  4692. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4693. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4694. $res = Database::query($sql);
  4695. if ($res === false)
  4696. return false;
  4697. $num = Database :: num_rows($res);
  4698. // First check the order is correct, globally (might be wrong because
  4699. // of versions < 1.8.4).
  4700. if ($num > 0) {
  4701. $i = 1;
  4702. while ($row = Database :: fetch_array($res)) {
  4703. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4704. $need_fix = true;
  4705. $sql = "UPDATE $lp_table SET display_order = $i
  4706. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4707. Database::query($sql);
  4708. }
  4709. $i++;
  4710. }
  4711. }
  4712. return true;
  4713. }
  4714. /**
  4715. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4716. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4717. */
  4718. public function update_reinit()
  4719. {
  4720. $course_id = api_get_course_int_id();
  4721. if ($this->debug > 0) {
  4722. error_log('New LP - In learnpath::update_reinit()', 0);
  4723. }
  4724. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4725. $sql = "SELECT * FROM $lp_table
  4726. 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. {
  4784. $course_id = api_get_course_int_id();
  4785. switch ($mode) {
  4786. case 'seriousgame' :
  4787. $sg_mode = 1;
  4788. $prevent_reinit = 1;
  4789. break;
  4790. case 'single' :
  4791. $sg_mode = 0;
  4792. $prevent_reinit = 1;
  4793. break;
  4794. case 'multiple' :
  4795. $sg_mode = 0;
  4796. $prevent_reinit = 0;
  4797. break;
  4798. default :
  4799. $sg_mode = 0;
  4800. $prevent_reinit = 0;
  4801. break;
  4802. }
  4803. $this->prevent_reinit = $prevent_reinit;
  4804. $this->seriousgame_mode = $sg_mode;
  4805. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4806. $sql = "UPDATE $lp_table SET
  4807. prevent_reinit = $prevent_reinit ,
  4808. seriousgame_mode = $sg_mode
  4809. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4810. $res = Database::query($sql);
  4811. if ($res) {
  4812. return true;
  4813. } else {
  4814. return false;
  4815. }
  4816. }
  4817. /**
  4818. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4819. *
  4820. * @return boolean
  4821. * @author ndiechburg <noel@cblue.be>
  4822. **/
  4823. public function switch_attempt_mode()
  4824. {
  4825. if ($this->debug > 0) {
  4826. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4827. }
  4828. $mode = $this->get_attempt_mode();
  4829. switch ($mode) {
  4830. case 'single' :
  4831. $next_mode = 'multiple';
  4832. break;
  4833. case 'multiple' :
  4834. $next_mode = 'seriousgame';
  4835. break;
  4836. case 'seriousgame' :
  4837. $next_mode = 'single';
  4838. break;
  4839. default :
  4840. $next_mode = 'single';
  4841. break;
  4842. }
  4843. $this->set_attempt_mode($next_mode);
  4844. }
  4845. /**
  4846. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4847. * but possibility to do again a completed item.
  4848. *
  4849. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4850. * @author ndiechburg <noel@cblue.be>
  4851. **/
  4852. public function set_seriousgame_mode()
  4853. {
  4854. $course_id = api_get_course_int_id();
  4855. if ($this->debug > 0) {
  4856. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4857. }
  4858. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4859. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4860. $res = Database::query($sql);
  4861. if (Database :: num_rows($res) > 0) {
  4862. $row = Database :: fetch_array($res);
  4863. $force = $row['seriousgame_mode'];
  4864. if ($force == 1) {
  4865. $force = 0;
  4866. } elseif ($force == 0) {
  4867. $force = 1;
  4868. }
  4869. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4870. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4871. Database::query($sql);
  4872. $this->seriousgame_mode = $force;
  4873. return $force;
  4874. } else {
  4875. if ($this->debug > 2) {
  4876. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4877. }
  4878. }
  4879. return -1;
  4880. }
  4881. /**
  4882. * Updates the "scorm_debug" value that shows or hide the debug window
  4883. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4884. */
  4885. public function update_scorm_debug()
  4886. {
  4887. $course_id = api_get_course_int_id();
  4888. if ($this->debug > 0) {
  4889. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4890. }
  4891. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4892. $sql = "SELECT * FROM $lp_table
  4893. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4894. $res = Database::query($sql);
  4895. if (Database :: num_rows($res) > 0) {
  4896. $row = Database :: fetch_array($res);
  4897. $force = $row['debug'];
  4898. if ($force == 1) {
  4899. $force = 0;
  4900. } elseif ($force == 0) {
  4901. $force = 1;
  4902. }
  4903. $sql = "UPDATE $lp_table SET debug = $force
  4904. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4905. $res = Database::query($sql);
  4906. $this->scorm_debug = $force;
  4907. return $force;
  4908. } else {
  4909. if ($this->debug > 2) {
  4910. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4911. }
  4912. }
  4913. return -1;
  4914. }
  4915. /**
  4916. * Function that makes a call to the function sort_tree_array and create_tree_array
  4917. * @author Kevin Van Den Haute
  4918. * @param array
  4919. */
  4920. public function tree_array($array)
  4921. {
  4922. if ($this->debug > 1) {
  4923. error_log('New LP - In learnpath::tree_array()', 0);
  4924. }
  4925. $array = $this->sort_tree_array($array);
  4926. $this->create_tree_array($array);
  4927. }
  4928. /**
  4929. * Creates an array with the elements of the learning path tree in it
  4930. *
  4931. * @author Kevin Van Den Haute
  4932. * @param array $array
  4933. * @param int $parent
  4934. * @param int $depth
  4935. * @param array $tmp
  4936. */
  4937. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4938. {
  4939. if ($this->debug > 1) {
  4940. error_log('New LP - In learnpath::create_tree_array())', 0);
  4941. }
  4942. if (is_array($array)) {
  4943. for ($i = 0; $i < count($array); $i++) {
  4944. if ($array[$i]['parent_item_id'] == $parent) {
  4945. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4946. $tmp[] = $array[$i]['parent_item_id'];
  4947. $depth++;
  4948. }
  4949. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4950. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4951. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4952. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4953. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4954. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4955. $this->arrMenu[] = array(
  4956. 'id' => $array[$i]['id'],
  4957. 'ref' => $ref,
  4958. 'item_type' => $array[$i]['item_type'],
  4959. 'title' => $array[$i]['title'],
  4960. 'path' => $path,
  4961. 'description' => $array[$i]['description'],
  4962. 'parent_item_id' => $array[$i]['parent_item_id'],
  4963. 'previous_item_id' => $array[$i]['previous_item_id'],
  4964. 'next_item_id' => $array[$i]['next_item_id'],
  4965. 'min_score' => $array[$i]['min_score'],
  4966. 'max_score' => $array[$i]['max_score'],
  4967. 'mastery_score' => $array[$i]['mastery_score'],
  4968. 'display_order' => $array[$i]['display_order'],
  4969. 'prerequisite' => $preq,
  4970. 'depth' => $depth,
  4971. 'audio' => $audio,
  4972. 'prerequisite_min_score' => $prerequisiteMinScore,
  4973. 'prerequisite_max_score' => $prerequisiteMaxScore
  4974. );
  4975. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4976. }
  4977. }
  4978. }
  4979. }
  4980. /**
  4981. * Sorts a multi dimensional array by parent id and display order
  4982. * @author Kevin Van Den Haute
  4983. *
  4984. * @param array $array (array with al the learning path items in it)
  4985. *
  4986. * @return array
  4987. */
  4988. public function sort_tree_array($array) {
  4989. foreach ($array as $key => $row) {
  4990. $parent[$key] = $row['parent_item_id'];
  4991. $position[$key] = $row['display_order'];
  4992. }
  4993. if (count($array) > 0)
  4994. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4995. return $array;
  4996. }
  4997. /**
  4998. * Function that creates a html list of learning path items so that we can add audio files to them
  4999. * @author Kevin Van Den Haute
  5000. * @param int $lp_id
  5001. * @return string
  5002. */
  5003. public function overview()
  5004. {
  5005. if ($this->debug > 0) {
  5006. error_log('New LP - In learnpath::overview()', 0);
  5007. }
  5008. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5009. $return = '';
  5010. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5011. // we need to start a form when we want to update all the mp3 files
  5012. if ($update_audio == 'true') {
  5013. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&action=' . Security :: remove_XSS($_GET['action']) . '&lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5014. }
  5015. $return .= '<div id="message"></div>';
  5016. if (count($this->items) == 0) {
  5017. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5018. } else {
  5019. $return_audio = '<table class="data_table">';
  5020. $return_audio .= '<tr>';
  5021. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5022. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5023. $return_audio .= '</tr>';
  5024. if ($update_audio != 'true') {
  5025. $return .= '<div class="col-md-12">';
  5026. $return .= self::return_new_tree($update_audio);
  5027. $return .='</div>';
  5028. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5029. } else {
  5030. $return_audio .= self::return_new_tree($update_audio);
  5031. $return .= $return_audio.'</table>';
  5032. }
  5033. // We need to close the form when we are updating the mp3 files.
  5034. if ($update_audio == 'true') {
  5035. $return .= '<div class="footer-audio">';
  5036. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5037. $return .= '</div>';
  5038. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5039. }
  5040. }
  5041. // We need to close the form when we are updating the mp3 files.
  5042. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5043. $return .= '</form>';
  5044. }
  5045. return $return;
  5046. }
  5047. /**
  5048. * @param string string $update_audio
  5049. * @param bool $drop_element_here
  5050. * @return string
  5051. */
  5052. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5053. {
  5054. $return = '';
  5055. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5056. $course_id = api_get_course_int_id();
  5057. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5058. $sql = "SELECT * FROM $tbl_lp_item
  5059. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5060. $result = Database::query($sql);
  5061. $arrLP = array();
  5062. while ($row = Database :: fetch_array($result)) {
  5063. $arrLP[] = array(
  5064. 'id' => $row['id'],
  5065. 'item_type' => $row['item_type'],
  5066. 'title' => Security :: remove_XSS($row['title']),
  5067. 'path' => $row['path'],
  5068. 'description' => Security::remove_XSS($row['description']),
  5069. 'parent_item_id' => $row['parent_item_id'],
  5070. 'previous_item_id' => $row['previous_item_id'],
  5071. 'next_item_id' => $row['next_item_id'],
  5072. 'max_score' => $row['max_score'],
  5073. 'min_score' => $row['min_score'],
  5074. 'mastery_score' => $row['mastery_score'],
  5075. 'prerequisite' => $row['prerequisite'],
  5076. 'display_order' => $row['display_order'],
  5077. 'audio' => $row['audio'],
  5078. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5079. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5080. );
  5081. }
  5082. $this->tree_array($arrLP);
  5083. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5084. unset ($this->arrMenu);
  5085. $default_data = null;
  5086. $default_content = null;
  5087. $elements = array();
  5088. $return_audio = null;
  5089. for ($i = 0; $i < count($arrLP); $i++) {
  5090. $title = $arrLP[$i]['title'];
  5091. $title_cut = cut($arrLP[$i]['title'], 25);
  5092. //Link for the documents
  5093. if ($arrLP[$i]['item_type'] == 'document') {
  5094. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5095. $title_cut = Display::url(
  5096. $title_cut,
  5097. $url,
  5098. array(
  5099. 'class' => 'ajax',
  5100. 'data-title' => $title_cut
  5101. )
  5102. );
  5103. }
  5104. if (($i % 2) == 0) {
  5105. $oddClass = 'row_odd';
  5106. } else {
  5107. $oddClass = 'row_even';
  5108. }
  5109. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5110. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5111. $icon = '';
  5112. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5113. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5114. } else {
  5115. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5116. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5117. } else {
  5118. $icon = '<img src="../img/folder_document.gif" />';
  5119. }
  5120. }
  5121. // The audio column.
  5122. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5123. $audio = '';
  5124. if (!$update_audio || $update_audio <> 'true') {
  5125. if (!empty($arrLP[$i]['audio'])) {
  5126. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5127. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5128. $audio .= '<script type="text/javascript">
  5129. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5130. s1.addParam("allowscriptaccess","always");
  5131. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5132. s1.write("container' . $i . '");
  5133. </script>';*/
  5134. } else {
  5135. $audio .= '';
  5136. }
  5137. } else {
  5138. $types = self::getChapterTypes();
  5139. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5140. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5141. if (!empty ($arrLP[$i]['audio'])) {
  5142. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5143. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5144. }
  5145. }
  5146. }
  5147. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5148. $return_audio .= '</td>';
  5149. $move_icon = '';
  5150. $move_item_icon = '';
  5151. $edit_icon = '';
  5152. $delete_icon = '';
  5153. $audio_icon = '';
  5154. $prerequisities_icon = '';
  5155. if ($is_allowed_to_edit) {
  5156. if (!$update_audio || $update_audio <> 'true') {
  5157. $move_icon .= '<a class="moved" href="#">';
  5158. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5159. $move_icon .= '</a>';
  5160. }
  5161. // No edit for this item types
  5162. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5163. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5164. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&view=build&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
  5165. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5166. $edit_icon .= '</a>';
  5167. } else {
  5168. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
  5169. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5170. $edit_icon .= '</a>';
  5171. }
  5172. }
  5173. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
  5174. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5175. $delete_icon .= '</a>';
  5176. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5177. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5178. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5179. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5180. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5181. }
  5182. }
  5183. if ($update_audio != 'true') {
  5184. $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'));
  5185. } else {
  5186. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5187. }
  5188. $parent_id = $arrLP[$i]['parent_item_id'];
  5189. $default_data[$arrLP[$i]['id']] = $row;
  5190. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5191. if (empty($parent_id)) {
  5192. $elements[$arrLP[$i]['id']]['data'] = $row;
  5193. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5194. } else {
  5195. $parent_arrays = array();
  5196. if ($arrLP[$i]['depth'] > 1) {
  5197. //Getting list of parents
  5198. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5199. foreach($arrLP as $item) {
  5200. if ($item['id'] == $parent_id) {
  5201. if ($item['parent_item_id'] == 0) {
  5202. $parent_id = $item['id'];
  5203. break;
  5204. } else {
  5205. $parent_id = $item['parent_item_id'];
  5206. if (empty($parent_arrays)) {
  5207. $parent_arrays[] = intval($item['id']);
  5208. }
  5209. $parent_arrays[] = $parent_id;
  5210. break;
  5211. }
  5212. }
  5213. }
  5214. }
  5215. }
  5216. if (!empty($parent_arrays)) {
  5217. $parent_arrays = array_reverse($parent_arrays);
  5218. $val = '$elements';
  5219. $x = 0;
  5220. foreach($parent_arrays as $item) {
  5221. if ($x != count($parent_arrays) -1) {
  5222. $val .= '["'.$item.'"]["children"]';
  5223. } else {
  5224. $val .= '["'.$item.'"]["children"]';
  5225. }
  5226. $x++;
  5227. }
  5228. $val .= "";
  5229. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5230. eval($code_str);
  5231. } else {
  5232. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5233. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5234. }
  5235. }
  5236. }
  5237. $list = '<ul id="lp_item_list">';
  5238. $tree = self::print_recursive($elements, $default_data, $default_content);
  5239. if (!empty($tree)) {
  5240. $list .= $tree;
  5241. } else {
  5242. if ($drop_element_here) {
  5243. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5244. }
  5245. }
  5246. $list .= '</ul>';
  5247. //$return .= Display::panel($list, $this->name);
  5248. $return .= Display::panelCollapse($this->name, $list, 'scorm-list', null, 'scorm-list-accordion', 'scorm-list-collapse');
  5249. if ($update_audio == 'true') {
  5250. $return = $return_audio;
  5251. }
  5252. return $return;
  5253. }
  5254. /**
  5255. * @param array $elements
  5256. * @param array $default_data
  5257. * @param array $default_content
  5258. * @return string
  5259. */
  5260. public function print_recursive($elements, $default_data, $default_content)
  5261. {
  5262. $return = '';
  5263. foreach ($elements as $key => $item) {
  5264. if (isset($item['load_data']) || empty($item['data'])) {
  5265. $item['data'] = $default_data[$item['load_data']];
  5266. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5267. }
  5268. $sub_list = '';
  5269. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5270. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5271. }
  5272. if (empty($item['children'])) {
  5273. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5274. $active = null;
  5275. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5276. $active = 'active';
  5277. }
  5278. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5279. } else {
  5280. //sections
  5281. if (isset($item['children'])) {
  5282. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5283. }
  5284. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5285. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5286. }
  5287. }
  5288. return $return;
  5289. }
  5290. /**
  5291. * This function builds the action menu
  5292. * @param bool $returnContent
  5293. * @return void
  5294. */
  5295. public function build_action_menu($returnContent = false)
  5296. {
  5297. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5298. $return = '<div class="actions">';
  5299. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&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> ';
  5300. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=admin_view&lp_id=' . $_SESSION['oLP']->lp_id . '&updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5301. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&action=edit&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5302. $buttons = array(
  5303. array(
  5304. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5305. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5306. ),
  5307. array(
  5308. 'title' => get_lang('ClearAllPrerequisites'),
  5309. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5310. ),
  5311. );
  5312. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5313. $return .= '</div>';
  5314. if ($returnContent) {
  5315. return $return;
  5316. }
  5317. echo $return;
  5318. }
  5319. /**
  5320. * Creates the default learning path folder
  5321. * @param array $course
  5322. * @return bool
  5323. */
  5324. public static function generate_learning_path_folder($course)
  5325. {
  5326. // Creating learning_path folder
  5327. $dir = '/learning_path';
  5328. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5329. $folder = false;
  5330. if (!is_dir($filepath.'/'.$dir)) {
  5331. $folderData = create_unexisting_directory(
  5332. $course,
  5333. api_get_user_id(),
  5334. api_get_session_id(),
  5335. 0,
  5336. 0,
  5337. $filepath,
  5338. $dir,
  5339. get_lang('LearningPaths'),
  5340. 0
  5341. );
  5342. if (!empty($folderData)) {
  5343. $folder = true;
  5344. }
  5345. } else {
  5346. $folder = true;
  5347. }
  5348. return $folder;
  5349. }
  5350. /**
  5351. * @param array $course
  5352. * @param string $lp_name
  5353. * @return array
  5354. */
  5355. public function generate_lp_folder($course, $lp_name = null)
  5356. {
  5357. $filepath = '';
  5358. $dir = '/learning_path/';
  5359. if (empty($lp_name)) {
  5360. $lp_name = $this->name;
  5361. }
  5362. $folder = self::generate_learning_path_folder($course);
  5363. // Creating LP folder
  5364. if ($folder) {
  5365. //Limits title size
  5366. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5367. $dir = $dir.$title;
  5368. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5369. if (!is_dir($filepath.'/'.$dir)) {
  5370. $folderData = create_unexisting_directory(
  5371. $course,
  5372. api_get_user_id(),
  5373. 0,
  5374. 0,
  5375. 0,
  5376. $filepath,
  5377. $dir,
  5378. $lp_name
  5379. );
  5380. if (!empty($folderData)) {
  5381. $folder = true;
  5382. }
  5383. } else {
  5384. $folder = true;
  5385. }
  5386. $dir = $dir.'/';
  5387. if ($folder) {
  5388. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5389. }
  5390. }
  5391. $array = array(
  5392. 'dir' => $dir,
  5393. 'filepath' => $filepath,
  5394. 'folder' => $folder
  5395. );
  5396. return $array;
  5397. }
  5398. /**
  5399. * Create a new document //still needs some finetuning
  5400. * @param array $_course
  5401. * @return string
  5402. */
  5403. public function create_document($_course)
  5404. {
  5405. $course_id = api_get_course_int_id();
  5406. global $charset;
  5407. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5408. // Please, do not modify this dirname formatting.
  5409. if (strstr($dir, '..'))
  5410. $dir = '/';
  5411. if ($dir[0] == '.')
  5412. $dir = substr($dir, 1);
  5413. if ($dir[0] != '/')
  5414. $dir = '/' . $dir;
  5415. if ($dir[strlen($dir) - 1] != '/')
  5416. $dir .= '/';
  5417. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5418. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5419. //Generates folder
  5420. $result = $this->generate_lp_folder($_course);
  5421. $dir = $result['dir'];
  5422. $filepath = $result['filepath'];
  5423. }
  5424. if (!is_dir($filepath)) {
  5425. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5426. $dir = '/';
  5427. }
  5428. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5429. // is already escaped twice when it gets here.
  5430. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5431. $title = disable_dangerous_file($title);
  5432. $filename = $title;
  5433. $content = $_POST['content_lp'];
  5434. $tmp_filename = $filename;
  5435. $i = 0;
  5436. while (file_exists($filepath . $tmp_filename . '.html'))
  5437. $tmp_filename = $filename . '_' . ++ $i;
  5438. $filename = $tmp_filename . '.html';
  5439. $content = stripslashes($content);
  5440. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5441. // Change the path of mp3 to absolute.
  5442. // The first regexp deals with :// urls.
  5443. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5444. // The second regexp deals with audio/ urls.
  5445. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5446. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5447. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5448. if (!file_exists($filepath . $filename)) {
  5449. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5450. fputs($fp, $content);
  5451. fclose($fp);
  5452. $file_size = filesize($filepath . $filename);
  5453. $save_file_path = $dir.$filename;
  5454. $document_id = add_document(
  5455. $_course,
  5456. $save_file_path,
  5457. 'file',
  5458. $file_size,
  5459. $tmp_filename
  5460. );
  5461. if ($document_id) {
  5462. api_item_property_update(
  5463. $_course,
  5464. TOOL_DOCUMENT,
  5465. $document_id,
  5466. 'DocumentAdded',
  5467. api_get_user_id(),
  5468. null,
  5469. null,
  5470. null,
  5471. null,
  5472. api_get_session_id()
  5473. );
  5474. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5475. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5476. if ($new_comment || $new_title) {
  5477. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5478. $ct = '';
  5479. if ($new_comment)
  5480. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5481. if ($new_title)
  5482. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5483. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5484. Database::query($sql_update);
  5485. }
  5486. }
  5487. return $document_id;
  5488. }
  5489. }
  5490. }
  5491. /**
  5492. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5493. * @param array $_course array
  5494. * @return void
  5495. */
  5496. public function edit_document($_course)
  5497. {
  5498. $course_id = api_get_course_int_id();
  5499. global $_configuration;
  5500. // Please, do not modify this dirname formatting.
  5501. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5502. if (strstr($dir, '..'))
  5503. $dir = '/';
  5504. if ($dir[0] == '.')
  5505. $dir = substr($dir, 1);
  5506. if ($dir[0] != '/')
  5507. $dir = '/' . $dir;
  5508. if ($dir[strlen($dir) - 1] != '/')
  5509. $dir .= '/';
  5510. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5511. if (!is_dir($filepath)) {
  5512. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5513. $dir = '/';
  5514. }
  5515. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5516. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5517. $document_id = intval($_POST['path']);
  5518. $sql = "SELECT path FROM " . $table_doc . "
  5519. WHERE c_id = $course_id AND id = " . $document_id;
  5520. $res = Database::query($sql);
  5521. $row = Database :: fetch_array($res);
  5522. $content = stripslashes($_POST['content_lp']);
  5523. $file = $filepath . $row['path'];
  5524. if ($fp = @ fopen($file, 'w')) {
  5525. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5526. // Change the path of mp3 to absolute.
  5527. // The first regexp deals with :// urls.
  5528. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5529. // The second regexp deals with audio/ urls.
  5530. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5531. fputs($fp, $content);
  5532. fclose($fp);
  5533. $sql = "UPDATE " . $table_doc ." SET
  5534. title='".Database::escape_string($_POST['title'])."'
  5535. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5536. Database::query($sql);
  5537. }
  5538. }
  5539. }
  5540. /**
  5541. * Displays the selected item, with a panel for manipulating the item
  5542. * @param int $item_id
  5543. * @param string $msg
  5544. * @return string
  5545. */
  5546. public function display_item($item_id, $msg = null, $show_actions = true)
  5547. {
  5548. $course_id = api_get_course_int_id();
  5549. $return = '';
  5550. if (is_numeric($item_id)) {
  5551. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5552. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5553. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5554. $result = Database::query($sql);
  5555. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5556. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5557. // Prevents wrong parent selection for document, see Bug#1251.
  5558. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5559. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5560. }
  5561. if ($show_actions) {
  5562. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5563. }
  5564. $return .= '<div style="padding:10px;">';
  5565. if ($msg != '')
  5566. $return .= $msg;
  5567. $return .= '<h3>'.$row['title'].'</h3>';
  5568. switch ($row['item_type']) {
  5569. case TOOL_QUIZ:
  5570. if (!empty($row['path'])) {
  5571. $exercise = new Exercise();
  5572. $exercise->read($row['path']);
  5573. $return .= $exercise->description.'<br />';
  5574. }
  5575. break;
  5576. case TOOL_DOCUMENT:
  5577. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5578. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5579. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5580. $result = Database::query($sql_doc);
  5581. $path_file = Database::result($result, 0, 0);
  5582. $path_parts = pathinfo($path_file);
  5583. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5584. if (in_array($path_parts['extension'], array(
  5585. 'html',
  5586. 'txt',
  5587. 'png',
  5588. 'jpg',
  5589. 'JPG',
  5590. 'jpeg',
  5591. 'JPEG',
  5592. 'gif',
  5593. 'swf'
  5594. ))) {
  5595. $return .= $this->display_document($row['path'], true, true);
  5596. }
  5597. break;
  5598. }
  5599. $return .= '</div>';
  5600. }
  5601. }
  5602. return $return;
  5603. }
  5604. /**
  5605. * Shows the needed forms for editing a specific item
  5606. * @param int $item_id
  5607. * @return string
  5608. */
  5609. public function display_edit_item($item_id)
  5610. {
  5611. $course_id = api_get_course_int_id();
  5612. $return = '';
  5613. if (is_numeric($item_id)) {
  5614. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5615. $sql = "SELECT * FROM $tbl_lp_item
  5616. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5617. $res = Database::query($sql);
  5618. $row = Database::fetch_array($res);
  5619. switch ($row['item_type']) {
  5620. case 'dokeos_chapter' :
  5621. case 'dir' :
  5622. case 'asset' :
  5623. case 'sco' :
  5624. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5625. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5626. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5627. } else {
  5628. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5629. }
  5630. break;
  5631. case TOOL_DOCUMENT :
  5632. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5633. $sql = "SELECT lp.*, doc.path as dir
  5634. FROM " . $tbl_lp_item . " as lp
  5635. LEFT JOIN " . $tbl_doc . " as doc
  5636. ON doc.id = lp.path
  5637. WHERE
  5638. lp.c_id = $course_id AND
  5639. doc.c_id = $course_id AND
  5640. lp.id = " . intval($item_id);
  5641. $res_step = Database::query($sql);
  5642. $row_step = Database :: fetch_array($res_step);
  5643. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5644. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5645. break;
  5646. case TOOL_LINK :
  5647. $link_id = (string) $row['path'];
  5648. if (ctype_digit($link_id)) {
  5649. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5650. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5651. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5652. $res_link = Database::query($sql_select);
  5653. $row_link = Database :: fetch_array($res_link);
  5654. if (is_array($row_link)) {
  5655. $row['url'] = $row_link['url'];
  5656. }
  5657. }
  5658. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5659. $return .= $this->display_link_form('edit', $item_id, $row);
  5660. break;
  5661. case 'dokeos_module' :
  5662. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5663. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5664. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5665. } else {
  5666. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5667. }
  5668. break;
  5669. case TOOL_QUIZ :
  5670. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5671. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5672. break;
  5673. case TOOL_HOTPOTATOES :
  5674. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5675. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5676. break;
  5677. case TOOL_STUDENTPUBLICATION :
  5678. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5679. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5680. break;
  5681. case TOOL_FORUM :
  5682. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5683. $return .= $this->display_forum_form('edit', $item_id, $row);
  5684. break;
  5685. case TOOL_THREAD :
  5686. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5687. $return .= $this->display_thread_form('edit', $item_id, $row);
  5688. break;
  5689. }
  5690. }
  5691. return $return;
  5692. }
  5693. /**
  5694. * Function that displays a list with al the resources that
  5695. * could be added to the learning path
  5696. * @return string
  5697. */
  5698. public function display_resources()
  5699. {
  5700. $course_code = api_get_course_id();
  5701. // Get all the docs.
  5702. $documents = $this->get_documents(true);
  5703. // Get all the exercises.
  5704. $exercises = $this->get_exercises();
  5705. // Get all the links.
  5706. $links = $this->get_links();
  5707. // Get al the student publications.
  5708. $works = $this->get_student_publications();
  5709. // Get al the forums.
  5710. $forums = $this->get_forums(null, $course_code);
  5711. $headers = array(
  5712. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5713. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5714. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5715. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5716. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5717. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5718. );
  5719. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5720. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5721. echo Display::tabs(
  5722. $headers,
  5723. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5724. );
  5725. return true;
  5726. }
  5727. /**
  5728. * Returns the extension of a document
  5729. * @param string filename
  5730. * @return string Extension (part after the last dot)
  5731. */
  5732. public function get_extension($filename)
  5733. {
  5734. $explode = explode('.', $filename);
  5735. return $explode[count($explode) - 1];
  5736. }
  5737. /**
  5738. * Displays a document by id
  5739. *
  5740. * @param int $id
  5741. * @return string
  5742. */
  5743. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5744. {
  5745. $_course = api_get_course_info();
  5746. $course_id = api_get_course_int_id();
  5747. $return = '';
  5748. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5749. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5750. WHERE c_id = ".$course_id." AND id = " . $id;
  5751. $res_doc = Database::query($sql_doc);
  5752. $row_doc = Database :: fetch_array($res_doc);
  5753. // TODO: Add a path filter.
  5754. if ($iframe) {
  5755. $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>';
  5756. } else {
  5757. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5758. }
  5759. return $return;
  5760. }
  5761. /**
  5762. * Return HTML form to add/edit a quiz
  5763. * @param string Action (add/edit)
  5764. * @param integer Item ID if already exists
  5765. * @param mixed Extra information (quiz ID if integer)
  5766. * @return string HTML form
  5767. */
  5768. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5769. {
  5770. $course_id = api_get_course_int_id();
  5771. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5772. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5773. if ($id != 0 && is_array($extra_info)) {
  5774. $item_title = $extra_info['title'];
  5775. $item_description = $extra_info['description'];
  5776. } elseif (is_numeric($extra_info)) {
  5777. $sql = "SELECT title, description
  5778. FROM " . $tbl_quiz . "
  5779. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5780. $result = Database::query($sql);
  5781. $row = Database::fetch_array($result);
  5782. $item_title = $row['title'];
  5783. $item_description = $row['description'];
  5784. } else {
  5785. $item_title = '';
  5786. $item_description = '';
  5787. }
  5788. $item_title = Security::remove_XSS($item_title);
  5789. $item_description = Security::remove_XSS($item_description);
  5790. $legend = '<legend>';
  5791. if ($id != 0 && is_array($extra_info))
  5792. $parent = $extra_info['parent_item_id'];
  5793. else
  5794. $parent = 0;
  5795. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5796. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5797. $result = Database::query($sql);
  5798. $arrLP = array ();
  5799. while ($row = Database :: fetch_array($result)) {
  5800. $arrLP[] = array (
  5801. 'id' => $row['id'],
  5802. 'item_type' => $row['item_type'],
  5803. 'title' => $row['title'],
  5804. 'path' => $row['path'],
  5805. 'description' => $row['description'],
  5806. 'parent_item_id' => $row['parent_item_id'],
  5807. 'previous_item_id' => $row['previous_item_id'],
  5808. 'next_item_id' => $row['next_item_id'],
  5809. 'display_order' => $row['display_order'],
  5810. 'max_score' => $row['max_score'],
  5811. 'min_score' => $row['min_score'],
  5812. 'mastery_score' => $row['mastery_score'],
  5813. 'prerequisite' => $row['prerequisite'],
  5814. 'max_time_allowed' => $row['max_time_allowed']
  5815. );
  5816. }
  5817. $this->tree_array($arrLP);
  5818. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5819. unset ($this->arrMenu);
  5820. if ($action == 'add') {
  5821. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5822. } elseif ($action == 'move') {
  5823. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5824. } else {
  5825. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5826. }
  5827. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5828. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5829. }
  5830. $legend .= '</legend>';
  5831. $return = '';
  5832. $return .= '<div class="sectioncomment">';
  5833. $return .= '<form method="POST">';
  5834. $return .= $legend;
  5835. $return .= '<table class="lp_form">';
  5836. if ($action != 'move') {
  5837. $return .= '<tr>';
  5838. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5839. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5840. $return .= '</tr>';
  5841. }
  5842. $return .= '<tr>';
  5843. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5844. $return .= '<td class="input">';
  5845. // Select for Parent item, root or chapter
  5846. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5847. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5848. $arrHide = array (
  5849. $id
  5850. );
  5851. for ($i = 0; $i < count($arrLP); $i++) {
  5852. if ($action != 'add') {
  5853. 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)) {
  5854. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5855. } else {
  5856. $arrHide[] = $arrLP[$i]['id'];
  5857. }
  5858. } else {
  5859. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5860. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5861. }
  5862. }
  5863. if (is_array($arrLP)) {
  5864. reset($arrLP);
  5865. }
  5866. $return .= '</select>';
  5867. $return .= '</td>';
  5868. $return .= '</tr>';
  5869. $return .= '<tr>';
  5870. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5871. $return .= '<td class="input">';
  5872. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5873. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5874. for ($i = 0; $i < count($arrLP); $i++) {
  5875. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5876. if (is_array($extra_info)) {
  5877. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  5878. $selected = 'selected="selected" ';
  5879. }
  5880. } elseif ($action == 'add') {
  5881. $selected = 'selected="selected" ';
  5882. } else {
  5883. $selected = '';
  5884. }
  5885. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5886. }
  5887. }
  5888. $return .= '</select>';
  5889. $return .= '</td>';
  5890. $return .= '</tr>';
  5891. if ($action != 'move') {
  5892. $id_prerequisite = 0;
  5893. if (is_array($arrLP)) {
  5894. foreach ($arrLP as $key => $value) {
  5895. if ($value['id'] == $id) {
  5896. $id_prerequisite = $value['prerequisite'];
  5897. break;
  5898. }
  5899. }
  5900. }
  5901. $arrHide = array ();
  5902. for ($i = 0; $i < count($arrLP); $i++) {
  5903. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5904. if (is_array($extra_info)) {
  5905. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  5906. $s_selected_position = $arrLP[$i]['id'];
  5907. }
  5908. } elseif ($action == 'add') {
  5909. $s_selected_position = 0;
  5910. }
  5911. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5912. }
  5913. }
  5914. /*// Commented the prerequisites, only visible in edit (exercise).
  5915. $return .= '<tr>';
  5916. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5917. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5918. foreach($arrHide as $key => $value){
  5919. if($key==$s_selected_position && $action == 'add'){
  5920. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5921. }
  5922. elseif($key==$id_prerequisite && $action == 'edit'){
  5923. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5924. }
  5925. else{
  5926. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5927. }
  5928. }
  5929. $return .= "</select></td>";
  5930. */
  5931. $return .= '</tr>';
  5932. /*$return .= '<tr>';
  5933. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5934. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5935. // Remove temporarily the test description.
  5936. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5937. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5938. $return .= '</tr>'; */
  5939. }
  5940. $return .= '<tr>';
  5941. if ($action == 'add') {
  5942. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5943. } else {
  5944. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5945. }
  5946. $return .= '</tr>';
  5947. $return .= '</table>';
  5948. if ($action == 'move') {
  5949. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5950. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5951. }
  5952. if (is_numeric($extra_info)) {
  5953. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5954. } elseif (is_array($extra_info)) {
  5955. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5956. }
  5957. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5958. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5959. $return .= '</form>';
  5960. $return .= '</div>';
  5961. return $return;
  5962. }
  5963. /**
  5964. * Addition of Hotpotatoes tests
  5965. * @param string Action
  5966. * @param integer Internal ID of the item
  5967. * @param mixed Extra information - can be an array with title and description indexes
  5968. * @return string HTML structure to display the hotpotatoes addition formular
  5969. */
  5970. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  5971. {
  5972. $course_id = api_get_course_int_id();
  5973. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5974. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5975. if ($id != 0 && is_array($extra_info)) {
  5976. $item_title = stripslashes($extra_info['title']);
  5977. $item_description = stripslashes($extra_info['description']);
  5978. } elseif (is_numeric($extra_info)) {
  5979. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5980. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  5981. WHERE
  5982. c_id = ".$course_id." AND
  5983. path LIKE '" . $uploadPath . "/%/%htm%' AND
  5984. id = " . (int) $extra_info . "
  5985. ORDER BY id ASC";
  5986. $res_hot = Database::query($sql);
  5987. $row = Database::fetch_array($res_hot);
  5988. $item_title = $row['title'];
  5989. $item_description = $row['description'];
  5990. if (!empty ($row['comment'])) {
  5991. $item_title = $row['comment'];
  5992. }
  5993. } else {
  5994. $item_title = '';
  5995. $item_description = '';
  5996. }
  5997. if ($id != 0 && is_array($extra_info)) {
  5998. $parent = $extra_info['parent_item_id'];
  5999. } else {
  6000. $parent = 0;
  6001. }
  6002. $sql = "SELECT * FROM $tbl_lp_item
  6003. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6004. $result = Database::query($sql);
  6005. $arrLP = array ();
  6006. while ($row = Database :: fetch_array($result)) {
  6007. $arrLP[] = array (
  6008. 'id' => $row['id'],
  6009. 'item_type' => $row['item_type'],
  6010. 'title' => $row['title'],
  6011. 'path' => $row['path'],
  6012. 'description' => $row['description'],
  6013. 'parent_item_id' => $row['parent_item_id'],
  6014. 'previous_item_id' => $row['previous_item_id'],
  6015. 'next_item_id' => $row['next_item_id'],
  6016. 'display_order' => $row['display_order'],
  6017. 'max_score' => $row['max_score'],
  6018. 'min_score' => $row['min_score'],
  6019. 'mastery_score' => $row['mastery_score'],
  6020. 'prerequisite' => $row['prerequisite'],
  6021. 'max_time_allowed' => $row['max_time_allowed']
  6022. );
  6023. }
  6024. $legend = '<legend>';
  6025. if ($action == 'add')
  6026. $legend .= get_lang('CreateTheExercise');
  6027. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6028. else
  6029. $legend .= get_lang('EditCurrentExecice');
  6030. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6031. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6032. }
  6033. $legend .= '</legend>';
  6034. $return = '<form method="POST">';
  6035. $return .= $legend;
  6036. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6037. $return .= '<tr>';
  6038. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6039. $return .= '<td class="input">';
  6040. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6041. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6042. $arrHide = array (
  6043. $id
  6044. );
  6045. if (count($arrLP) > 0) {
  6046. for ($i = 0; $i < count($arrLP); $i++) {
  6047. if ($action != 'add') {
  6048. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6049. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6050. } else {
  6051. $arrHide[] = $arrLP[$i]['id'];
  6052. }
  6053. } else {
  6054. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6055. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6056. }
  6057. }
  6058. reset($arrLP);
  6059. }
  6060. $return .= '</select>';
  6061. $return .= '</td>';
  6062. $return .= '</tr>';
  6063. $return .= '<tr>';
  6064. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6065. $return .= '<td class="input">';
  6066. $return .= '<select id="previous" name="previous" size="1">';
  6067. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6068. for ($i = 0; $i < count($arrLP); $i++) {
  6069. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6070. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6071. $selected = 'selected="selected" ';
  6072. elseif ($action == 'add') $selected = 'selected="selected" ';
  6073. else
  6074. $selected = '';
  6075. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6076. }
  6077. }
  6078. $return .= '</select>';
  6079. $return .= '</td>';
  6080. $return .= '</tr>';
  6081. if ($action != 'move') {
  6082. $return .= '<tr>';
  6083. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6084. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6085. $return .= '</tr>';
  6086. $id_prerequisite = 0;
  6087. if (is_array($arrLP) && count($arrLP) > 0) {
  6088. foreach ($arrLP as $key => $value) {
  6089. if ($value['id'] == $id) {
  6090. $id_prerequisite = $value['prerequisite'];
  6091. break;
  6092. }
  6093. }
  6094. $arrHide = array ();
  6095. for ($i = 0; $i < count($arrLP); $i++) {
  6096. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6097. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6098. $s_selected_position = $arrLP[$i]['id'];
  6099. elseif ($action == 'add') $s_selected_position = 0;
  6100. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6101. }
  6102. }
  6103. }
  6104. }
  6105. $return .= '<tr>';
  6106. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6107. $return .= '</tr>';
  6108. $return .= '</table>';
  6109. if ($action == 'move') {
  6110. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6111. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6112. }
  6113. if (is_numeric($extra_info)) {
  6114. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6115. } elseif (is_array($extra_info)) {
  6116. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6117. }
  6118. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6119. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6120. $return .= '</form>';
  6121. return $return;
  6122. }
  6123. /**
  6124. * Return the form to display the forum edit/add option
  6125. * @param string Action (add/edit)
  6126. * @param integer ID of the lp_item if already exists
  6127. * @param mixed Forum ID or title
  6128. * @return string HTML form
  6129. */
  6130. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6131. {
  6132. $course_id = api_get_course_int_id();
  6133. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6134. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6135. if ($id != 0 && is_array($extra_info)) {
  6136. $item_title = stripslashes($extra_info['title']);
  6137. } elseif (is_numeric($extra_info)) {
  6138. $sql = "SELECT forum_title as title, forum_comment as comment
  6139. FROM " . $tbl_forum . "
  6140. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6141. $result = Database::query($sql);
  6142. $row = Database :: fetch_array($result);
  6143. $item_title = $row['title'];
  6144. $item_description = $row['comment'];
  6145. } else {
  6146. $item_title = '';
  6147. $item_description = '';
  6148. }
  6149. $legend = '<legend>';
  6150. if ($id != 0 && is_array($extra_info)) {
  6151. $parent = $extra_info['parent_item_id'];
  6152. } else {
  6153. $parent = 0;
  6154. }
  6155. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6156. WHERE
  6157. c_id = ".$course_id." AND
  6158. lp_id = " . $this->lp_id;
  6159. $result = Database::query($sql);
  6160. $arrLP = array();
  6161. while ($row = Database :: fetch_array($result)) {
  6162. $arrLP[] = array (
  6163. 'id' => $row['id'],
  6164. 'item_type' => $row['item_type'],
  6165. 'title' => $row['title'],
  6166. 'path' => $row['path'],
  6167. 'description' => $row['description'],
  6168. 'parent_item_id' => $row['parent_item_id'],
  6169. 'previous_item_id' => $row['previous_item_id'],
  6170. 'next_item_id' => $row['next_item_id'],
  6171. 'display_order' => $row['display_order'],
  6172. 'max_score' => $row['max_score'],
  6173. 'min_score' => $row['min_score'],
  6174. 'mastery_score' => $row['mastery_score'],
  6175. 'prerequisite' => $row['prerequisite']
  6176. );
  6177. }
  6178. $this->tree_array($arrLP);
  6179. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6180. unset($this->arrMenu);
  6181. if ($action == 'add')
  6182. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6183. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6184. else
  6185. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6186. $legend .= '</legend>';
  6187. $return = '<div class="sectioncomment">';
  6188. $return .= '<form method="POST">';
  6189. $return .= $legend;
  6190. $return .= '<table class="lp_form">';
  6191. if ($action != 'move') {
  6192. $return .= '<tr>';
  6193. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6194. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6195. $return .= '</tr>';
  6196. }
  6197. $return .= '<tr>';
  6198. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6199. $return .= '<td class="input">';
  6200. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6201. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6202. $arrHide = array(
  6203. $id
  6204. );
  6205. //$parent_item_id = $_SESSION['parent_item_id'];
  6206. for ($i = 0; $i < count($arrLP); $i++) {
  6207. if ($action != 'add') {
  6208. 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)) {
  6209. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6210. } else {
  6211. $arrHide[] = $arrLP[$i]['id'];
  6212. }
  6213. } else {
  6214. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6215. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6216. }
  6217. }
  6218. if (is_array($arrLP)) {
  6219. reset($arrLP);
  6220. }
  6221. $return .= '</select>';
  6222. $return .= '</td>';
  6223. $return .= '</tr>';
  6224. $return .= '<tr>';
  6225. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6226. $return .= '<td class="input">';
  6227. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6228. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6229. for ($i = 0; $i < count($arrLP); $i++) {
  6230. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6231. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6232. $selected = 'selected="selected" ';
  6233. elseif ($action == 'add') $selected = 'selected="selected" ';
  6234. else
  6235. $selected = '';
  6236. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6237. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6238. }
  6239. }
  6240. $return .= '</select>';
  6241. $return .= '</td>';
  6242. $return .= '</tr>';
  6243. if ($action != 'move') {
  6244. $return .= '<tr>';
  6245. $return .= '</tr>';
  6246. $id_prerequisite = 0;
  6247. if (is_array($arrLP)) {
  6248. foreach ($arrLP as $key => $value) {
  6249. if ($value['id'] == $id) {
  6250. $id_prerequisite = $value['prerequisite'];
  6251. break;
  6252. }
  6253. }
  6254. }
  6255. $arrHide = array();
  6256. for ($i = 0; $i < count($arrLP); $i++) {
  6257. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6258. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6259. $s_selected_position = $arrLP[$i]['id'];
  6260. elseif ($action == 'add') $s_selected_position = 0;
  6261. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6262. }
  6263. }
  6264. $return .= '</tr>';
  6265. }
  6266. $return .= '<tr>';
  6267. if ($action == 'add') {
  6268. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6269. } else {
  6270. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6271. }
  6272. $return .= '</tr>';
  6273. $return .= '</table>';
  6274. if ($action == 'move') {
  6275. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6276. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6277. }
  6278. if (is_numeric($extra_info)) {
  6279. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6280. } elseif (is_array($extra_info)) {
  6281. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6282. }
  6283. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6284. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6285. $return .= '</form>';
  6286. $return .= '</div>';
  6287. return $return;
  6288. }
  6289. /**
  6290. * Return HTML form to add/edit forum threads
  6291. * @param string Action (add/edit)
  6292. * @param integer Item ID if already exists in learning path
  6293. * @param mixed Extra information (thread ID if integer)
  6294. * @return string HTML form
  6295. */
  6296. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6297. {
  6298. $course_id = api_get_course_int_id();
  6299. if (empty($course_id)) {
  6300. return null;
  6301. }
  6302. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6303. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6304. if ($id != 0 && is_array($extra_info)) {
  6305. $item_title = stripslashes($extra_info['title']);
  6306. } elseif (is_numeric($extra_info)) {
  6307. $sql = "SELECT thread_title as title FROM $tbl_forum
  6308. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6309. $result = Database::query($sql);
  6310. $row = Database :: fetch_array($result);
  6311. $item_title = $row['title'];
  6312. $item_description = '';
  6313. } else {
  6314. $item_title = '';
  6315. $item_description = '';
  6316. }
  6317. $return = null;
  6318. if ($id != 0 && is_array($extra_info)) {
  6319. $parent = $extra_info['parent_item_id'];
  6320. } else {
  6321. $parent = 0;
  6322. }
  6323. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6324. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6325. $result = Database::query($sql);
  6326. $arrLP = array ();
  6327. while ($row = Database :: fetch_array($result)) {
  6328. $arrLP[] = array (
  6329. 'id' => $row['id'],
  6330. 'item_type' => $row['item_type'],
  6331. 'title' => $row['title'],
  6332. 'path' => $row['path'],
  6333. 'description' => $row['description'],
  6334. 'parent_item_id' => $row['parent_item_id'],
  6335. 'previous_item_id' => $row['previous_item_id'],
  6336. 'next_item_id' => $row['next_item_id'],
  6337. 'display_order' => $row['display_order'],
  6338. 'max_score' => $row['max_score'],
  6339. 'min_score' => $row['min_score'],
  6340. 'mastery_score' => $row['mastery_score'],
  6341. 'prerequisite' => $row['prerequisite']
  6342. );
  6343. }
  6344. $this->tree_array($arrLP);
  6345. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6346. unset ($this->arrMenu);
  6347. $return .= '<form method="POST">';
  6348. if ($action == 'add')
  6349. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6350. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6351. else
  6352. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6353. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6354. $return .= '<tr>';
  6355. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6356. $return .= '<td class="input">';
  6357. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6358. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6359. $arrHide = array (
  6360. $id
  6361. );
  6362. for ($i = 0; $i < count($arrLP); $i++) {
  6363. if ($action != 'add') {
  6364. 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)) {
  6365. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6366. } else {
  6367. $arrHide[] = $arrLP[$i]['id'];
  6368. }
  6369. } else {
  6370. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6371. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6372. }
  6373. }
  6374. if ($arrLP != null) {
  6375. reset($arrLP);
  6376. }
  6377. $return .= '</select>';
  6378. $return .= '</td>';
  6379. $return .= '</tr>';
  6380. $return .= '<tr>';
  6381. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6382. $return .= '<td class="input">';
  6383. $return .= '<select id="previous" name="previous" size="1">';
  6384. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6385. for ($i = 0; $i < count($arrLP); $i++) {
  6386. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6387. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6388. $selected = 'selected="selected" ';
  6389. elseif ($action == 'add') $selected = 'selected="selected" ';
  6390. else
  6391. $selected = '';
  6392. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6393. }
  6394. }
  6395. $return .= '</select>';
  6396. $return .= '</td>';
  6397. $return .= '</tr>';
  6398. if ($action != 'move') {
  6399. $return .= '<tr>';
  6400. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6401. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6402. $return .= '</tr>';
  6403. $return .= '<tr>';
  6404. $return .= '</tr>';
  6405. $id_prerequisite = 0;
  6406. if ($arrLP != null) {
  6407. foreach ($arrLP as $key => $value) {
  6408. if ($value['id'] == $id) {
  6409. $id_prerequisite = $value['prerequisite'];
  6410. break;
  6411. }
  6412. }
  6413. }
  6414. $arrHide = array();
  6415. for ($i = 0; $i < count($arrLP); $i++) {
  6416. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6417. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6418. $s_selected_position = $arrLP[$i]['id'];
  6419. elseif ($action == 'add') $s_selected_position = 0;
  6420. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6421. }
  6422. }
  6423. $return .= '<tr>';
  6424. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6425. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6426. foreach ($arrHide as $key => $value) {
  6427. if ($key == $s_selected_position && $action == 'add') {
  6428. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6429. }
  6430. elseif ($key == $id_prerequisite && $action == 'edit') {
  6431. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6432. } else {
  6433. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6434. }
  6435. }
  6436. $return .= "</select></td>";
  6437. $return .= '</tr>';
  6438. }
  6439. $return .= '<tr>';
  6440. $return .= '<td></td><td>
  6441. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6442. $return .= '</tr>';
  6443. $return .= '</table>';
  6444. if ($action == 'move') {
  6445. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6446. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6447. }
  6448. if (is_numeric($extra_info)) {
  6449. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6450. }
  6451. elseif (is_array($extra_info)) {
  6452. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6453. }
  6454. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6455. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6456. $return .= '</form>';
  6457. $return .= '</div>';
  6458. return $return;
  6459. }
  6460. /**
  6461. * Return the HTML form to display an item (generally a section/module item)
  6462. * @param string Item type (module/dokeos_module)
  6463. * @param string Title (optional, only when creating)
  6464. * @param string Action ('add'/'edit')
  6465. * @param integer lp_item ID
  6466. * @param mixed Extra info
  6467. * @return string HTML form
  6468. */
  6469. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6470. {
  6471. $course_id = api_get_course_int_id();
  6472. $_course = api_get_course_info();
  6473. global $charset;
  6474. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6475. if ($id != 0 && is_array($extra_info)) {
  6476. $item_title = $extra_info['title'];
  6477. $item_description = $extra_info['description'];
  6478. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6479. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6480. } else {
  6481. $item_title = '';
  6482. $item_description = '';
  6483. $item_path_fck = '';
  6484. }
  6485. if ($id != 0 && is_array($extra_info)) {
  6486. $parent = $extra_info['parent_item_id'];
  6487. } else {
  6488. $parent = 0;
  6489. }
  6490. $id = intval($id);
  6491. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6492. WHERE
  6493. c_id = ".$course_id." AND
  6494. lp_id = " . $this->lp_id . " AND
  6495. id != $id";
  6496. if ($item_type == 'module')
  6497. $sql .= " AND parent_item_id = 0";
  6498. $result = Database::query($sql);
  6499. $arrLP = array ();
  6500. while ($row = Database :: fetch_array($result)) {
  6501. $arrLP[] = array(
  6502. 'id' => $row['id'],
  6503. 'item_type' => $row['item_type'],
  6504. 'title' => $row['title'],
  6505. 'path' => $row['path'],
  6506. 'description' => $row['description'],
  6507. 'parent_item_id' => $row['parent_item_id'],
  6508. 'previous_item_id' => $row['previous_item_id'],
  6509. 'next_item_id' => $row['next_item_id'],
  6510. 'max_score' => $row['max_score'],
  6511. 'min_score' => $row['min_score'],
  6512. 'mastery_score' => $row['mastery_score'],
  6513. 'prerequisite' => $row['prerequisite'],
  6514. 'display_order' => $row['display_order']
  6515. );
  6516. }
  6517. $this->tree_array($arrLP);
  6518. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6519. unset ($this->arrMenu);
  6520. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6521. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6522. $form = new FormValidator('form', 'POST', $url);
  6523. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6524. $defaults['description'] = $item_description;
  6525. $form->addElement('header', $title);
  6526. //$arrHide = array($id);
  6527. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6528. $arrHide[0]['padding'] = 3;
  6529. $charset = api_get_system_encoding();
  6530. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6531. for ($i = 0; $i < count($arrLP); $i++) {
  6532. if ($action != 'add') {
  6533. 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)) {
  6534. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6535. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6536. if ($parent == $arrLP[$i]['id']) {
  6537. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6538. }
  6539. }
  6540. } else {
  6541. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6542. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6543. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6544. if ($parent == $arrLP[$i]['id']) {
  6545. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6546. }
  6547. }
  6548. }
  6549. }
  6550. if ($action != 'move') {
  6551. $form->addElement('text', 'title', get_lang('Title'));
  6552. $form->applyFilter('title', 'html_filter');
  6553. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6554. } else {
  6555. $form->addElement('hidden', 'title');
  6556. }
  6557. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6558. foreach ($arrHide as $key => $value) {
  6559. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6560. }
  6561. if (!empty($s_selected_parent)) {
  6562. $parent_select->setSelected($s_selected_parent);
  6563. }
  6564. }
  6565. if (is_array($arrLP)) {
  6566. reset($arrLP);
  6567. }
  6568. $arrHide = array();
  6569. // POSITION
  6570. for ($i = 0; $i < count($arrLP); $i++) {
  6571. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6572. //this is the same!
  6573. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6574. $s_selected_position = $arrLP[$i]['id'];
  6575. } elseif ($action == 'add') {
  6576. $s_selected_position = $arrLP[$i]['id'];
  6577. }
  6578. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6579. }
  6580. }
  6581. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6582. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6583. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6584. foreach ($arrHide as $key => $value) {
  6585. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6586. }
  6587. if (!empty ($s_selected_position)) {
  6588. $position->setSelected($s_selected_position);
  6589. }
  6590. if (is_array($arrLP)) {
  6591. reset($arrLP);
  6592. }
  6593. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6594. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6595. $form->addElement('hidden', 'parent', '0');
  6596. }
  6597. //fix in order to use the tab
  6598. if ($item_type == 'chapter') {
  6599. $form->addElement('hidden', 'type', 'chapter');
  6600. }
  6601. $extension = null;
  6602. if (!empty($item_path)) {
  6603. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6604. }
  6605. //assets can't be modified
  6606. //$item_type == 'asset' ||
  6607. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6608. if ($item_type == 'sco') {
  6609. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6610. }
  6611. $renderer = $form->defaultRenderer();
  6612. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6613. $relative_prefix = '';
  6614. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6615. 'Width' => '100%',
  6616. 'Height' => '500',
  6617. 'FullPage' => true,
  6618. 'CreateDocumentDir' => $relative_prefix,
  6619. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6620. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6621. );
  6622. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6623. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6624. //$defaults['content_lp'] = file_get_contents($item_path);
  6625. $defaults['content_lp'] = file_get_contents($content_path);
  6626. }
  6627. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6628. $form->addElement('hidden', 'post_time', time());
  6629. $form->setDefaults($defaults);
  6630. return $form->return_form();
  6631. }
  6632. /**
  6633. * Returns the form to update or create a document
  6634. * @param string Action (add/edit)
  6635. * @param integer ID of the lp_item (if already exists)
  6636. * @param mixed Integer if document ID, string if info ('new')
  6637. * @return string HTML form
  6638. */
  6639. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6640. {
  6641. $course_id = api_get_course_int_id();
  6642. $_course = api_get_course_info();
  6643. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6644. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6645. $no_display_edit_textarea = false;
  6646. //If action==edit document
  6647. //We don't display the document form if it's not an editable document (html or txt file)
  6648. if ($action == "edit") {
  6649. if (is_array($extra_info)) {
  6650. $path_parts = pathinfo($extra_info['dir']);
  6651. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6652. $no_display_edit_textarea = true;
  6653. }
  6654. }
  6655. }
  6656. $no_display_add = false;
  6657. // If action==add an existing document
  6658. // We don't display the document form if it's not an editable document (html or txt file).
  6659. if ($action == "add") {
  6660. if (is_numeric($extra_info)) {
  6661. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6662. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6663. $result = Database::query($sql_doc);
  6664. $path_file = Database :: result($result, 0, 0);
  6665. $path_parts = pathinfo($path_file);
  6666. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6667. $no_display_add = true;
  6668. }
  6669. }
  6670. }
  6671. if ($id != 0 && is_array($extra_info)) {
  6672. $item_title = stripslashes($extra_info['title']);
  6673. $item_description = stripslashes($extra_info['description']);
  6674. $item_terms = stripslashes($extra_info['terms']);
  6675. if (empty ($item_title)) {
  6676. $path_parts = pathinfo($extra_info['path']);
  6677. $item_title = stripslashes($path_parts['filename']);
  6678. }
  6679. } elseif (is_numeric($extra_info)) {
  6680. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6681. WHERE
  6682. c_id = ".$course_id." AND
  6683. id = " . intval($extra_info);
  6684. $result = Database::query($sql_doc);
  6685. $row = Database::fetch_array($result);
  6686. $item_title = $row['title'];
  6687. $item_title = str_replace('_', ' ', $item_title);
  6688. if (empty ($item_title)) {
  6689. $path_parts = pathinfo($row['path']);
  6690. $item_title = stripslashes($path_parts['filename']);
  6691. }
  6692. } else {
  6693. $item_title = '';
  6694. $item_description = '';
  6695. }
  6696. $return = '<legend>';
  6697. if ($id != 0 && is_array($extra_info)) {
  6698. $parent = $extra_info['parent_item_id'];
  6699. } else {
  6700. $parent = 0;
  6701. }
  6702. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6703. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6704. $result = Database::query($sql);
  6705. $arrLP = array ();
  6706. while ($row = Database :: fetch_array($result)) {
  6707. $arrLP[] = array(
  6708. 'id' => $row['id'],
  6709. 'item_type' => $row['item_type'],
  6710. 'title' => $row['title'],
  6711. 'path' => $row['path'],
  6712. 'description' => $row['description'],
  6713. 'parent_item_id' => $row['parent_item_id'],
  6714. 'previous_item_id' => $row['previous_item_id'],
  6715. 'next_item_id' => $row['next_item_id'],
  6716. 'display_order' => $row['display_order'],
  6717. 'max_score' => $row['max_score'],
  6718. 'min_score' => $row['min_score'],
  6719. 'mastery_score' => $row['mastery_score'],
  6720. 'prerequisite' => $row['prerequisite']
  6721. );
  6722. }
  6723. $this->tree_array($arrLP);
  6724. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6725. unset ($this->arrMenu);
  6726. if ($action == 'add') {
  6727. $return .= get_lang('CreateTheDocument');
  6728. } elseif ($action == 'move') {
  6729. $return .= get_lang('MoveTheCurrentDocument');
  6730. } else {
  6731. $return .= get_lang('EditTheCurrentDocument');
  6732. }
  6733. $return .= '</legend>';
  6734. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6735. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6736. }
  6737. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6738. $defaults['title'] = Security :: remove_XSS($item_title);
  6739. if (empty($item_title)) {
  6740. $defaults['title'] = Security::remove_XSS($item_title);
  6741. }
  6742. $defaults['description'] = $item_description;
  6743. $form->addElement('html', $return);
  6744. if ($action != 'move') {
  6745. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  6746. $form->applyFilter('title', 'html_filter');
  6747. }
  6748. $arrHide[0]['value'] = $this->name;
  6749. $arrHide[0]['padding'] = 3;
  6750. for ($i = 0; $i < count($arrLP); $i++) {
  6751. if ($action != 'add') {
  6752. 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)) {
  6753. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6754. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6755. if ($parent == $arrLP[$i]['id']) {
  6756. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6757. }
  6758. }
  6759. } else {
  6760. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6761. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6762. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6763. if ($parent == $arrLP[$i]['id']) {
  6764. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6765. }
  6766. }
  6767. }
  6768. }
  6769. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="form-control" id="idParent" " onchange="javascript: load_cbo(this.value);"');
  6770. $my_count=0;
  6771. foreach ($arrHide as $key => $value) {
  6772. if ($my_count!=0) {
  6773. // The LP name is also the first section and is not in the same charset like the other sections.
  6774. $value['value'] = Security :: remove_XSS($value['value']);
  6775. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6776. } else {
  6777. $value['value'] = Security :: remove_XSS($value['value']);
  6778. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6779. }
  6780. $my_count++;
  6781. }
  6782. if (!empty($id)) {
  6783. $parent_select->setSelected($parent);
  6784. } else {
  6785. $parent_item_id = $_SESSION['parent_item_id'];
  6786. $parent_select->setSelected($parent_item_id);
  6787. }
  6788. if (is_array($arrLP)) {
  6789. reset($arrLP);
  6790. }
  6791. $arrHide = array();
  6792. $s_selected_position = null;
  6793. //POSITION
  6794. for ($i = 0; $i < count($arrLP); $i++) {
  6795. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6796. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6797. $s_selected_position = $arrLP[$i]['id'];
  6798. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6799. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6800. }
  6801. }
  6802. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="form-control"');
  6803. $position->addOption(get_lang('FirstPosition'), 0);
  6804. foreach ($arrHide as $key => $value) {
  6805. $padding = isset($value['padding']) ? $value['padding']: 0;
  6806. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6807. }
  6808. $position->setSelected($s_selected_position);
  6809. if (is_array($arrLP)) {
  6810. reset($arrLP);
  6811. }
  6812. if ($action != 'move') {
  6813. $id_prerequisite = 0;
  6814. if (is_array($arrLP)) {
  6815. foreach ($arrLP as $key => $value) {
  6816. if ($value['id'] == $id) {
  6817. $id_prerequisite = $value['prerequisite'];
  6818. break;
  6819. }
  6820. }
  6821. }
  6822. $arrHide = array();
  6823. for ($i = 0; $i < count($arrLP); $i++) {
  6824. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6825. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6826. $s_selected_position = $arrLP[$i]['id'];
  6827. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6828. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6829. }
  6830. }
  6831. if (!$no_display_add) {
  6832. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6833. if (isset ($_POST['content']))
  6834. $content = stripslashes($_POST['content']);
  6835. elseif (is_array($extra_info)) {
  6836. //If it's an html document or a text file
  6837. if (!$no_display_edit_textarea) {
  6838. $content = $this->display_document($extra_info['path'], false, false);
  6839. }
  6840. } elseif (is_numeric($extra_info))
  6841. $content = $this->display_document($extra_info, false, false);
  6842. else
  6843. $content = '';
  6844. if (!$no_display_edit_textarea) {
  6845. // We need to calculate here some specific settings for the online editor.
  6846. // The calculated settings work for documents in the Documents tool
  6847. // (on the root or in subfolders).
  6848. // For documents in native scorm packages it is unclear whether the
  6849. // online editor should be activated or not.
  6850. // A new document, it is in the root of the repository.
  6851. $relative_path = '';
  6852. $relative_prefix = '';
  6853. if (is_array($extra_info) && $extra_info != 'new') {
  6854. // The document already exists. Whe have to determine its relative path towards the repository root.
  6855. $relative_path = explode('/', $extra_info['dir']);
  6856. $cnt = count($relative_path) - 2;
  6857. if ($cnt < 0) {
  6858. $cnt = 0;
  6859. }
  6860. $relative_prefix = str_repeat('../', $cnt);
  6861. $relative_path = array_slice($relative_path, 1, $cnt);
  6862. $relative_path = implode('/', $relative_path);
  6863. if (strlen($relative_path) > 0) {
  6864. $relative_path = $relative_path . '/';
  6865. }
  6866. } else {
  6867. $result = $this->generate_lp_folder($_course);
  6868. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6869. $relative_prefix = '../../';
  6870. }
  6871. $editor_config = array(
  6872. 'ToolbarSet'=> 'LearningPathDocuments',
  6873. 'Width' => '100%',
  6874. 'Height' => '500',
  6875. 'FullPage' => true,
  6876. 'CreateDocumentDir' => $relative_prefix,
  6877. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6878. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6879. );
  6880. if ($_GET['action'] == 'add_item') {
  6881. $class = 'add';
  6882. $text = get_lang('LPCreateDocument');
  6883. } else {
  6884. if ($_GET['action'] == 'edit_item') {
  6885. $class = 'save';
  6886. $text = get_lang('SaveDocument');
  6887. }
  6888. }
  6889. $form->addButtonSave($text, 'submit_button');
  6890. $renderer = $form->defaultRenderer();
  6891. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  6892. $form->addElement('html', '<div class="editor-lp">');
  6893. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  6894. $form->addElement('html', '</div>');
  6895. $defaults['content_lp'] = $content;
  6896. }
  6897. } elseif (is_numeric($extra_info)) {
  6898. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6899. $return = $this->display_document($extra_info, true, true, true);
  6900. $form->addElement('html', $return);
  6901. }
  6902. }
  6903. }
  6904. if ($action == 'move') {
  6905. $form->addElement('hidden', 'title', $item_title);
  6906. $form->addElement('hidden', 'description', $item_description);
  6907. }
  6908. if (is_numeric($extra_info)) {
  6909. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6910. $form->addElement('hidden', 'path', $extra_info);
  6911. } elseif (is_array($extra_info)) {
  6912. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6913. $form->addElement('hidden', 'path', $extra_info['path']);
  6914. }
  6915. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6916. $form->addElement('hidden', 'post_time', time());
  6917. $form->setDefaults($defaults);
  6918. return $form->return_form();
  6919. }
  6920. /**
  6921. * Return HTML form to add/edit a link item
  6922. * @param string $action (add/edit)
  6923. * @param integer $id Item ID if exists
  6924. * @param mixed $extra_info
  6925. * @return string HTML form
  6926. */
  6927. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6928. {
  6929. $course_id = api_get_course_int_id();
  6930. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6931. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6932. if ($id != 0 && is_array($extra_info)) {
  6933. $item_title = stripslashes($extra_info['title']);
  6934. $item_description = stripslashes($extra_info['description']);
  6935. $item_url = stripslashes($extra_info['url']);
  6936. } elseif (is_numeric($extra_info)) {
  6937. $extra_info = intval($extra_info);
  6938. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  6939. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6940. $result = Database::query($sql);
  6941. $row = Database :: fetch_array($result);
  6942. $item_title = $row['title'];
  6943. $item_description = $row['description'];
  6944. $item_url = $row['url'];
  6945. } else {
  6946. $item_title = '';
  6947. $item_description = '';
  6948. $item_url = '';
  6949. }
  6950. $legend = '<legend>';
  6951. if ($id != 0 && is_array($extra_info)) {
  6952. $parent = $extra_info['parent_item_id'];
  6953. } else {
  6954. $parent = 0;
  6955. }
  6956. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6957. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6958. $result = Database::query($sql);
  6959. $arrLP = array();
  6960. while ($row = Database :: fetch_array($result)) {
  6961. $arrLP[] = array(
  6962. 'id' => $row['id'],
  6963. 'item_type' => $row['item_type'],
  6964. 'title' => $row['title'],
  6965. 'path' => $row['path'],
  6966. 'description' => $row['description'],
  6967. 'parent_item_id' => $row['parent_item_id'],
  6968. 'previous_item_id' => $row['previous_item_id'],
  6969. 'next_item_id' => $row['next_item_id'],
  6970. 'display_order' => $row['display_order'],
  6971. 'max_score' => $row['max_score'],
  6972. 'min_score' => $row['min_score'],
  6973. 'mastery_score' => $row['mastery_score'],
  6974. 'prerequisite' => $row['prerequisite']
  6975. );
  6976. }
  6977. $this->tree_array($arrLP);
  6978. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6979. unset ($this->arrMenu);
  6980. if ($action == 'add')
  6981. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6982. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6983. else
  6984. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  6985. $legend .= '</legend>';
  6986. $return = '<div class="sectioncomment">';
  6987. $return .= '<form method="POST">';
  6988. $return .= $legend;
  6989. $return .= '<table>';
  6990. if ($action != 'move') {
  6991. $return .= '<tr>';
  6992. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6993. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  6994. $return .= '</tr>';
  6995. }
  6996. $return .= '<tr>';
  6997. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6998. $return .= '<td class="input">';
  6999. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7000. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7001. $arrHide = array(
  7002. $id
  7003. );
  7004. $parent_item_id = $_SESSION['parent_item_id'];
  7005. for ($i = 0; $i < count($arrLP); $i++) {
  7006. if ($action != 'add') {
  7007. 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)) {
  7008. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7009. } else {
  7010. $arrHide[] = $arrLP[$i]['id'];
  7011. }
  7012. } else {
  7013. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7014. $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>';
  7015. }
  7016. }
  7017. if (is_array($arrLP)) {
  7018. reset($arrLP);
  7019. }
  7020. $return .= '</select>';
  7021. $return .= '</td>';
  7022. $return .= '</tr>';
  7023. $return .= '<tr>';
  7024. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7025. $return .= '<td class="input">';
  7026. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7027. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7028. for ($i = 0; $i < count($arrLP); $i++) {
  7029. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7030. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7031. $selected = 'selected="selected" ';
  7032. elseif ($action == 'add')
  7033. $selected = 'selected="selected" ';
  7034. else
  7035. $selected = '';
  7036. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7037. }
  7038. }
  7039. $return .= '</select>';
  7040. $return .= '</td>';
  7041. $return .= '</tr>';
  7042. if ($action != 'move') {
  7043. $return .= '<tr>';
  7044. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7045. $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>';
  7046. $return .= '</tr>';
  7047. $id_prerequisite = 0;
  7048. if (is_array($arrLP)) {
  7049. foreach ($arrLP as $key => $value) {
  7050. if ($value['id'] == $id) {
  7051. $id_prerequisite = $value['prerequisite'];
  7052. break;
  7053. }
  7054. }
  7055. }
  7056. $arrHide = array();
  7057. for ($i = 0; $i < count($arrLP); $i++) {
  7058. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7059. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7060. $s_selected_position = $arrLP[$i]['id'];
  7061. elseif ($action == 'add') $s_selected_position = 0;
  7062. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7063. }
  7064. }
  7065. $return .= '</tr>';
  7066. }
  7067. $return .= '<tr>';
  7068. if ($action == 'add') {
  7069. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7070. } else {
  7071. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7072. }
  7073. $return .= '</tr>';
  7074. $return .= '</table>';
  7075. if ($action == 'move') {
  7076. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7077. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7078. }
  7079. if (is_numeric($extra_info)) {
  7080. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7081. } elseif (is_array($extra_info)) {
  7082. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7083. }
  7084. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7085. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7086. $return .= '</form>';
  7087. $return .= '</div>';
  7088. return $return;
  7089. }
  7090. /**
  7091. * Return HTML form to add/edit a student publication (work)
  7092. * @param string Action (add/edit)
  7093. * @param integer Item ID if already exists
  7094. * @param mixed Extra info (work ID if integer)
  7095. * @return string HTML form
  7096. */
  7097. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7098. {
  7099. $course_id = api_get_course_int_id();
  7100. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7101. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7102. if ($id != 0 && is_array($extra_info)) {
  7103. $item_title = stripslashes($extra_info['title']);
  7104. $item_description = stripslashes($extra_info['description']);
  7105. } elseif (is_numeric($extra_info)) {
  7106. $extra_info = intval($extra_info);
  7107. $sql = "SELECT title, description
  7108. FROM " . $tbl_publication . "
  7109. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7110. $result = Database::query($sql);
  7111. $row = Database :: fetch_array($result);
  7112. $item_title = $row['title'];
  7113. } else {
  7114. $item_title = get_lang('Student_publication');
  7115. }
  7116. $legend = '<legend>';
  7117. if ($id != 0 && is_array($extra_info)) {
  7118. $parent = $extra_info['parent_item_id'];
  7119. } else {
  7120. $parent = 0;
  7121. }
  7122. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7123. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7124. $result = Database::query($sql);
  7125. $arrLP = array();
  7126. while ($row = Database :: fetch_array($result)) {
  7127. $arrLP[] = array (
  7128. 'id' => $row['id'],
  7129. 'item_type' => $row['item_type'],
  7130. 'title' => $row['title'],
  7131. 'path' => $row['path'],
  7132. 'description' => $row['description'],
  7133. 'parent_item_id' => $row['parent_item_id'],
  7134. 'previous_item_id' => $row['previous_item_id'],
  7135. 'next_item_id' => $row['next_item_id'],
  7136. 'display_order' => $row['display_order'],
  7137. 'max_score' => $row['max_score'],
  7138. 'min_score' => $row['min_score'],
  7139. 'mastery_score' => $row['mastery_score'],
  7140. 'prerequisite' => $row['prerequisite']
  7141. );
  7142. }
  7143. $this->tree_array($arrLP);
  7144. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7145. unset ($this->arrMenu);
  7146. if ($action == 'add') {
  7147. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7148. } elseif ($action == 'move') {
  7149. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7150. } else {
  7151. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7152. }
  7153. $legend .= '</legend>';
  7154. $return = '<div class="sectioncomment">';
  7155. $return .= '<form method="POST">';
  7156. $return .= $legend;
  7157. $return .= '<table class="lp_form">';
  7158. if ($action != 'move') {
  7159. $return .= '<tr>';
  7160. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7161. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7162. $return .= '</tr>';
  7163. }
  7164. $return .= '<tr>';
  7165. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7166. $return .= '<td class="input">';
  7167. $return .= '<select id="idParent" name="parent" style="width:100%;" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7168. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7169. $arrHide = array (
  7170. $id
  7171. );
  7172. for ($i = 0; $i < count($arrLP); $i++) {
  7173. if ($action != 'add') {
  7174. 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)) {
  7175. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7176. } else {
  7177. $arrHide[] = $arrLP[$i]['id'];
  7178. }
  7179. } else {
  7180. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7181. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7182. }
  7183. }
  7184. if (is_array($arrLP)) {
  7185. reset($arrLP);
  7186. }
  7187. $return .= '</select>';
  7188. $return .= '</td>';
  7189. $return .= '</tr>';
  7190. $return .= '<tr>';
  7191. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7192. $return .= '<td class="input">';
  7193. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7194. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7195. for ($i = 0; $i < count($arrLP); $i++) {
  7196. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7197. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7198. $selected = 'selected="selected" ';
  7199. elseif ($action == 'add') $selected = 'selected="selected" ';
  7200. else
  7201. $selected = '';
  7202. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7203. }
  7204. }
  7205. $return .= '</select>';
  7206. $return .= '</td>';
  7207. $return .= '</tr>';
  7208. if ($action != 'move') {
  7209. $id_prerequisite = 0;
  7210. if (is_array($arrLP)) {
  7211. foreach ($arrLP as $key => $value) {
  7212. if ($value['id'] == $id) {
  7213. $id_prerequisite = $value['prerequisite'];
  7214. break;
  7215. }
  7216. }
  7217. }
  7218. $arrHide = array ();
  7219. for ($i = 0; $i < count($arrLP); $i++) {
  7220. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7221. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7222. $s_selected_position = $arrLP[$i]['id'];
  7223. elseif ($action == 'add') $s_selected_position = 0;
  7224. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7225. }
  7226. }
  7227. $return .= '</tr>';
  7228. }
  7229. $return .= '<tr>';
  7230. if ($action == 'add') {
  7231. $return .= '<td>&nbsp</td><td><button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7232. } else {
  7233. $return .= '<td>&nbsp</td><td><button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7234. }
  7235. $return .= '</tr>';
  7236. $return .= '</table>';
  7237. if ($action == 'move') {
  7238. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7239. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7240. }
  7241. if (is_numeric($extra_info)) {
  7242. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7243. } elseif (is_array($extra_info)) {
  7244. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7245. }
  7246. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7247. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7248. $return .= '</form>';
  7249. $return .= '</div>';
  7250. return $return;
  7251. }
  7252. /**
  7253. * Displays the menu for manipulating a step
  7254. *
  7255. * @param $item_id
  7256. * @param string $item_type
  7257. * @return string
  7258. */
  7259. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7260. {
  7261. $_course = api_get_course_info();
  7262. $course_id = api_get_course_int_id();
  7263. $course_code = api_get_course_id();
  7264. $return = '<div class="actions">';
  7265. switch ($item_type) {
  7266. case 'dokeos_chapter' :
  7267. case 'chapter' :
  7268. // Commented the message cause should not show it.
  7269. //$lang = get_lang('TitleManipulateChapter');
  7270. break;
  7271. case 'dokeos_module' :
  7272. case 'module' :
  7273. // Commented the message cause should not show it.
  7274. //$lang = get_lang('TitleManipulateModule');
  7275. break;
  7276. case TOOL_DOCUMENT :
  7277. // Commented the message cause should not show it.
  7278. //$lang = get_lang('TitleManipulateDocument');
  7279. break;
  7280. case TOOL_LINK :
  7281. case 'link' :
  7282. // Commented the message cause should not show it.
  7283. //$lang = get_lang('TitleManipulateLink');
  7284. break;
  7285. case TOOL_QUIZ :
  7286. // Commented the message cause should not show it.
  7287. //$lang = get_lang('TitleManipulateQuiz');
  7288. break;
  7289. case TOOL_STUDENTPUBLICATION :
  7290. // Commented the message cause should not show it.
  7291. //$lang = get_lang('TitleManipulateStudentPublication');
  7292. break;
  7293. }
  7294. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7295. $item_id = intval($item_id);
  7296. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7297. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7298. $result = Database::query($sql);
  7299. $row = Database::fetch_assoc($result);
  7300. $audio_player = null;
  7301. // We display an audio player if needed.
  7302. if (!empty($row['audio'])) {
  7303. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7304. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7305. </div>';
  7306. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7307. $audio_player .= '<script>
  7308. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7309. s1.addParam("allowscriptaccess","always");
  7310. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7311. s1.write("container");
  7312. </script>';
  7313. }
  7314. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7315. $return .= Display::url(
  7316. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7317. $url.'&action=edit_item&path_item=' . $row['path']
  7318. );
  7319. $return .= Display::url(
  7320. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7321. $url.'&action=move_item'
  7322. );
  7323. // Commented for now as prerequisites cannot be added to chapters.
  7324. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7325. $return .= Display::url(
  7326. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7327. $url.'&action=edit_item_prereq'
  7328. );
  7329. }
  7330. $return .= Display::url(
  7331. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7332. $url.'&action=delete_item'
  7333. );
  7334. if ($item_type == TOOL_HOTPOTATOES ) {
  7335. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7336. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7337. }
  7338. if ($item_type == TOOL_DOCUMENT ) {
  7339. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7340. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7341. }
  7342. $return .= '</div>';
  7343. if (!empty($audio_player)) {
  7344. $return .= '<br />'.$audio_player;
  7345. }
  7346. return $return;
  7347. }
  7348. /**
  7349. * Creates the javascript needed for filling up the checkboxes without page reload
  7350. * @return string
  7351. */
  7352. public function get_js_dropdown_array()
  7353. {
  7354. $course_id = api_get_course_int_id();
  7355. $return = 'var child_name = new Array();' . "\n";
  7356. $return .= 'var child_value = new Array();' . "\n\n";
  7357. $return .= 'child_name[0] = new Array();' . "\n";
  7358. $return .= 'child_value[0] = new Array();' . "\n\n";
  7359. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7360. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7361. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7362. ORDER BY display_order ASC";
  7363. $res_zero = Database::query($sql_zero);
  7364. $i = 0;
  7365. while ($row_zero = Database :: fetch_array($res_zero)) {
  7366. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7367. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7368. }
  7369. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7370. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7371. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7372. }
  7373. $return .= "\n";
  7374. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7375. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7376. $res = Database::query($sql);
  7377. while ($row = Database :: fetch_array($res)) {
  7378. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7379. WHERE
  7380. c_id = ".$course_id." AND
  7381. parent_item_id = " . $row['id'] . "
  7382. ORDER BY display_order ASC";
  7383. $res_parent = Database::query($sql_parent);
  7384. $i = 0;
  7385. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7386. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7387. while ($row_parent = Database :: fetch_array($res_parent)) {
  7388. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7389. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7390. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7391. }
  7392. $return .= "\n";
  7393. }
  7394. return $return;
  7395. }
  7396. /**
  7397. * Display the form to allow moving an item
  7398. * @param integer Item ID
  7399. * @return string HTML form
  7400. */
  7401. public function display_move_item($item_id)
  7402. {
  7403. $course_id = api_get_course_int_id();
  7404. $return = '';
  7405. if (is_numeric($item_id)) {
  7406. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7407. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7408. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7409. $res = Database::query($sql);
  7410. $row = Database :: fetch_array($res);
  7411. switch ($row['item_type']) {
  7412. case 'dokeos_chapter' :
  7413. case 'dir' :
  7414. case 'asset' :
  7415. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7416. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7417. break;
  7418. case 'dokeos_module' :
  7419. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7420. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7421. break;
  7422. case TOOL_DOCUMENT :
  7423. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7424. $return .= $this->display_document_form('move', $item_id, $row);
  7425. break;
  7426. case TOOL_LINK :
  7427. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7428. $return .= $this->display_link_form('move', $item_id, $row);
  7429. break;
  7430. case TOOL_HOTPOTATOES :
  7431. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7432. $return .= $this->display_link_form('move', $item_id, $row);
  7433. break;
  7434. case TOOL_QUIZ :
  7435. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7436. $return .= $this->display_quiz_form('move', $item_id, $row);
  7437. break;
  7438. case TOOL_STUDENTPUBLICATION :
  7439. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7440. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7441. break;
  7442. case TOOL_FORUM :
  7443. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7444. $return .= $this->display_forum_form('move', $item_id, $row);
  7445. break;
  7446. case TOOL_THREAD :
  7447. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7448. $return .= $this->display_forum_form('move', $item_id, $row);
  7449. break;
  7450. }
  7451. }
  7452. return $return;
  7453. }
  7454. /**
  7455. * Displays a basic form on the overview page for changing the item title and the item description.
  7456. * @param string $item_type
  7457. * @param string $title
  7458. * @param array $data
  7459. * @return string
  7460. */
  7461. public function display_item_small_form($item_type, $title = '', $data = array())
  7462. {
  7463. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7464. $form = new FormValidator('small_form', 'post', $url);
  7465. $form->addElement('header', $title);
  7466. $form->addElement('text', 'title', get_lang('Title'));
  7467. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7468. $form->addElement('hidden', 'id', $data['id']);
  7469. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7470. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7471. $form->setDefaults(array('title' => $data['title']));
  7472. return $form->toHtml();
  7473. }
  7474. /**
  7475. * Return HTML form to allow prerequisites selection
  7476. * @todo use FormValidator
  7477. * @param integer Item ID
  7478. * @return string HTML form
  7479. */
  7480. public function display_item_prerequisites_form($item_id)
  7481. {
  7482. $course_id = api_get_course_int_id();
  7483. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7484. $item_id = intval($item_id);
  7485. /* Current prerequisite */
  7486. $sql = "SELECT * FROM $tbl_lp_item
  7487. WHERE c_id = $course_id AND id = " . $item_id;
  7488. $result = Database::query($sql);
  7489. $row = Database::fetch_array($result);
  7490. $prerequisiteId = $row['prerequisite'];
  7491. $return = '<legend>';
  7492. $return .= get_lang('AddEditPrerequisites');
  7493. $return .= '</legend>';
  7494. $return .= '<form method="POST">';
  7495. $return .= '<table class="data_table">';
  7496. $return .= '<tr>';
  7497. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7498. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7499. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7500. $return .= '</tr>';
  7501. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7502. $return .= '<tr >';
  7503. $return .= '<td colspan="3" class="radio">';
  7504. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7505. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7506. $return .= '</tr>';
  7507. $sql = "SELECT * FROM $tbl_lp_item
  7508. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7509. $result = Database::query($sql);
  7510. $arrLP = array();
  7511. $selectedMinScore = array();
  7512. $selectedMaxScore = array();
  7513. while ($row = Database :: fetch_array($result)) {
  7514. if ($row['id'] == $item_id) {
  7515. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7516. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7517. }
  7518. $arrLP[] = array(
  7519. 'id' => $row['id'],
  7520. 'item_type' => $row['item_type'],
  7521. 'title' => $row['title'],
  7522. 'ref' => $row['ref'],
  7523. 'description' => $row['description'],
  7524. 'parent_item_id' => $row['parent_item_id'],
  7525. 'previous_item_id' => $row['previous_item_id'],
  7526. 'next_item_id' => $row['next_item_id'],
  7527. 'max_score' => $row['max_score'],
  7528. 'min_score' => $row['min_score'],
  7529. 'mastery_score' => $row['mastery_score'],
  7530. 'prerequisite' => $row['prerequisite'],
  7531. 'next_item_id' => $row['next_item_id'],
  7532. 'display_order' => $row['display_order'],
  7533. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7534. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7535. );
  7536. }
  7537. $this->tree_array($arrLP);
  7538. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7539. unset($this->arrMenu);
  7540. for ($i = 0; $i < count($arrLP); $i++) {
  7541. $item = $arrLP[$i];
  7542. if ($item['id'] == $item_id) {
  7543. break;
  7544. }
  7545. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7546. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7547. $return .= '<tr>';
  7548. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7549. $return .= '<label for="id' . $item['id'] . '">';
  7550. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . (($item['item_type'] == 'dokeos_module' || $item['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7551. $icon_name = str_replace(' ', '', $item['item_type']);
  7552. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7553. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7554. } else {
  7555. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7556. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7557. } else {
  7558. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7559. }
  7560. }
  7561. $return .= $item['title'] . '</label>';
  7562. $return .= '</td>';
  7563. if ($item['item_type'] == TOOL_QUIZ) {
  7564. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7565. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7566. $tmp_obj_exercice = new Exercise();
  7567. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7568. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7569. $tmp_obj_lp_item->update_in_bdd();
  7570. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7571. $return .= '<td class="exercise">';
  7572. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7573. $return .= '</td>';
  7574. $return .= '<td class="exercise">';
  7575. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7576. $return .= '</td>';
  7577. }
  7578. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7579. $return .= '<td class="exercise">';
  7580. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7581. $return .= '</td>';
  7582. $return .= '<td class="exercise"">';
  7583. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7584. $return .= '</td>';
  7585. }
  7586. $return .= '</tr>';
  7587. }
  7588. $return .= '<tr>';
  7589. $return .= '</tr>';
  7590. $return .= '</table>';
  7591. $return .= '<div style="padding-top:3px;">';
  7592. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7593. $return .= '</form>';
  7594. return $return;
  7595. }
  7596. /**
  7597. * Return HTML list to allow prerequisites selection for lp
  7598. * @param integer Item ID
  7599. * @return string HTML form
  7600. */
  7601. public function display_lp_prerequisites_list()
  7602. {
  7603. $course_id = api_get_course_int_id();
  7604. $lp_id = $this->lp_id;
  7605. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7606. // get current prerequisite
  7607. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7608. $result = Database::query($sql);
  7609. $row = Database :: fetch_array($result);
  7610. $prerequisiteId = $row['prerequisite'];
  7611. $session_id = api_get_session_id();
  7612. $session_condition = api_get_session_condition($session_id);
  7613. $sql = "SELECT * FROM $tbl_lp
  7614. WHERE c_id = $course_id $session_condition
  7615. ORDER BY display_order ";
  7616. $rs = Database::query($sql);
  7617. $return = '';
  7618. $return .= '<select name="prerequisites" class="form-control">';
  7619. $return .= '<option value="0">'.get_lang('None').'</option>';
  7620. if (Database::num_rows($rs) > 0) {
  7621. while ($row = Database::fetch_array($rs)) {
  7622. if ($row['id'] == $lp_id) {
  7623. continue;
  7624. }
  7625. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7626. }
  7627. }
  7628. $return .= '</select>';
  7629. return $return;
  7630. }
  7631. /**
  7632. * Creates a list with all the documents in it
  7633. * @param bool $showInvisibleFiles
  7634. * @return string
  7635. */
  7636. public function get_documents($showInvisibleFiles = false)
  7637. {
  7638. $course_info = api_get_course_info();
  7639. $sessionId = api_get_session_id();
  7640. $document_tree = DocumentManager::get_document_preview(
  7641. $course_info,
  7642. $this->lp_id,
  7643. null,
  7644. $sessionId,
  7645. true,
  7646. null,
  7647. null,
  7648. $showInvisibleFiles,
  7649. true
  7650. );
  7651. return $document_tree;
  7652. }
  7653. /**
  7654. * Creates a list with all the exercises (quiz) in it
  7655. * @return string
  7656. */
  7657. public function get_exercises()
  7658. {
  7659. $course_id = api_get_course_int_id();
  7660. // New for hotpotatoes.
  7661. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7662. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7663. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7664. $session_id = api_get_session_id();
  7665. $condition_session = api_get_session_condition($session_id);
  7666. $sql_quiz = "SELECT * FROM $tbl_quiz
  7667. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7668. ORDER BY title ASC";
  7669. $sql_hot = "SELECT * FROM $tbl_doc
  7670. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7671. ORDER BY id ASC";
  7672. $res_quiz = Database::query($sql_quiz);
  7673. $res_hot = Database::query($sql_hot);
  7674. $return = '<ul class="lp_resource">';
  7675. $return .= '<li class="lp_resource_element">';
  7676. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7677. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7678. get_lang('NewExercise') . '</a>';
  7679. $return .= '</li>';
  7680. // Display hotpotatoes
  7681. while ($row_hot = Database :: fetch_array($res_hot)) {
  7682. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7683. $return .= '<a class="moved" href="#">';
  7684. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7685. $return .= '</a> ';
  7686. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7687. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7688. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7689. $return .= '</li>';
  7690. }
  7691. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7692. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7693. $return .= '<a class="moved" href="#">';
  7694. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7695. $return .= '</a> ';
  7696. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7697. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7698. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7699. '</a>';
  7700. $return .= '</li>';
  7701. }
  7702. $return .= '</ul>';
  7703. return $return;
  7704. }
  7705. /**
  7706. * Creates a list with all the links in it
  7707. * @return string
  7708. */
  7709. public function get_links()
  7710. {
  7711. $selfUrl = api_get_self();
  7712. $courseIdReq = api_get_cidreq();
  7713. $course = api_get_course_info();
  7714. $course_id = $course['real_id'];
  7715. $tbl_link = Database::get_course_table(TABLE_LINK);
  7716. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7717. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7718. $session_id = api_get_session_id();
  7719. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7720. $sql = "SELECT link.id as link_id,
  7721. link.title as link_title,
  7722. link.category_id as category_id,
  7723. link_category.category_title as category_title
  7724. FROM $tbl_link as link
  7725. LEFT JOIN $linkCategoryTable as link_category
  7726. ON link.category_id = link_category.id
  7727. WHERE link.c_id = ".$course_id." $condition_session
  7728. ORDER BY link_category.category_title ASC, link.title ASC";
  7729. $links = Database::query($sql);
  7730. $categorizedLinks = array();
  7731. $categories = array();
  7732. while ($link = Database :: fetch_array($links)) {
  7733. if (!$link['category_id']) {
  7734. $link['category_title'] = get_lang('Uncategorized');
  7735. }
  7736. $categories[$link['category_id']] = $link['category_title'];
  7737. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7738. }
  7739. $linksHtmlCode =
  7740. '<script>
  7741. function toggle_tool(tool, id){
  7742. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7743. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7744. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7745. } else {
  7746. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7747. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7748. }
  7749. }
  7750. </script>
  7751. <ul class="lp_resource">
  7752. <li class="lp_resource_element">
  7753. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7754. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7755. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7756. </li>';
  7757. foreach ($categorizedLinks as $categoryId => $links) {
  7758. $linkNodes = null;
  7759. foreach ($links as $key => $title) {
  7760. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7761. $linkNodes .=
  7762. '<li class="lp_resource_element" data_id="'.$key.
  7763. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7764. <a class="moved" href="#">'.
  7765. $moveEverywhereIcon.
  7766. '</a>
  7767. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7768. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7769. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7770. Security::remove_XSS($title).
  7771. '</a>
  7772. </li>';
  7773. }
  7774. }
  7775. $linksHtmlCode .=
  7776. '<li>
  7777. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7778. style="vertical-align:middle">
  7779. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7780. align="absbottom" />
  7781. </a>
  7782. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7783. </li>
  7784. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7785. }
  7786. $linksHtmlCode .= '</ul>';
  7787. return $linksHtmlCode;
  7788. }
  7789. /**
  7790. * Creates a list with all the student publications in it
  7791. * @return unknown
  7792. */
  7793. public function get_student_publications()
  7794. {
  7795. $return = '<div class="lp_resource" >';
  7796. $return .= '<div class="lp_resource_element">';
  7797. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7798. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7799. $return .= '</div>';
  7800. $return .= '</div>';
  7801. return $return;
  7802. }
  7803. /**
  7804. * Creates a list with all the forums in it
  7805. * @return string
  7806. */
  7807. public function get_forums()
  7808. {
  7809. require_once '../forum/forumfunction.inc.php';
  7810. require_once '../forum/forumconfig.inc.php';
  7811. $a_forums = get_forums();
  7812. $return = '<ul class="lp_resource">';
  7813. //First add link
  7814. $return .= '<li class="lp_resource_element">';
  7815. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7816. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&content=forum&origin=learnpath&lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7817. $return .= '</li>';
  7818. $return .= '<script>
  7819. function toggle_forum(forum_id){
  7820. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7821. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7822. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7823. } else {
  7824. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7825. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7826. }
  7827. }
  7828. </script>';
  7829. foreach ($a_forums as $forum) {
  7830. if (!empty($forum['forum_id'])) {
  7831. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7832. $return .= '<a class="moved" href="#">';
  7833. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7834. $return .= ' </a>';
  7835. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7836. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7837. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7838. </a>
  7839. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  7840. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7841. $return .= '</li>';
  7842. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7843. $a_threads = get_threads($forum['forum_id']);
  7844. if (is_array($a_threads)) {
  7845. foreach ($a_threads as $thread) {
  7846. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7847. $return .= '&nbsp;<a class="moved" href="#">';
  7848. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7849. $return .= ' </a>';
  7850. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7851. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  7852. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7853. $return .= '</li>';
  7854. }
  7855. }
  7856. $return .= '</div>';
  7857. }
  7858. }
  7859. $return .= '</ul>';
  7860. return $return;
  7861. }
  7862. /**
  7863. * // TODO: The output encoding should be equal to the system encoding.
  7864. *
  7865. * Exports the learning path as a SCORM package. This is the main function that
  7866. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7867. * whole thing and returns the zip.
  7868. *
  7869. * This method needs to be called in PHP5, as it will fail with non-adequate
  7870. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7871. * you need to call it on a learnpath object.
  7872. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7873. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7874. * path has been modified, it should use the generic method here below.
  7875. * @TODO link this function with the export_lp() function in the same class
  7876. * @param string Optional name of zip file. If none, title of learnpath is
  7877. * domesticated and trailed with ".zip"
  7878. * @return string Returns the zip package string, or null if error
  7879. */
  7880. public function scorm_export()
  7881. {
  7882. $_course = api_get_course_info();
  7883. $course_id = $_course['real_id'];
  7884. // Remove memory and time limits as much as possible as this might be a long process...
  7885. if (function_exists('ini_set')) {
  7886. api_set_memory_limit('128M');
  7887. ini_set('max_execution_time', 600);
  7888. }
  7889. // Create the zip handler (this will remain available throughout the method).
  7890. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7891. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7892. $temp_dir_short = uniqid();
  7893. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7894. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7895. $zip_folder = new PclZip($temp_zip_file);
  7896. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7897. $root_path = $main_path = api_get_path(SYS_PATH);
  7898. $files_cleanup = array();
  7899. // Place to temporarily stash the zip file.
  7900. // create the temp dir if it doesn't exist
  7901. // or do a cleanup before creating the zip file.
  7902. if (!is_dir($temp_zip_dir)) {
  7903. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7904. } else {
  7905. // Cleanup: Check the temp dir for old files and delete them.
  7906. $handle = opendir($temp_zip_dir);
  7907. while (false !== ($file = readdir($handle))) {
  7908. if ($file != '.' && $file != '..') {
  7909. unlink("$temp_zip_dir/$file");
  7910. }
  7911. }
  7912. closedir($handle);
  7913. }
  7914. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7915. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7916. // Remove the possible . at the end of the path.
  7917. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7918. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7919. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7920. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7921. }
  7922. // Build a dummy imsmanifest structure.
  7923. // Do not add to the zip yet (we still need it).
  7924. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7925. // Aggregation Model official document, section "2.3 Content Packaging".
  7926. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7927. $xmldoc = new DOMDocument('1.0');
  7928. $root = $xmldoc->createElement('manifest');
  7929. $root->setAttribute('identifier', 'SingleCourseManifest');
  7930. $root->setAttribute('version', '1.1');
  7931. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7932. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7933. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7934. $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');
  7935. // Build mandatory sub-root container elements.
  7936. $metadata = $xmldoc->createElement('metadata');
  7937. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7938. $metadata->appendChild($md_schema);
  7939. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7940. $metadata->appendChild($md_schemaversion);
  7941. $root->appendChild($metadata);
  7942. $organizations = $xmldoc->createElement('organizations');
  7943. $resources = $xmldoc->createElement('resources');
  7944. // Build the only organization we will use in building our learnpaths.
  7945. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7946. $organization = $xmldoc->createElement('organization');
  7947. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7948. // To set the title of the SCORM entity (=organization), we take the name given
  7949. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7950. // learning path charset) as it is the encoding that defines how it is stored
  7951. // in the database. Then we convert it to HTML entities again as the "&" character
  7952. // alone is not authorized in XML (must be &amp;).
  7953. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7954. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7955. $organization->appendChild($org_title);
  7956. $folder_name = 'document';
  7957. // Removes the learning_path/scorm_folder path when exporting see #4841
  7958. $path_to_remove = null;
  7959. $result = $this->generate_lp_folder($_course);
  7960. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7961. $path_to_remove = 'document'.$result['dir'];
  7962. $path_to_replace = $folder_name.'/';
  7963. }
  7964. //Fixes chamilo scorm exports
  7965. if ($this->ref == 'chamilo_scorm_export') {
  7966. $path_to_remove = 'scorm/'.$this->path.'/document/';
  7967. }
  7968. // For each element, add it to the imsmanifest structure, then add it to the zip.
  7969. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  7970. $link_updates = array();
  7971. $links_to_create = array();
  7972. foreach ($this->items as $index => $item) {
  7973. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  7974. // Get included documents from this item.
  7975. if ($item->type == 'sco') {
  7976. $inc_docs = $item->get_resources_from_source(
  7977. null,
  7978. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  7979. );
  7980. } else {
  7981. $inc_docs = $item->get_resources_from_source();
  7982. }
  7983. // Give a child element <item> to the <organization> element.
  7984. $my_item_id = $item->get_id();
  7985. $my_item = $xmldoc->createElement('item');
  7986. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  7987. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  7988. $my_item->setAttribute('isvisible', 'true');
  7989. // Give a child element <title> to the <item> element.
  7990. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7991. $my_item->appendChild($my_title);
  7992. // Give a child element <adlcp:prerequisites> to the <item> element.
  7993. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  7994. $my_prereqs->setAttribute('type', 'aicc_script');
  7995. $my_item->appendChild($my_prereqs);
  7996. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7997. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  7998. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7999. //$xmldoc->createElement('adlcp:timelimitaction','');
  8000. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8001. //$xmldoc->createElement('adlcp:datafromlms','');
  8002. // Give a child element <adlcp:masteryscore> to the <item> element.
  8003. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8004. $my_item->appendChild($my_masteryscore);
  8005. // Attach this item to the organization element or hits parent if there is one.
  8006. if (!empty($item->parent) && $item->parent != 0) {
  8007. $children = $organization->childNodes;
  8008. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8009. if (is_object($possible_parent)) {
  8010. $possible_parent->appendChild($my_item);
  8011. } else {
  8012. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8013. }
  8014. } else {
  8015. if ($this->debug > 0) { error_log('No parent'); }
  8016. $organization->appendChild($my_item);
  8017. }
  8018. // Get the path of the file(s) from the course directory root.
  8019. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8020. if (!empty($path_to_remove)) {
  8021. //From docs
  8022. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8023. //From quiz
  8024. if ($this->ref == 'chamilo_scorm_export') {
  8025. $path_to_remove = 'scorm/'.$this->path.'/';
  8026. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8027. }
  8028. } else {
  8029. $my_xml_file_path = $my_file_path;
  8030. }
  8031. $my_sub_dir = dirname($my_file_path);
  8032. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8033. $my_xml_sub_dir = $my_sub_dir;
  8034. // Give a <resource> child to the <resources> element
  8035. $my_resource = $xmldoc->createElement('resource');
  8036. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8037. $my_resource->setAttribute('type', 'webcontent');
  8038. $my_resource->setAttribute('href', $my_xml_file_path);
  8039. // adlcp:scormtype can be either 'sco' or 'asset'.
  8040. if ($item->type == 'sco') {
  8041. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8042. } else {
  8043. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8044. }
  8045. // xml:base is the base directory to find the files declared in this resource.
  8046. $my_resource->setAttribute('xml:base', '');
  8047. // Give a <file> child to the <resource> element.
  8048. $my_file = $xmldoc->createElement('file');
  8049. $my_file->setAttribute('href', $my_xml_file_path);
  8050. $my_resource->appendChild($my_file);
  8051. // Dependency to other files - not yet supported.
  8052. $i = 1;
  8053. foreach ($inc_docs as $doc_info) {
  8054. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8055. $my_dep = $xmldoc->createElement('resource');
  8056. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8057. $my_dep->setAttribute('identifier', $res_id);
  8058. $my_dep->setAttribute('type', 'webcontent');
  8059. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8060. $my_dep_file = $xmldoc->createElement('file');
  8061. // Check type of URL.
  8062. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8063. if ($doc_info[1] == 'remote') {
  8064. // Remote file. Save url as is.
  8065. $my_dep_file->setAttribute('href', $doc_info[0]);
  8066. $my_dep->setAttribute('xml:base', '');
  8067. } elseif ($doc_info[1] == 'local') {
  8068. switch ($doc_info[2]) {
  8069. case 'url': // Local URL - save path as url for now, don't zip file.
  8070. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8071. $current_dir = dirname($abs_path);
  8072. $current_dir = str_replace('\\', '/', $current_dir);
  8073. $file_path = realpath($abs_path);
  8074. $file_path = str_replace('\\', '/', $file_path);
  8075. $my_dep_file->setAttribute('href', $file_path);
  8076. $my_dep->setAttribute('xml:base', '');
  8077. if (strstr($file_path, $main_path) !== false) {
  8078. // The calculated real path is really inside Chamilo's root path.
  8079. // Reduce file path to what's under the DocumentRoot.
  8080. $file_path = substr($file_path, strlen($root_path) - 1);
  8081. //echo $file_path;echo '<br /><br />';
  8082. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8083. $zip_files_abs[] = $file_path;
  8084. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8085. $my_dep_file->setAttribute('href', $file_path);
  8086. $my_dep->setAttribute('xml:base', '');
  8087. } elseif (empty($file_path)) {
  8088. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8089. if (strpos($document_root, -1) == '/') {
  8090. $document_root = substr(0, -1, $document_root);
  8091. }*/
  8092. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8093. $file_path = str_replace('//', '/', $file_path);
  8094. if (file_exists($file_path)) {
  8095. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8096. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8097. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8098. $my_dep_file->setAttribute('href', $file_path);
  8099. $my_dep->setAttribute('xml:base', '');
  8100. }
  8101. }
  8102. break;
  8103. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8104. $my_dep_file->setAttribute('href', $doc_info[0]);
  8105. $my_dep->setAttribute('xml:base', '');
  8106. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8107. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8108. $abs_img_path_without_subdir = $doc_info[0];
  8109. $relp = api_get_path(REL_PATH); // The url-append config param.
  8110. $pos = strpos($abs_img_path_without_subdir, $relp);
  8111. if ($pos === 0) {
  8112. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8113. }
  8114. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8115. $file_path = str_replace('\\', '/', $file_path);
  8116. $file_path = str_replace('//', '/', $file_path);
  8117. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8118. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8119. // Check if the current document is in that path.
  8120. if (strstr($file_path, $cur_path) !== false) {
  8121. // The document is in that path, now get the relative path
  8122. // to the containing document.
  8123. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8124. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8125. $relative_path = '';
  8126. if (strstr($file_path, $cur_path) !== false) {
  8127. //$relative_path = substr($file_path, strlen($orig_file_path));
  8128. $relative_path = str_replace($cur_path, '', $file_path);
  8129. $file_path = substr($file_path, strlen($cur_path));
  8130. } else {
  8131. // This case is still a problem as it's difficult to calculate a relative path easily
  8132. // might still generate wrong links.
  8133. //$file_path = substr($file_path,strlen($cur_path));
  8134. // Calculate the directory path to the current file (without trailing slash).
  8135. $my_relative_path = dirname($file_path);
  8136. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8137. $my_relative_file = basename($file_path);
  8138. // Calculate the directory path to the containing file (without trailing slash).
  8139. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8140. $dotdot = '';
  8141. $subdir = '';
  8142. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8143. $my_relative_path2 = dirname($my_relative_path);
  8144. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8145. $my_orig_file_path = dirname($my_orig_file_path);
  8146. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8147. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8148. $dotdot += '../';
  8149. $my_relative_path = $my_relative_path2;
  8150. }
  8151. $relative_path = $dotdot.$subdir.$my_relative_file;
  8152. }
  8153. // Put the current document in the zip (this array is the array
  8154. // that will manage documents already in the course folder - relative).
  8155. $zip_files[] = $file_path;
  8156. // Update the links to the current document in the containing document (make them relative).
  8157. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8158. $my_dep_file->setAttribute('href', $file_path);
  8159. $my_dep->setAttribute('xml:base', '');
  8160. } elseif (strstr($file_path, $main_path) !== false) {
  8161. // The calculated real path is really inside Chamilo's root path.
  8162. // Reduce file path to what's under the DocumentRoot.
  8163. $file_path = substr($file_path, strlen($root_path));
  8164. //echo $file_path;echo '<br /><br />';
  8165. //error_log('Reduced path: '.$file_path, 0);
  8166. $zip_files_abs[] = $file_path;
  8167. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8168. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8169. $my_dep->setAttribute('xml:base', '');
  8170. } elseif (empty($file_path)) {
  8171. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8172. if(strpos($document_root,-1) == '/') {
  8173. $document_root = substr(0, -1, $document_root);
  8174. }*/
  8175. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8176. $file_path = str_replace('//', '/', $file_path);
  8177. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8178. $current_dir = dirname($abs_path);
  8179. $current_dir = str_replace('\\', '/', $current_dir);
  8180. if (file_exists($file_path)) {
  8181. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8182. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8183. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8184. $my_dep_file->setAttribute('href','document/'.$file_path);
  8185. $my_dep->setAttribute('xml:base', '');
  8186. }
  8187. }
  8188. break;
  8189. case 'rel':
  8190. // Path relative to the current document.
  8191. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8192. if (substr($doc_info[0], 0, 2) == '..') {
  8193. // Relative path going up.
  8194. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8195. $current_dir = str_replace('\\', '/', $current_dir);
  8196. $file_path = realpath($current_dir.$doc_info[0]);
  8197. $file_path = str_replace('\\', '/', $file_path);
  8198. //error_log($file_path.' <-> '.$main_path,0);
  8199. if (strstr($file_path, $main_path) !== false) {
  8200. // The calculated real path is really inside Chamilo's root path.
  8201. // Reduce file path to what's under the DocumentRoot.
  8202. $file_path = substr($file_path, strlen($root_path));
  8203. //error_log('Reduced path: '.$file_path, 0);
  8204. $zip_files_abs[] = $file_path;
  8205. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8206. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8207. $my_dep->setAttribute('xml:base', '');
  8208. }
  8209. } else {
  8210. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8211. $my_dep_file->setAttribute('href', $doc_info[0]);
  8212. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8213. }
  8214. break;
  8215. default:
  8216. $my_dep_file->setAttribute('href', $doc_info[0]);
  8217. $my_dep->setAttribute('xml:base', '');
  8218. break;
  8219. }
  8220. }
  8221. $my_dep->appendChild($my_dep_file);
  8222. $resources->appendChild($my_dep);
  8223. $dependency = $xmldoc->createElement('dependency');
  8224. $dependency->setAttribute('identifierref', $res_id);
  8225. $my_resource->appendChild($dependency);
  8226. $i++;
  8227. }
  8228. $resources->appendChild($my_resource);
  8229. $zip_files[] = $my_file_path;
  8230. } else {
  8231. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8232. switch ($item->type) {
  8233. case TOOL_LINK:
  8234. $my_item = $xmldoc->createElement('item');
  8235. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8236. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8237. $my_item->setAttribute('isvisible', 'true');
  8238. // Give a child element <title> to the <item> element.
  8239. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8240. $my_item->appendChild($my_title);
  8241. // Give a child element <adlcp:prerequisites> to the <item> element.
  8242. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8243. $my_prereqs->setAttribute('type', 'aicc_script');
  8244. $my_item->appendChild($my_prereqs);
  8245. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8246. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8247. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8248. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8249. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8250. //$xmldoc->createElement('adlcp:datafromlms', '');
  8251. // Give a child element <adlcp:masteryscore> to the <item> element.
  8252. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8253. $my_item->appendChild($my_masteryscore);
  8254. // Attach this item to the organization element or its parent if there is one.
  8255. if (!empty($item->parent) && $item->parent != 0) {
  8256. $children = $organization->childNodes;
  8257. for ($i = 0; $i < $children->length; $i++) {
  8258. $item_temp = $children->item($i);
  8259. if ($item_temp -> nodeName == 'item') {
  8260. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8261. $item_temp -> appendChild($my_item);
  8262. }
  8263. }
  8264. }
  8265. } else {
  8266. $organization->appendChild($my_item);
  8267. }
  8268. $my_file_path = 'link_'.$item->get_id().'.html';
  8269. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8270. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8271. $rs = Database::query($sql);
  8272. if ($link = Database :: fetch_array($rs)) {
  8273. $url = $link['url'];
  8274. $title = stripslashes($link['title']);
  8275. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8276. $my_xml_file_path = $my_file_path;
  8277. $my_sub_dir = dirname($my_file_path);
  8278. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8279. $my_xml_sub_dir = $my_sub_dir;
  8280. // Give a <resource> child to the <resources> element.
  8281. $my_resource = $xmldoc->createElement('resource');
  8282. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8283. $my_resource->setAttribute('type', 'webcontent');
  8284. $my_resource->setAttribute('href', $my_xml_file_path);
  8285. // adlcp:scormtype can be either 'sco' or 'asset'.
  8286. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8287. // xml:base is the base directory to find the files declared in this resource.
  8288. $my_resource->setAttribute('xml:base', '');
  8289. // give a <file> child to the <resource> element.
  8290. $my_file = $xmldoc->createElement('file');
  8291. $my_file->setAttribute('href', $my_xml_file_path);
  8292. $my_resource->appendChild($my_file);
  8293. $resources->appendChild($my_resource);
  8294. }
  8295. break;
  8296. case TOOL_QUIZ:
  8297. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8298. $exe = new Exercise();
  8299. $exe->read($exe_id);
  8300. $my_item = $xmldoc->createElement('item');
  8301. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8302. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8303. $my_item->setAttribute('isvisible', 'true');
  8304. // Give a child element <title> to the <item> element.
  8305. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8306. $my_item->appendChild($my_title);
  8307. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8308. //$my_item->appendChild($my_max_score);
  8309. // Give a child element <adlcp:prerequisites> to the <item> element.
  8310. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8311. $my_prereqs->setAttribute('type','aicc_script');
  8312. $my_item->appendChild($my_prereqs);
  8313. // Give a child element <adlcp:masteryscore> to the <item> element.
  8314. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8315. $my_item->appendChild($my_masteryscore);
  8316. // Attach this item to the organization element or hits parent if there is one.
  8317. if (!empty($item->parent) && $item->parent != 0) {
  8318. $children = $organization->childNodes;
  8319. for ($i = 0; $i < $children->length; $i++) {
  8320. $item_temp = $children->item($i);
  8321. if ($item_temp -> nodeName == 'item') {
  8322. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8323. $item_temp -> appendChild($my_item);
  8324. }
  8325. }
  8326. }
  8327. } else {
  8328. $organization->appendChild($my_item);
  8329. }
  8330. // Get the path of the file(s) from the course directory root
  8331. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8332. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8333. // Write the contents of the exported exercise into a (big) html file
  8334. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8335. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8336. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8337. $res = file_put_contents($tmp_file_path, $contents);
  8338. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8339. $files_cleanup[] = $tmp_file_path;
  8340. //error_log($tmp_path); die();
  8341. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8342. $my_xml_file_path = $my_file_path;
  8343. $my_sub_dir = dirname($my_file_path);
  8344. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8345. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8346. $my_xml_sub_dir = $my_sub_dir;
  8347. // Give a <resource> child to the <resources> element.
  8348. $my_resource = $xmldoc->createElement('resource');
  8349. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8350. $my_resource->setAttribute('type', 'webcontent');
  8351. $my_resource->setAttribute('href', $my_xml_file_path);
  8352. // adlcp:scormtype can be either 'sco' or 'asset'.
  8353. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8354. // xml:base is the base directory to find the files declared in this resource.
  8355. $my_resource->setAttribute('xml:base', '');
  8356. // Give a <file> child to the <resource> element.
  8357. $my_file = $xmldoc->createElement('file');
  8358. $my_file->setAttribute('href', $my_xml_file_path);
  8359. $my_resource->appendChild($my_file);
  8360. // Get included docs.
  8361. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8362. // Dependency to other files - not yet supported.
  8363. $i = 1;
  8364. foreach ($inc_docs as $doc_info) {
  8365. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8366. $my_dep = $xmldoc->createElement('resource');
  8367. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8368. $my_dep->setAttribute('identifier', $res_id);
  8369. $my_dep->setAttribute('type', 'webcontent');
  8370. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8371. $my_dep_file = $xmldoc->createElement('file');
  8372. // Check type of URL.
  8373. if ($doc_info[1] == 'remote') {
  8374. // Remote file. Save url as is.
  8375. $my_dep_file->setAttribute('href', $doc_info[0]);
  8376. $my_dep->setAttribute('xml:base', '');
  8377. } elseif ($doc_info[1] == 'local') {
  8378. switch ($doc_info[2]) {
  8379. case 'url': // Local URL - save path as url for now, don't zip file.
  8380. // Save file but as local file (retrieve from URL).
  8381. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8382. $current_dir = dirname($abs_path);
  8383. $current_dir = str_replace('\\', '/', $current_dir);
  8384. $file_path = realpath($abs_path);
  8385. $file_path = str_replace('\\', '/', $file_path);
  8386. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8387. $my_dep->setAttribute('xml:base', '');
  8388. if (strstr($file_path, $main_path) !== false) {
  8389. // The calculated real path is really inside the chamilo root path.
  8390. // Reduce file path to what's under the DocumentRoot.
  8391. $file_path = substr($file_path, strlen($root_path));
  8392. //echo $file_path;echo '<br /><br />';
  8393. //error_log('Reduced path: '.$file_path, 0);
  8394. $zip_files_abs[] = $file_path;
  8395. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8396. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8397. $my_dep->setAttribute('xml:base', '');
  8398. } elseif (empty($file_path)) {
  8399. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8400. if (strpos($document_root,-1) == '/') {
  8401. $document_root = substr(0, -1, $document_root);
  8402. }*/
  8403. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8404. $file_path = str_replace('//', '/', $file_path);
  8405. if (file_exists($file_path)) {
  8406. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8407. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8408. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8409. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8410. $my_dep->setAttribute('xml:base', '');
  8411. }
  8412. }
  8413. break;
  8414. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8415. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8416. $current_dir = str_replace('\\', '/', $current_dir);
  8417. $file_path = realpath($doc_info[0]);
  8418. $file_path = str_replace('\\', '/', $file_path);
  8419. $my_dep_file->setAttribute('href', $file_path);
  8420. $my_dep->setAttribute('xml:base', '');
  8421. if (strstr($file_path,$main_path) !== false) {
  8422. // The calculated real path is really inside the chamilo root path.
  8423. // Reduce file path to what's under the DocumentRoot.
  8424. $file_path = substr($file_path, strlen($root_path));
  8425. //echo $file_path;echo '<br /><br />';
  8426. //error_log('Reduced path: '.$file_path, 0);
  8427. $zip_files_abs[] = $file_path;
  8428. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8429. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8430. $my_dep->setAttribute('xml:base', '');
  8431. } elseif (empty($file_path)) {
  8432. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8433. if (strpos($document_root,-1) == '/') {
  8434. $document_root = substr(0, -1, $document_root);
  8435. }*/
  8436. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8437. $file_path = str_replace('//', '/', $file_path);
  8438. if (file_exists($file_path)) {
  8439. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8440. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8441. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8442. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8443. $my_dep->setAttribute('xml:base', '');
  8444. }
  8445. }
  8446. break;
  8447. 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
  8448. if (substr($doc_info[0], 0, 2) == '..') {
  8449. // Relative path going up.
  8450. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8451. $current_dir = str_replace('\\', '/', $current_dir);
  8452. $file_path = realpath($current_dir.$doc_info[0]);
  8453. $file_path = str_replace('\\', '/', $file_path);
  8454. //error_log($file_path.' <-> '.$main_path, 0);
  8455. if (strstr($file_path, $main_path) !== false) {
  8456. // The calculated real path is really inside Chamilo's root path.
  8457. // Reduce file path to what's under the DocumentRoot.
  8458. $file_path = substr($file_path, strlen($root_path));
  8459. $file_path_dest = $file_path;
  8460. // File path is courses/CHAMILO/document/....
  8461. $info_file_path = explode('/', $file_path);
  8462. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8463. $file_path_dest = 'document/'.$file_path;
  8464. }
  8465. //error_log('Reduced path: '.$file_path, 0);
  8466. $zip_files_abs[] = $file_path;
  8467. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8468. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8469. $my_dep->setAttribute('xml:base', '');
  8470. }
  8471. } else {
  8472. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8473. $my_dep_file->setAttribute('href', $doc_info[0]);
  8474. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8475. }
  8476. break;
  8477. default:
  8478. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8479. $my_dep->setAttribute('xml:base', '');
  8480. break;
  8481. }
  8482. }
  8483. $my_dep->appendChild($my_dep_file);
  8484. $resources->appendChild($my_dep);
  8485. $dependency = $xmldoc->createElement('dependency');
  8486. $dependency->setAttribute('identifierref', $res_id);
  8487. $my_resource->appendChild($dependency);
  8488. $i++;
  8489. }
  8490. $resources->appendChild($my_resource);
  8491. $zip_files[] = $my_file_path;
  8492. break;
  8493. default:
  8494. // Get the path of the file(s) from the course directory root
  8495. $my_file_path = 'non_exportable.html';
  8496. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8497. $my_xml_file_path = $my_file_path;
  8498. $my_sub_dir = dirname($my_file_path);
  8499. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8500. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8501. $my_xml_sub_dir = $my_sub_dir;
  8502. // Give a <resource> child to the <resources> element.
  8503. $my_resource = $xmldoc->createElement('resource');
  8504. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8505. $my_resource->setAttribute('type', 'webcontent');
  8506. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8507. // adlcp:scormtype can be either 'sco' or 'asset'.
  8508. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8509. // xml:base is the base directory to find the files declared in this resource.
  8510. $my_resource->setAttribute('xml:base', '');
  8511. // Give a <file> child to the <resource> element.
  8512. $my_file = $xmldoc->createElement('file');
  8513. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8514. $my_resource->appendChild($my_file);
  8515. $resources->appendChild($my_resource);
  8516. break;
  8517. }
  8518. }
  8519. }
  8520. $organizations->appendChild($organization);
  8521. $root->appendChild($organizations);
  8522. $root->appendChild($resources);
  8523. $xmldoc->appendChild($root);
  8524. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8525. // then add the file to the zip, then destroy the file (this is done automatically).
  8526. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8527. foreach ($zip_files as $file_path) {
  8528. if (empty($file_path)) {
  8529. continue;
  8530. }
  8531. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8532. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8533. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8534. }
  8535. $this->create_path($dest_file);
  8536. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8537. // Check if the file needs a link update.
  8538. if (in_array($file_path, array_keys($link_updates))) {
  8539. $string = file_get_contents($dest_file);
  8540. unlink($dest_file);
  8541. foreach ($link_updates[$file_path] as $old_new) {
  8542. // This is an ugly hack that allows .flv files to be found by the flv player that
  8543. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8544. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8545. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8546. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8547. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8548. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8549. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8550. }
  8551. //Fix to avoid problems with default_course_document
  8552. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8553. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8554. $newDestination = $old_new['dest'];
  8555. } else {
  8556. $newDestination = str_replace('document/', '', $old_new['dest']);
  8557. }
  8558. $string = str_replace($old_new['orig'], $newDestination, $string);
  8559. //Add files inside the HTMLs
  8560. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8561. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8562. if (file_exists($sys_course_path.$new_path)) {
  8563. copy($sys_course_path.$new_path, $destinationFile);
  8564. }
  8565. }
  8566. file_put_contents($dest_file, $string);
  8567. }
  8568. }
  8569. foreach ($zip_files_abs as $file_path) {
  8570. if (empty($file_path)) {
  8571. continue;
  8572. }
  8573. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8574. continue;
  8575. }
  8576. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8577. $this->create_path($dest_file);
  8578. copy($main_path.$file_path, $dest_file);
  8579. // Check if the file needs a link update.
  8580. if (in_array($file_path, array_keys($link_updates))) {
  8581. $string = file_get_contents($dest_file);
  8582. unlink($dest_file);
  8583. foreach ($link_updates[$file_path] as $old_new) {
  8584. // This is an ugly hack that allows .flv files to be found by the flv player that
  8585. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8586. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8587. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8588. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8589. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8590. }
  8591. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8592. }
  8593. file_put_contents($dest_file, $string);
  8594. }
  8595. }
  8596. if (is_array($links_to_create)) {
  8597. foreach ($links_to_create as $file => $link) {
  8598. $file_content = '<!DOCTYPE html><head>
  8599. <meta charset="'.api_get_language_isocode().'" />
  8600. <title>'.$link['title'].'</title>
  8601. </head>
  8602. <body dir="'.api_get_text_direction().'">
  8603. <div style="text-align:center">
  8604. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8605. </body>
  8606. </html>';
  8607. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8608. }
  8609. }
  8610. // Add non exportable message explanation.
  8611. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8612. $file_content = '<!DOCTYPE html><head>
  8613. <meta charset="'.api_get_language_isocode().'" />
  8614. <title>'.$lang_not_exportable.'</title>
  8615. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8616. </head>
  8617. <body dir="'.api_get_text_direction().'">';
  8618. $file_content .=
  8619. <<<EOD
  8620. <style>
  8621. .error-message {
  8622. font-family: arial, verdana, helvetica, sans-serif;
  8623. border-width: 1px;
  8624. border-style: solid;
  8625. left: 50%;
  8626. margin: 10px auto;
  8627. min-height: 30px;
  8628. padding: 5px;
  8629. right: 50%;
  8630. width: 500px;
  8631. background-color: #FFD1D1;
  8632. border-color: #FF0000;
  8633. color: #000;
  8634. }
  8635. </style>
  8636. <body>
  8637. <div class="error-message">
  8638. $lang_not_exportable
  8639. </div>
  8640. </body>
  8641. </html>
  8642. EOD;
  8643. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8644. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8645. }
  8646. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8647. // Add the extra files that go along with a SCORM package.
  8648. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8649. $extra_files = scandir($main_code_path);
  8650. foreach ($extra_files as $extra_file) {
  8651. if (strpos($extra_file, '.') === 0)
  8652. continue;
  8653. else {
  8654. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8655. $this->create_path($dest_file);
  8656. copy($main_code_path.$extra_file, $dest_file);
  8657. }
  8658. }
  8659. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8660. $manifest = @$xmldoc->saveXML();
  8661. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8662. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8663. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8664. // Clean possible temporary files.
  8665. foreach ($files_cleanup as $file) {
  8666. $res = unlink($file);
  8667. if ($res === false) {
  8668. error_log(
  8669. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8670. 0
  8671. );
  8672. }
  8673. }
  8674. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8675. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8676. }
  8677. /**
  8678. * @param int $lp_id
  8679. * @return bool
  8680. */
  8681. public function scorm_export_to_pdf($lp_id)
  8682. {
  8683. $lp_id = intval($lp_id);
  8684. $files_to_export = array();
  8685. $course_data = api_get_course_info($this->cc);
  8686. if (!empty($course_data)) {
  8687. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8688. $list = self::get_flat_ordered_items_list($lp_id);
  8689. if (!empty($list)) {
  8690. foreach ($list as $item_id) {
  8691. $item = $this->items[$item_id];
  8692. switch ($item->type) {
  8693. case 'document':
  8694. //Getting documents from a LP with chamilo documents
  8695. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8696. // Try loading document from the base course.
  8697. if (empty($file_data) && !empty($sessionId)) {
  8698. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8699. }
  8700. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8701. if (file_exists($file_path)) {
  8702. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8703. }
  8704. break;
  8705. case 'asset': //commes from a scorm package generated by chamilo
  8706. case 'sco':
  8707. $file_path = $scorm_path.'/'.$item->path;
  8708. if (file_exists($file_path)) {
  8709. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8710. }
  8711. break;
  8712. case 'dokeos_chapter':
  8713. case 'dir':
  8714. case 'chapter':
  8715. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8716. break;
  8717. }
  8718. }
  8719. }
  8720. $pdf = new PDF();
  8721. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8722. return $result;
  8723. }
  8724. return false;
  8725. }
  8726. /**
  8727. * Temp function to be moved in main_api or the best place around for this.
  8728. * Creates a file path if it doesn't exist
  8729. * @param string $path
  8730. */
  8731. public function create_path($path)
  8732. {
  8733. $path_bits = explode('/', dirname($path));
  8734. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8735. $path_built = IS_WINDOWS_OS ? '' : '/';
  8736. foreach ($path_bits as $bit) {
  8737. if (!empty ($bit)) {
  8738. $new_path = $path_built . $bit;
  8739. if (is_dir($new_path)) {
  8740. $path_built = $new_path . '/';
  8741. } else {
  8742. mkdir($new_path, api_get_permissions_for_new_directories());
  8743. $path_built = $new_path . '/';
  8744. }
  8745. }
  8746. }
  8747. }
  8748. /**
  8749. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8750. * @return boolean The results of the unlink function, or false if there was no image to start with
  8751. */
  8752. public function delete_lp_image()
  8753. {
  8754. $img = $this->get_preview_image();
  8755. if ($img != '') {
  8756. $del_file = $this->get_preview_image_path(null, 'sys');
  8757. if (isset($del_file) && file_exists($del_file)) {
  8758. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8759. if (file_exists($del_file_2)) {
  8760. unlink($del_file_2);
  8761. }
  8762. $this->set_preview_image('');
  8763. return @unlink($del_file);
  8764. }
  8765. }
  8766. return false;
  8767. }
  8768. /**
  8769. * Uploads an author image to the upload/learning_path/images directory
  8770. * @param array The image array, coming from the $_FILES superglobal
  8771. * @return boolean True on success, false on error
  8772. */
  8773. public function upload_image($image_array)
  8774. {
  8775. $image_moved = false;
  8776. if (!empty ($image_array['name'])) {
  8777. $upload_ok = process_uploaded_file($image_array);
  8778. $has_attachment = true;
  8779. } else {
  8780. $image_moved = true;
  8781. }
  8782. if ($upload_ok) {
  8783. if ($has_attachment) {
  8784. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8785. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8786. $updir = $sys_course_path . $courseDir;
  8787. // Try to add an extension to the file if it hasn't one.
  8788. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8789. if (!filter_extension($new_file_name)) {
  8790. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8791. $image_moved = false;
  8792. } else {
  8793. $file_extension = explode('.', $image_array['name']);
  8794. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8795. $filename = uniqid('');
  8796. $new_file_name = $filename.'.'.$file_extension;
  8797. $new_path = $updir.'/'.$new_file_name;
  8798. // Resize the image.
  8799. $temp = new Image($image_array['tmp_name']);
  8800. $picture_infos = $temp->get_image_info();
  8801. if ($picture_infos['width'] > 104) {
  8802. $thumbwidth = 104;
  8803. } else {
  8804. $thumbwidth = $picture_infos['width'];
  8805. }
  8806. if ($picture_infos['height'] > 96) {
  8807. $new_height = 96;
  8808. } else {
  8809. $new_height = $picture_infos['height'];
  8810. }
  8811. $temp->resize($thumbwidth, $new_height, 0);
  8812. $result = $temp->send_image($new_path);
  8813. // Storing the image filename.
  8814. if ($result) {
  8815. $image_moved = true;
  8816. $this->set_preview_image($new_file_name);
  8817. //Resize to 64px to use on course homepage
  8818. $temp->resize(64, 64, 0);
  8819. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8820. return true;
  8821. }
  8822. }
  8823. }
  8824. }
  8825. return false;
  8826. }
  8827. /**
  8828. * @param int $lp_id
  8829. * @param string $status
  8830. */
  8831. public function set_autolaunch($lp_id, $status)
  8832. {
  8833. $course_id = api_get_course_int_id();
  8834. $lp_id = intval($lp_id);
  8835. $status = intval($status);
  8836. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8837. // Setting everything to autolaunch = 0
  8838. $attributes['autolaunch'] = 0;
  8839. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8840. Database::update($lp_table, $attributes, $where);
  8841. if ($status == 1) {
  8842. //Setting my lp_id to autolaunch = 1
  8843. $attributes['autolaunch'] = 1;
  8844. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8845. Database::update($lp_table, $attributes, $where );
  8846. }
  8847. }
  8848. /**
  8849. * Gets previous_item_id for the next element of the lp_item table
  8850. * @author Isaac flores paz
  8851. * @return integer Previous item ID
  8852. */
  8853. public function select_previous_item_id()
  8854. {
  8855. $course_id = api_get_course_int_id();
  8856. if ($this->debug > 0) {
  8857. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8858. }
  8859. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8860. // Get the max order of the items
  8861. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8862. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8863. $rs_max_order = Database::query($sql_max_order);
  8864. $row_max_order = Database::fetch_object($rs_max_order);
  8865. $max_order = $row_max_order->display_order;
  8866. // Get the previous item ID
  8867. $sql = "SELECT id as previous FROM $table_lp_item
  8868. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8869. $rs_max = Database::query($sql);
  8870. $row_max = Database::fetch_object($rs_max);
  8871. // Return the previous item ID
  8872. return $row_max->previous;
  8873. }
  8874. /**
  8875. * Copies an LP
  8876. */
  8877. public function copy()
  8878. {
  8879. $main_path = api_get_path(SYS_CODE_PATH);
  8880. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8881. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8882. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8883. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8884. //Course builder
  8885. $cb = new CourseBuilder();
  8886. //Setting tools that will be copied
  8887. $cb->set_tools_to_build(array('learnpaths'));
  8888. //Setting elements that will be copied
  8889. $cb->set_tools_specific_id_list(
  8890. array('learnpaths' => array($this->lp_id))
  8891. );
  8892. $course = $cb->build();
  8893. //Course restorer
  8894. $course_restorer = new CourseRestorer($course);
  8895. $course_restorer->set_add_text_in_items(true);
  8896. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8897. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8898. }
  8899. public function verify_document_size($s)
  8900. {
  8901. $post_max = ini_get('post_max_size');
  8902. if (substr($post_max, -1, 1) == 'M') {
  8903. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  8904. } elseif (substr($post_max, -1, 1) == 'G') {
  8905. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  8906. }
  8907. $upl_max = ini_get('upload_max_filesize');
  8908. if (substr($upl_max, -1, 1) == 'M') {
  8909. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  8910. } elseif (substr($upl_max, -1, 1) == 'G') {
  8911. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  8912. }
  8913. $documents_total_space = DocumentManager::documents_total_space();
  8914. $course_max_space = DocumentManager::get_course_quota();
  8915. $total_size = filesize($s) + $documents_total_space;
  8916. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8917. return true;
  8918. } else{
  8919. return false;
  8920. }
  8921. }
  8922. /**
  8923. * Clear LP prerequisites
  8924. */
  8925. public function clear_prerequisites()
  8926. {
  8927. $course_id = $this->get_course_int_id();
  8928. if ($this->debug > 0) {
  8929. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8930. }
  8931. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8932. $lp_id = $this->get_id();
  8933. //Cleaning prerequisites
  8934. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8935. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8936. Database::query($sql);
  8937. //Cleaning mastery score for exercises
  8938. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8939. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8940. Database::query($sql);
  8941. }
  8942. public function set_previous_step_as_prerequisite_for_all_items()
  8943. {
  8944. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8945. $course_id = $this->get_course_int_id();
  8946. $lp_id = $this->get_id();
  8947. if (!empty($this->items)) {
  8948. $previous_item_id = null;
  8949. $previous_item_max = 0;
  8950. $previous_item_type = null;
  8951. $last_item_not_chapter = null;
  8952. $last_item_not_chapter_type = null;
  8953. $last_item_not_chapter_max = null;
  8954. foreach ($this->items as $item) {
  8955. // if there was a previous item... (otherwise jump to set it)
  8956. if (!empty($previous_item_id)) {
  8957. $current_item_id = $item->get_id(); //save current id
  8958. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8959. // Current item is not a folder, so it qualifies to get a prerequisites
  8960. if ($last_item_not_chapter_type == 'quiz') {
  8961. // if previous is quiz, mark its max score as default score to be achieved
  8962. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  8963. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  8964. Database::query($sql);
  8965. }
  8966. // now simply update the prerequisite to set it to the last non-chapter item
  8967. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  8968. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  8969. Database::query($sql);
  8970. // record item as 'non-chapter' reference
  8971. $last_item_not_chapter = $item->get_id();
  8972. $last_item_not_chapter_type = $item->get_type();
  8973. $last_item_not_chapter_max = $item->get_max();
  8974. }
  8975. } else {
  8976. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8977. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  8978. $last_item_not_chapter = $item->get_id();
  8979. $last_item_not_chapter_type = $item->get_type();
  8980. $last_item_not_chapter_max = $item->get_max();
  8981. }
  8982. }
  8983. // Saving the item as "previous item" for the next loop
  8984. $previous_item_id = $item->get_id();
  8985. $previous_item_max = $item->get_max();
  8986. $previous_item_type = $item->get_type();
  8987. }
  8988. }
  8989. }
  8990. /**
  8991. * @param array $params
  8992. */
  8993. public static function createCategory($params)
  8994. {
  8995. $em = Database::getManager();
  8996. $item = new CLpCategory();
  8997. $item->setName($params['name']);
  8998. $item->setCId($params['c_id']);
  8999. $em->persist($item);
  9000. $em->flush();
  9001. }
  9002. /**
  9003. * @param array $params
  9004. */
  9005. public static function updateCategory($params)
  9006. {
  9007. $em = Database::getManager();
  9008. /** @var CLpCategory $item */
  9009. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9010. if ($item) {
  9011. $item->setName($params['name']);
  9012. $em->merge($item);
  9013. $em->flush();
  9014. }
  9015. }
  9016. /**
  9017. * @param int $id
  9018. */
  9019. public static function moveUpCategory($id)
  9020. {
  9021. $em = Database::getManager();
  9022. /** @var CLpCategory $item */
  9023. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9024. if ($item) {
  9025. $position = $item->getPosition() - 1;
  9026. $item->setPosition($position);
  9027. $em->persist($item);
  9028. $em->flush();
  9029. }
  9030. }
  9031. /**
  9032. * @param int $id
  9033. */
  9034. public static function moveDownCategory($id)
  9035. {
  9036. $em = Database::getManager();
  9037. /** @var CLpCategory $item */
  9038. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9039. if ($item) {
  9040. $position = $item->getPosition() + 1;
  9041. $item->setPosition($position);
  9042. $em->persist($item);
  9043. $em->flush();
  9044. }
  9045. }
  9046. /**
  9047. * @param int $courseId
  9048. * @return int|mixed
  9049. */
  9050. public static function getCountCategories($courseId)
  9051. {
  9052. if (empty($course_id)) {
  9053. return 0;
  9054. }
  9055. $em = Database::getManager();
  9056. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9057. $query->setParameter('id', $courseId);
  9058. return $query->getSingleScalarResult();
  9059. }
  9060. /**
  9061. * @param int $courseId
  9062. *
  9063. * @return mixed
  9064. */
  9065. public static function getCategories($courseId)
  9066. {
  9067. $em = Database::getManager();
  9068. //Default behaviour
  9069. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9070. array('cId' => $course_id),
  9071. array('name' => 'ASC')
  9072. );*/
  9073. // Using doctrine extensions
  9074. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9075. array('cId' => $courseId)
  9076. )->getResult();
  9077. return $items;
  9078. }
  9079. /**
  9080. * @param int $id
  9081. *
  9082. * @return mixed
  9083. */
  9084. public static function getCategory($id)
  9085. {
  9086. $em = Database::getManager();
  9087. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9088. return $item;
  9089. }
  9090. /**
  9091. * @param int $courseId
  9092. * @return array
  9093. */
  9094. public static function getCategoryByCourse($courseId)
  9095. {
  9096. $em = Database::getManager();
  9097. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9098. return $items;
  9099. }
  9100. /**
  9101. * @param int $id
  9102. *
  9103. * @return mixed
  9104. */
  9105. public static function deleteCategory($id)
  9106. {
  9107. $em = Database::getManager();
  9108. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9109. if ($item) {
  9110. $courseId = $item->getCId();
  9111. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9112. $query->setParameter('id', $courseId);
  9113. $query->setParameter('catId', $item->getId());
  9114. $lps = $query->getResult();
  9115. // Setting category = 0.
  9116. if ($lps) {
  9117. foreach ($lps as $lpItem) {
  9118. $lpItem->setCategoryId(0);
  9119. }
  9120. }
  9121. // Removing category.
  9122. $em->remove($item);
  9123. $em->flush();
  9124. }
  9125. }
  9126. /**
  9127. * @param int $courseId
  9128. * @param bool $addSelectOption
  9129. *
  9130. * @return mixed
  9131. */
  9132. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9133. {
  9134. $items = self::getCategoryByCourse($courseId);
  9135. $cats = array();
  9136. if ($addSelectOption) {
  9137. $cats = array(get_lang('SelectACategory'));
  9138. }
  9139. if (!empty($items)) {
  9140. foreach ($items as $cat) {
  9141. $cats[$cat->getId()] = $cat->getName();
  9142. }
  9143. }
  9144. return $cats;
  9145. }
  9146. /**
  9147. * Return the scorm item type object with spaces replaced with _
  9148. * The return result is use to build a css classname like scorm_type_$return
  9149. * @param $in_type
  9150. * @return mixed
  9151. */
  9152. private static function format_scorm_type_item($in_type)
  9153. {
  9154. return str_replace(' ', '_', $in_type);
  9155. }
  9156. /**
  9157. * @return \learnpath
  9158. */
  9159. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9160. {
  9161. $lpObject = Session::read('lpobject');
  9162. $learnPath = null;
  9163. if (isset($lpObject)) {
  9164. $learnPath = unserialize($lpObject);
  9165. }
  9166. if (!is_object($learnPath)) {
  9167. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9168. }
  9169. return $learnPath;
  9170. }
  9171. /**
  9172. * @param int $itemId
  9173. * @return learnpathItem|false
  9174. */
  9175. public function getItem($itemId)
  9176. {
  9177. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9178. return $this->items[$itemId];
  9179. }
  9180. return false;
  9181. }
  9182. /**
  9183. * @return int
  9184. */
  9185. public function getCategoryId()
  9186. {
  9187. return $this->categoryId;
  9188. }
  9189. /**
  9190. * @param int $categoryId
  9191. * @return bool
  9192. */
  9193. public function setCategoryId($categoryId)
  9194. {
  9195. $this->categoryId = intval($categoryId);
  9196. $courseId = api_get_course_int_id();
  9197. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9198. $lp_id = $this->get_id();
  9199. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9200. WHERE c_id = $courseId AND id = $lp_id";
  9201. Database::query($sql);
  9202. return true;
  9203. }
  9204. /**
  9205. * Get whether this is a learning path with the possibility to subscribe
  9206. * users or not
  9207. * @return int
  9208. */
  9209. public function getSubscribeUsers()
  9210. {
  9211. return $this->subscribeUsers;
  9212. }
  9213. /**
  9214. * Set whether this is a learning path with the possibility to subscribe
  9215. * users or not
  9216. * @param int $subscribeUsers (0 = false, 1 = true)
  9217. */
  9218. public function setSubscribeUsers($value)
  9219. {
  9220. if ($this->debug > 0) {
  9221. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9222. }
  9223. $this->subscribeUsers = intval($value);;
  9224. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9225. $lp_id = $this->get_id();
  9226. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9227. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9228. Database::query($sql);
  9229. return true;
  9230. }
  9231. /**
  9232. * Calculate the count of stars for a user in this LP
  9233. * @param int $sessionId Optional. The session ID
  9234. * @return int The count of stars
  9235. */
  9236. public function getCalculateStars($sessionId = 0)
  9237. {
  9238. $stars = 0;
  9239. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9240. if ($progress > 50) {
  9241. $stars++;
  9242. }
  9243. // Calculate stars chapters evaluation
  9244. $exercisesItems = $this->getExercisesItems();
  9245. if (!empty($exercisesItems)) {
  9246. $totalResult = 0;
  9247. foreach ($exercisesItems as $exerciseItem) {
  9248. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9249. $this->user_id,
  9250. $exerciseItem->path,
  9251. $this->course_int_id,
  9252. $sessionId,
  9253. $this->lp_id,
  9254. $exerciseItem->db_id
  9255. );
  9256. $exerciseResult = 0;
  9257. foreach ($exerciseResultInfo as $result) {
  9258. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9259. }
  9260. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9261. $totalResult += $exerciseAverage;
  9262. }
  9263. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9264. if ($totalExerciseAverage >= 50) {
  9265. $stars++;
  9266. }
  9267. if ($totalExerciseAverage >= 80) {
  9268. $stars++;
  9269. }
  9270. }
  9271. // Calculate star for final evaluation
  9272. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9273. if (!empty($finalEvaluationItem)) {
  9274. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9275. $this->user_id,
  9276. $finalEvaluationItem->path,
  9277. $this->course_int_id,
  9278. $sessionId,
  9279. $this->lp_id,
  9280. $finalEvaluationItem->db_id
  9281. );
  9282. $evaluationResult = 0;
  9283. foreach ($evaluationResultInfo as $result) {
  9284. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9285. }
  9286. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9287. $evaluationAverage = $evaluationResult / $averageDivisor;
  9288. if ($evaluationAverage >= 80) {
  9289. $stars++;
  9290. }
  9291. }
  9292. return $stars;
  9293. }
  9294. /**
  9295. * Get the items of exercise type
  9296. * @return array The items. Otherwise return false
  9297. */
  9298. public function getExercisesItems()
  9299. {
  9300. $exercises = [];
  9301. foreach ($this->items as $item) {
  9302. if ($item->type != 'quiz') {
  9303. continue;
  9304. }
  9305. $exercises[] = $item;
  9306. }
  9307. array_pop($exercises);
  9308. return $exercises;
  9309. }
  9310. /**
  9311. * Get the item of exercise type (evaluation type)
  9312. * @return array The final evaluation. Otherwise return false
  9313. */
  9314. public function getFinalEvaluationItem()
  9315. {
  9316. $exercises = [];
  9317. foreach ($this->items as $item) {
  9318. if ($item->type != 'quiz') {
  9319. continue;
  9320. }
  9321. $exercises[] = $item;
  9322. }
  9323. return array_pop($exercises);
  9324. }
  9325. /**
  9326. * Calculate the total points achieved for the current user in this learning path
  9327. * @param int $sessionId Optional. The session Id
  9328. * @return int
  9329. */
  9330. public function getCalculateScore($sessionId = 0)
  9331. {
  9332. // Calculate stars chapters evaluation
  9333. $exercisesItems = $this->getExercisesItems();
  9334. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9335. $totalExercisesResult = 0;
  9336. $totalEvaluationResult = 0;
  9337. if ($exercisesItems !== false) {
  9338. foreach ($exercisesItems as $exerciseItem) {
  9339. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9340. $this->user_id,
  9341. $exerciseItem->path,
  9342. $this->course_int_id,
  9343. $sessionId,
  9344. $this->lp_id,
  9345. $exerciseItem->db_id
  9346. );
  9347. $exerciseResult = 0;
  9348. foreach ($exerciseResultInfo as $result) {
  9349. $exerciseResult += $result['exe_result'];
  9350. }
  9351. $totalExercisesResult += $exerciseResult;
  9352. }
  9353. }
  9354. if (!empty($finalEvaluationItem)) {
  9355. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9356. $this->user_id,
  9357. $finalEvaluationItem->path,
  9358. $this->course_int_id,
  9359. $sessionId,
  9360. $this->lp_id,
  9361. $finalEvaluationItem->db_id
  9362. );
  9363. foreach ($evaluationResultInfo as $result) {
  9364. $totalEvaluationResult += $result['exe_result'];
  9365. }
  9366. }
  9367. return $totalExercisesResult + $totalEvaluationResult;
  9368. }
  9369. /**
  9370. * Check if URL is not allowed to be show in a iframe
  9371. * @param string $src
  9372. *
  9373. * @return string
  9374. */
  9375. public function fixBlockedLinks($src)
  9376. {
  9377. $urlInfo = parse_url($src);
  9378. //$platformProtocol = api_get_protocol();
  9379. $platformProtocol = 'https';
  9380. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9381. $platformProtocol = 'http';
  9382. }
  9383. $protocolFixApplied = false;
  9384. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9385. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9386. if ($platformProtocol != $scheme) {
  9387. $_SESSION['x_frame_source'] = $src;
  9388. $src = 'blank.php?error=x_frames_options';
  9389. $protocolFixApplied = true;
  9390. }
  9391. if ($protocolFixApplied == false) {
  9392. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9393. // Check X-Frame-Options
  9394. $ch = curl_init();
  9395. $options = array(
  9396. CURLOPT_URL => $src,
  9397. CURLOPT_RETURNTRANSFER => true,
  9398. CURLOPT_HEADER => true,
  9399. CURLOPT_FOLLOWLOCATION => true,
  9400. CURLOPT_ENCODING => "",
  9401. CURLOPT_AUTOREFERER => true,
  9402. CURLOPT_CONNECTTIMEOUT => 120,
  9403. CURLOPT_TIMEOUT => 120,
  9404. CURLOPT_MAXREDIRS => 10,
  9405. );
  9406. curl_setopt_array($ch, $options);
  9407. $response = curl_exec($ch);
  9408. $httpCode = curl_getinfo($ch);
  9409. $headers = substr($response, 0, $httpCode['header_size']);
  9410. $error = false;
  9411. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9412. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9413. ) {
  9414. $error = true;
  9415. }
  9416. if ($error) {
  9417. $_SESSION['x_frame_source'] = $src;
  9418. $src = 'blank.php?error=x_frames_options';
  9419. }
  9420. }
  9421. }
  9422. return $src;
  9423. }
  9424. }
  9425. if (!function_exists('trim_value')) {
  9426. function trim_value(& $value) {
  9427. $value = trim($value);
  9428. }
  9429. }