learnpath.class.php 441 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388
  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_ins,
  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 WHERE c_id = $course_id AND name = '$name'";
  667. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  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. * Appends a message to the message attribute
  761. * @param string $string Message to append.
  762. */
  763. public function append_message($string)
  764. {
  765. if ($this->debug > 0) {
  766. error_log('New LP - In learnpath::append_message()', 0);
  767. }
  768. $this->message .= $string;
  769. }
  770. /**
  771. * Auto completes the parents of an item in case it's been completed or passed
  772. * @param integer $item Optional ID of the item from which to look for parents
  773. */
  774. public function autocomplete_parents($item)
  775. {
  776. $debug = $this->debug;
  777. if ($debug) {
  778. error_log('Learnpath::autocomplete_parents()', 0);
  779. }
  780. if (empty($item)) {
  781. $item = $this->current;
  782. }
  783. $currentItem = $this->getItem($item);
  784. if ($currentItem) {
  785. $parent_id = $currentItem->get_parent();
  786. $parent = $this->getItem($parent_id);
  787. if ($parent) {
  788. // if $item points to an object and there is a parent.
  789. if ($debug) {
  790. error_log(
  791. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  792. 0
  793. );
  794. }
  795. // New experiment including failed and browsed in completed status.
  796. //$current_status = $currentItem->get_status();
  797. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  798. // Fixes chapter auto complete
  799. if (true) {
  800. // If the current item is completed or passes or succeeded.
  801. $updateParentStatus = true;
  802. if ($debug) {
  803. error_log('Status of current item is alright', 0);
  804. }
  805. foreach ($parent->get_children() as $childItemId) {
  806. $childItem = $this->getItem($childItemId);
  807. // If children was not set try to get the info
  808. if (empty($childItem->db_item_view_id)) {
  809. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  810. }
  811. // Check all his brothers (parent's children) for completion status.
  812. if ($childItemId != $item) {
  813. if ($debug) {
  814. error_log(
  815. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  816. 0
  817. );
  818. }
  819. // Trying completing parents of failed and browsed items as well.
  820. if ($childItem->status_is(
  821. array(
  822. 'completed',
  823. 'passed',
  824. 'succeeded',
  825. 'browsed',
  826. 'failed'
  827. )
  828. )
  829. ) {
  830. // Keep completion status to true.
  831. continue;
  832. } else {
  833. if ($debug > 2) {
  834. error_log(
  835. '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,
  836. 0
  837. );
  838. }
  839. $updateParentStatus = false;
  840. break;
  841. }
  842. }
  843. }
  844. if ($updateParentStatus) {
  845. // If all the children were completed:
  846. $parent->set_status('completed');
  847. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  848. // Force the status to "completed"
  849. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  850. $this->update_queue[$parent->get_id()] = 'completed';
  851. if ($debug) {
  852. error_log(
  853. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  854. print_r($this->update_queue, 1),
  855. 0
  856. );
  857. }
  858. // Recursive call.
  859. $this->autocomplete_parents($parent->get_id());
  860. }
  861. }
  862. } else {
  863. if ($debug) {
  864. error_log("Parent #$parent_id does not exists");
  865. }
  866. }
  867. } else {
  868. if ($debug) {
  869. error_log("#$item is an item that doesn't have parents");
  870. }
  871. }
  872. }
  873. /**
  874. * Auto saves the current results into the database for the whole learnpath
  875. */
  876. public function autosave()
  877. {
  878. if ($this->debug > 0) {
  879. error_log('New LP - In learnpath::autosave()', 0);
  880. }
  881. // TODO: Add save operations for the learnpath itself.
  882. }
  883. /**
  884. * Clears the message attribute
  885. */
  886. public function clear_message()
  887. {
  888. if ($this->debug > 0) {
  889. error_log('New LP - In learnpath::clear_message()', 0);
  890. }
  891. $this->message = '';
  892. }
  893. /**
  894. * Closes the current resource
  895. *
  896. * Stops the timer
  897. * Saves into the database if required
  898. * Clears the current resource data from this object
  899. * @return boolean True on success, false on failure
  900. */
  901. public function close()
  902. {
  903. $course_id = api_get_course_int_id();
  904. if ($this->debug > 0) {
  905. error_log('New LP - In learnpath::close()', 0);
  906. }
  907. if (empty ($this->lp_id)) {
  908. $this->error = 'Trying to close this learnpath but no ID is set';
  909. return false;
  910. }
  911. $this->current_time_stop = time();
  912. $this->ordered_items = array();
  913. $this->index = 0;
  914. unset ($this->lp_id);
  915. //unset other stuff
  916. return true;
  917. }
  918. /**
  919. * Static admin function allowing removal of a learnpath
  920. * @param string Course code
  921. * @param integer Learnpath ID
  922. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  923. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  924. */
  925. public function delete($course = null, $id = null, $delete = 'keep')
  926. {
  927. $course_id = api_get_course_int_id();
  928. // TODO: Implement a way of getting this to work when the current object is not set.
  929. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  930. // If an ID is specifically given and the current LP is not the same, prevent delete.
  931. if (!empty ($id) && ($id != $this->lp_id)) {
  932. return false;
  933. }
  934. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  935. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  936. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  937. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  938. // Delete lp item id.
  939. foreach ($this->items as $id => $dummy) {
  940. $sql = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  941. Database::query($sql);
  942. }
  943. // Proposed by Christophe (nickname: clefevre)
  944. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  945. Database::query($sql);
  946. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  947. Database::query($sql);
  948. self::toggle_publish($this->lp_id, 'i');
  949. if ($this->type == 2 || $this->type == 3) {
  950. // This is a scorm learning path, delete the files as well.
  951. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  952. $res = Database::query($sql);
  953. if (Database :: num_rows($res) > 0) {
  954. $row = Database :: fetch_array($res);
  955. $path = $row['path'];
  956. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  957. $res = Database::query($sql);
  958. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  959. if ($this->debug > 2) {
  960. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  961. }
  962. } else {
  963. // No other LP uses that directory, delete it.
  964. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  965. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  966. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  967. if ($this->debug > 2) {
  968. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  969. }
  970. // Proposed by Christophe (clefevre).
  971. if (strcmp(substr($path, -2), "/.") == 0) {
  972. $path = substr($path, 0, -1); // Remove "." at the end.
  973. }
  974. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  975. rmdirr($course_scorm_dir . $path);
  976. }
  977. }
  978. }
  979. }
  980. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  981. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  982. // Delete tools
  983. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  984. Database::query($sql);
  985. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  986. Database::query($sql);
  987. // Updates the display order of all lps.
  988. $this->update_display_order();
  989. api_item_property_update(
  990. api_get_course_info(),
  991. TOOL_LEARNPATH,
  992. $this->lp_id,
  993. 'delete',
  994. api_get_user_id()
  995. );
  996. // Delete link of gradebook tool
  997. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  998. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  999. $result = Database::query($sql);
  1000. $row = Database :: fetch_array($result, 'ASSOC');*/
  1001. // Fixing gradebook link deleted see #5229.
  1002. /*
  1003. if (!empty($row['id'])) {
  1004. $link = LinkFactory :: load($row['id']);
  1005. if ($link[0] != null) {
  1006. $link[0]->delete();
  1007. }
  1008. }*/
  1009. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1010. if ($link_info !== false) {
  1011. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1012. }
  1013. if (api_get_setting('search_enabled') == 'true') {
  1014. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1015. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1016. }
  1017. }
  1018. /**
  1019. * Removes all the children of one item - dangerous!
  1020. * @param integer $id Element ID of which children have to be removed
  1021. * @return integer Total number of children removed
  1022. */
  1023. public function delete_children_items($id)
  1024. {
  1025. $course_id = api_get_course_int_id();
  1026. if ($this->debug > 0) {
  1027. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1028. }
  1029. $num = 0;
  1030. if (empty ($id) || $id != strval(intval($id))) {
  1031. return false;
  1032. }
  1033. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1034. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1035. $res = Database::query($sql);
  1036. while ($row = Database :: fetch_array($res)) {
  1037. $num += $this->delete_children_items($row['id']);
  1038. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1039. Database::query($sql_del);
  1040. $num++;
  1041. }
  1042. return $num;
  1043. }
  1044. /**
  1045. * Removes an item from the current learnpath
  1046. * @param integer $id Elem ID (0 if first)
  1047. * @param integer $remove Whether to remove the resource/data from the
  1048. * system or leave it (default: 'keep', others 'remove')
  1049. * @return integer Number of elements moved
  1050. * @todo implement resource removal
  1051. */
  1052. public function delete_item($id, $remove = 'keep')
  1053. {
  1054. $course_id = api_get_course_int_id();
  1055. if ($this->debug > 0) {
  1056. error_log('New LP - In learnpath::delete_item()', 0);
  1057. }
  1058. // TODO: Implement the resource removal.
  1059. if (empty ($id) || $id != strval(intval($id))) {
  1060. return false;
  1061. }
  1062. // First select item to get previous, next, and display order.
  1063. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1064. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1065. $res_sel = Database::query($sql_sel);
  1066. if (Database :: num_rows($res_sel) < 1) {
  1067. return false;
  1068. }
  1069. $row = Database :: fetch_array($res_sel);
  1070. $previous = $row['previous_item_id'];
  1071. $next = $row['next_item_id'];
  1072. $display = $row['display_order'];
  1073. $parent = $row['parent_item_id'];
  1074. $lp = $row['lp_id'];
  1075. // Delete children items.
  1076. $num = $this->delete_children_items($id);
  1077. if ($this->debug > 2) {
  1078. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1079. }
  1080. // Now delete the item.
  1081. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1082. if ($this->debug > 2) {
  1083. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1084. }
  1085. Database::query($sql_del);
  1086. // Now update surrounding items.
  1087. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1088. WHERE c_id = ".$course_id." AND id = $previous";
  1089. Database::query($sql_upd);
  1090. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1091. WHERE c_id = ".$course_id." AND id = $next";
  1092. Database::query($sql_upd);
  1093. // Now update all following items with new display order.
  1094. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1095. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1096. Database::query($sql_all);
  1097. //Removing prerequisites since the item will not longer exist
  1098. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1099. Database::query($sql_all);
  1100. // Remove from search engine if enabled.
  1101. if (api_get_setting('search_enabled') == 'true') {
  1102. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1103. $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';
  1104. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1105. $res = Database::query($sql);
  1106. if (Database :: num_rows($res) > 0) {
  1107. $row2 = Database :: fetch_array($res);
  1108. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1109. $di = new ChamiloIndexer();
  1110. $di->remove_document((int) $row2['search_did']);
  1111. }
  1112. $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';
  1113. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1114. Database::query($sql);
  1115. }
  1116. }
  1117. /**
  1118. * Updates an item's content in place
  1119. * @param integer $id Element ID
  1120. * @param integer $parent Parent item ID
  1121. * @param integer $previous Previous item ID
  1122. * @param string $title Item title
  1123. * @param string $description Item description
  1124. * @param string $prerequisites Prerequisites (optional)
  1125. * @param array $audio The array resulting of the $_FILES[mp3] element
  1126. * @param int $max_time_allowed
  1127. * @param string $url
  1128. * @return boolean True on success, false on error
  1129. */
  1130. public function edit_item(
  1131. $id,
  1132. $parent,
  1133. $previous,
  1134. $title,
  1135. $description,
  1136. $prerequisites = '0',
  1137. $audio = array(),
  1138. $max_time_allowed = 0,
  1139. $url = ''
  1140. ) {
  1141. $course_id = api_get_course_int_id();
  1142. $_course = api_get_course_info();
  1143. if ($this->debug > 0) {
  1144. error_log('New LP - In learnpath::edit_item()', 0);
  1145. }
  1146. if (empty ($max_time_allowed)) {
  1147. $max_time_allowed = 0;
  1148. }
  1149. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1150. return false;
  1151. }
  1152. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1153. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1154. $res_select = Database::query($sql_select);
  1155. $row_select = Database :: fetch_array($res_select);
  1156. $audio_update_sql = '';
  1157. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1158. // Create the audio folder if it does not exist yet.
  1159. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1160. if (!is_dir($filepath . 'audio')) {
  1161. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1162. $audio_id = add_document(
  1163. $_course,
  1164. '/audio',
  1165. 'folder',
  1166. 0,
  1167. 'audio'
  1168. );
  1169. api_item_property_update(
  1170. $_course,
  1171. TOOL_DOCUMENT,
  1172. $audio_id,
  1173. 'FolderCreated',
  1174. api_get_user_id(),
  1175. null,
  1176. null,
  1177. null,
  1178. null,
  1179. api_get_session_id()
  1180. );
  1181. api_item_property_update(
  1182. $_course,
  1183. TOOL_DOCUMENT,
  1184. $audio_id,
  1185. 'invisible',
  1186. api_get_user_id(),
  1187. null,
  1188. null,
  1189. null,
  1190. null,
  1191. api_get_session_id()
  1192. );
  1193. }
  1194. // Upload file in documents.
  1195. $pi = pathinfo($audio['name']);
  1196. if ($pi['extension'] == 'mp3') {
  1197. $c_det = api_get_course_info($this->cc);
  1198. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1199. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1200. $path = substr($path, 7);
  1201. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1202. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1203. }
  1204. }
  1205. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1206. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1207. // TODO: htmlspecialchars to be checked for encoding related problems.
  1208. if ($same_parent && $same_previous) {
  1209. // Only update title and description.
  1210. $sql = "UPDATE " . $tbl_lp_item . "
  1211. SET title = '" . Database::escape_string($title) . "',
  1212. prerequisite = '" . $prerequisites . "',
  1213. description = '" . Database::escape_string($description) . "'
  1214. " . $audio_update_sql . ",
  1215. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1216. WHERE c_id = ".$course_id." AND id = " . $id;
  1217. Database::query($sql);
  1218. } else {
  1219. $old_parent = $row_select['parent_item_id'];
  1220. $old_previous = $row_select['previous_item_id'];
  1221. $old_next = $row_select['next_item_id'];
  1222. $old_order = $row_select['display_order'];
  1223. $old_prerequisite = $row_select['prerequisite'];
  1224. $old_max_time_allowed = $row_select['max_time_allowed'];
  1225. /* BEGIN -- virtually remove the current item id */
  1226. /* for the next and previous item it is like the current item doesn't exist anymore */
  1227. if ($old_previous != 0) {
  1228. // Next
  1229. $sql = "UPDATE " . $tbl_lp_item . "
  1230. SET next_item_id = " . $old_next . "
  1231. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1232. Database::query($sql);
  1233. }
  1234. if ($old_next != 0) {
  1235. // Previous
  1236. $sql = "UPDATE " . $tbl_lp_item . "
  1237. SET previous_item_id = " . $old_previous . "
  1238. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1239. Database::query($sql);
  1240. }
  1241. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1242. $sql = "UPDATE " . $tbl_lp_item . "
  1243. SET display_order = display_order - 1
  1244. WHERE
  1245. c_id = ".$course_id." AND
  1246. display_order > " . $old_order . " AND
  1247. lp_id = " . $this->lp_id . " AND
  1248. parent_item_id = " . $old_parent;
  1249. Database::query($sql);
  1250. /* END -- virtually remove the current item id */
  1251. /* BEGIN -- update the current item id to his new location */
  1252. if ($previous == 0) {
  1253. // Select the data of the item that should come after the current item.
  1254. $sql = "SELECT id, display_order
  1255. FROM " . $tbl_lp_item . "
  1256. WHERE
  1257. c_id = ".$course_id." AND
  1258. lp_id = " . $this->lp_id . " AND
  1259. parent_item_id = " . $parent . " AND
  1260. previous_item_id = " . $previous;
  1261. $res_select_old = Database::query($sql);
  1262. $row_select_old = Database::fetch_array($res_select_old);
  1263. // If the new parent didn't have children before.
  1264. if (Database :: num_rows($res_select_old) == 0) {
  1265. $new_next = 0;
  1266. $new_order = 1;
  1267. } else {
  1268. $new_next = $row_select_old['id'];
  1269. $new_order = $row_select_old['display_order'];
  1270. }
  1271. } else {
  1272. // Select the data of the item that should come before the current item.
  1273. $sql = "SELECT next_item_id, display_order
  1274. FROM " . $tbl_lp_item . "
  1275. WHERE c_id = ".$course_id." AND id = " . $previous;
  1276. $res_select_old = Database::query($sql);
  1277. $row_select_old = Database :: fetch_array($res_select_old);
  1278. $new_next = $row_select_old['next_item_id'];
  1279. $new_order = $row_select_old['display_order'] + 1;
  1280. }
  1281. // TODO: htmlspecialchars to be checked for encoding related problems.
  1282. // Update the current item with the new data.
  1283. $sql = "UPDATE " . $tbl_lp_item . "
  1284. SET
  1285. title = '" . Database::escape_string($title) . "',
  1286. description = '" . Database::escape_string($description) . "',
  1287. parent_item_id = " . $parent . ",
  1288. previous_item_id = " . $previous . ",
  1289. next_item_id = " . $new_next . ",
  1290. display_order = " . $new_order . "
  1291. " . $audio_update_sql . "
  1292. WHERE c_id = ".$course_id." AND id = " . $id;
  1293. Database::query($sql);
  1294. if ($previous != 0) {
  1295. // Update the previous item's next_item_id.
  1296. $sql = "UPDATE " . $tbl_lp_item . "
  1297. SET next_item_id = " . $id . "
  1298. WHERE c_id = ".$course_id." AND id = " . $previous;
  1299. Database::query($sql);
  1300. }
  1301. if ($new_next != 0) {
  1302. // Update the next item's previous_item_id.
  1303. $sql = "UPDATE " . $tbl_lp_item . "
  1304. SET previous_item_id = " . $id . "
  1305. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1306. Database::query($sql);
  1307. }
  1308. if ($old_prerequisite != $prerequisites) {
  1309. $sql = "UPDATE " . $tbl_lp_item . "
  1310. SET prerequisite = '" . $prerequisites . "'
  1311. WHERE c_id = ".$course_id." AND id = " . $id;
  1312. Database::query($sql);
  1313. }
  1314. if ($old_max_time_allowed != $max_time_allowed) {
  1315. // update max time allowed
  1316. $sql = "UPDATE " . $tbl_lp_item . "
  1317. SET max_time_allowed = " . $max_time_allowed . "
  1318. WHERE c_id = ".$course_id." AND id = " . $id;
  1319. Database::query($sql);
  1320. }
  1321. // Update all the items with the same or a bigger display_order than the current item.
  1322. $sql = "UPDATE " . $tbl_lp_item . "
  1323. SET display_order = display_order + 1
  1324. WHERE
  1325. c_id = ".$course_id." AND
  1326. lp_id = " . $this->get_id() . " AND
  1327. id <> " . $id . " AND
  1328. parent_item_id = " . $parent . " AND
  1329. display_order >= " . $new_order;
  1330. Database::query($sql);
  1331. }
  1332. if ($row_select['item_type'] == 'link') {
  1333. $link = new Link();
  1334. $linkId = $row_select['path'];
  1335. $link->updateLink($linkId, $url);
  1336. }
  1337. }
  1338. /**
  1339. * Updates an item's prereq in place
  1340. * @param integer $id Element ID
  1341. * @param string $prerequisite_id Prerequisite Element ID
  1342. * @param int $mastery_score Prerequisite min score
  1343. * @param int $max_score Prerequisite max score
  1344. *
  1345. * @return boolean True on success, false on error
  1346. */
  1347. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1348. {
  1349. $course_id = api_get_course_int_id();
  1350. if ($this->debug > 0) {
  1351. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1352. }
  1353. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1354. return false;
  1355. }
  1356. $prerequisite_id = intval($prerequisite_id);
  1357. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1358. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1359. $mastery_score = 0;
  1360. }
  1361. if (!is_numeric($max_score) || $max_score < 0) {
  1362. $max_score = 100;
  1363. }
  1364. /*if ($mastery_score > $max_score) {
  1365. $max_score = $mastery_score;
  1366. }*/
  1367. if (!is_numeric($prerequisite_id)) {
  1368. $prerequisite_id = 'NULL';
  1369. }
  1370. $mastery_score = floatval($mastery_score);
  1371. $max_score = floatval($max_score);
  1372. $sql = " UPDATE $tbl_lp_item
  1373. SET
  1374. prerequisite = $prerequisite_id ,
  1375. prerequisite_min_score = $mastery_score ,
  1376. prerequisite_max_score = $max_score
  1377. WHERE c_id = $course_id AND id = $id";
  1378. Database::query($sql);
  1379. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1380. // Will this be enough to ensure unicity?
  1381. /*$sql = " UPDATE $tbl_lp_item
  1382. SET mastery_score = $mastery_score
  1383. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1384. Database::query($sql);*/
  1385. }
  1386. // TODO: Update the item object (can be ignored for now because refreshed).
  1387. return true;
  1388. }
  1389. /**
  1390. * Escapes a string with the available database escape function
  1391. * @param string String to escape
  1392. * @return string String escaped
  1393. * @deprecated use Database::escape_string
  1394. */
  1395. public function escape_string($string)
  1396. {
  1397. return Database::escape_string($string);
  1398. }
  1399. /**
  1400. * Static admin function exporting a learnpath into a zip file
  1401. * @param string Export type (scorm, zip, cd)
  1402. * @param string Course code
  1403. * @param integer Learnpath ID
  1404. * @param string Zip file name
  1405. * @return string Zip file path (or false on error)
  1406. */
  1407. public function export_lp($type, $course, $id, $zipname)
  1408. {
  1409. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1410. return false;
  1411. }
  1412. $url = '';
  1413. switch ($type) {
  1414. case 'scorm':
  1415. break;
  1416. case 'zip':
  1417. break;
  1418. case 'cdrom':
  1419. break;
  1420. }
  1421. return $url;
  1422. }
  1423. /**
  1424. * Gets all the chapters belonging to the same parent as the item/chapter given
  1425. * Can also be called as abstract method
  1426. * @param integer Item ID
  1427. * @return array A list of all the "brother items" (or an empty array on failure)
  1428. */
  1429. public function get_brother_chapters($id)
  1430. {
  1431. $course_id = api_get_course_int_id();
  1432. if ($this->debug > 0) {
  1433. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1434. }
  1435. if (empty($id)|| $id != strval(intval($id))) {
  1436. return array ();
  1437. }
  1438. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1439. $sql_parent = "SELECT * FROM $lp_item
  1440. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1441. $res_parent = Database::query($sql_parent);
  1442. if (Database :: num_rows($res_parent) > 0) {
  1443. $row_parent = Database :: fetch_array($res_parent);
  1444. $parent = $row_parent['parent_item_id'];
  1445. $sql_bros = "SELECT * FROM $lp_item
  1446. WHERE
  1447. c_id = ".$course_id." AND
  1448. parent_item_id = $parent AND
  1449. id = $id AND
  1450. item_type='dokeos_chapter'
  1451. ORDER BY display_order";
  1452. $res_bros = Database::query($sql_bros);
  1453. $list = array ();
  1454. while ($row_bro = Database :: fetch_array($res_bros)) {
  1455. $list[] = $row_bro;
  1456. }
  1457. return $list;
  1458. }
  1459. return array ();
  1460. }
  1461. /**
  1462. * Gets all the items belonging to the same parent as the item given
  1463. * Can also be called as abstract method
  1464. * @param integer Item ID
  1465. * @return array A list of all the "brother items" (or an empty array on failure)
  1466. */
  1467. public function get_brother_items($id) {
  1468. $course_id = api_get_course_int_id();
  1469. if ($this->debug > 0) {
  1470. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1471. }
  1472. if (empty ($id) || $id != strval(intval($id))) {
  1473. return array ();
  1474. }
  1475. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1476. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1477. $res_parent = Database::query($sql_parent);
  1478. if (Database :: num_rows($res_parent) > 0) {
  1479. $row_parent = Database :: fetch_array($res_parent);
  1480. $parent = $row_parent['parent_item_id'];
  1481. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1482. ORDER BY display_order";
  1483. $res_bros = Database::query($sql_bros);
  1484. $list = array ();
  1485. while ($row_bro = Database :: fetch_array($res_bros)) {
  1486. $list[] = $row_bro;
  1487. }
  1488. return $list;
  1489. }
  1490. return array ();
  1491. }
  1492. /**
  1493. * Get the specific prefix index terms of this learning path
  1494. * @param string $prefix
  1495. * @return array Array of terms
  1496. */
  1497. public function get_common_index_terms_by_prefix($prefix)
  1498. {
  1499. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1500. $terms = get_specific_field_values_list_by_prefix(
  1501. $prefix,
  1502. $this->cc,
  1503. TOOL_LEARNPATH,
  1504. $this->lp_id
  1505. );
  1506. $prefix_terms = array();
  1507. if (!empty($terms)) {
  1508. foreach ($terms as $term) {
  1509. $prefix_terms[] = $term['value'];
  1510. }
  1511. }
  1512. return $prefix_terms;
  1513. }
  1514. /**
  1515. * Gets the number of items currently completed
  1516. * @return integer The number of items currently completed
  1517. */
  1518. public function get_complete_items_count()
  1519. {
  1520. if ($this->debug > 0) {
  1521. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1522. }
  1523. $i = 0;
  1524. $completedStatusList = array(
  1525. 'completed',
  1526. 'passed',
  1527. 'succeeded',
  1528. 'browsed',
  1529. 'failed'
  1530. );
  1531. foreach ($this->items as $id => $dummy) {
  1532. // Trying failed and browsed considered "progressed" as well.
  1533. if ($this->items[$id]->status_is($completedStatusList) &&
  1534. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1535. $this->items[$id]->get_type() != 'dir'
  1536. ) {
  1537. $i++;
  1538. }
  1539. }
  1540. return $i;
  1541. }
  1542. /**
  1543. * Gets the current item ID
  1544. * @return integer The current learnpath item id
  1545. */
  1546. public function get_current_item_id()
  1547. {
  1548. $current = 0;
  1549. if ($this->debug > 0) {
  1550. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1551. }
  1552. if (!empty($this->current)) {
  1553. $current = $this->current;
  1554. }
  1555. if ($this->debug > 2) {
  1556. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1557. }
  1558. return $current;
  1559. }
  1560. /**
  1561. * Force to get the first learnpath item id
  1562. * @return integer The current learnpath item id
  1563. */
  1564. public function get_first_item_id()
  1565. {
  1566. $current = 0;
  1567. if (is_array($this->ordered_items)) {
  1568. $current = $this->ordered_items[0];
  1569. }
  1570. return $current;
  1571. }
  1572. /**
  1573. * Gets the total number of items available for viewing in this SCORM
  1574. * @return integer The total number of items
  1575. */
  1576. public function get_total_items_count()
  1577. {
  1578. if ($this->debug > 0) {
  1579. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1580. }
  1581. return count($this->items);
  1582. }
  1583. /**
  1584. * Gets the total number of items available for viewing in this SCORM but without chapters
  1585. * @return integer The total no-chapters number of items
  1586. */
  1587. public function get_total_items_count_without_chapters()
  1588. {
  1589. if ($this->debug > 0) {
  1590. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1591. }
  1592. $total = 0;
  1593. $typeListNotToCount = self::getChapterTypes();
  1594. foreach ($this->items as $temp2) {
  1595. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1596. $total++;
  1597. }
  1598. }
  1599. return $total;
  1600. }
  1601. /**
  1602. * Gets the first element URL.
  1603. * @return string URL to load into the viewer
  1604. */
  1605. public function first()
  1606. {
  1607. if ($this->debug > 0) {
  1608. error_log('New LP - In learnpath::first()', 0);
  1609. error_log('$this->last_item_seen '.$this->last_item_seen);
  1610. }
  1611. // Test if the last_item_seen exists and is not a dir.
  1612. if (count($this->ordered_items) == 0) {
  1613. $this->index = 0;
  1614. }
  1615. if ($this->debug > 0) {
  1616. if (isset($this->items[$this->last_item_seen])) {
  1617. $status = $this->items[$this->last_item_seen]->get_status();
  1618. }
  1619. error_log('status '.$status);
  1620. }
  1621. if (!empty($this->last_item_seen) &&
  1622. !empty($this->items[$this->last_item_seen]) &&
  1623. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1624. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1625. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1626. //&& !$this->items[$this->last_item_seen]->is_done()
  1627. ) {
  1628. if ($this->debug > 2) {
  1629. 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);
  1630. }
  1631. $index = -1;
  1632. foreach ($this->ordered_items as $myindex => $item_id) {
  1633. if ($item_id == $this->last_item_seen) {
  1634. $index = $myindex;
  1635. break;
  1636. }
  1637. }
  1638. if ($index == -1) {
  1639. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1640. if ($this->debug > 2) {
  1641. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1642. }
  1643. return false;
  1644. } else {
  1645. $this->last = $this->last_item_seen;
  1646. $this->current = $this->last_item_seen;
  1647. $this->index = $index;
  1648. }
  1649. } else {
  1650. if ($this->debug > 2) {
  1651. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1652. }
  1653. $index = 0;
  1654. // Loop through all ordered items and stop at the first item that is
  1655. // not a directory *and* that has not been completed yet.
  1656. while ( !empty($this->ordered_items[$index]) AND
  1657. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1658. (
  1659. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1660. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1661. $this->items[$this->ordered_items[$index]]->is_done() === true
  1662. ) AND $index < $this->max_ordered_items) {
  1663. $index++;
  1664. }
  1665. $this->last = $this->current;
  1666. // current is
  1667. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1668. $this->index = $index;
  1669. if ($this->debug > 2) {
  1670. error_log('$index ' . $index);
  1671. }
  1672. if ($this->debug > 2) {
  1673. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1674. }
  1675. }
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1678. }
  1679. }
  1680. /**
  1681. * Gets the information about an item in a format usable as JavaScript to update
  1682. * the JS API by just printing this content into the <head> section of the message frame
  1683. * @param int $item_id
  1684. * @return string
  1685. */
  1686. public function get_js_info($item_id = '')
  1687. {
  1688. if ($this->debug > 0) {
  1689. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1690. }
  1691. $info = '';
  1692. $item_id = intval($item_id);
  1693. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1694. //if item is defined, return values from DB
  1695. $oItem = $this->items[$item_id];
  1696. $info .= '<script language="javascript">';
  1697. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1698. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1699. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1700. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1701. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1702. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1703. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1704. $info .= "top.set_flag_synchronized();";
  1705. $info .= '</script>';
  1706. if ($this->debug > 2) {
  1707. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1708. }
  1709. return $info;
  1710. } else {
  1711. // If item_id is empty, just update to default SCORM data.
  1712. $info .= '<script language="javascript">';
  1713. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1714. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1715. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1716. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1717. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1718. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1719. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1720. $info .= "top.set_flag_synchronized();";
  1721. $info .= '</script>';
  1722. if ($this->debug > 2) {
  1723. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1724. }
  1725. return $info;
  1726. }
  1727. }
  1728. /**
  1729. * Gets the js library from the database
  1730. * @return string The name of the javascript library to be used
  1731. */
  1732. public function get_js_lib()
  1733. {
  1734. $lib = '';
  1735. if (!empty ($this->js_lib)) {
  1736. $lib = $this->js_lib;
  1737. }
  1738. return $lib;
  1739. }
  1740. /**
  1741. * Gets the learnpath database ID
  1742. * @return integer Learnpath ID in the lp table
  1743. */
  1744. public function get_id()
  1745. {
  1746. if (!empty ($this->lp_id)) {
  1747. return $this->lp_id;
  1748. } else {
  1749. return 0;
  1750. }
  1751. }
  1752. /**
  1753. * Gets the last element URL.
  1754. * @return string URL to load into the viewer
  1755. */
  1756. public function get_last()
  1757. {
  1758. if ($this->debug > 0) {
  1759. error_log('New LP - In learnpath::get_last()', 0);
  1760. }
  1761. $this->index = count($this->ordered_items) - 1;
  1762. return $this->ordered_items[$this->index];
  1763. }
  1764. /**
  1765. * Gets the navigation bar for the learnpath display screen
  1766. * @return string The HTML string to use as a navigation bar
  1767. */
  1768. public function get_navigation_bar($idBar = null, $display=null) {
  1769. if ($this->debug > 0) {
  1770. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1771. }
  1772. if(empty($idBar)){
  1773. $idBar='control-top';
  1774. }
  1775. if(empty($display)){
  1776. $display='display:block';
  1777. }
  1778. $navbar = null;
  1779. $lp_id = $this->lp_id;
  1780. $mycurrentitemid = $this->get_current_item_id();
  1781. if ($this->mode == 'fullscreen') {
  1782. $navbar = '
  1783. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1784. <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>
  1785. <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>
  1786. <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>.
  1787. <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>
  1788. </div>';
  1789. } else {
  1790. $navbar = '
  1791. <div id="'.$idBar.'" class="buttons well" style="'.$display.'">
  1792. <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>
  1793. <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>
  1794. <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>
  1795. </div>';
  1796. }
  1797. return $navbar;
  1798. }
  1799. /**
  1800. * Gets the next resource in queue (url).
  1801. * @return string URL to load into the viewer
  1802. */
  1803. public function get_next_index()
  1804. {
  1805. if ($this->debug > 0) {
  1806. error_log('New LP - In learnpath::get_next_index()', 0);
  1807. }
  1808. // TODO
  1809. $index = $this->index;
  1810. $index++;
  1811. if ($this->debug > 2) {
  1812. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1813. }
  1814. 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) {
  1815. $index++;
  1816. if ($index == $this->max_ordered_items){
  1817. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1818. return $this->index;
  1819. } else {
  1820. return $index;
  1821. }
  1822. }
  1823. }
  1824. if (empty ($this->ordered_items[$index])) {
  1825. return $this->index;
  1826. }
  1827. if ($this->debug > 2) {
  1828. error_log('New LP - index is now ' . $index, 0);
  1829. }
  1830. return $index;
  1831. }
  1832. /**
  1833. * Gets item_id for the next element
  1834. * @return integer Next item (DB) ID
  1835. */
  1836. public function get_next_item_id()
  1837. {
  1838. if ($this->debug > 0) {
  1839. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1840. }
  1841. $new_index = $this->get_next_index();
  1842. if (!empty ($new_index)) {
  1843. if (isset ($this->ordered_items[$new_index])) {
  1844. if ($this->debug > 2) {
  1845. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1846. }
  1847. return $this->ordered_items[$new_index];
  1848. }
  1849. }
  1850. if ($this->debug > 2) {
  1851. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1852. }
  1853. return 0;
  1854. }
  1855. /**
  1856. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1857. *
  1858. * Generally, the package provided is in the form of a zip file, so the function
  1859. * has been written to test a zip file. If not a zip, the function will return the
  1860. * default return value: ''
  1861. * @param string the path to the file
  1862. * @param string the original name of the file
  1863. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1864. */
  1865. public static function get_package_type($file_path, $file_name)
  1866. {
  1867. // Get name of the zip file without the extension.
  1868. $file_info = pathinfo($file_name);
  1869. $filename = $file_info['basename']; // Name including extension.
  1870. $extension = $file_info['extension']; // Extension only.
  1871. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1872. 'dll',
  1873. 'exe'
  1874. ))) {
  1875. return 'oogie';
  1876. }
  1877. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1878. 'dll',
  1879. 'exe'
  1880. ))) {
  1881. return 'woogie';
  1882. }
  1883. // Filename without its extension.
  1884. $file_base_name = str_replace('.' . $extension, '', $filename);
  1885. $zipFile = new PclZip($file_path);
  1886. // Check the zip content (real size and file extension).
  1887. $zipContentArray = $zipFile->listContent();
  1888. $package_type = '';
  1889. $at_root = false;
  1890. $manifest = '';
  1891. $aicc_match_crs = 0;
  1892. $aicc_match_au = 0;
  1893. $aicc_match_des = 0;
  1894. $aicc_match_cst = 0;
  1895. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1896. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1897. foreach ($zipContentArray as $thisContent) {
  1898. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1899. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1900. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1901. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1902. $package_type = 'scorm';
  1903. break; // Exit the foreach loop.
  1904. } elseif (
  1905. preg_match('/aicc\//i', $thisContent['filename']) ||
  1906. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1907. ) {
  1908. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1909. switch ($ext) {
  1910. case 'crs':
  1911. $aicc_match_crs = 1;
  1912. break;
  1913. case 'au':
  1914. $aicc_match_au = 1;
  1915. break;
  1916. case 'des':
  1917. $aicc_match_des = 1;
  1918. break;
  1919. case 'cst':
  1920. $aicc_match_cst = 1;
  1921. break;
  1922. default:
  1923. break;
  1924. }
  1925. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1926. } else {
  1927. $package_type = '';
  1928. }
  1929. }
  1930. }
  1931. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1932. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1933. $package_type = 'aicc';
  1934. }
  1935. return $package_type;
  1936. }
  1937. /**
  1938. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1939. * @return string URL to load into the viewer
  1940. */
  1941. public function get_previous_index()
  1942. {
  1943. if ($this->debug > 0) {
  1944. error_log('New LP - In learnpath::get_previous_index()', 0);
  1945. }
  1946. $index = $this->index;
  1947. if (isset ($this->ordered_items[$index -1])) {
  1948. $index--;
  1949. while (isset($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1950. $index--;
  1951. if ($index < 0) {
  1952. return $this->index;
  1953. }
  1954. }
  1955. } else {
  1956. if ($this->debug > 2) {
  1957. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1958. }
  1959. // There is no previous item.
  1960. }
  1961. return $index;
  1962. }
  1963. /**
  1964. * Gets item_id for the next element
  1965. * @return integer Previous item (DB) ID
  1966. */
  1967. public function get_previous_item_id()
  1968. {
  1969. if ($this->debug > 0) {
  1970. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1971. }
  1972. $new_index = $this->get_previous_index();
  1973. return $this->ordered_items[$new_index];
  1974. }
  1975. /**
  1976. * Gets the progress value from the progress_db attribute
  1977. * @return integer Current progress value
  1978. */
  1979. public function get_progress()
  1980. {
  1981. if ($this->debug > 0) {
  1982. error_log('New LP - In learnpath::get_progress()', 0);
  1983. }
  1984. if (!empty ($this->progress_db)) {
  1985. return $this->progress_db;
  1986. }
  1987. return 0;
  1988. }
  1989. /**
  1990. * Returns the HTML necessary to print a mediaplayer block inside a page
  1991. * @return string The mediaplayer HTML
  1992. */
  1993. public function get_mediaplayer($autostart = 'true')
  1994. {
  1995. $course_id = api_get_course_int_id();
  1996. $_course = api_get_course_info();
  1997. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1998. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1999. // Getting all the information about the item.
  2000. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2001. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2002. ON lp.id = lp_view.lp_item_id
  2003. WHERE
  2004. lp.id = '".$_SESSION['oLP']->current."' AND
  2005. lp.c_id = $course_id AND
  2006. lp_view.c_id = $course_id";
  2007. $result = Database::query($sql);
  2008. $row = Database::fetch_assoc($result);
  2009. $output = '';
  2010. if (!empty ($row['audio'])) {
  2011. $list = $_SESSION['oLP']->get_toc();
  2012. $type_quiz = false;
  2013. foreach($list as $toc) {
  2014. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2015. $type_quiz = true;
  2016. }
  2017. }
  2018. if ($type_quiz) {
  2019. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2020. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2021. } else {
  2022. $autostart_audio = $autostart;
  2023. }
  2024. } else {
  2025. $autostart_audio = 'true';
  2026. }
  2027. $courseInfo = api_get_course_info();
  2028. $audio = $row['audio'];
  2029. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2030. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2031. if (!file_exists($file)) {
  2032. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2033. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2034. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2035. }
  2036. $player = Display::getMediaPlayer(
  2037. $file,
  2038. array(
  2039. 'id' => 'lp_audio_media_player',
  2040. 'url' => $url,
  2041. 'autoplay' => $autostart_audio,
  2042. 'width' => '100%'
  2043. )
  2044. );
  2045. // The mp3 player.
  2046. $output = '<div id="container">';
  2047. $output .= $player;
  2048. $output .= '</div>';
  2049. }
  2050. return $output;
  2051. }
  2052. /**
  2053. * Checks if the learning path is visible for student after the progress
  2054. * of its prerequisite is completed, considering the time availability and
  2055. * the LP visibility.
  2056. * @param int $lp_id
  2057. * @param int $student_id
  2058. * @param string Course code (optional)
  2059. * @param int $sessionId
  2060. * @return bool
  2061. */
  2062. public static function is_lp_visible_for_student(
  2063. $lp_id,
  2064. $student_id,
  2065. $courseCode = null,
  2066. $sessionId = null
  2067. ) {
  2068. $lp_id = (int)$lp_id;
  2069. $courseInfo = api_get_course_info($courseCode);
  2070. $sessionId = intval($sessionId);
  2071. if (empty($sessionId)) {
  2072. $sessionId = api_get_session_id();
  2073. }
  2074. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2075. // Get current prerequisite
  2076. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2077. FROM $tbl_learnpath
  2078. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2079. $itemInfo = api_get_item_property_info(
  2080. $courseInfo['real_id'],
  2081. TOOL_LEARNPATH,
  2082. $lp_id,
  2083. $sessionId
  2084. );
  2085. // If the item was deleted.
  2086. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2087. return false;
  2088. }
  2089. $rs = Database::query($sql);
  2090. $now = time();
  2091. if (Database::num_rows($rs) > 0) {
  2092. $row = Database::fetch_array($rs, 'ASSOC');
  2093. $prerequisite = $row['prerequisite'];
  2094. $is_visible = true;
  2095. if (!empty($prerequisite)) {
  2096. $progress = self::getProgress(
  2097. $prerequisite,
  2098. $student_id,
  2099. $courseInfo['real_id'],
  2100. $sessionId
  2101. );
  2102. $progress = intval($progress);
  2103. if ($progress < 100) {
  2104. $is_visible = false;
  2105. }
  2106. }
  2107. // Also check the time availability of the LP
  2108. if ($is_visible) {
  2109. // Adding visibility restrictions
  2110. if (!empty($row['publicated_on']) &&
  2111. $row['publicated_on'] != '0000-00-00 00:00:00'
  2112. ) {
  2113. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2114. //api_not_allowed();
  2115. $is_visible = false;
  2116. }
  2117. }
  2118. // Blocking empty start times see BT#2800
  2119. global $_custom;
  2120. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2121. $_custom['lps_hidden_when_no_start_date']
  2122. ) {
  2123. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2124. //api_not_allowed();
  2125. $is_visible = false;
  2126. }
  2127. }
  2128. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2129. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2130. //api_not_allowed();
  2131. $is_visible = false;
  2132. }
  2133. }
  2134. }
  2135. // Check if the subscription users/group to a LP is ON
  2136. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2137. // Try group
  2138. $is_visible = false;
  2139. // Checking only the user visibility
  2140. $userVisibility = api_get_item_visibility(
  2141. $courseInfo,
  2142. 'learnpath',
  2143. $row['id'],
  2144. $sessionId,
  2145. $student_id,
  2146. 'LearnpathSubscription'
  2147. );
  2148. if ($userVisibility == 1) {
  2149. $is_visible = true;
  2150. } else {
  2151. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2152. if (!empty($userGroups)) {
  2153. foreach ($userGroups as $groupInfo) {
  2154. $groupId = $groupInfo['iid'];
  2155. $userVisibility = api_get_item_visibility(
  2156. $courseInfo,
  2157. 'learnpath',
  2158. $row['id'],
  2159. $sessionId,
  2160. null,
  2161. 'LearnpathSubscription',
  2162. $groupId
  2163. );
  2164. if ($userVisibility == 1) {
  2165. $is_visible = true;
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. return $is_visible;
  2173. }
  2174. return false;
  2175. }
  2176. /**
  2177. * @param int $lpId
  2178. * @param int $userId
  2179. * @param int $courseId
  2180. * @param int $sessionId
  2181. * @return int
  2182. */
  2183. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2184. {
  2185. $lpId = intval($lpId);
  2186. $userId = intval($userId);
  2187. $courseId = intval($courseId);
  2188. $sessionId = intval($sessionId);
  2189. $progress = 0;
  2190. $sessionCondition = api_get_session_condition($sessionId);
  2191. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2192. $sql = "SELECT * FROM $table
  2193. WHERE
  2194. c_id = ".$courseId." AND
  2195. lp_id = $lpId AND
  2196. user_id = $userId $sessionCondition";
  2197. $res = Database::query($sql);
  2198. if (Database :: num_rows($res) > 0) {
  2199. $row = Database:: fetch_array($res);
  2200. $progress = $row['progress'];
  2201. }
  2202. return $progress;
  2203. }
  2204. /**
  2205. * Displays a progress bar
  2206. * completed so far.
  2207. * @param integer $percentage Progress value to display
  2208. * @param string $text_add Text to display near the progress value
  2209. * @return string HTML string containing the progress bar
  2210. */
  2211. public static function get_progress_bar($percentage = -1, $text_add = '')
  2212. {
  2213. $text = $percentage . $text_add;
  2214. $output = '<div class="progress">
  2215. <div id="progress_bar_value" class="progress-bar" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2216. '. $text .'
  2217. </div>
  2218. </div>';
  2219. return $output;
  2220. }
  2221. /**
  2222. * @param string $mode can be '%' or 'abs'
  2223. * otherwise this value will be used $this->progress_bar_mode
  2224. * @return string
  2225. */
  2226. public function getProgressBar($mode = null)
  2227. {
  2228. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2229. return self::get_progress_bar($percentage, $text_add);
  2230. }
  2231. /**
  2232. * Gets the progress bar info to display inside the progress bar.
  2233. * Also used by scorm_api.php
  2234. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2235. * we display a number of completed elements per total elements
  2236. * @param integer $add Additional steps to fake as completed
  2237. * @return list array Percentage or number and symbol (% or /xx)
  2238. */
  2239. public function get_progress_bar_text($mode = '', $add = 0)
  2240. {
  2241. if ($this->debug > 0) {
  2242. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2243. }
  2244. if (empty($mode)) {
  2245. $mode = $this->progress_bar_mode;
  2246. }
  2247. $total_items = $this->get_total_items_count_without_chapters();
  2248. if ($this->debug > 2) {
  2249. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2250. }
  2251. $completeItems = $this->get_complete_items_count();
  2252. if ($this->debug > 2) {
  2253. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2254. }
  2255. if ($add != 0) {
  2256. $completeItems += $add;
  2257. if ($this->debug > 2) {
  2258. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2259. }
  2260. }
  2261. $text = '';
  2262. if ($completeItems > $total_items) {
  2263. $completeItems = $total_items;
  2264. }
  2265. $percentage = 0;
  2266. if ($mode == '%') {
  2267. if ($total_items > 0) {
  2268. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2269. } else {
  2270. $percentage = 0;
  2271. }
  2272. $percentage = number_format($percentage, 0);
  2273. $text = '%';
  2274. } elseif ($mode == 'abs') {
  2275. $percentage = $completeItems;
  2276. $text = '/' . $total_items;
  2277. }
  2278. return array(
  2279. $percentage,
  2280. $text
  2281. );
  2282. }
  2283. /**
  2284. * Gets the progress bar mode
  2285. * @return string The progress bar mode attribute
  2286. */
  2287. public function get_progress_bar_mode()
  2288. {
  2289. if ($this->debug > 0) {
  2290. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2291. }
  2292. if (!empty ($this->progress_bar_mode)) {
  2293. return $this->progress_bar_mode;
  2294. } else {
  2295. return '%';
  2296. }
  2297. }
  2298. /**
  2299. * Gets the learnpath proximity (remote or local)
  2300. * @return string Learnpath proximity
  2301. */
  2302. public function get_proximity()
  2303. {
  2304. if ($this->debug > 0) {
  2305. error_log('New LP - In learnpath::get_proximity()', 0);
  2306. }
  2307. if (!empty ($this->proximity)) {
  2308. return $this->proximity;
  2309. } else {
  2310. return '';
  2311. }
  2312. }
  2313. /**
  2314. * Gets the learnpath theme (remote or local)
  2315. * @return string Learnpath theme
  2316. */
  2317. public function get_theme()
  2318. {
  2319. if ($this->debug > 0) {
  2320. error_log('New LP - In learnpath::get_theme()', 0);
  2321. }
  2322. if (!empty ($this->theme)) {
  2323. return $this->theme;
  2324. } else {
  2325. return '';
  2326. }
  2327. }
  2328. /**
  2329. * Gets the learnpath session id
  2330. * @return string Learnpath theme
  2331. */
  2332. public function get_lp_session_id()
  2333. {
  2334. if ($this->debug > 0) {
  2335. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2336. }
  2337. if (!empty ($this->lp_session_id)) {
  2338. return $this->lp_session_id;
  2339. } else {
  2340. return 0;
  2341. }
  2342. }
  2343. /**
  2344. * Gets the learnpath image
  2345. * @return string Web URL of the LP image
  2346. */
  2347. public function get_preview_image()
  2348. {
  2349. if ($this->debug > 0) {
  2350. error_log('New LP - In learnpath::get_preview_image()', 0);
  2351. }
  2352. if (!empty($this->preview_image)) {
  2353. return $this->preview_image;
  2354. } else {
  2355. return '';
  2356. }
  2357. }
  2358. /**
  2359. * @param string $size
  2360. * @param string $path_type
  2361. * @return bool|string
  2362. */
  2363. public function get_preview_image_path($size = null, $path_type = 'web')
  2364. {
  2365. $preview_image = $this->get_preview_image();
  2366. if (isset($preview_image) && !empty($preview_image)) {
  2367. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2368. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2369. if (isset($size)) {
  2370. $info = pathinfo($preview_image);
  2371. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2372. if (file_exists($image_sys_path.$image_custom_size)) {
  2373. if ($path_type == 'web') {
  2374. return $image_path.$image_custom_size;
  2375. } else {
  2376. return $image_sys_path.$image_custom_size;
  2377. }
  2378. }
  2379. } else {
  2380. if ($path_type == 'web') {
  2381. return $image_path.$preview_image;
  2382. } else {
  2383. return $image_sys_path.$preview_image;
  2384. }
  2385. }
  2386. }
  2387. return false;
  2388. }
  2389. /**
  2390. * Gets the learnpath author
  2391. * @return string LP's author
  2392. */
  2393. public function get_author()
  2394. {
  2395. if ($this->debug > 0) {
  2396. error_log('New LP - In learnpath::get_author()', 0);
  2397. }
  2398. if (!empty ($this->author)) {
  2399. return $this->author;
  2400. } else {
  2401. return '';
  2402. }
  2403. }
  2404. /**
  2405. * Gets the learnpath author
  2406. * @return string LP's author
  2407. */
  2408. public function get_hide_toc_frame()
  2409. {
  2410. if ($this->debug > 0) {
  2411. error_log('New LP - In learnpath::get_author()', 0);
  2412. }
  2413. if (!empty ($this->hide_toc_frame)) {
  2414. return $this->hide_toc_frame;
  2415. } else {
  2416. return '';
  2417. }
  2418. }
  2419. /**
  2420. * Generate a new prerequisites string for a given item. If this item was a sco and
  2421. * its prerequisites were strings (instead of IDs), then transform those strings into
  2422. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2423. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2424. * same rule as the scorm_export() method
  2425. * @param integer Item ID
  2426. * @return string Prerequisites string ready for the export as SCORM
  2427. */
  2428. public function get_scorm_prereq_string($item_id)
  2429. {
  2430. if ($this->debug > 0) {
  2431. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2432. }
  2433. if (!is_object($this->items[$item_id])) {
  2434. return false;
  2435. }
  2436. /** @var learnpathItem $oItem */
  2437. $oItem = $this->items[$item_id];
  2438. $prereq = $oItem->get_prereq_string();
  2439. if (empty($prereq)) {
  2440. return '';
  2441. }
  2442. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2443. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2444. // then simply return it (with the ITEM_ prefix).
  2445. //return 'ITEM_' . $prereq;
  2446. return $this->items[$prereq]->ref;
  2447. } else {
  2448. if (isset($this->refs_list[$prereq])) {
  2449. // It's a simple string item from which the ID can be found in the refs list,
  2450. // so we can transform it directly to an ID for export.
  2451. return $this->items[$this->refs_list[$prereq]]->ref;
  2452. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2453. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2454. } else {
  2455. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2456. // and replace them, one by one, by the internal IDs (chamilo db)
  2457. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2458. // by a space as well.
  2459. $find = array (
  2460. '&',
  2461. '|',
  2462. '~',
  2463. '=',
  2464. '<>',
  2465. '{',
  2466. '}',
  2467. '*',
  2468. '(',
  2469. ')'
  2470. );
  2471. $replace = array (
  2472. ' ',
  2473. ' ',
  2474. ' ',
  2475. ' ',
  2476. ' ',
  2477. ' ',
  2478. ' ',
  2479. ' ',
  2480. ' ',
  2481. ' '
  2482. );
  2483. $prereq_mod = str_replace($find, $replace, $prereq);
  2484. $ids = explode(' ', $prereq_mod);
  2485. foreach ($ids as $id) {
  2486. $id = trim($id);
  2487. if (isset ($this->refs_list[$id])) {
  2488. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2489. }
  2490. }
  2491. return $prereq;
  2492. }
  2493. }
  2494. }
  2495. /**
  2496. * Returns the XML DOM document's node
  2497. * @param resource Reference to a list of objects to search for the given ITEM_*
  2498. * @param string The identifier to look for
  2499. * @return mixed The reference to the element found with that identifier. False if not found
  2500. */
  2501. public function get_scorm_xml_node(& $children, $id)
  2502. {
  2503. for ($i = 0; $i < $children->length; $i++) {
  2504. $item_temp = $children->item($i);
  2505. if ($item_temp->nodeName == 'item') {
  2506. if ($item_temp->getAttribute('identifier') == $id) {
  2507. return $item_temp;
  2508. }
  2509. }
  2510. $subchildren = $item_temp->childNodes;
  2511. if ($subchildren->length > 0) {
  2512. $val = $this->get_scorm_xml_node($subchildren, $id);
  2513. if (is_object($val)) {
  2514. return $val;
  2515. }
  2516. }
  2517. }
  2518. return false;
  2519. }
  2520. /**
  2521. * Returns a usable array of stats related to the current learnpath and user
  2522. * @return array Well-formatted array containing status for the current learnpath
  2523. */
  2524. public function get_stats()
  2525. {
  2526. if ($this->debug > 0) {
  2527. error_log('New LP - In learnpath::get_stats()', 0);
  2528. }
  2529. }
  2530. /**
  2531. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2532. * the given course (for all learnpaths and all users)
  2533. * @param string Course code
  2534. * @return array Well-formatted array containing status for the course's learnpaths
  2535. */
  2536. public function get_stats_course($course)
  2537. {
  2538. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2539. // TODO
  2540. }
  2541. /**
  2542. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2543. * the given course and learnpath (for all users)
  2544. * @param string Course code
  2545. * @param integer Learnpath ID
  2546. * @return array Well-formatted array containing status for the specified learnpath
  2547. */
  2548. public function get_stats_lp($course, $lp)
  2549. {
  2550. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2551. // TODO
  2552. }
  2553. /**
  2554. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2555. * the given course, learnpath and user.
  2556. * @param string Course code
  2557. * @param integer Learnpath ID
  2558. * @param integer User ID
  2559. * @return array Well-formatted array containing status for the specified learnpath and user
  2560. */
  2561. public function get_stats_lp_user($course, $lp, $user)
  2562. {
  2563. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2564. // TODO
  2565. }
  2566. /**
  2567. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2568. * the given course and learnpath (for all users)
  2569. * @param string Course code
  2570. * @param integer User ID
  2571. * @return array Well-formatted array containing status for the user's learnpaths
  2572. */
  2573. public function get_stats_user($course, $user) {
  2574. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2575. // TODO
  2576. }
  2577. /**
  2578. * Gets the status list for all LP's items
  2579. * @return array Array of [index] => [item ID => current status]
  2580. */
  2581. public function get_items_status_list()
  2582. {
  2583. if ($this->debug > 0) {
  2584. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2585. }
  2586. $list = array ();
  2587. foreach ($this->ordered_items as $item_id) {
  2588. $list[] = array (
  2589. $item_id => $this->items[$item_id]->get_status()
  2590. );
  2591. }
  2592. return $list;
  2593. }
  2594. /**
  2595. * Return the number of interactions for the given learnpath Item View ID.
  2596. * This method can be used as static.
  2597. * @param integer Item View ID
  2598. * @param integer course id
  2599. * @return integer Number of interactions
  2600. */
  2601. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2602. {
  2603. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2604. $lp_iv_id = intval($lp_iv_id);
  2605. $course_id = intval($course_id);
  2606. $sql = "SELECT count(*) FROM $table
  2607. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2608. $res = Database::query($sql);
  2609. $num = 0;
  2610. if (Database::num_rows($res)) {
  2611. $row = Database::fetch_array($res);
  2612. $num = $row[0];
  2613. }
  2614. return $num;
  2615. }
  2616. /**
  2617. * Return the interactions as an array for the given lp_iv_id.
  2618. * This method can be used as static.
  2619. * @param integer Learnpath Item View ID
  2620. * @return array
  2621. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2622. */
  2623. public static function get_iv_interactions_array($lp_iv_id)
  2624. {
  2625. $course_id = api_get_course_int_id();
  2626. $list = array();
  2627. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2628. if (empty($lp_iv_id)) {
  2629. return array();
  2630. }
  2631. $sql = "SELECT * FROM $table
  2632. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2633. ORDER BY order_id ASC";
  2634. $res = Database::query($sql);
  2635. $num = Database :: num_rows($res);
  2636. if ($num > 0) {
  2637. $list[] = array (
  2638. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2639. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2640. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2641. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2642. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2643. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2644. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2645. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2646. );
  2647. while ($row = Database :: fetch_array($res)) {
  2648. $list[] = array (
  2649. 'order_id' => ($row['order_id'] + 1),
  2650. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2651. 'type' => $row['interaction_type'],
  2652. 'time' => $row['completion_time'],
  2653. //'correct_responses' => $row['correct_responses'],
  2654. 'correct_responses' => '', // Hide correct responses from students.
  2655. 'student_response' => $row['student_response'],
  2656. 'result' => $row['result'],
  2657. 'latency' => $row['latency']
  2658. );
  2659. }
  2660. }
  2661. return $list;
  2662. }
  2663. /**
  2664. * Return the number of objectives for the given learnpath Item View ID.
  2665. * This method can be used as static.
  2666. * @param integer Item View ID
  2667. * @return integer Number of objectives
  2668. */
  2669. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2670. {
  2671. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2672. $course_id = intval($course_id);
  2673. $lp_iv_id = intval($lp_iv_id);
  2674. $sql = "SELECT count(*) FROM $table
  2675. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2676. //@todo seems that this always returns 0
  2677. $res = Database::query($sql);
  2678. $num = 0;
  2679. if (Database::num_rows($res)) {
  2680. $row = Database :: fetch_array($res);
  2681. $num = $row[0];
  2682. }
  2683. return $num;
  2684. }
  2685. /**
  2686. * Return the objectives as an array for the given lp_iv_id.
  2687. * This method can be used as static.
  2688. * @param integer Learnpath Item View ID
  2689. * @return array
  2690. * @todo Translate labels
  2691. */
  2692. public static function get_iv_objectives_array($lp_iv_id = 0)
  2693. {
  2694. $course_id = api_get_course_int_id();
  2695. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2696. $sql = "SELECT * FROM $table
  2697. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2698. ORDER BY order_id ASC";
  2699. $res = Database::query($sql);
  2700. $num = Database :: num_rows($res);
  2701. $list = array();
  2702. if ($num > 0) {
  2703. $list[] = array(
  2704. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2705. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2706. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2707. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2708. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2709. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2710. );
  2711. while ($row = Database :: fetch_array($res)) {
  2712. $list[] = array (
  2713. 'order_id' => ($row['order_id'] + 1),
  2714. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2715. 'score_raw' => $row['score_raw'],
  2716. 'score_max' => $row['score_max'],
  2717. 'score_min' => $row['score_min'],
  2718. 'status' => $row['status']
  2719. );
  2720. }
  2721. }
  2722. return $list;
  2723. }
  2724. /**
  2725. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2726. * used by get_html_toc() to be ready to display
  2727. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2728. */
  2729. public function get_toc()
  2730. {
  2731. if ($this->debug > 0) {
  2732. error_log('learnpath::get_toc()', 0);
  2733. }
  2734. $toc = array();
  2735. foreach ($this->ordered_items as $item_id) {
  2736. if ($this->debug > 2) {
  2737. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2738. }
  2739. // TODO: Change this link generation and use new function instead.
  2740. $toc[] = array (
  2741. 'id' => $item_id,
  2742. 'title' => $this->items[$item_id]->get_title(),
  2743. 'status' => $this->items[$item_id]->get_status(),
  2744. 'level' => $this->items[$item_id]->get_level(),
  2745. 'type' => $this->items[$item_id]->get_type(),
  2746. 'description' => $this->items[$item_id]->get_description(),
  2747. 'path' => $this->items[$item_id]->get_path(),
  2748. );
  2749. }
  2750. if ($this->debug > 2) {
  2751. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2752. }
  2753. return $toc;
  2754. }
  2755. /**
  2756. * Generate and return the table of contents for this learnpath. The JS
  2757. * table returned is used inside of scorm_api.php
  2758. * @return string A JS array vairiable construction
  2759. */
  2760. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2761. {
  2762. if ($this->debug > 0) {
  2763. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2764. }
  2765. $toc = $varname.' = new Array();';
  2766. foreach ($this->ordered_items as $item_id) {
  2767. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2768. }
  2769. if ($this->debug > 2) {
  2770. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2771. }
  2772. return $toc;
  2773. }
  2774. /**
  2775. * Gets the learning path type
  2776. * @param boolean Return the name? If false, return the ID. Default is false.
  2777. * @return mixed Type ID or name, depending on the parameter
  2778. */
  2779. public function get_type($get_name = false)
  2780. {
  2781. $res = false;
  2782. if ($this->debug > 0) {
  2783. error_log('New LP - In learnpath::get_type()', 0);
  2784. }
  2785. if (!empty ($this->type)) {
  2786. if ($get_name) {
  2787. // Get it from the lp_type table in main db.
  2788. } else {
  2789. $res = $this->type;
  2790. }
  2791. }
  2792. if ($this->debug > 2) {
  2793. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2794. }
  2795. return $res;
  2796. }
  2797. /**
  2798. * Gets the learning path type as static method
  2799. * @param boolean Return the name? If false, return the ID. Default is false.
  2800. * @return mixed Type ID or name, depending on the parameter
  2801. */
  2802. public static function get_type_static($lp_id = 0)
  2803. {
  2804. $course_id = api_get_course_int_id();
  2805. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2806. $lp_id = intval($lp_id);
  2807. $sql = "SELECT lp_type FROM $tbl_lp
  2808. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2809. $res = Database::query($sql);
  2810. if ($res === false) {
  2811. return null;
  2812. }
  2813. if (Database :: num_rows($res) <= 0) {
  2814. return null;
  2815. }
  2816. $row = Database :: fetch_array($res);
  2817. return $row['lp_type'];
  2818. }
  2819. /**
  2820. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2821. * This method can be used as abstract and is recursive
  2822. * @param integer Learnpath ID
  2823. * @param integer Parent ID of the items to look for
  2824. * @return mixed Ordered list of item IDs or false on error
  2825. */
  2826. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2827. {
  2828. if (empty($course_id)) {
  2829. $course_id = api_get_course_int_id();
  2830. } else {
  2831. $course_id = intval($course_id);
  2832. }
  2833. $list = array();
  2834. if (empty($lp)) {
  2835. return false;
  2836. }
  2837. $lp = intval($lp);
  2838. $parent = intval($parent);
  2839. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2840. $sql = "SELECT id FROM $tbl_lp_item
  2841. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2842. ORDER BY display_order";
  2843. $res = Database::query($sql);
  2844. while ($row = Database :: fetch_array($res)) {
  2845. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2846. $list[] = $row['id'];
  2847. foreach ($sublist as $item) {
  2848. $list[] = $item;
  2849. }
  2850. }
  2851. return $list;
  2852. }
  2853. /**
  2854. * @return array
  2855. */
  2856. public static function getChapterTypes()
  2857. {
  2858. return array(
  2859. 'dokeos_chapter',
  2860. 'dokeos_module',
  2861. 'chapter',
  2862. 'dir'
  2863. );
  2864. }
  2865. /**
  2866. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2867. * @return string HTML TOC ready to display
  2868. */
  2869. public function get_html_toc($toc_list = null)
  2870. {
  2871. global $_configuration;
  2872. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2873. if ($this->debug > 0) {
  2874. error_log('In learnpath::get_html_toc()', 0);
  2875. }
  2876. if (empty($toc_list)) {
  2877. $toc_list = $this->get_toc();
  2878. }
  2879. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2880. $html = '<div class="scorm-body">';
  2881. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2882. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2883. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2884. if ($this->get_lp_session_id() == api_get_session_id()) {
  2885. $html .= '<div id="actions_lp" class="actions_lp">';
  2886. $html .= '<div class="btn-group">';
  2887. $html .= "<a class='btn btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2888. $html .= "<a class='btn btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2889. $html .= '<a class="btn btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2890. $html .= '</div>';
  2891. $html .= '</div>';
  2892. }
  2893. }
  2894. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2895. require_once 'resourcelinker.inc.php';
  2896. // Temporary variables.
  2897. $mycurrentitemid = $this->get_current_item_id();
  2898. $color_counter = 0;
  2899. $i = 0;
  2900. foreach ($toc_list as $item) {
  2901. // TODO: Complete this
  2902. $icon_name = array (
  2903. 'not attempted' => '../img/notattempted.gif',
  2904. 'incomplete' => '../img/incomplete.png',
  2905. 'failed' => '../img/delete.png',
  2906. 'completed' => '../img/completed.png',
  2907. 'passed' => '../img/passed.png',
  2908. 'succeeded' => '../img/succeeded.png',
  2909. 'browsed' => '../img/completed.png',
  2910. );
  2911. // Style Status
  2912. $class_name = array (
  2913. 'not attempted' => 'scorm_not_attempted',
  2914. 'incomplete' => 'scorm_not_attempted',
  2915. 'failed' => 'scorm_failed',
  2916. 'completed' => 'scorm_completed',
  2917. 'passed' => 'scorm_completed',
  2918. 'succeeded' => 'scorm_completed',
  2919. 'browsed' => 'scorm_completed',
  2920. );
  2921. $scorm_color_background = 'row_odd';
  2922. $style_item = '';
  2923. if ($color_counter % 2 == 0) {
  2924. $scorm_color_background = 'row_even';
  2925. }
  2926. $dirTypes = self::getChapterTypes();
  2927. if (in_array($item['type'], $dirTypes)) {
  2928. $scorm_color_background ='scorm_item_section ';
  2929. $style_item = '';
  2930. }
  2931. if ($item['id'] == $this->current) {
  2932. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2933. } elseif (!in_array($item['type'], $dirTypes)) {
  2934. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2935. }
  2936. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2937. // Learning path title
  2938. $title = $item['title'];
  2939. if (empty ($title)) {
  2940. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2941. }
  2942. $title = Security::remove_XSS($title);
  2943. // Learning path personalization
  2944. // build the LP tree
  2945. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2946. $description = $item['description'];
  2947. if (empty($description)) {
  2948. $description = $title;
  2949. }
  2950. if (in_array($item['type'], $dirTypes)) {
  2951. // Chapters
  2952. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2953. } else {
  2954. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2955. $html .= '<a name="atoc_'.$item['id'].'" />';
  2956. }
  2957. if (in_array($item['type'], $dirTypes)) {
  2958. // Chapter
  2959. // if you want to put an image before, you should use css
  2960. $html .= stripslashes($title);
  2961. } else {
  2962. $this->get_link('http', $item['id'], $toc_list);
  2963. $html .= '<a class="items-list" href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2964. }
  2965. $html .= "</div>";
  2966. if ($scorm_color_background != '') {
  2967. $html .= '</div>';
  2968. }
  2969. $color_counter++;
  2970. }
  2971. $html .= "</div>";
  2972. $html .= "</div>";
  2973. return $html;
  2974. }
  2975. /**
  2976. * Gets the learnpath maker name - generally the editor's name
  2977. * @return string Learnpath maker name
  2978. */
  2979. public function get_maker()
  2980. {
  2981. if ($this->debug > 0) {
  2982. error_log('New LP - In learnpath::get_maker()', 0);
  2983. }
  2984. if (!empty ($this->maker)) {
  2985. return $this->maker;
  2986. } else {
  2987. return '';
  2988. }
  2989. }
  2990. /**
  2991. * Gets the user-friendly message stored in $this->message
  2992. * @return string Message
  2993. */
  2994. public function get_message()
  2995. {
  2996. if ($this->debug > 0) {
  2997. error_log('New LP - In learnpath::get_message()', 0);
  2998. }
  2999. return $this->message;
  3000. }
  3001. /**
  3002. * Gets the learnpath name/title
  3003. * @return string Learnpath name/title
  3004. */
  3005. public function get_name()
  3006. {
  3007. if ($this->debug > 0) {
  3008. error_log('New LP - In learnpath::get_name()', 0);
  3009. }
  3010. if (!empty ($this->name)) {
  3011. return $this->name;
  3012. } else {
  3013. return 'N/A';
  3014. }
  3015. }
  3016. /**
  3017. * Gets a link to the resource from the present location, depending on item ID.
  3018. * @param string $type Type of link expected
  3019. * @param integer $item_id Learnpath item ID
  3020. * @return string $provided_toc Link to the lp_item resource
  3021. */
  3022. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3023. {
  3024. $course_id = $this->get_course_int_id();
  3025. if ($this->debug > 0) {
  3026. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3027. }
  3028. if (empty($item_id)) {
  3029. if ($this->debug > 2) {
  3030. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3031. }
  3032. $item_id = $this->get_current_item_id();
  3033. }
  3034. if (empty($item_id)) {
  3035. if ($this->debug > 2) {
  3036. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3037. }
  3038. //still empty, this means there was no item_id given and we are not in an object context or
  3039. //the object property is empty, return empty link
  3040. $item_id = $this->first();
  3041. return '';
  3042. }
  3043. $file = '';
  3044. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3045. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3046. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3047. $item_id = intval($item_id);
  3048. $sql = "SELECT
  3049. l.lp_type as ltype,
  3050. l.path as lpath,
  3051. li.item_type as litype,
  3052. li.path as lipath,
  3053. li.parameters as liparams
  3054. FROM $lp_table l
  3055. INNER JOIN $lp_item_table li
  3056. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3057. WHERE li.id = $item_id ";
  3058. if ($this->debug > 2) {
  3059. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3060. }
  3061. $res = Database::query($sql);
  3062. if (Database :: num_rows($res) > 0) {
  3063. $row = Database :: fetch_array($res);
  3064. $lp_type = $row['ltype'];
  3065. $lp_path = $row['lpath'];
  3066. $lp_item_type = $row['litype'];
  3067. $lp_item_path = $row['lipath'];
  3068. $lp_item_params = $row['liparams'];
  3069. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3070. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3071. }
  3072. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3073. if ($type == 'http') {
  3074. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3075. } else {
  3076. $course_path = $sys_course_path; //system path
  3077. }
  3078. // 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.
  3079. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3080. $lp_type = 1;
  3081. }
  3082. if ($this->debug > 2) {
  3083. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3084. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3085. }
  3086. // Now go through the specific cases to get the end of the path
  3087. // @todo Use constants instead of int values.
  3088. switch ($lp_type) {
  3089. case 1 :
  3090. if ($lp_item_type == 'dokeos_chapter') {
  3091. $file = 'lp_content.php?type=dir';
  3092. } else {
  3093. require_once 'resourcelinker.inc.php';
  3094. $file = rl_get_resource_link_for_learnpath(
  3095. $course_id,
  3096. $this->get_id(),
  3097. $item_id,
  3098. $this->get_view_id()
  3099. );
  3100. if ($this->debug > 0) {
  3101. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3102. }
  3103. if ($lp_item_type == 'link') {
  3104. if (Link::is_youtube_link($file)) {
  3105. $src = Link::get_youtube_video_id($file);
  3106. $file = 'embed.php?type=youtube&source='.$src;
  3107. } elseif (Link::isVimeoLink($file)) {
  3108. $src = Link::getVimeoLinkId($file);
  3109. $file = 'embed.php?type=vimeo&source='.$src;
  3110. } else {
  3111. // If the current site is HTTPS and the link is
  3112. // HTTP, browsers will refuse opening the link
  3113. $urlId = api_get_current_access_url_id();
  3114. $url = api_get_access_url($urlId, false);
  3115. $protocol = substr($url['url'], 0, 5);
  3116. if ($protocol === 'https') {
  3117. $linkProtocol = substr($file, 0, 5);
  3118. if ($linkProtocol === 'http:') {
  3119. //this is the special intervention case
  3120. $file = 'embed.php?type=nonhttps&source=' . urlencode($file);
  3121. }
  3122. }
  3123. }
  3124. } else {
  3125. // Check how much attempts of a exercise exits in lp
  3126. $lp_item_id = $this->get_current_item_id();
  3127. $lp_view_id = $this->get_view_id();
  3128. $prevent_reinit = null;
  3129. if (isset($this->items[$this->current])) {
  3130. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3131. }
  3132. if (empty($provided_toc)) {
  3133. if ($this->debug > 0) {
  3134. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3135. }
  3136. $list = $this->get_toc();
  3137. } else {
  3138. if ($this->debug > 0) {
  3139. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3140. }
  3141. $list = $provided_toc;
  3142. }
  3143. $type_quiz = false;
  3144. foreach ($list as $toc) {
  3145. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3146. $type_quiz = true;
  3147. }
  3148. }
  3149. if ($type_quiz) {
  3150. $lp_item_id = intval($lp_item_id);
  3151. $lp_view_id = intval($lp_view_id);
  3152. $sql = "SELECT count(*) FROM $lp_item_view_table
  3153. WHERE
  3154. c_id = $course_id AND
  3155. lp_item_id='" . $lp_item_id . "' AND
  3156. lp_view_id ='" . $lp_view_id . "' AND
  3157. status='completed'";
  3158. $result = Database::query($sql);
  3159. $row_count = Database :: fetch_row($result);
  3160. $count_item_view = (int) $row_count[0];
  3161. $not_multiple_attempt = 0;
  3162. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3163. $not_multiple_attempt = 1;
  3164. }
  3165. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3166. }
  3167. $tmp_array = explode('/', $file);
  3168. $document_name = $tmp_array[count($tmp_array) - 1];
  3169. if (strpos($document_name, '_DELETED_')) {
  3170. $file = 'blank.php?error=document_deleted';
  3171. }
  3172. }
  3173. }
  3174. break;
  3175. case 2 :
  3176. if ($this->debug > 2) {
  3177. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3178. }
  3179. if ($lp_item_type != 'dir') {
  3180. // Quite complex here:
  3181. // We want to make sure 'http://' (and similar) links can
  3182. // be loaded as is (withouth the Chamilo path in front) but
  3183. // some contents use this form: resource.htm?resource=http://blablabla
  3184. // which means we have to find a protocol at the path's start, otherwise
  3185. // it should not be considered as an external URL.
  3186. //if ($this->prerequisites_match($item_id)) {
  3187. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3188. if ($this->debug > 2) {
  3189. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3190. }
  3191. // Distant url, return as is.
  3192. $file = $lp_item_path;
  3193. } else {
  3194. if ($this->debug > 2) {
  3195. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3196. }
  3197. // Prevent getting untranslatable urls.
  3198. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3199. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3200. // Prepare the path.
  3201. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3202. // TODO: Fix this for urls with protocol header.
  3203. $file = str_replace('//', '/', $file);
  3204. $file = str_replace(':/', '://', $file);
  3205. if (substr($lp_path, -1) == '/') {
  3206. $lp_path = substr($lp_path, 0, -1);
  3207. }
  3208. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3209. // if file not found.
  3210. $decoded = html_entity_decode($lp_item_path);
  3211. list ($decoded) = explode('?', $decoded);
  3212. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3213. require_once 'resourcelinker.inc.php';
  3214. $file = rl_get_resource_link_for_learnpath(
  3215. $course_id,
  3216. $this->get_id(),
  3217. $item_id,
  3218. $this->get_view_id()
  3219. );
  3220. if (empty($file)) {
  3221. $file = 'blank.php?error=document_not_found';
  3222. } else {
  3223. $tmp_array = explode('/', $file);
  3224. $document_name = $tmp_array[count($tmp_array) - 1];
  3225. if (strpos($document_name, '_DELETED_')) {
  3226. $file = 'blank.php?error=document_deleted';
  3227. } else {
  3228. $file = 'blank.php?error=document_not_found';
  3229. }
  3230. }
  3231. } else {
  3232. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3233. }
  3234. }
  3235. }
  3236. // We want to use parameters if they were defined in the imsmanifest
  3237. if (strpos($file, 'blank.php') === false) {
  3238. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3239. }
  3240. } else {
  3241. $file = 'lp_content.php?type=dir';
  3242. }
  3243. break;
  3244. case 3 :
  3245. if ($this->debug > 2) {
  3246. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3247. }
  3248. // Formatting AICC HACP append URL.
  3249. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3250. if (!empty($lp_item_params)) {
  3251. $aicc_append .= $lp_item_params . '&';
  3252. }
  3253. if ($lp_item_type != 'dir') {
  3254. // Quite complex here:
  3255. // We want to make sure 'http://' (and similar) links can
  3256. // be loaded as is (withouth the Chamilo path in front) but
  3257. // some contents use this form: resource.htm?resource=http://blablabla
  3258. // which means we have to find a protocol at the path's start, otherwise
  3259. // it should not be considered as an external URL.
  3260. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3261. if ($this->debug > 2) {
  3262. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3263. }
  3264. // Distant url, return as is.
  3265. $file = $lp_item_path;
  3266. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3267. /*
  3268. if (stristr($file,'<servername>') !== false) {
  3269. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3270. }
  3271. */
  3272. if (stripos($file, '<servername>') !== false) {
  3273. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3274. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3275. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3276. }
  3277. //
  3278. $file .= $aicc_append;
  3279. } else {
  3280. if ($this->debug > 2) {
  3281. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3282. }
  3283. // Prevent getting untranslatable urls.
  3284. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3285. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3286. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3287. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3288. // TODO: Fix this for urls with protocol header.
  3289. $file = str_replace('//', '/', $file);
  3290. $file = str_replace(':/', '://', $file);
  3291. $file .= $aicc_append;
  3292. }
  3293. } else {
  3294. $file = 'lp_content.php?type=dir';
  3295. }
  3296. break;
  3297. case 4 :
  3298. break;
  3299. default :
  3300. break;
  3301. }
  3302. // Replace &amp; by & because &amp; will break URL with params
  3303. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3304. }
  3305. if ($this->debug > 2) {
  3306. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3307. }
  3308. return $file;
  3309. }
  3310. /**
  3311. * Gets the latest usable view or generate a new one
  3312. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3313. * @return integer DB lp_view id
  3314. */
  3315. public function get_view($attempt_num = 0)
  3316. {
  3317. if ($this->debug > 0) {
  3318. error_log('New LP - In learnpath::get_view()', 0);
  3319. }
  3320. $search = '';
  3321. // Use $attempt_num to enable multi-views management (disabled so far).
  3322. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3323. $search = 'AND view_count = ' . $attempt_num;
  3324. }
  3325. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3326. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3327. $course_id = api_get_course_int_id();
  3328. $sessionId = api_get_session_id();
  3329. $sql = "SELECT id, view_count FROM $lp_view_table
  3330. WHERE
  3331. c_id = " . $course_id . " AND
  3332. lp_id = " . $this->get_id() . " AND
  3333. user_id = " . $this->get_user_id() . " AND
  3334. session_id = $sessionId
  3335. $search
  3336. ORDER BY view_count DESC";
  3337. $res = Database::query($sql);
  3338. if (Database :: num_rows($res) > 0) {
  3339. $row = Database :: fetch_array($res);
  3340. $this->lp_view_id = $row['id'];
  3341. } else if (!api_is_invitee()) {
  3342. // There is no database record, create one.
  3343. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3344. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3345. Database::query($sql);
  3346. $id = Database :: insert_id();
  3347. $this->lp_view_id = $id;
  3348. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3349. Database::query($sql);
  3350. }
  3351. return $this->lp_view_id;
  3352. }
  3353. /**
  3354. * Gets the current view id
  3355. * @return integer View ID (from lp_view)
  3356. */
  3357. public function get_view_id()
  3358. {
  3359. if ($this->debug > 0) {
  3360. error_log('New LP - In learnpath::get_view_id()', 0);
  3361. }
  3362. if (!empty ($this->lp_view_id)) {
  3363. return $this->lp_view_id;
  3364. } else {
  3365. return 0;
  3366. }
  3367. }
  3368. /**
  3369. * Gets the update queue
  3370. * @return array Array containing IDs of items to be updated by JavaScript
  3371. */
  3372. public function get_update_queue()
  3373. {
  3374. if ($this->debug > 0) {
  3375. error_log('New LP - In learnpath::get_update_queue()', 0);
  3376. }
  3377. return $this->update_queue;
  3378. }
  3379. /**
  3380. * Gets the user ID
  3381. * @return integer User ID
  3382. */
  3383. public function get_user_id()
  3384. {
  3385. if ($this->debug > 0) {
  3386. error_log('New LP - In learnpath::get_user_id()', 0);
  3387. }
  3388. if (!empty ($this->user_id)) {
  3389. return $this->user_id;
  3390. } else {
  3391. return false;
  3392. }
  3393. }
  3394. /**
  3395. * Checks if any of the items has an audio element attached
  3396. * @return bool True or false
  3397. */
  3398. public function has_audio()
  3399. {
  3400. if ($this->debug > 1) {
  3401. error_log('New LP - In learnpath::has_audio()', 0);
  3402. }
  3403. $has = false;
  3404. foreach ($this->items as $i => $item) {
  3405. if (!empty ($this->items[$i]->audio)) {
  3406. $has = true;
  3407. break;
  3408. }
  3409. }
  3410. return $has;
  3411. }
  3412. /**
  3413. * Logs a message into a file
  3414. * @param string Message to log
  3415. * @return boolean True on success, false on error or if msg empty
  3416. */
  3417. public function log($msg)
  3418. {
  3419. if ($this->debug > 0) {
  3420. error_log('New LP - In learnpath::log()', 0);
  3421. }
  3422. // TODO
  3423. $this->error .= $msg;
  3424. return true;
  3425. }
  3426. /**
  3427. * Moves an item up and down at its level
  3428. * @param integer Item to move up and down
  3429. * @param string Direction 'up' or 'down'
  3430. * @return integer New display order, or false on error
  3431. */
  3432. public function move_item($id, $direction)
  3433. {
  3434. $course_id = api_get_course_int_id();
  3435. if ($this->debug > 0) {
  3436. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3437. }
  3438. if (empty($id) || empty($direction)) {
  3439. return false;
  3440. }
  3441. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3442. $sql_sel = "SELECT *
  3443. FROM " . $tbl_lp_item . "
  3444. WHERE c_id = ".$course_id." AND id = " . $id;
  3445. $res_sel = Database::query($sql_sel);
  3446. // Check if elem exists.
  3447. if (Database :: num_rows($res_sel) < 1) {
  3448. return false;
  3449. }
  3450. // Gather data.
  3451. $row = Database :: fetch_array($res_sel);
  3452. $previous = $row['previous_item_id'];
  3453. $next = $row['next_item_id'];
  3454. $display = $row['display_order'];
  3455. $parent = $row['parent_item_id'];
  3456. $lp = $row['lp_id'];
  3457. // Update the item (switch with previous/next one).
  3458. switch ($direction) {
  3459. case 'up':
  3460. if ($this->debug > 2) {
  3461. error_log('Movement up detected', 0);
  3462. }
  3463. if ($display <= 1) { /*do nothing*/
  3464. } else {
  3465. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3466. WHERE c_id = ".$course_id." AND id = $previous";
  3467. if ($this->debug > 2) {
  3468. error_log('Selecting previous: ' . $sql_sel2, 0);
  3469. }
  3470. $res_sel2 = Database::query($sql_sel2);
  3471. if (Database :: num_rows($res_sel2) < 1) {
  3472. $previous_previous = 0;
  3473. }
  3474. // Gather data.
  3475. $row2 = Database :: fetch_array($res_sel2);
  3476. $previous_previous = $row2['previous_item_id'];
  3477. // Update previous_previous item (switch "next" with current).
  3478. if ($previous_previous != 0) {
  3479. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3480. next_item_id = $id
  3481. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3482. if ($this->debug > 2) {
  3483. error_log($sql_upd2, 0);
  3484. }
  3485. Database::query($sql_upd2);
  3486. }
  3487. // Update previous item (switch with current).
  3488. if ($previous != 0) {
  3489. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3490. next_item_id = $next,
  3491. previous_item_id = $id,
  3492. display_order = display_order +1
  3493. WHERE c_id = ".$course_id." AND id = $previous";
  3494. if ($this->debug > 2) {
  3495. error_log($sql_upd2, 0);
  3496. }
  3497. Database::query($sql_upd2);
  3498. }
  3499. // Update current item (switch with previous).
  3500. if ($id != 0) {
  3501. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3502. next_item_id = $previous,
  3503. previous_item_id = $previous_previous,
  3504. display_order = display_order-1
  3505. WHERE c_id = ".$course_id." AND id = $id";
  3506. if ($this->debug > 2) {
  3507. error_log($sql_upd2, 0);
  3508. }
  3509. Database::query($sql_upd2);
  3510. }
  3511. // Update next item (new previous item).
  3512. if ($next != 0) {
  3513. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3514. WHERE c_id = ".$course_id." AND id = $next";
  3515. if ($this->debug > 2) {
  3516. error_log($sql_upd2, 0);
  3517. }
  3518. Database::query($sql_upd2);
  3519. }
  3520. $display = $display -1;
  3521. }
  3522. break;
  3523. case 'down':
  3524. if ($this->debug > 2) {
  3525. error_log('Movement down detected', 0);
  3526. }
  3527. if ($next == 0) { /* Do nothing. */
  3528. } else {
  3529. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3530. if ($this->debug > 2) {
  3531. error_log('Selecting next: ' . $sql_sel2, 0);
  3532. }
  3533. $res_sel2 = Database::query($sql_sel2);
  3534. if (Database :: num_rows($res_sel2) < 1) {
  3535. $next_next = 0;
  3536. }
  3537. // Gather data.
  3538. $row2 = Database :: fetch_array($res_sel2);
  3539. $next_next = $row2['next_item_id'];
  3540. // Update previous item (switch with current).
  3541. if ($previous != 0) {
  3542. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3543. WHERE c_id = ".$course_id." AND id = $previous";
  3544. Database::query($sql_upd2);
  3545. }
  3546. // Update current item (switch with previous).
  3547. if ($id != 0) {
  3548. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3549. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3550. WHERE c_id = ".$course_id." AND id = $id";
  3551. Database::query($sql_upd2);
  3552. }
  3553. // Update next item (new previous item).
  3554. if ($next != 0) {
  3555. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3556. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3557. WHERE c_id = ".$course_id." AND id = $next";
  3558. Database::query($sql_upd2);
  3559. }
  3560. // Update next_next item (switch "previous" with current).
  3561. if ($next_next != 0) {
  3562. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3563. previous_item_id = $id
  3564. WHERE c_id = ".$course_id." AND id = $next_next";
  3565. Database::query($sql_upd2);
  3566. }
  3567. $display = $display +1;
  3568. }
  3569. break;
  3570. default :
  3571. return false;
  3572. }
  3573. return $display;
  3574. }
  3575. /**
  3576. * Move a learnpath up (display_order)
  3577. * @param integer Learnpath ID
  3578. */
  3579. public function move_up($lp_id)
  3580. {
  3581. $course_id = api_get_course_int_id();
  3582. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3583. $sql = "SELECT * FROM $lp_table
  3584. WHERE c_id = ".$course_id."
  3585. ORDER BY display_order";
  3586. $res = Database::query($sql);
  3587. if ($res === false)
  3588. return false;
  3589. $lps = array ();
  3590. $lp_order = array ();
  3591. $num = Database :: num_rows($res);
  3592. // First check the order is correct, globally (might be wrong because
  3593. // of versions < 1.8.4)
  3594. if ($num > 0) {
  3595. $i = 1;
  3596. while ($row = Database :: fetch_array($res)) {
  3597. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3598. $need_fix = true;
  3599. $sql_u = "UPDATE $lp_table SET display_order = $i
  3600. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3601. Database::query($sql_u);
  3602. }
  3603. $row['display_order'] = $i;
  3604. $lps[$row['id']] = $row;
  3605. $lp_order[$i] = $row['id'];
  3606. $i++;
  3607. }
  3608. }
  3609. if ($num > 1) { // If there's only one element, no need to sort.
  3610. $order = $lps[$lp_id]['display_order'];
  3611. if ($order > 1) { // If it's the first element, no need to move up.
  3612. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3613. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3614. Database::query($sql_u1);
  3615. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3616. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3617. Database::query($sql_u2);
  3618. }
  3619. }
  3620. }
  3621. /**
  3622. * Move a learnpath down (display_order)
  3623. * @param integer Learnpath ID
  3624. */
  3625. public function move_down($lp_id)
  3626. {
  3627. $course_id = api_get_course_int_id();
  3628. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3629. $sql = "SELECT * FROM $lp_table
  3630. WHERE c_id = ".$course_id."
  3631. ORDER BY display_order";
  3632. $res = Database::query($sql);
  3633. if ($res === false)
  3634. return false;
  3635. $lps = array ();
  3636. $lp_order = array ();
  3637. $num = Database :: num_rows($res);
  3638. $max = 0;
  3639. // First check the order is correct, globally (might be wrong because
  3640. // of versions < 1.8.4).
  3641. if ($num > 0) {
  3642. $i = 1;
  3643. while ($row = Database :: fetch_array($res)) {
  3644. $max = $i;
  3645. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3646. $need_fix = true;
  3647. $sql_u = "UPDATE $lp_table SET display_order = $i
  3648. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3649. $res_u = Database::query($sql_u);
  3650. }
  3651. $row['display_order'] = $i;
  3652. $lps[$row['id']] = $row;
  3653. $lp_order[$i] = $row['id'];
  3654. $i++;
  3655. }
  3656. }
  3657. if ($num > 1) { // If there's only one element, no need to sort.
  3658. $order = $lps[$lp_id]['display_order'];
  3659. if ($order < $max) { // If it's the first element, no need to move up.
  3660. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3661. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3662. Database::query($sql_u1);
  3663. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3664. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3665. Database::query($sql_u2);
  3666. }
  3667. }
  3668. }
  3669. /**
  3670. * Updates learnpath attributes to point to the next element
  3671. * The last part is similar to set_current_item but processing the other way around
  3672. */
  3673. public function next()
  3674. {
  3675. if ($this->debug > 0) {
  3676. error_log('New LP - In learnpath::next()', 0);
  3677. }
  3678. $this->last = $this->get_current_item_id();
  3679. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3680. $this->autocomplete_parents($this->last);
  3681. $new_index = $this->get_next_index();
  3682. if ($this->debug > 2) {
  3683. error_log('New LP - New index: ' . $new_index, 0);
  3684. }
  3685. $this->index = $new_index;
  3686. if ($this->debug > 2) {
  3687. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3688. }
  3689. $this->current = $this->ordered_items[$new_index];
  3690. if ($this->debug > 2) {
  3691. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3692. }
  3693. }
  3694. /**
  3695. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3696. * class, this might be redefined to allow several behaviours depending on the document type.
  3697. * @param integer Resource ID
  3698. * @return boolean True on success, false otherwise
  3699. */
  3700. public function open($id)
  3701. {
  3702. if ($this->debug > 0) {
  3703. error_log('New LP - In learnpath::open()', 0);
  3704. }
  3705. // TODO:
  3706. // set the current resource attribute to this resource
  3707. // switch on element type (redefine in child class?)
  3708. // set status for this item to "opened"
  3709. // start timer
  3710. // initialise score
  3711. $this->index = 0; //or = the last item seen (see $this->last)
  3712. }
  3713. /**
  3714. * Check that all prerequisites are fulfilled. Returns true and an
  3715. * empty string on succes, returns false
  3716. * and the prerequisite string on error.
  3717. * This function is based on the rules for aicc_script language as
  3718. * described in the SCORM 1.2 CAM documentation page 108.
  3719. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3720. * @return boolean True if prerequisites are matched, false otherwise -
  3721. * Empty string if true returned, prerequisites string otherwise.
  3722. */
  3723. public function prerequisites_match($itemId = null)
  3724. {
  3725. $debug = $this->debug;
  3726. if ($debug > 0) {
  3727. error_log('In learnpath::prerequisites_match()', 0);
  3728. }
  3729. if (empty($itemId)) {
  3730. $itemId = $this->current;
  3731. }
  3732. $currentItem = $this->getItem($itemId);
  3733. if ($currentItem) {
  3734. if ($this->type == 2) {
  3735. // Getting prereq from scorm
  3736. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3737. } else {
  3738. $prereq_string = $currentItem->get_prereq_string();
  3739. }
  3740. if (empty($prereq_string)) {
  3741. if ($debug > 0) {
  3742. error_log('Found prereq_string is empty return true');
  3743. }
  3744. return true;
  3745. }
  3746. // Clean spaces.
  3747. $prereq_string = str_replace(' ', '', $prereq_string);
  3748. if ($debug > 0) {
  3749. error_log('Found prereq_string: ' . $prereq_string, 0);
  3750. }
  3751. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3752. $result = $currentItem->parse_prereq(
  3753. $prereq_string,
  3754. $this->items,
  3755. $this->refs_list,
  3756. $this->get_user_id()
  3757. );
  3758. if ($result === false) {
  3759. $this->set_error_msg($currentItem->prereq_alert);
  3760. }
  3761. } else {
  3762. $result = true;
  3763. if ($debug > 1) {
  3764. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3765. }
  3766. }
  3767. if ($debug > 1) {
  3768. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3769. }
  3770. return $result;
  3771. }
  3772. /**
  3773. * Updates learnpath attributes to point to the previous element
  3774. * The last part is similar to set_current_item but processing the other way around
  3775. */
  3776. public function previous()
  3777. {
  3778. if ($this->debug > 0) {
  3779. error_log('New LP - In learnpath::previous()', 0);
  3780. }
  3781. $this->last = $this->get_current_item_id();
  3782. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3783. $this->autocomplete_parents($this->last);
  3784. $new_index = $this->get_previous_index();
  3785. $this->index = $new_index;
  3786. $this->current = $this->ordered_items[$new_index];
  3787. }
  3788. /**
  3789. * Publishes a learnpath. This basically means show or hide the learnpath
  3790. * to normal users.
  3791. * Can be used as abstract
  3792. * @param integer Learnpath ID
  3793. * @param string New visibility
  3794. */
  3795. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3796. {
  3797. $action = 'visible';
  3798. if ($set_visibility != 1) {
  3799. $action = 'invisible';
  3800. self::toggle_publish($lp_id, 'i');
  3801. }
  3802. return api_item_property_update(
  3803. api_get_course_info(),
  3804. TOOL_LEARNPATH,
  3805. $lp_id,
  3806. $action,
  3807. api_get_user_id()
  3808. );
  3809. }
  3810. /**
  3811. * Publishes a learnpath. This basically means show or hide the learnpath
  3812. * on the course homepage
  3813. * Can be used as abstract
  3814. * @param integer $lp_id Learnpath id
  3815. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3816. * @return bool
  3817. */
  3818. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3819. {
  3820. $course_id = api_get_course_int_id();
  3821. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3822. $lp_id = intval($lp_id);
  3823. $sql = "SELECT * FROM $tbl_lp
  3824. WHERE c_id = ".$course_id." AND id = $lp_id";
  3825. $result = Database::query($sql);
  3826. if (Database::num_rows($result)) {
  3827. $row = Database :: fetch_array($result);
  3828. $name = domesticate($row['name']);
  3829. if ($set_visibility == 'i') {
  3830. $s = $name . " " . get_lang('LearnpathNotPublished');
  3831. $dialogBox = $s;
  3832. $v = 0;
  3833. }
  3834. if ($set_visibility == 'v') {
  3835. $s = $name . " " . get_lang('LearnpathPublished');
  3836. $dialogBox = $s;
  3837. $v = 1;
  3838. }
  3839. } else {
  3840. return false;
  3841. }
  3842. $session_id = api_get_session_id();
  3843. $session_condition = api_get_session_condition($session_id);
  3844. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3845. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3846. $sql = "SELECT * FROM $tbl_tool
  3847. WHERE
  3848. c_id = ".$course_id." AND
  3849. link='$link' and
  3850. image='scormbuilder.gif' and
  3851. link LIKE '$link%'
  3852. $session_condition
  3853. ";
  3854. $result = Database::query($sql);
  3855. $num = Database :: num_rows($result);
  3856. if ($set_visibility == 'i' && $num > 0) {
  3857. $sql = "DELETE FROM $tbl_tool
  3858. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3859. Database::query($sql);
  3860. } elseif ($set_visibility == 'v' && $num == 0) {
  3861. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3862. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3863. Database::query($sql);
  3864. $insertId = Database::insert_id();
  3865. if ($insertId) {
  3866. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3867. Database::query($sql);
  3868. }
  3869. } elseif ($set_visibility == 'v' && $num > 0) {
  3870. $sql = "UPDATE $tbl_tool SET
  3871. c_id = $course_id,
  3872. name = '$name',
  3873. link = '$link',
  3874. image = 'scormbuilder.gif',
  3875. visibility = '$v',
  3876. admin = '0',
  3877. address = 'pastillegris.gif',
  3878. added_tool = 0,
  3879. session_id = $session_id
  3880. WHERE
  3881. c_id = ".$course_id." AND
  3882. (link='$link' and image='scormbuilder.gif' $session_condition)
  3883. ";
  3884. Database::query($sql);
  3885. } else {
  3886. // Parameter and database incompatible, do nothing, exit.
  3887. return false;
  3888. }
  3889. }
  3890. /**
  3891. * Restart the whole learnpath. Return the URL of the first element.
  3892. * Make sure the results are saved with anoter method. This method should probably be
  3893. * redefined in children classes.
  3894. * To use a similar method statically, use the create_new_attempt() method
  3895. * @return string URL to load in the viewer
  3896. */
  3897. public function restart()
  3898. {
  3899. if ($this->debug > 0) {
  3900. error_log('New LP - In learnpath::restart()', 0);
  3901. }
  3902. // TODO
  3903. // Call autosave method to save the current progress.
  3904. //$this->index = 0;
  3905. if (api_is_invitee()) {
  3906. return false;
  3907. }
  3908. $session_id = api_get_session_id();
  3909. $course_id = api_get_course_int_id();
  3910. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3911. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3912. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3913. if ($this->debug > 2) {
  3914. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3915. }
  3916. $res = Database::query($sql);
  3917. $view_id = Database::insert_id();
  3918. if ($view_id) {
  3919. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3920. Database::query($sql);
  3921. $this->lp_view_id = $view_id;
  3922. $this->attempt = $this->attempt + 1;
  3923. } else {
  3924. $this->error = 'Could not insert into item_view table...';
  3925. return false;
  3926. }
  3927. $this->autocomplete_parents($this->current);
  3928. foreach ($this->items as $index => $dummy) {
  3929. $this->items[$index]->restart();
  3930. $this->items[$index]->set_lp_view($this->lp_view_id);
  3931. }
  3932. $this->first();
  3933. return true;
  3934. }
  3935. /**
  3936. * Saves the current item
  3937. * @return boolean
  3938. */
  3939. public function save_current()
  3940. {
  3941. if ($this->debug > 0) {
  3942. error_log('learnpath::save_current()', 0);
  3943. }
  3944. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3945. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3946. if ($this->debug > 2) {
  3947. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3948. }
  3949. if ($this->debug > 2) {
  3950. error_log('' . print_r($this->items, true), 0);
  3951. }
  3952. if (isset($this->items[$this->current]) &&
  3953. is_object($this->items[$this->current])
  3954. ) {
  3955. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3956. $this->autocomplete_parents($this->current);
  3957. $status = $this->items[$this->current]->get_status();
  3958. $this->append_message('new_item_status: ' . $status);
  3959. $this->update_queue[$this->current] = $status;
  3960. return $res;
  3961. }
  3962. return false;
  3963. }
  3964. /**
  3965. * Saves the given item
  3966. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3967. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3968. * @return boolean
  3969. */
  3970. public function save_item($item_id = null, $from_outside = true)
  3971. {
  3972. $debug = $this->debug;
  3973. if ($debug) {
  3974. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3975. }
  3976. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3977. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3978. if (empty($item_id)) {
  3979. $item_id = intval($_REQUEST['id']);
  3980. }
  3981. if (empty($item_id)) {
  3982. $item_id = $this->get_current_item_id();
  3983. }
  3984. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3985. if ($debug) {
  3986. error_log('Object exists');
  3987. }
  3988. // Saving the item.
  3989. $res = $this->items[$item_id]->save(
  3990. $from_outside,
  3991. $this->prerequisites_match($item_id)
  3992. );
  3993. if ($debug) {
  3994. error_log('update_queue before:');
  3995. error_log(print_r($this->update_queue,1));
  3996. }
  3997. $this->autocomplete_parents($item_id);
  3998. $status = $this->items[$item_id]->get_status();
  3999. $this->update_queue[$item_id] = $status;
  4000. if ($debug) {
  4001. error_log('get_status(): ' . $status);
  4002. error_log('update_queue after:');
  4003. error_log(print_r($this->update_queue,1));
  4004. }
  4005. return $res;
  4006. }
  4007. return false;
  4008. }
  4009. /**
  4010. * Saves the last item seen's ID only in case
  4011. */
  4012. public function save_last()
  4013. {
  4014. $course_id = api_get_course_int_id();
  4015. if ($this->debug > 0) {
  4016. error_log('New LP - In learnpath::save_last()', 0);
  4017. }
  4018. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4019. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4020. if (isset($this->current) && !api_is_invitee()) {
  4021. if ($this->debug > 2) {
  4022. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4023. }
  4024. $sql = "UPDATE $table SET
  4025. last_item = " . intval($this->get_current_item_id()). "
  4026. WHERE
  4027. c_id = $course_id AND
  4028. lp_id = " . $this->get_id() . " AND
  4029. user_id = " . $this->get_user_id()." ".$session_condition;
  4030. if ($this->debug > 2) {
  4031. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4032. }
  4033. Database::query($sql);
  4034. }
  4035. if (!api_is_invitee()) {
  4036. // Save progress.
  4037. list($progress, $text) = $this->get_progress_bar_text('%');
  4038. if ($progress >= 0 && $progress <= 100) {
  4039. $progress = (int) $progress;
  4040. $sql = "UPDATE $table SET
  4041. progress = $progress
  4042. WHERE
  4043. c_id = ".$course_id." AND
  4044. lp_id = " . $this->get_id() . " AND
  4045. user_id = " . $this->get_user_id()." ".$session_condition;
  4046. // Ignore errors as some tables might not have the progress field just yet.
  4047. Database::query($sql);
  4048. $this->progress_db = $progress;
  4049. }
  4050. }
  4051. }
  4052. /**
  4053. * Sets the current item ID (checks if valid and authorized first)
  4054. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4055. */
  4056. public function set_current_item($item_id = null)
  4057. {
  4058. if ($this->debug > 0) {
  4059. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4060. }
  4061. if (empty ($item_id)) {
  4062. if ($this->debug > 2) {
  4063. error_log('New LP - No new current item given, ignore...', 0);
  4064. }
  4065. // Do nothing.
  4066. } else {
  4067. if ($this->debug > 2) {
  4068. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4069. }
  4070. if (is_numeric($item_id)) {
  4071. $item_id = intval($item_id);
  4072. // TODO: Check in database here.
  4073. $this->last = $this->current;
  4074. $this->current = $item_id;
  4075. // TODO: Update $this->index as well.
  4076. foreach ($this->ordered_items as $index => $item) {
  4077. if ($item == $this->current) {
  4078. $this->index = $index;
  4079. break;
  4080. }
  4081. }
  4082. if ($this->debug > 2) {
  4083. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4084. }
  4085. } else {
  4086. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4087. }
  4088. }
  4089. }
  4090. /**
  4091. * Sets the encoding
  4092. * @param string New encoding
  4093. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4094. */
  4095. public function set_encoding($enc = 'UTF-8')
  4096. {
  4097. if ($this->debug > 0) {
  4098. error_log('New LP - In learnpath::set_encoding()', 0);
  4099. }
  4100. $course_id = api_get_course_int_id();
  4101. $enc = api_refine_encoding_id($enc);
  4102. if (empty($enc)) {
  4103. $enc = api_get_system_encoding();
  4104. }
  4105. if (api_is_encoding_supported($enc)) {
  4106. $lp = $this->get_id();
  4107. if ($lp != 0) {
  4108. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4109. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4110. $res = Database::query($sql);
  4111. return $res;
  4112. }
  4113. }
  4114. return false;
  4115. }
  4116. /**
  4117. * Sets the JS lib setting in the database directly.
  4118. * This is the JavaScript library file this lp needs to load on startup
  4119. * @param string Proximity setting
  4120. * @return boolean True on update success. False otherwise.
  4121. */
  4122. public function set_jslib($lib = '')
  4123. {
  4124. if ($this->debug > 0) {
  4125. error_log('New LP - In learnpath::set_jslib()', 0);
  4126. }
  4127. $lp = $this->get_id();
  4128. $course_id = api_get_course_int_id();
  4129. if ($lp != 0) {
  4130. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4131. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4132. $res = Database::query($sql);
  4133. return $res;
  4134. } else {
  4135. return false;
  4136. }
  4137. }
  4138. /**
  4139. * Sets the name of the LP maker (publisher) (and save)
  4140. * @param string Optional string giving the new content_maker of this learnpath
  4141. * @return boolean True
  4142. */
  4143. public function set_maker($name = '')
  4144. {
  4145. if ($this->debug > 0) {
  4146. error_log('New LP - In learnpath::set_maker()', 0);
  4147. }
  4148. if (empty ($name))
  4149. return false;
  4150. $this->maker = $name;
  4151. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4152. $course_id = api_get_course_int_id();
  4153. $lp_id = $this->get_id();
  4154. $sql = "UPDATE $lp_table SET
  4155. content_maker = '" . Database::escape_string($this->maker) . "'
  4156. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4157. if ($this->debug > 2) {
  4158. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4159. }
  4160. Database::query($sql);
  4161. return true;
  4162. }
  4163. /**
  4164. * Sets the name of the current learnpath (and save)
  4165. * @param string $name Optional string giving the new name of this learnpath
  4166. * @return boolean True/False
  4167. */
  4168. public function set_name($name = null)
  4169. {
  4170. if ($this->debug > 0) {
  4171. error_log('New LP - In learnpath::set_name()', 0);
  4172. }
  4173. if (empty($name)) {
  4174. return false;
  4175. }
  4176. $this->name = $name;
  4177. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4178. $lp_id = $this->get_id();
  4179. $course_id = api_get_course_int_id();
  4180. $sql = "UPDATE $lp_table SET
  4181. name = '" . Database::escape_string($this->name). "'
  4182. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4183. if ($this->debug > 2) {
  4184. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4185. }
  4186. $result = Database::query($sql);
  4187. // If the lp is visible on the homepage, change his name there.
  4188. if (Database::affected_rows($result)) {
  4189. $session_id = api_get_session_id();
  4190. $session_condition = api_get_session_condition($session_id);
  4191. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4192. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4193. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4194. WHERE
  4195. c_id = $course_id AND
  4196. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4197. Database::query($sql);
  4198. return true;
  4199. } else {
  4200. return false;
  4201. }
  4202. }
  4203. /**
  4204. * Set index specified prefix terms for all items in this path
  4205. * @param string Comma-separated list of terms
  4206. * @param char Xapian term prefix
  4207. * @return boolean False on error, true otherwise
  4208. */
  4209. public function set_terms_by_prefix($terms_string, $prefix)
  4210. {
  4211. $course_id = api_get_course_int_id();
  4212. if (api_get_setting('search_enabled') !== 'true')
  4213. return false;
  4214. if (!extension_loaded('xapian')) {
  4215. return false;
  4216. }
  4217. $terms_string = trim($terms_string);
  4218. $terms = explode(',', $terms_string);
  4219. array_walk($terms, 'trim_value');
  4220. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4221. // Don't do anything if no change, verify only at DB, not the search engine.
  4222. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4223. return false;
  4224. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4225. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4226. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4227. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4228. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4229. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4230. $lp_id = intval($_POST['lp_id']);
  4231. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4232. $result = Database::query($sql);
  4233. $di = new ChamiloIndexer();
  4234. while ($lp_item = Database :: fetch_array($result)) {
  4235. // Get search_did.
  4236. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4237. $sql = 'SELECT * FROM %s
  4238. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4239. LIMIT 1';
  4240. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4241. //echo $sql; echo '<br>';
  4242. $res = Database::query($sql);
  4243. if (Database::num_rows($res) > 0) {
  4244. $se_ref = Database :: fetch_array($res);
  4245. // Compare terms.
  4246. $doc = $di->get_document($se_ref['search_did']);
  4247. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4248. $xterms = array();
  4249. foreach ($xapian_terms as $xapian_term) {
  4250. $xterms[] = substr($xapian_term['name'], 1);
  4251. }
  4252. $dterms = $terms;
  4253. $missing_terms = array_diff($dterms, $xterms);
  4254. $deprecated_terms = array_diff($xterms, $dterms);
  4255. // Save it to search engine.
  4256. foreach ($missing_terms as $term) {
  4257. $doc->add_term($prefix . $term, 1);
  4258. }
  4259. foreach ($deprecated_terms as $term) {
  4260. $doc->remove_term($prefix . $term);
  4261. }
  4262. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4263. $di->getDb()->flush();
  4264. } else {
  4265. //@todo What we should do here?
  4266. }
  4267. }
  4268. return true;
  4269. }
  4270. /**
  4271. * Sets the theme of the LP (local/remote) (and save)
  4272. * @param string Optional string giving the new theme of this learnpath
  4273. * @return bool Returns true if theme name is not empty
  4274. */
  4275. public function set_theme($name = '')
  4276. {
  4277. $course_id = api_get_course_int_id();
  4278. if ($this->debug > 0) {
  4279. error_log('New LP - In learnpath::set_theme()', 0);
  4280. }
  4281. $this->theme = $name;
  4282. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4283. $lp_id = $this->get_id();
  4284. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4285. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4286. if ($this->debug > 2) {
  4287. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4288. }
  4289. Database::query($sql);
  4290. return true;
  4291. }
  4292. /**
  4293. * Sets the image of an LP (and save)
  4294. * @param string Optional string giving the new image of this learnpath
  4295. * @return bool Returns true if theme name is not empty
  4296. */
  4297. public function set_preview_image($name = '')
  4298. {
  4299. $course_id = api_get_course_int_id();
  4300. if ($this->debug > 0) {
  4301. error_log('New LP - In learnpath::set_preview_image()', 0);
  4302. }
  4303. $this->preview_image = $name;
  4304. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4305. $lp_id = $this->get_id();
  4306. $sql = "UPDATE $lp_table SET
  4307. preview_image = '" . Database::escape_string($this->preview_image). "'
  4308. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4309. if ($this->debug > 2) {
  4310. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4311. }
  4312. Database::query($sql);
  4313. return true;
  4314. }
  4315. /**
  4316. * Sets the author of a LP (and save)
  4317. * @param string Optional string giving the new author of this learnpath
  4318. * @return bool Returns true if author's name is not empty
  4319. */
  4320. public function set_author($name = '')
  4321. {
  4322. $course_id = api_get_course_int_id();
  4323. if ($this->debug > 0) {
  4324. error_log('New LP - In learnpath::set_author()', 0);
  4325. }
  4326. $this->author = $name;
  4327. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4328. $lp_id = $this->get_id();
  4329. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  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 author : ' . $this->author, 0);
  4333. }
  4334. Database::query($sql);
  4335. return true;
  4336. }
  4337. /**
  4338. * Sets the hide_toc_frame parameter of a LP (and save)
  4339. * @param int 1 if frame is hidden 0 then else
  4340. * @return bool Returns true if author's name is not empty
  4341. */
  4342. public function set_hide_toc_frame($hide)
  4343. {
  4344. $course_id = api_get_course_int_id();
  4345. if ($this->debug > 0) {
  4346. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4347. }
  4348. if (intval($hide) == $hide){
  4349. $this->hide_toc_frame = $hide;
  4350. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4351. $lp_id = $this->get_id();
  4352. $sql = "UPDATE $lp_table SET
  4353. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4354. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4355. if ($this->debug > 2) {
  4356. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4357. }
  4358. Database::query($sql);
  4359. return true;
  4360. } else {
  4361. return false;
  4362. }
  4363. }
  4364. /**
  4365. * Sets the prerequisite of a LP (and save)
  4366. * @param int integer giving the new prerequisite of this learnpath
  4367. * @return bool returns true if prerequisite is not empty
  4368. */
  4369. public function set_prerequisite($prerequisite)
  4370. {
  4371. $course_id = api_get_course_int_id();
  4372. if ($this->debug > 0) {
  4373. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4374. }
  4375. $this->prerequisite = intval($prerequisite);
  4376. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4377. $lp_id = $this->get_id();
  4378. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4379. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4380. if ($this->debug > 2) {
  4381. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4382. }
  4383. Database::query($sql);
  4384. return true;
  4385. }
  4386. /**
  4387. * Sets the location/proximity of the LP (local/remote) (and save)
  4388. * @param string Optional string giving the new location of this learnpath
  4389. * @return boolean True on success / False on error
  4390. */
  4391. public function set_proximity($name = '')
  4392. {
  4393. $course_id = api_get_course_int_id();
  4394. if ($this->debug > 0) {
  4395. error_log('New LP - In learnpath::set_proximity()', 0);
  4396. }
  4397. if (empty ($name))
  4398. return false;
  4399. $this->proximity = $name;
  4400. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4401. $lp_id = $this->get_id();
  4402. $sql = "UPDATE $lp_table SET
  4403. content_local = '" . Database::escape_string($name) . "'
  4404. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4405. if ($this->debug > 2) {
  4406. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4407. }
  4408. Database::query($sql);
  4409. return true;
  4410. }
  4411. /**
  4412. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4413. * @param integer DB ID of the item
  4414. */
  4415. public function set_previous_item($id)
  4416. {
  4417. if ($this->debug > 0) {
  4418. error_log('New LP - In learnpath::set_previous_item()', 0);
  4419. }
  4420. $this->last = $id;
  4421. }
  4422. /**
  4423. * Sets use_max_score
  4424. * @param string $use_max_score Optional string giving the new location of this learnpath
  4425. * @return boolean True on success / False on error
  4426. */
  4427. public function set_use_max_score($use_max_score = 1)
  4428. {
  4429. $course_id = api_get_course_int_id();
  4430. if ($this->debug > 0) {
  4431. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4432. }
  4433. $use_max_score = intval($use_max_score);
  4434. $this->use_max_score = $use_max_score;
  4435. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4436. $lp_id = $this->get_id();
  4437. $sql = "UPDATE $lp_table SET
  4438. use_max_score = '" . $this->use_max_score . "'
  4439. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4440. if ($this->debug > 2) {
  4441. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4442. }
  4443. Database::query($sql);
  4444. return true;
  4445. }
  4446. /**
  4447. * Sets and saves the expired_on date
  4448. * @param string $expired_on Optional string giving the new author of this learnpath
  4449. * @return bool Returns true if author's name is not empty
  4450. */
  4451. public function set_expired_on($expired_on)
  4452. {
  4453. $course_id = api_get_course_int_id();
  4454. if ($this->debug > 0) {
  4455. error_log('New LP - In learnpath::set_expired_on()', 0);
  4456. }
  4457. if (!empty($expired_on)) {
  4458. $this->expired_on = api_get_utc_datetime($expired_on);
  4459. } else {
  4460. $this->expired_on = '';
  4461. }
  4462. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4463. $lp_id = $this->get_id();
  4464. $sql = "UPDATE $lp_table SET
  4465. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4466. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4467. if ($this->debug > 2) {
  4468. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4469. }
  4470. Database::query($sql);
  4471. return true;
  4472. }
  4473. /**
  4474. * Sets and saves the publicated_on date
  4475. * @param string $publicated_on Optional string giving the new author of this learnpath
  4476. * @return bool Returns true if author's name is not empty
  4477. */
  4478. public function set_publicated_on($publicated_on)
  4479. {
  4480. $course_id = api_get_course_int_id();
  4481. if ($this->debug > 0) {
  4482. error_log('New LP - In learnpath::set_expired_on()', 0);
  4483. }
  4484. if (!empty($publicated_on)) {
  4485. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4486. } else {
  4487. $this->publicated_on = '';
  4488. }
  4489. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4490. $lp_id = $this->get_id();
  4491. $sql = "UPDATE $lp_table SET
  4492. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4493. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4494. if ($this->debug > 2) {
  4495. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4496. }
  4497. Database::query($sql);
  4498. return true;
  4499. }
  4500. /**
  4501. * Sets and saves the expired_on date
  4502. * @return bool Returns true if author's name is not empty
  4503. */
  4504. public function set_modified_on()
  4505. {
  4506. $course_id = api_get_course_int_id();
  4507. if ($this->debug > 0) {
  4508. error_log('New LP - In learnpath::set_expired_on()', 0);
  4509. }
  4510. $this->modified_on = api_get_utc_datetime();
  4511. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4512. $lp_id = $this->get_id();
  4513. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4514. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4515. if ($this->debug > 2) {
  4516. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4517. }
  4518. Database::query($sql);
  4519. return true;
  4520. }
  4521. /**
  4522. * Sets the object's error message
  4523. * @param string Error message. If empty, reinits the error string
  4524. * @return void
  4525. */
  4526. public function set_error_msg($error = '')
  4527. {
  4528. if ($this->debug > 0) {
  4529. error_log('New LP - In learnpath::set_error_msg()', 0);
  4530. }
  4531. if (empty ($error)) {
  4532. $this->error = '';
  4533. } else {
  4534. $this->error .= $error;
  4535. }
  4536. }
  4537. /**
  4538. * Launches the current item if not 'sco'
  4539. * (starts timer and make sure there is a record ready in the DB)
  4540. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4541. * @return boolean
  4542. */
  4543. public function start_current_item($allow_new_attempt = false)
  4544. {
  4545. if ($this->debug > 0) {
  4546. error_log('New LP - In learnpath::start_current_item()', 0);
  4547. }
  4548. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4549. $type = $this->get_type();
  4550. $item_type = $this->items[$this->current]->get_type();
  4551. if (($type == 2 && $item_type != 'sco') ||
  4552. ($type == 3 && $item_type != 'au') ||
  4553. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4554. ) {
  4555. $this->items[$this->current]->open($allow_new_attempt);
  4556. $this->autocomplete_parents($this->current);
  4557. $prereq_check = $this->prerequisites_match($this->current);
  4558. $this->items[$this->current]->save(false, $prereq_check);
  4559. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4560. } else {
  4561. // If sco, then it is supposed to have been updated by some other call.
  4562. }
  4563. if ($item_type == 'sco') {
  4564. $this->items[$this->current]->restart();
  4565. }
  4566. }
  4567. if ($this->debug > 0) {
  4568. error_log('New LP - End of learnpath::start_current_item()', 0);
  4569. }
  4570. return true;
  4571. }
  4572. /**
  4573. * Stops the processing and counters for the old item (as held in $this->last)
  4574. * @return boolean True/False
  4575. */
  4576. public function stop_previous_item()
  4577. {
  4578. if ($this->debug > 0) {
  4579. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4580. }
  4581. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4582. if ($this->debug > 2) {
  4583. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4584. }
  4585. switch ($this->get_type()) {
  4586. case '3' :
  4587. if ($this->items[$this->last]->get_type() != 'au') {
  4588. if ($this->debug > 2) {
  4589. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4590. }
  4591. $this->items[$this->last]->close();
  4592. //$this->autocomplete_parents($this->last);
  4593. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4594. } else {
  4595. if ($this->debug > 2) {
  4596. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4597. }
  4598. }
  4599. case '2' :
  4600. if ($this->items[$this->last]->get_type() != 'sco') {
  4601. if ($this->debug > 2) {
  4602. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4603. }
  4604. $this->items[$this->last]->close();
  4605. //$this->autocomplete_parents($this->last);
  4606. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4607. } else {
  4608. if ($this->debug > 2) {
  4609. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4610. }
  4611. }
  4612. break;
  4613. case '1' :
  4614. default :
  4615. if ($this->debug > 2) {
  4616. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4617. }
  4618. $this->items[$this->last]->close();
  4619. break;
  4620. }
  4621. } else {
  4622. if ($this->debug > 2) {
  4623. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4624. }
  4625. return false;
  4626. }
  4627. return true;
  4628. }
  4629. /**
  4630. * Updates the default view mode from fullscreen to embedded and inversely
  4631. * @return string The current default view mode ('fullscreen' or 'embedded')
  4632. */
  4633. public function update_default_view_mode()
  4634. {
  4635. $course_id = api_get_course_int_id();
  4636. if ($this->debug > 0) {
  4637. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4638. }
  4639. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4640. $sql = "SELECT * FROM $lp_table
  4641. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4642. $res = Database::query($sql);
  4643. if (Database :: num_rows($res) > 0) {
  4644. $row = Database :: fetch_array($res);
  4645. $default_view_mode = $row['default_view_mod'];
  4646. $view_mode = $default_view_mode;
  4647. switch ($default_view_mode) {
  4648. case 'fullscreen':
  4649. $view_mode = 'embedded';
  4650. break;
  4651. case 'embedded':
  4652. $view_mode = 'embedframe';
  4653. break;
  4654. case 'embedframe':
  4655. $view_mode = 'impress';
  4656. break;
  4657. case 'impress':
  4658. $view_mode = 'fullscreen';
  4659. break;
  4660. }
  4661. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4662. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4663. Database::query($sql);
  4664. $this->mode = $view_mode;
  4665. return $view_mode;
  4666. } else {
  4667. if ($this->debug > 2) {
  4668. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4669. }
  4670. }
  4671. return -1;
  4672. }
  4673. /**
  4674. * Updates the default behaviour about auto-commiting SCORM updates
  4675. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4676. */
  4677. public function update_default_scorm_commit()
  4678. {
  4679. $course_id = api_get_course_int_id();
  4680. if ($this->debug > 0) {
  4681. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4682. }
  4683. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4684. $sql = "SELECT * FROM $lp_table
  4685. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4686. $res = Database::query($sql);
  4687. if (Database :: num_rows($res) > 0) {
  4688. $row = Database :: fetch_array($res);
  4689. $force = $row['force_commit'];
  4690. if ($force == 1) {
  4691. $force = 0;
  4692. $force_return = false;
  4693. } elseif ($force == 0) {
  4694. $force = 1;
  4695. $force_return = true;
  4696. }
  4697. $sql = "UPDATE $lp_table SET force_commit = $force
  4698. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4699. Database::query($sql);
  4700. $this->force_commit = $force_return;
  4701. return $force_return;
  4702. } else {
  4703. if ($this->debug > 2) {
  4704. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4705. }
  4706. }
  4707. return -1;
  4708. }
  4709. /**
  4710. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4711. * @return bool True on success, false on failure
  4712. */
  4713. public function update_display_order()
  4714. {
  4715. $course_id = api_get_course_int_id();
  4716. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4717. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4718. $res = Database::query($sql);
  4719. if ($res === false)
  4720. return false;
  4721. $num = Database :: num_rows($res);
  4722. // First check the order is correct, globally (might be wrong because
  4723. // of versions < 1.8.4).
  4724. if ($num > 0) {
  4725. $i = 1;
  4726. while ($row = Database :: fetch_array($res)) {
  4727. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4728. $need_fix = true;
  4729. $sql = "UPDATE $lp_table SET display_order = $i
  4730. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4731. Database::query($sql);
  4732. }
  4733. $i++;
  4734. }
  4735. }
  4736. return true;
  4737. }
  4738. /**
  4739. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4740. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4741. */
  4742. public function update_reinit()
  4743. {
  4744. $course_id = api_get_course_int_id();
  4745. if ($this->debug > 0) {
  4746. error_log('New LP - In learnpath::update_reinit()', 0);
  4747. }
  4748. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4749. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4750. $res = Database::query($sql);
  4751. if (Database :: num_rows($res) > 0) {
  4752. $row = Database :: fetch_array($res);
  4753. $force = $row['prevent_reinit'];
  4754. if ($force == 1) {
  4755. $force = 0;
  4756. } elseif ($force == 0) {
  4757. $force = 1;
  4758. }
  4759. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4760. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4761. Database::query($sql);
  4762. $this->prevent_reinit = $force;
  4763. return $force;
  4764. } else {
  4765. if ($this->debug > 2) {
  4766. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4767. }
  4768. }
  4769. return -1;
  4770. }
  4771. /**
  4772. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4773. *
  4774. * @return string 'single', 'multi' or 'seriousgame'
  4775. * @author ndiechburg <noel@cblue.be>
  4776. **/
  4777. public function get_attempt_mode()
  4778. {
  4779. //Set default value for seriousgame_mode
  4780. if (!isset($this->seriousgame_mode)) {
  4781. $this->seriousgame_mode=0;
  4782. }
  4783. // Set default value for prevent_reinit
  4784. if (!isset($this->prevent_reinit)) {
  4785. $this->prevent_reinit =1;
  4786. }
  4787. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4788. return 'seriousgame';
  4789. }
  4790. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4791. return 'single';
  4792. }
  4793. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4794. return 'multiple';
  4795. }
  4796. return 'single';
  4797. }
  4798. /**
  4799. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4800. *
  4801. * @param string 'seriousgame', 'single' or 'multiple'
  4802. * @return boolean
  4803. * @author ndiechburg <noel@cblue.be>
  4804. **/
  4805. public function set_attempt_mode($mode)
  4806. {
  4807. $course_id = api_get_course_int_id();
  4808. switch ($mode) {
  4809. case 'seriousgame' :
  4810. $sg_mode = 1;
  4811. $prevent_reinit = 1;
  4812. break;
  4813. case 'single' :
  4814. $sg_mode = 0;
  4815. $prevent_reinit = 1;
  4816. break;
  4817. case 'multiple' :
  4818. $sg_mode = 0;
  4819. $prevent_reinit = 0;
  4820. break;
  4821. default :
  4822. $sg_mode = 0;
  4823. $prevent_reinit = 0;
  4824. break;
  4825. }
  4826. $this->prevent_reinit = $prevent_reinit;
  4827. $this->seriousgame_mode = $sg_mode;
  4828. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4829. $sql = "UPDATE $lp_table SET
  4830. prevent_reinit = $prevent_reinit ,
  4831. seriousgame_mode = $sg_mode
  4832. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4833. $res = Database::query($sql);
  4834. if ($res) {
  4835. return true;
  4836. } else {
  4837. return false;
  4838. }
  4839. }
  4840. /**
  4841. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4842. *
  4843. * @return boolean
  4844. * @author ndiechburg <noel@cblue.be>
  4845. **/
  4846. public function switch_attempt_mode()
  4847. {
  4848. if ($this->debug > 0) {
  4849. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4850. }
  4851. $mode = $this->get_attempt_mode();
  4852. switch ($mode) {
  4853. case 'single' :
  4854. $next_mode = 'multiple';
  4855. break;
  4856. case 'multiple' :
  4857. $next_mode = 'seriousgame';
  4858. break;
  4859. case 'seriousgame' :
  4860. $next_mode = 'single';
  4861. break;
  4862. default :
  4863. $next_mode = 'single';
  4864. break;
  4865. }
  4866. $this->set_attempt_mode($next_mode);
  4867. }
  4868. /**
  4869. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4870. * but possibility to do again a completed item.
  4871. *
  4872. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4873. * @author ndiechburg <noel@cblue.be>
  4874. **/
  4875. public function set_seriousgame_mode()
  4876. {
  4877. $course_id = api_get_course_int_id();
  4878. if ($this->debug > 0) {
  4879. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4880. }
  4881. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4882. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4883. $res = Database::query($sql);
  4884. if (Database :: num_rows($res) > 0) {
  4885. $row = Database :: fetch_array($res);
  4886. $force = $row['seriousgame_mode'];
  4887. if ($force == 1) {
  4888. $force = 0;
  4889. } elseif ($force == 0) {
  4890. $force = 1;
  4891. }
  4892. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4893. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4894. Database::query($sql);
  4895. $this->seriousgame_mode = $force;
  4896. return $force;
  4897. } else {
  4898. if ($this->debug > 2) {
  4899. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4900. }
  4901. }
  4902. return -1;
  4903. }
  4904. /**
  4905. * Updates the "scorm_debug" value that shows or hide the debug window
  4906. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4907. */
  4908. public function update_scorm_debug()
  4909. {
  4910. $course_id = api_get_course_int_id();
  4911. if ($this->debug > 0) {
  4912. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4913. }
  4914. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4915. $sql = "SELECT * FROM $lp_table
  4916. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4917. $res = Database::query($sql);
  4918. if (Database :: num_rows($res) > 0) {
  4919. $row = Database :: fetch_array($res);
  4920. $force = $row['debug'];
  4921. if ($force == 1) {
  4922. $force = 0;
  4923. } elseif ($force == 0) {
  4924. $force = 1;
  4925. }
  4926. $sql = "UPDATE $lp_table SET debug = $force
  4927. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4928. $res = Database::query($sql);
  4929. $this->scorm_debug = $force;
  4930. return $force;
  4931. } else {
  4932. if ($this->debug > 2) {
  4933. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4934. }
  4935. }
  4936. return -1;
  4937. }
  4938. /**
  4939. * Function that makes a call to the function sort_tree_array and create_tree_array
  4940. * @author Kevin Van Den Haute
  4941. * @param array
  4942. */
  4943. public function tree_array($array)
  4944. {
  4945. if ($this->debug > 1) {
  4946. error_log('New LP - In learnpath::tree_array()', 0);
  4947. }
  4948. $array = $this->sort_tree_array($array);
  4949. $this->create_tree_array($array);
  4950. }
  4951. /**
  4952. * Creates an array with the elements of the learning path tree in it
  4953. *
  4954. * @author Kevin Van Den Haute
  4955. * @param array $array
  4956. * @param int $parent
  4957. * @param int $depth
  4958. * @param array $tmp
  4959. */
  4960. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4961. {
  4962. if ($this->debug > 1) {
  4963. error_log('New LP - In learnpath::create_tree_array())', 0);
  4964. }
  4965. if (is_array($array)) {
  4966. for ($i = 0; $i < count($array); $i++) {
  4967. if ($array[$i]['parent_item_id'] == $parent) {
  4968. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4969. $tmp[] = $array[$i]['parent_item_id'];
  4970. $depth++;
  4971. }
  4972. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4973. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4974. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4975. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4976. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4977. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4978. $this->arrMenu[] = array(
  4979. 'id' => $array[$i]['id'],
  4980. 'ref' => $ref,
  4981. 'item_type' => $array[$i]['item_type'],
  4982. 'title' => $array[$i]['title'],
  4983. 'path' => $path,
  4984. 'description' => $array[$i]['description'],
  4985. 'parent_item_id' => $array[$i]['parent_item_id'],
  4986. 'previous_item_id' => $array[$i]['previous_item_id'],
  4987. 'next_item_id' => $array[$i]['next_item_id'],
  4988. 'min_score' => $array[$i]['min_score'],
  4989. 'max_score' => $array[$i]['max_score'],
  4990. 'mastery_score' => $array[$i]['mastery_score'],
  4991. 'display_order' => $array[$i]['display_order'],
  4992. 'prerequisite' => $preq,
  4993. 'depth' => $depth,
  4994. 'audio' => $audio,
  4995. 'prerequisite_min_score' => $prerequisiteMinScore,
  4996. 'prerequisite_max_score' => $prerequisiteMaxScore
  4997. );
  4998. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4999. }
  5000. }
  5001. }
  5002. }
  5003. /**
  5004. * Sorts a multi dimensional array by parent id and display order
  5005. * @author Kevin Van Den Haute
  5006. *
  5007. * @param array $array (array with al the learning path items in it)
  5008. *
  5009. * @return array
  5010. */
  5011. public function sort_tree_array($array) {
  5012. foreach ($array as $key => $row) {
  5013. $parent[$key] = $row['parent_item_id'];
  5014. $position[$key] = $row['display_order'];
  5015. }
  5016. if (count($array) > 0)
  5017. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5018. return $array;
  5019. }
  5020. /**
  5021. * Function that creates a html list of learning path items so that we can add audio files to them
  5022. * @author Kevin Van Den Haute
  5023. * @param int $lp_id
  5024. * @return string
  5025. */
  5026. public function overview()
  5027. {
  5028. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5029. $_course = api_get_course_info();
  5030. if ($this->debug > 0) {
  5031. error_log('New LP - In learnpath::overview()', 0);
  5032. }
  5033. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5034. $return = '';
  5035. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5036. // we need to start a form when we want to update all the mp3 files
  5037. if ($update_audio == 'true') {
  5038. $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">';
  5039. }
  5040. $return .= '<div id="message"></div>';
  5041. if (count($this->items) == 0) {
  5042. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5043. } else {
  5044. $return_audio = '<table class="data_table">';
  5045. $return_audio .= '<tr>';
  5046. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5047. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5048. $return_audio .= '</tr>';
  5049. if ($update_audio != 'true') {
  5050. $return .= '<div class="col-md-12">';
  5051. $return .= self::return_new_tree($update_audio);
  5052. $return .='</div>';
  5053. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5054. } else {
  5055. $return_audio .= self::return_new_tree($update_audio);
  5056. $return .= $return_audio.'</table>';
  5057. }
  5058. // We need to close the form when we are updating the mp3 files.
  5059. if ($update_audio == 'true') {
  5060. $return .= '<div class="footer-audio">';
  5061. $return .= Display::button('save_audio','<i class="fa fa-file-audio-o"></i> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5062. $return .= '</div>';
  5063. //$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?
  5064. }
  5065. }
  5066. // We need to close the form when we are updating the mp3 files.
  5067. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  5068. $return .= '</form>';
  5069. }
  5070. return $return;
  5071. }
  5072. /**
  5073. * @param string string $update_audio
  5074. * @param bool $drop_element_here
  5075. * @return string
  5076. */
  5077. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5078. {
  5079. $return = '';
  5080. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5081. $course_id = api_get_course_int_id();
  5082. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5083. $sql = "SELECT * FROM $tbl_lp_item
  5084. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5085. $result = Database::query($sql);
  5086. $arrLP = array();
  5087. while ($row = Database :: fetch_array($result)) {
  5088. $arrLP[] = array(
  5089. 'id' => $row['id'],
  5090. 'item_type' => $row['item_type'],
  5091. 'title' => Security :: remove_XSS($row['title']),
  5092. 'path' => $row['path'],
  5093. 'description' => Security::remove_XSS($row['description']),
  5094. 'parent_item_id' => $row['parent_item_id'],
  5095. 'previous_item_id' => $row['previous_item_id'],
  5096. 'next_item_id' => $row['next_item_id'],
  5097. 'max_score' => $row['max_score'],
  5098. 'min_score' => $row['min_score'],
  5099. 'mastery_score' => $row['mastery_score'],
  5100. 'prerequisite' => $row['prerequisite'],
  5101. 'display_order' => $row['display_order'],
  5102. 'audio' => $row['audio'],
  5103. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5104. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5105. );
  5106. }
  5107. $this->tree_array($arrLP);
  5108. $arrLP = $this->arrMenu;
  5109. unset ($this->arrMenu);
  5110. $default_data = null;
  5111. $default_content = null;
  5112. $elements = array();
  5113. $return_audio = null;
  5114. for ($i = 0; $i < count($arrLP); $i++) {
  5115. $title = $arrLP[$i]['title'];
  5116. $title_cut = cut($arrLP[$i]['title'], 25);
  5117. //Link for the documents
  5118. if ($arrLP[$i]['item_type'] == 'document') {
  5119. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5120. $title_cut = Display::url(
  5121. $title_cut,
  5122. $url,
  5123. array(
  5124. 'class' => 'ajax',
  5125. 'data-title' => $title_cut
  5126. )
  5127. );
  5128. }
  5129. if (($i % 2) == 0) {
  5130. $oddClass = 'row_odd';
  5131. } else {
  5132. $oddClass = 'row_even';
  5133. }
  5134. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5135. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5136. $icon = '';
  5137. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5138. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5139. } else {
  5140. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5141. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5142. } else {
  5143. $icon = '<img src="../img/folder_document.gif" />';
  5144. }
  5145. }
  5146. // The audio column.
  5147. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5148. $audio = '';
  5149. if (!$update_audio || $update_audio <> 'true') {
  5150. if (!empty($arrLP[$i]['audio'])) {
  5151. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5152. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5153. $audio .= '<script type="text/javascript">
  5154. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5155. s1.addParam("allowscriptaccess","always");
  5156. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5157. s1.write("container' . $i . '");
  5158. </script>';*/
  5159. } else {
  5160. $audio .= '';
  5161. }
  5162. } else {
  5163. $types = self::getChapterTypes();
  5164. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5165. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5166. if (!empty ($arrLP[$i]['audio'])) {
  5167. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5168. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5169. }
  5170. }
  5171. }
  5172. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5173. $return_audio .= '</td>';
  5174. $move_icon = '';
  5175. $move_item_icon = '';
  5176. $edit_icon = '';
  5177. $delete_icon = '';
  5178. $audio_icon = '';
  5179. $prerequisities_icon = '';
  5180. if ($is_allowed_to_edit) {
  5181. if (!$update_audio || $update_audio <> 'true') {
  5182. $move_icon .= '<a class="moved" href="#">';
  5183. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5184. $move_icon .= '</a>';
  5185. }
  5186. // No edit for this item types
  5187. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5188. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5189. $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'] . '">';
  5190. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5191. $edit_icon .= '</a>';
  5192. } else {
  5193. $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'] . '">';
  5194. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5195. $edit_icon .= '</a>';
  5196. }
  5197. }
  5198. $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) . '\');">';
  5199. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5200. $delete_icon .= '</a>';
  5201. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5202. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5203. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5204. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5205. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5206. }
  5207. }
  5208. if ($update_audio != 'true') {
  5209. $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'));
  5210. } else {
  5211. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5212. }
  5213. $parent_id = $arrLP[$i]['parent_item_id'];
  5214. $default_data[$arrLP[$i]['id']] = $row;
  5215. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5216. if (empty($parent_id)) {
  5217. $elements[$arrLP[$i]['id']]['data'] = $row;
  5218. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5219. } else {
  5220. $parent_arrays = array();
  5221. if ($arrLP[$i]['depth'] > 1) {
  5222. //Getting list of parents
  5223. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5224. foreach($arrLP as $item) {
  5225. if ($item['id'] == $parent_id) {
  5226. if ($item['parent_item_id'] == 0) {
  5227. $parent_id = $item['id'];
  5228. break;
  5229. } else {
  5230. $parent_id = $item['parent_item_id'];
  5231. if (empty($parent_arrays)) {
  5232. $parent_arrays[] = intval($item['id']);
  5233. }
  5234. $parent_arrays[] = $parent_id;
  5235. break;
  5236. }
  5237. }
  5238. }
  5239. }
  5240. }
  5241. if (!empty($parent_arrays)) {
  5242. $parent_arrays = array_reverse($parent_arrays);
  5243. $val = '$elements';
  5244. $x = 0;
  5245. foreach($parent_arrays as $item) {
  5246. if ($x != count($parent_arrays) -1) {
  5247. $val .= '["'.$item.'"]["children"]';
  5248. } else {
  5249. $val .= '["'.$item.'"]["children"]';
  5250. }
  5251. $x++;
  5252. }
  5253. $val .= "";
  5254. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5255. eval($code_str);
  5256. } else {
  5257. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5258. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5259. }
  5260. }
  5261. }
  5262. $list = '<ul id="lp_item_list">';
  5263. $tree = self::print_recursive($elements, $default_data, $default_content);
  5264. if (!empty($tree)) {
  5265. $list .= $tree;
  5266. } else {
  5267. if ($drop_element_here) {
  5268. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5269. }
  5270. }
  5271. $list .= '</ul>';
  5272. $return .= Display::panel($list, $this->name);
  5273. if ($update_audio == 'true') {
  5274. $return = $return_audio;
  5275. }
  5276. return $return;
  5277. }
  5278. /**
  5279. * @param array $elements
  5280. * @param array $default_data
  5281. * @param array $default_content
  5282. * @return string
  5283. */
  5284. public function print_recursive($elements, $default_data, $default_content)
  5285. {
  5286. $return = '';
  5287. foreach ($elements as $key => $item) {
  5288. if (isset($item['load_data']) || empty($item['data'])) {
  5289. $item['data'] = $default_data[$item['load_data']];
  5290. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5291. }
  5292. $sub_list = '';
  5293. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5294. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5295. }
  5296. if (empty($item['children'])) {
  5297. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5298. $active = null;
  5299. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5300. $active = 'active';
  5301. }
  5302. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5303. } else {
  5304. //sections
  5305. if (isset($item['children'])) {
  5306. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5307. }
  5308. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5309. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5310. }
  5311. }
  5312. return $return;
  5313. }
  5314. /**
  5315. * This function builds the action menu
  5316. * @param bool $returnContent
  5317. * @return void
  5318. */
  5319. public function build_action_menu($returnContent = false)
  5320. {
  5321. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5322. $return = '<div class="actions">';
  5323. $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> ';
  5324. $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>';
  5325. $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>';
  5326. $buttons = array(
  5327. array(
  5328. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5329. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=set_previous_step_as_prerequisite&lp_id=' . $_SESSION['oLP']->lp_id,
  5330. ),
  5331. array(
  5332. 'title' => get_lang('ClearAllPrerequisites'),
  5333. 'href' => 'lp_controller.php?'.api_get_cidreq().'&action=clear_prerequisites&lp_id=' . $_SESSION['oLP']->lp_id,
  5334. ),
  5335. );
  5336. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5337. $return .= '</div>';
  5338. if ($returnContent) {
  5339. return $return;
  5340. }
  5341. echo $return;
  5342. }
  5343. /**
  5344. * Creates the default learning path folder
  5345. * @param array $course
  5346. * @return bool
  5347. */
  5348. public static function generate_learning_path_folder($course)
  5349. {
  5350. // Creating learning_path folder
  5351. $dir = '/learning_path';
  5352. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5353. $folder = false;
  5354. if (!is_dir($filepath.'/'.$dir)) {
  5355. $folderData = create_unexisting_directory(
  5356. $course,
  5357. api_get_user_id(),
  5358. api_get_session_id(),
  5359. 0,
  5360. 0,
  5361. $filepath,
  5362. $dir,
  5363. get_lang('LearningPaths'),
  5364. 0
  5365. );
  5366. if (!empty($folderData)) {
  5367. $folder = true;
  5368. }
  5369. } else {
  5370. $folder = true;
  5371. }
  5372. return $folder;
  5373. }
  5374. /**
  5375. * @param array $course
  5376. * @param string $lp_name
  5377. * @return array
  5378. */
  5379. public function generate_lp_folder($course, $lp_name = null)
  5380. {
  5381. $filepath = '';
  5382. $dir = '/learning_path/';
  5383. if (empty($lp_name)) {
  5384. $lp_name = $this->name;
  5385. }
  5386. $folder = self::generate_learning_path_folder($course);
  5387. // Creating LP folder
  5388. if ($folder) {
  5389. //Limits title size
  5390. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5391. $dir = $dir.$title;
  5392. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5393. if (!is_dir($filepath.'/'.$dir)) {
  5394. $folderData = create_unexisting_directory(
  5395. $course,
  5396. api_get_user_id(),
  5397. 0,
  5398. 0,
  5399. 0,
  5400. $filepath,
  5401. $dir,
  5402. $lp_name
  5403. );
  5404. if (!empty($folderData)) {
  5405. $folder = true;
  5406. }
  5407. } else {
  5408. $folder = true;
  5409. }
  5410. $dir = $dir.'/';
  5411. if ($folder) {
  5412. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5413. }
  5414. }
  5415. $array = array(
  5416. 'dir' => $dir,
  5417. 'filepath' => $filepath,
  5418. 'folder' => $folder
  5419. );
  5420. return $array;
  5421. }
  5422. /**
  5423. * Create a new document //still needs some finetuning
  5424. * @param array $_course
  5425. * @return string
  5426. */
  5427. public function create_document($_course)
  5428. {
  5429. $course_id = api_get_course_int_id();
  5430. global $charset;
  5431. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5432. if (strstr($dir, '..'))
  5433. $dir = '/';
  5434. if ($dir[0] == '.')
  5435. $dir = substr($dir, 1);
  5436. if ($dir[0] != '/')
  5437. $dir = '/' . $dir;
  5438. if ($dir[strlen($dir) - 1] != '/')
  5439. $dir .= '/';
  5440. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5441. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5442. //Generates folder
  5443. $result = $this->generate_lp_folder($_course);
  5444. $dir = $result['dir'];
  5445. $filepath = $result['filepath'];
  5446. }
  5447. if (!is_dir($filepath)) {
  5448. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5449. $dir = '/';
  5450. }
  5451. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5452. // is already escaped twice when it gets here.
  5453. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5454. $title = disable_dangerous_file($title);
  5455. $filename = $title;
  5456. $content = $_POST['content_lp'];
  5457. $tmp_filename = $filename;
  5458. $i = 0;
  5459. while (file_exists($filepath . $tmp_filename . '.html'))
  5460. $tmp_filename = $filename . '_' . ++ $i;
  5461. $filename = $tmp_filename . '.html';
  5462. $content = stripslashes($content);
  5463. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5464. // Change the path of mp3 to absolute.
  5465. // The first regexp deals with :// urls.
  5466. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5467. // The second regexp deals with audio/ urls.
  5468. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5469. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5470. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5471. if (!file_exists($filepath . $filename)) {
  5472. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5473. fputs($fp, $content);
  5474. fclose($fp);
  5475. $file_size = filesize($filepath . $filename);
  5476. $save_file_path = $dir.$filename;
  5477. $document_id = add_document(
  5478. $_course,
  5479. $save_file_path,
  5480. 'file',
  5481. $file_size,
  5482. $tmp_filename
  5483. );
  5484. if ($document_id) {
  5485. api_item_property_update(
  5486. $_course,
  5487. TOOL_DOCUMENT,
  5488. $document_id,
  5489. 'DocumentAdded',
  5490. api_get_user_id(),
  5491. null,
  5492. null,
  5493. null,
  5494. null,
  5495. api_get_session_id()
  5496. );
  5497. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5498. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5499. if ($new_comment || $new_title) {
  5500. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5501. $ct = '';
  5502. if ($new_comment)
  5503. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5504. if ($new_title)
  5505. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5506. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5507. Database::query($sql_update);
  5508. }
  5509. }
  5510. return $document_id;
  5511. }
  5512. }
  5513. }
  5514. /**
  5515. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5516. * @param array $_course array
  5517. * @return void
  5518. */
  5519. public function edit_document($_course)
  5520. {
  5521. $course_id = api_get_course_int_id();
  5522. global $_configuration;
  5523. // Please, do not modify this dirname formatting.
  5524. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5525. if (strstr($dir, '..'))
  5526. $dir = '/';
  5527. if ($dir[0] == '.')
  5528. $dir = substr($dir, 1);
  5529. if ($dir[0] != '/')
  5530. $dir = '/' . $dir;
  5531. if ($dir[strlen($dir) - 1] != '/')
  5532. $dir .= '/';
  5533. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5534. if (!is_dir($filepath)) {
  5535. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5536. $dir = '/';
  5537. }
  5538. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5539. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5540. $document_id = intval($_POST['path']);
  5541. $sql = "SELECT path FROM " . $table_doc . "
  5542. WHERE c_id = $course_id AND id = " . $document_id;
  5543. $res = Database::query($sql);
  5544. $row = Database :: fetch_array($res);
  5545. $content = stripslashes($_POST['content_lp']);
  5546. $file = $filepath . $row['path'];
  5547. if ($fp = @ fopen($file, 'w')) {
  5548. $content = text_filter($content);
  5549. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5550. // Change the path of mp3 to absolute.
  5551. // The first regexp deals with :// urls.
  5552. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5553. // The second regexp deals with audio/ urls.
  5554. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5555. fputs($fp, $content);
  5556. fclose($fp);
  5557. $sql = "UPDATE " . $table_doc ." SET
  5558. title='".Database::escape_string($_POST['title'])."'
  5559. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5560. Database::query($sql);
  5561. }
  5562. }
  5563. }
  5564. /**
  5565. * Displays the selected item, with a panel for manipulating the item
  5566. * @param int $item_id
  5567. * @param string $msg
  5568. * @return string
  5569. */
  5570. public function display_item($item_id, $msg = null, $show_actions = true)
  5571. {
  5572. $course_id = api_get_course_int_id();
  5573. $return = '';
  5574. if (is_numeric($item_id)) {
  5575. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5576. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5577. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5578. $result = Database::query($sql);
  5579. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5580. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5581. // Prevents wrong parent selection for document, see Bug#1251.
  5582. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5583. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5584. }
  5585. if ($show_actions) {
  5586. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5587. }
  5588. $return .= '<div style="padding:10px;">';
  5589. if ($msg != '')
  5590. $return .= $msg;
  5591. $return .= '<h3>'.$row['title'].'</h3>';
  5592. switch ($row['item_type']) {
  5593. case TOOL_QUIZ:
  5594. if (!empty($row['path'])) {
  5595. $exercise = new Exercise();
  5596. $exercise->read($row['path']);
  5597. $return .= $exercise->description.'<br />';
  5598. }
  5599. break;
  5600. case TOOL_DOCUMENT:
  5601. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5602. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5603. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5604. $result = Database::query($sql_doc);
  5605. $path_file = Database::result($result, 0, 0);
  5606. $path_parts = pathinfo($path_file);
  5607. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5608. if (in_array($path_parts['extension'], array(
  5609. 'html',
  5610. 'txt',
  5611. 'png',
  5612. 'jpg',
  5613. 'JPG',
  5614. 'jpeg',
  5615. 'JPEG',
  5616. 'gif',
  5617. 'swf'
  5618. ))) {
  5619. $return .= $this->display_document($row['path'], true, true);
  5620. }
  5621. break;
  5622. }
  5623. $return .= '</div>';
  5624. }
  5625. }
  5626. return $return;
  5627. }
  5628. /**
  5629. * Shows the needed forms for editing a specific item
  5630. * @param int $item_id
  5631. * @return string
  5632. */
  5633. public function display_edit_item($item_id)
  5634. {
  5635. $course_id = api_get_course_int_id();
  5636. $return = '';
  5637. if (is_numeric($item_id)) {
  5638. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5639. $sql = "SELECT * FROM $tbl_lp_item
  5640. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5641. $res = Database::query($sql);
  5642. $row = Database::fetch_array($res);
  5643. switch ($row['item_type']) {
  5644. case 'dokeos_chapter' :
  5645. case 'dir' :
  5646. case 'asset' :
  5647. case 'sco' :
  5648. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5649. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5650. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5651. } else {
  5652. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5653. }
  5654. break;
  5655. case TOOL_DOCUMENT :
  5656. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5657. $sql = "SELECT lp.*, doc.path as dir
  5658. FROM " . $tbl_lp_item . " as lp
  5659. LEFT JOIN " . $tbl_doc . " as doc
  5660. ON doc.id = lp.path
  5661. WHERE
  5662. lp.c_id = $course_id AND
  5663. doc.c_id = $course_id AND
  5664. lp.id = " . intval($item_id);
  5665. $res_step = Database::query($sql);
  5666. $row_step = Database :: fetch_array($res_step);
  5667. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5668. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5669. break;
  5670. case TOOL_LINK :
  5671. $link_id = (string) $row['path'];
  5672. if (ctype_digit($link_id)) {
  5673. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5674. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5675. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5676. $res_link = Database::query($sql_select);
  5677. $row_link = Database :: fetch_array($res_link);
  5678. if (is_array($row_link)) {
  5679. $row['url'] = $row_link['url'];
  5680. }
  5681. }
  5682. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5683. $return .= $this->display_link_form('edit', $item_id, $row);
  5684. break;
  5685. case 'dokeos_module' :
  5686. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5687. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5688. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5689. } else {
  5690. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5691. }
  5692. break;
  5693. case TOOL_QUIZ :
  5694. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5695. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5696. break;
  5697. case TOOL_HOTPOTATOES :
  5698. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5699. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5700. break;
  5701. case TOOL_STUDENTPUBLICATION :
  5702. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5703. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5704. break;
  5705. case TOOL_FORUM :
  5706. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5707. $return .= $this->display_forum_form('edit', $item_id, $row);
  5708. break;
  5709. case TOOL_THREAD :
  5710. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5711. $return .= $this->display_thread_form('edit', $item_id, $row);
  5712. break;
  5713. }
  5714. }
  5715. return $return;
  5716. }
  5717. /**
  5718. * Function that displays a list with al the resources that
  5719. * could be added to the learning path
  5720. * @return string
  5721. */
  5722. public function display_resources()
  5723. {
  5724. $course_code = api_get_course_id();
  5725. // Get all the docs.
  5726. $documents = $this->get_documents(true);
  5727. // Get all the exercises.
  5728. $exercises = $this->get_exercises();
  5729. // Get all the links.
  5730. $links = $this->get_links();
  5731. // Get al the student publications.
  5732. $works = $this->get_student_publications();
  5733. // Get al the forums.
  5734. $forums = $this->get_forums(null, $course_code);
  5735. $headers = array(
  5736. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5737. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5738. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5739. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5740. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5741. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5742. );
  5743. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5744. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5745. echo Display::tabs(
  5746. $headers,
  5747. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5748. );
  5749. return true;
  5750. }
  5751. /**
  5752. * Returns the extension of a document
  5753. * @param string filename
  5754. * @return string Extension (part after the last dot)
  5755. */
  5756. public function get_extension($filename)
  5757. {
  5758. $explode = explode('.', $filename);
  5759. return $explode[count($explode) - 1];
  5760. }
  5761. /**
  5762. * Displays a document by id
  5763. *
  5764. * @param int $id
  5765. * @return string
  5766. */
  5767. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5768. {
  5769. $_course = api_get_course_info();
  5770. $course_id = api_get_course_int_id();
  5771. $return = '';
  5772. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5773. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5774. WHERE c_id = ".$course_id." AND id = " . $id;
  5775. $res_doc = Database::query($sql_doc);
  5776. $row_doc = Database :: fetch_array($res_doc);
  5777. // TODO: Add a path filter.
  5778. if ($iframe) {
  5779. $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>';
  5780. } else {
  5781. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5782. }
  5783. return $return;
  5784. }
  5785. /**
  5786. * Return HTML form to add/edit a quiz
  5787. * @param string Action (add/edit)
  5788. * @param integer Item ID if already exists
  5789. * @param mixed Extra information (quiz ID if integer)
  5790. * @return string HTML form
  5791. */
  5792. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5793. {
  5794. $course_id = api_get_course_int_id();
  5795. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5796. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5797. if ($id != 0 && is_array($extra_info)) {
  5798. $item_title = $extra_info['title'];
  5799. $item_description = $extra_info['description'];
  5800. } elseif (is_numeric($extra_info)) {
  5801. $sql = "SELECT title, description
  5802. FROM " . $tbl_quiz . "
  5803. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5804. $result = Database::query($sql);
  5805. $row = Database::fetch_array($result);
  5806. $item_title = $row['title'];
  5807. $item_description = $row['description'];
  5808. } else {
  5809. $item_title = '';
  5810. $item_description = '';
  5811. }
  5812. $item_title = Security::remove_XSS($item_title);
  5813. $item_description = Security::remove_XSS($item_description);
  5814. $legend = '<legend>';
  5815. if ($id != 0 && is_array($extra_info))
  5816. $parent = $extra_info['parent_item_id'];
  5817. else
  5818. $parent = 0;
  5819. $sql = "SELECT * FROM " . $tbl_lp_item . "
  5820. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5821. $result = Database::query($sql);
  5822. $arrLP = array ();
  5823. while ($row = Database :: fetch_array($result)) {
  5824. $arrLP[] = array (
  5825. 'id' => $row['id'],
  5826. 'item_type' => $row['item_type'],
  5827. 'title' => $row['title'],
  5828. 'path' => $row['path'],
  5829. 'description' => $row['description'],
  5830. 'parent_item_id' => $row['parent_item_id'],
  5831. 'previous_item_id' => $row['previous_item_id'],
  5832. 'next_item_id' => $row['next_item_id'],
  5833. 'display_order' => $row['display_order'],
  5834. 'max_score' => $row['max_score'],
  5835. 'min_score' => $row['min_score'],
  5836. 'mastery_score' => $row['mastery_score'],
  5837. 'prerequisite' => $row['prerequisite'],
  5838. 'max_time_allowed' => $row['max_time_allowed']
  5839. );
  5840. }
  5841. $this->tree_array($arrLP);
  5842. $arrLP = $this->arrMenu;
  5843. unset ($this->arrMenu);
  5844. if ($action == 'add') {
  5845. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5846. } elseif ($action == 'move') {
  5847. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5848. } else {
  5849. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5850. }
  5851. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5852. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5853. }
  5854. $legend .= '</legend>';
  5855. $return = '';
  5856. $return .= '<div class="sectioncomment">';
  5857. $return .= '<form method="POST">';
  5858. $return .= $legend;
  5859. $return .= '<table class="lp_form">';
  5860. if ($action != 'move') {
  5861. $return .= '<tr>';
  5862. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5863. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5864. $return .= '</tr>';
  5865. }
  5866. $return .= '<tr>';
  5867. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5868. $return .= '<td class="input">';
  5869. // Select for Parent item, root or chapter
  5870. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5871. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5872. $arrHide = array (
  5873. $id
  5874. );
  5875. for ($i = 0; $i < count($arrLP); $i++) {
  5876. if ($action != 'add') {
  5877. 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)) {
  5878. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5879. } else {
  5880. $arrHide[] = $arrLP[$i]['id'];
  5881. }
  5882. } else {
  5883. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5884. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5885. }
  5886. }
  5887. if (is_array($arrLP)) {
  5888. reset($arrLP);
  5889. }
  5890. $return .= '</select>';
  5891. $return .= '</td>';
  5892. $return .= '</tr>';
  5893. $return .= '<tr>';
  5894. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5895. $return .= '<td class="input">';
  5896. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5897. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5898. for ($i = 0; $i < count($arrLP); $i++) {
  5899. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5900. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5901. $selected = 'selected="selected" ';
  5902. elseif ($action == 'add') $selected = 'selected="selected" ';
  5903. else
  5904. $selected = '';
  5905. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5906. }
  5907. }
  5908. $return .= '</select>';
  5909. $return .= '</td>';
  5910. $return .= '</tr>';
  5911. if ($action != 'move') {
  5912. $id_prerequisite = 0;
  5913. if (is_array($arrLP)) {
  5914. foreach ($arrLP as $key => $value) {
  5915. if ($value['id'] == $id) {
  5916. $id_prerequisite = $value['prerequisite'];
  5917. break;
  5918. }
  5919. }
  5920. }
  5921. $arrHide = array ();
  5922. for ($i = 0; $i < count($arrLP); $i++) {
  5923. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5924. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5925. $s_selected_position = $arrLP[$i]['id'];
  5926. elseif ($action == 'add') $s_selected_position = 0;
  5927. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5928. }
  5929. }
  5930. /*// Commented the prerequisites, only visible in edit (exercise).
  5931. $return .= '<tr>';
  5932. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5933. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5934. foreach($arrHide as $key => $value){
  5935. if($key==$s_selected_position && $action == 'add'){
  5936. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5937. }
  5938. elseif($key==$id_prerequisite && $action == 'edit'){
  5939. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5940. }
  5941. else{
  5942. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5943. }
  5944. }
  5945. $return .= "</select></td>";
  5946. */
  5947. $return .= '</tr>';
  5948. /*$return .= '<tr>';
  5949. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5950. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5951. // Remove temporarily the test description.
  5952. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5953. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5954. $return .= '</tr>'; */
  5955. }
  5956. $return .= '<tr>';
  5957. if ($action == 'add') {
  5958. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5959. } else {
  5960. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5961. }
  5962. $return .= '</tr>';
  5963. $return .= '</table>';
  5964. if ($action == 'move') {
  5965. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5966. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5967. }
  5968. if (is_numeric($extra_info)) {
  5969. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5970. } elseif (is_array($extra_info)) {
  5971. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5972. }
  5973. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5974. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5975. $return .= '</form>';
  5976. $return .= '</div>';
  5977. return $return;
  5978. }
  5979. /**
  5980. * Addition of Hotpotatoes tests
  5981. * @param string Action
  5982. * @param integer Internal ID of the item
  5983. * @param mixed Extra information - can be an array with title and description indexes
  5984. * @return string HTML structure to display the hotpotatoes addition formular
  5985. */
  5986. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  5987. {
  5988. $course_id = api_get_course_int_id();
  5989. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5990. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5991. if ($id != 0 && is_array($extra_info)) {
  5992. $item_title = stripslashes($extra_info['title']);
  5993. $item_description = stripslashes($extra_info['description']);
  5994. } elseif (is_numeric($extra_info)) {
  5995. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5996. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  5997. WHERE
  5998. c_id = ".$course_id." AND
  5999. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6000. id = " . (int) $extra_info . "
  6001. ORDER BY id ASC";
  6002. $res_hot = Database::query($sql);
  6003. $row = Database::fetch_array($res_hot);
  6004. $item_title = $row['title'];
  6005. $item_description = $row['description'];
  6006. if (!empty ($row['comment'])) {
  6007. $item_title = $row['comment'];
  6008. }
  6009. } else {
  6010. $item_title = '';
  6011. $item_description = '';
  6012. }
  6013. if ($id != 0 && is_array($extra_info)) {
  6014. $parent = $extra_info['parent_item_id'];
  6015. } else {
  6016. $parent = 0;
  6017. }
  6018. $sql = "SELECT * FROM $tbl_lp_item
  6019. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6020. $result = Database::query($sql);
  6021. $arrLP = array ();
  6022. while ($row = Database :: fetch_array($result)) {
  6023. $arrLP[] = array (
  6024. 'id' => $row['id'],
  6025. 'item_type' => $row['item_type'],
  6026. 'title' => $row['title'],
  6027. 'path' => $row['path'],
  6028. 'description' => $row['description'],
  6029. 'parent_item_id' => $row['parent_item_id'],
  6030. 'previous_item_id' => $row['previous_item_id'],
  6031. 'next_item_id' => $row['next_item_id'],
  6032. 'display_order' => $row['display_order'],
  6033. 'max_score' => $row['max_score'],
  6034. 'min_score' => $row['min_score'],
  6035. 'mastery_score' => $row['mastery_score'],
  6036. 'prerequisite' => $row['prerequisite'],
  6037. 'max_time_allowed' => $row['max_time_allowed']
  6038. );
  6039. }
  6040. $legend = '<legend>';
  6041. if ($action == 'add')
  6042. $legend .= get_lang('CreateTheExercise');
  6043. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6044. else
  6045. $legend .= get_lang('EditCurrentExecice');
  6046. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6047. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6048. }
  6049. $legend .= '</legend>';
  6050. $return = '<form method="POST">';
  6051. $return .= $legend;
  6052. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6053. $return .= '<tr>';
  6054. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6055. $return .= '<td class="input">';
  6056. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6057. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6058. $arrHide = array (
  6059. $id
  6060. );
  6061. if (count($arrLP) > 0) {
  6062. for ($i = 0; $i < count($arrLP); $i++) {
  6063. if ($action != 'add') {
  6064. 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)) {
  6065. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6066. } else {
  6067. $arrHide[] = $arrLP[$i]['id'];
  6068. }
  6069. } else {
  6070. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6071. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6072. }
  6073. }
  6074. reset($arrLP);
  6075. }
  6076. $return .= '</select>';
  6077. $return .= '</td>';
  6078. $return .= '</tr>';
  6079. $return .= '<tr>';
  6080. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6081. $return .= '<td class="input">';
  6082. $return .= '<select id="previous" name="previous" size="1">';
  6083. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6084. for ($i = 0; $i < count($arrLP); $i++) {
  6085. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6086. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6087. $selected = 'selected="selected" ';
  6088. elseif ($action == 'add') $selected = 'selected="selected" ';
  6089. else
  6090. $selected = '';
  6091. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6092. }
  6093. }
  6094. $return .= '</select>';
  6095. $return .= '</td>';
  6096. $return .= '</tr>';
  6097. if ($action != 'move') {
  6098. $return .= '<tr>';
  6099. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6100. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6101. $return .= '</tr>';
  6102. $id_prerequisite = 0;
  6103. if (is_array($arrLP) && count($arrLP) > 0) {
  6104. foreach ($arrLP as $key => $value) {
  6105. if ($value['id'] == $id) {
  6106. $id_prerequisite = $value['prerequisite'];
  6107. break;
  6108. }
  6109. }
  6110. $arrHide = array ();
  6111. for ($i = 0; $i < count($arrLP); $i++) {
  6112. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6113. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6114. $s_selected_position = $arrLP[$i]['id'];
  6115. elseif ($action == 'add') $s_selected_position = 0;
  6116. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6117. }
  6118. }
  6119. }
  6120. }
  6121. $return .= '<tr>';
  6122. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6123. $return .= '</tr>';
  6124. $return .= '</table>';
  6125. if ($action == 'move') {
  6126. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6127. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6128. }
  6129. if (is_numeric($extra_info)) {
  6130. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6131. } elseif (is_array($extra_info)) {
  6132. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6133. }
  6134. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6135. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6136. $return .= '</form>';
  6137. return $return;
  6138. }
  6139. /**
  6140. * Return the form to display the forum edit/add option
  6141. * @param string Action (add/edit)
  6142. * @param integer ID of the lp_item if already exists
  6143. * @param mixed Forum ID or title
  6144. * @return string HTML form
  6145. */
  6146. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6147. {
  6148. $course_id = api_get_course_int_id();
  6149. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6150. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6151. if ($id != 0 && is_array($extra_info)) {
  6152. $item_title = stripslashes($extra_info['title']);
  6153. } elseif (is_numeric($extra_info)) {
  6154. $sql = "SELECT forum_title as title, forum_comment as comment
  6155. FROM " . $tbl_forum . "
  6156. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6157. $result = Database::query($sql);
  6158. $row = Database :: fetch_array($result);
  6159. $item_title = $row['title'];
  6160. $item_description = $row['comment'];
  6161. } else {
  6162. $item_title = '';
  6163. $item_description = '';
  6164. }
  6165. $legend = '<legend>';
  6166. if ($id != 0 && is_array($extra_info)) {
  6167. $parent = $extra_info['parent_item_id'];
  6168. } else {
  6169. $parent = 0;
  6170. }
  6171. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6172. WHERE
  6173. c_id = ".$course_id." AND
  6174. lp_id = " . $this->lp_id;
  6175. $result = Database::query($sql);
  6176. $arrLP = array();
  6177. while ($row = Database :: fetch_array($result)) {
  6178. $arrLP[] = array (
  6179. 'id' => $row['id'],
  6180. 'item_type' => $row['item_type'],
  6181. 'title' => $row['title'],
  6182. 'path' => $row['path'],
  6183. 'description' => $row['description'],
  6184. 'parent_item_id' => $row['parent_item_id'],
  6185. 'previous_item_id' => $row['previous_item_id'],
  6186. 'next_item_id' => $row['next_item_id'],
  6187. 'display_order' => $row['display_order'],
  6188. 'max_score' => $row['max_score'],
  6189. 'min_score' => $row['min_score'],
  6190. 'mastery_score' => $row['mastery_score'],
  6191. 'prerequisite' => $row['prerequisite']
  6192. );
  6193. }
  6194. $this->tree_array($arrLP);
  6195. $arrLP = $this->arrMenu;
  6196. unset($this->arrMenu);
  6197. if ($action == 'add')
  6198. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6199. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6200. else
  6201. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6202. $legend .= '</legend>';
  6203. $return = '<div class="sectioncomment">';
  6204. $return .= '<form method="POST">';
  6205. $return .= $legend;
  6206. $return .= '<table class="lp_form">';
  6207. if ($action != 'move') {
  6208. $return .= '<tr>';
  6209. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6210. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6211. $return .= '</tr>';
  6212. }
  6213. $return .= '<tr>';
  6214. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6215. $return .= '<td class="input">';
  6216. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6217. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6218. $arrHide = array(
  6219. $id
  6220. );
  6221. //$parent_item_id = $_SESSION['parent_item_id'];
  6222. for ($i = 0; $i < count($arrLP); $i++) {
  6223. if ($action != 'add') {
  6224. 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)) {
  6225. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6226. } else {
  6227. $arrHide[] = $arrLP[$i]['id'];
  6228. }
  6229. } else {
  6230. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6231. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6232. }
  6233. }
  6234. if (is_array($arrLP)) {
  6235. reset($arrLP);
  6236. }
  6237. $return .= '</select>';
  6238. $return .= '</td>';
  6239. $return .= '</tr>';
  6240. $return .= '<tr>';
  6241. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6242. $return .= '<td class="input">';
  6243. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6244. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6245. for ($i = 0; $i < count($arrLP); $i++) {
  6246. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6247. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6248. $selected = 'selected="selected" ';
  6249. elseif ($action == 'add') $selected = 'selected="selected" ';
  6250. else
  6251. $selected = '';
  6252. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' .
  6253. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6254. }
  6255. }
  6256. $return .= '</select>';
  6257. $return .= '</td>';
  6258. $return .= '</tr>';
  6259. if ($action != 'move') {
  6260. $return .= '<tr>';
  6261. $return .= '</tr>';
  6262. $id_prerequisite = 0;
  6263. if (is_array($arrLP)) {
  6264. foreach ($arrLP as $key => $value) {
  6265. if ($value['id'] == $id) {
  6266. $id_prerequisite = $value['prerequisite'];
  6267. break;
  6268. }
  6269. }
  6270. }
  6271. $arrHide = array();
  6272. for ($i = 0; $i < count($arrLP); $i++) {
  6273. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6274. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6275. $s_selected_position = $arrLP[$i]['id'];
  6276. elseif ($action == 'add') $s_selected_position = 0;
  6277. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6278. }
  6279. }
  6280. $return .= '</tr>';
  6281. }
  6282. $return .= '<tr>';
  6283. if ($action == 'add') {
  6284. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6285. } else {
  6286. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6287. }
  6288. $return .= '</tr>';
  6289. $return .= '</table>';
  6290. if ($action == 'move') {
  6291. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6292. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6293. }
  6294. if (is_numeric($extra_info)) {
  6295. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6296. } elseif (is_array($extra_info)) {
  6297. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6298. }
  6299. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6300. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6301. $return .= '</form>';
  6302. $return .= '</div>';
  6303. return $return;
  6304. }
  6305. /**
  6306. * Return HTML form to add/edit forum threads
  6307. * @param string Action (add/edit)
  6308. * @param integer Item ID if already exists in learning path
  6309. * @param mixed Extra information (thread ID if integer)
  6310. * @return string HTML form
  6311. */
  6312. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6313. {
  6314. $course_id = api_get_course_int_id();
  6315. if (empty($course_id)) {
  6316. return null;
  6317. }
  6318. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6319. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6320. if ($id != 0 && is_array($extra_info)) {
  6321. $item_title = stripslashes($extra_info['title']);
  6322. } elseif (is_numeric($extra_info)) {
  6323. $sql = "SELECT thread_title as title FROM $tbl_forum
  6324. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6325. $result = Database::query($sql);
  6326. $row = Database :: fetch_array($result);
  6327. $item_title = $row['title'];
  6328. $item_description = '';
  6329. } else {
  6330. $item_title = '';
  6331. $item_description = '';
  6332. }
  6333. $return = null;
  6334. if ($id != 0 && is_array($extra_info)) {
  6335. $parent = $extra_info['parent_item_id'];
  6336. } else {
  6337. $parent = 0;
  6338. }
  6339. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6340. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6341. $result = Database::query($sql);
  6342. $arrLP = array ();
  6343. while ($row = Database :: fetch_array($result)) {
  6344. $arrLP[] = array (
  6345. 'id' => $row['id'],
  6346. 'item_type' => $row['item_type'],
  6347. 'title' => $row['title'],
  6348. 'path' => $row['path'],
  6349. 'description' => $row['description'],
  6350. 'parent_item_id' => $row['parent_item_id'],
  6351. 'previous_item_id' => $row['previous_item_id'],
  6352. 'next_item_id' => $row['next_item_id'],
  6353. 'display_order' => $row['display_order'],
  6354. 'max_score' => $row['max_score'],
  6355. 'min_score' => $row['min_score'],
  6356. 'mastery_score' => $row['mastery_score'],
  6357. 'prerequisite' => $row['prerequisite']
  6358. );
  6359. }
  6360. $this->tree_array($arrLP);
  6361. $arrLP = $this->arrMenu;
  6362. unset ($this->arrMenu);
  6363. $return .= '<form method="POST">';
  6364. if ($action == 'add')
  6365. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6366. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6367. else
  6368. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6369. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6370. $return .= '<tr>';
  6371. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6372. $return .= '<td class="input">';
  6373. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6374. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6375. $arrHide = array (
  6376. $id
  6377. );
  6378. for ($i = 0; $i < count($arrLP); $i++) {
  6379. if ($action != 'add') {
  6380. 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)) {
  6381. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6382. } else {
  6383. $arrHide[] = $arrLP[$i]['id'];
  6384. }
  6385. } else {
  6386. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6387. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6388. }
  6389. }
  6390. if ($arrLP != null) {
  6391. reset($arrLP);
  6392. }
  6393. $return .= '</select>';
  6394. $return .= '</td>';
  6395. $return .= '</tr>';
  6396. $return .= '<tr>';
  6397. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6398. $return .= '<td class="input">';
  6399. $return .= '<select id="previous" name="previous" size="1">';
  6400. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6401. for ($i = 0; $i < count($arrLP); $i++) {
  6402. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6403. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6404. $selected = 'selected="selected" ';
  6405. elseif ($action == 'add') $selected = 'selected="selected" ';
  6406. else
  6407. $selected = '';
  6408. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6409. }
  6410. }
  6411. $return .= '</select>';
  6412. $return .= '</td>';
  6413. $return .= '</tr>';
  6414. if ($action != 'move') {
  6415. $return .= '<tr>';
  6416. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6417. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6418. $return .= '</tr>';
  6419. $return .= '<tr>';
  6420. $return .= '</tr>';
  6421. $id_prerequisite = 0;
  6422. if ($arrLP != null) {
  6423. foreach ($arrLP as $key => $value) {
  6424. if ($value['id'] == $id) {
  6425. $id_prerequisite = $value['prerequisite'];
  6426. break;
  6427. }
  6428. }
  6429. }
  6430. $arrHide = array();
  6431. for ($i = 0; $i < count($arrLP); $i++) {
  6432. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6433. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6434. $s_selected_position = $arrLP[$i]['id'];
  6435. elseif ($action == 'add') $s_selected_position = 0;
  6436. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6437. }
  6438. }
  6439. $return .= '<tr>';
  6440. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6441. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6442. foreach ($arrHide as $key => $value) {
  6443. if ($key == $s_selected_position && $action == 'add') {
  6444. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6445. }
  6446. elseif ($key == $id_prerequisite && $action == 'edit') {
  6447. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6448. } else {
  6449. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6450. }
  6451. }
  6452. $return .= "</select></td>";
  6453. $return .= '</tr>';
  6454. }
  6455. $return .= '<tr>';
  6456. $return .= '<td></td><td>
  6457. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6458. $return .= '</tr>';
  6459. $return .= '</table>';
  6460. if ($action == 'move') {
  6461. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6462. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6463. }
  6464. if (is_numeric($extra_info)) {
  6465. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6466. }
  6467. elseif (is_array($extra_info)) {
  6468. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6469. }
  6470. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6471. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6472. $return .= '</form>';
  6473. $return .= '</div>';
  6474. return $return;
  6475. }
  6476. /**
  6477. * Return the HTML form to display an item (generally a section/module item)
  6478. * @param string Item type (module/dokeos_module)
  6479. * @param string Title (optional, only when creating)
  6480. * @param string Action ('add'/'edit')
  6481. * @param integer lp_item ID
  6482. * @param mixed Extra info
  6483. * @return string HTML form
  6484. */
  6485. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6486. {
  6487. $course_id = api_get_course_int_id();
  6488. $_course = api_get_course_info();
  6489. global $charset;
  6490. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6491. if ($id != 0 && is_array($extra_info)) {
  6492. $item_title = $extra_info['title'];
  6493. $item_description = $extra_info['description'];
  6494. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6495. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6496. } else {
  6497. $item_title = '';
  6498. $item_description = '';
  6499. $item_path_fck = '';
  6500. }
  6501. if ($id != 0 && is_array($extra_info)) {
  6502. $parent = $extra_info['parent_item_id'];
  6503. } else {
  6504. $parent = 0;
  6505. }
  6506. $id = intval($id);
  6507. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6508. WHERE
  6509. c_id = ".$course_id." AND
  6510. lp_id = " . $this->lp_id . " AND
  6511. id != $id";
  6512. if ($item_type == 'module')
  6513. $sql .= " AND parent_item_id = 0";
  6514. $result = Database::query($sql);
  6515. $arrLP = array ();
  6516. while ($row = Database :: fetch_array($result)) {
  6517. $arrLP[] = array(
  6518. 'id' => $row['id'],
  6519. 'item_type' => $row['item_type'],
  6520. 'title' => $row['title'],
  6521. 'path' => $row['path'],
  6522. 'description' => $row['description'],
  6523. 'parent_item_id' => $row['parent_item_id'],
  6524. 'previous_item_id' => $row['previous_item_id'],
  6525. 'next_item_id' => $row['next_item_id'],
  6526. 'max_score' => $row['max_score'],
  6527. 'min_score' => $row['min_score'],
  6528. 'mastery_score' => $row['mastery_score'],
  6529. 'prerequisite' => $row['prerequisite'],
  6530. 'display_order' => $row['display_order']
  6531. );
  6532. }
  6533. $this->tree_array($arrLP);
  6534. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6535. unset ($this->arrMenu);
  6536. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6537. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6538. $form = new FormValidator('form', 'POST', $url);
  6539. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6540. $defaults['description'] = $item_description;
  6541. $form->addElement('header', $title);
  6542. //$arrHide = array($id);
  6543. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6544. $arrHide[0]['padding'] = 3;
  6545. $charset = api_get_system_encoding();
  6546. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6547. for ($i = 0; $i < count($arrLP); $i++) {
  6548. if ($action != 'add') {
  6549. 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)) {
  6550. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6551. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6552. if ($parent == $arrLP[$i]['id']) {
  6553. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6554. }
  6555. }
  6556. } else {
  6557. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6558. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6559. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6560. if ($parent == $arrLP[$i]['id']) {
  6561. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6562. }
  6563. }
  6564. }
  6565. }
  6566. if ($action != 'move') {
  6567. $form->addElement('text', 'title', get_lang('Title'));
  6568. $form->applyFilter('title', 'html_filter');
  6569. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6570. } else {
  6571. $form->addElement('hidden', 'title');
  6572. }
  6573. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6574. foreach ($arrHide as $key => $value) {
  6575. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6576. }
  6577. if (!empty($s_selected_parent)) {
  6578. $parent_select->setSelected($s_selected_parent);
  6579. }
  6580. }
  6581. if (is_array($arrLP)) {
  6582. reset($arrLP);
  6583. }
  6584. $arrHide = array();
  6585. // POSITION
  6586. for ($i = 0; $i < count($arrLP); $i++) {
  6587. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6588. //this is the same!
  6589. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6590. $s_selected_position = $arrLP[$i]['id'];
  6591. } elseif ($action == 'add') {
  6592. $s_selected_position = $arrLP[$i]['id'];
  6593. }
  6594. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6595. }
  6596. }
  6597. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6598. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6599. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6600. foreach ($arrHide as $key => $value) {
  6601. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6602. }
  6603. if (!empty ($s_selected_position)) {
  6604. $position->setSelected($s_selected_position);
  6605. }
  6606. if (is_array($arrLP)) {
  6607. reset($arrLP);
  6608. }
  6609. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6610. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6611. $form->addElement('hidden', 'parent', '0');
  6612. }
  6613. //fix in order to use the tab
  6614. if ($item_type == 'chapter') {
  6615. $form->addElement('hidden', 'type', 'chapter');
  6616. }
  6617. $extension = null;
  6618. if (!empty($item_path)) {
  6619. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6620. }
  6621. //assets can't be modified
  6622. //$item_type == 'asset' ||
  6623. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6624. if ($item_type == 'sco') {
  6625. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6626. }
  6627. $renderer = $form->defaultRenderer();
  6628. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6629. $relative_prefix = '';
  6630. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6631. 'Width' => '100%',
  6632. 'Height' => '500',
  6633. 'FullPage' => true,
  6634. 'CreateDocumentDir' => $relative_prefix,
  6635. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6636. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6637. );
  6638. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6639. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6640. //$defaults['content_lp'] = file_get_contents($item_path);
  6641. $defaults['content_lp'] = file_get_contents($content_path);
  6642. }
  6643. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6644. $form->addElement('hidden', 'post_time', time());
  6645. $form->setDefaults($defaults);
  6646. return $form->return_form();
  6647. }
  6648. /**
  6649. * Returns the form to update or create a document
  6650. * @param string Action (add/edit)
  6651. * @param integer ID of the lp_item (if already exists)
  6652. * @param mixed Integer if document ID, string if info ('new')
  6653. * @return string HTML form
  6654. */
  6655. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6656. {
  6657. $course_id = api_get_course_int_id();
  6658. $_course = api_get_course_info();
  6659. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6660. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6661. $no_display_edit_textarea = false;
  6662. //If action==edit document
  6663. //We don't display the document form if it's not an editable document (html or txt file)
  6664. if ($action == "edit") {
  6665. if (is_array($extra_info)) {
  6666. $path_parts = pathinfo($extra_info['dir']);
  6667. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6668. $no_display_edit_textarea = true;
  6669. }
  6670. }
  6671. }
  6672. $no_display_add = false;
  6673. // If action==add an existing document
  6674. // We don't display the document form if it's not an editable document (html or txt file).
  6675. if ($action == "add") {
  6676. if (is_numeric($extra_info)) {
  6677. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6678. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6679. $result = Database::query($sql_doc);
  6680. $path_file = Database :: result($result, 0, 0);
  6681. $path_parts = pathinfo($path_file);
  6682. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6683. $no_display_add = true;
  6684. }
  6685. }
  6686. }
  6687. if ($id != 0 && is_array($extra_info)) {
  6688. $item_title = stripslashes($extra_info['title']);
  6689. $item_description = stripslashes($extra_info['description']);
  6690. $item_terms = stripslashes($extra_info['terms']);
  6691. if (empty ($item_title)) {
  6692. $path_parts = pathinfo($extra_info['path']);
  6693. $item_title = stripslashes($path_parts['filename']);
  6694. }
  6695. } elseif (is_numeric($extra_info)) {
  6696. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6697. WHERE
  6698. c_id = ".$course_id." AND
  6699. id = " . intval($extra_info);
  6700. $result = Database::query($sql_doc);
  6701. $row = Database::fetch_array($result);
  6702. $item_title = $row['title'];
  6703. $item_title = str_replace('_', ' ', $item_title);
  6704. if (empty ($item_title)) {
  6705. $path_parts = pathinfo($row['path']);
  6706. $item_title = stripslashes($path_parts['filename']);
  6707. }
  6708. } else {
  6709. $item_title = '';
  6710. $item_description = '';
  6711. }
  6712. $return = '<legend>';
  6713. if ($id != 0 && is_array($extra_info)) {
  6714. $parent = $extra_info['parent_item_id'];
  6715. } else {
  6716. $parent = 0;
  6717. }
  6718. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6719. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6720. $result = Database::query($sql);
  6721. $arrLP = array ();
  6722. while ($row = Database :: fetch_array($result)) {
  6723. $arrLP[] = array(
  6724. 'id' => $row['id'],
  6725. 'item_type' => $row['item_type'],
  6726. 'title' => $row['title'],
  6727. 'path' => $row['path'],
  6728. 'description' => $row['description'],
  6729. 'parent_item_id' => $row['parent_item_id'],
  6730. 'previous_item_id' => $row['previous_item_id'],
  6731. 'next_item_id' => $row['next_item_id'],
  6732. 'display_order' => $row['display_order'],
  6733. 'max_score' => $row['max_score'],
  6734. 'min_score' => $row['min_score'],
  6735. 'mastery_score' => $row['mastery_score'],
  6736. 'prerequisite' => $row['prerequisite']
  6737. );
  6738. }
  6739. $this->tree_array($arrLP);
  6740. $arrLP = $this->arrMenu;
  6741. unset ($this->arrMenu);
  6742. if ($action == 'add') {
  6743. $return .= get_lang('CreateTheDocument');
  6744. } elseif ($action == 'move') {
  6745. $return .= get_lang('MoveTheCurrentDocument');
  6746. } else {
  6747. $return .= get_lang('EditTheCurrentDocument');
  6748. }
  6749. $return .= '</legend>';
  6750. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6751. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6752. }
  6753. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6754. $defaults['title'] = Security :: remove_XSS($item_title);
  6755. if (empty($item_title)) {
  6756. $defaults['title'] = Security::remove_XSS($item_title);
  6757. }
  6758. $defaults['description'] = $item_description;
  6759. $form->addElement('html', $return);
  6760. if ($action != 'move') {
  6761. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6762. $form->applyFilter('title', 'html_filter');
  6763. }
  6764. $arrHide[0]['value'] = $this->name;
  6765. $arrHide[0]['padding'] = 3;
  6766. for ($i = 0; $i < count($arrLP); $i++) {
  6767. if ($action != 'add') {
  6768. 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)) {
  6769. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6770. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6771. if ($parent == $arrLP[$i]['id']) {
  6772. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6773. }
  6774. }
  6775. } else {
  6776. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6777. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6778. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6779. if ($parent == $arrLP[$i]['id']) {
  6780. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6781. }
  6782. }
  6783. }
  6784. }
  6785. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6786. $my_count=0;
  6787. foreach ($arrHide as $key => $value) {
  6788. if ($my_count!=0) {
  6789. // The LP name is also the first section and is not in the same charset like the other sections.
  6790. $value['value'] = Security :: remove_XSS($value['value']);
  6791. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6792. } else {
  6793. $value['value'] = Security :: remove_XSS($value['value']);
  6794. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6795. }
  6796. $my_count++;
  6797. }
  6798. if (!empty($id)) {
  6799. $parent_select->setSelected($parent);
  6800. } else {
  6801. $parent_item_id = $_SESSION['parent_item_id'];
  6802. $parent_select->setSelected($parent_item_id);
  6803. }
  6804. if (is_array($arrLP)) {
  6805. reset($arrLP);
  6806. }
  6807. $arrHide = array();
  6808. $s_selected_position = null;
  6809. //POSITION
  6810. for ($i = 0; $i < count($arrLP); $i++) {
  6811. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6812. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6813. $s_selected_position = $arrLP[$i]['id'];
  6814. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6815. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6816. }
  6817. }
  6818. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6819. $position->addOption(get_lang('FirstPosition'), 0);
  6820. foreach ($arrHide as $key => $value) {
  6821. $padding = isset($value['padding']) ? $value['padding']: 0;
  6822. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6823. }
  6824. $position->setSelected($s_selected_position);
  6825. if (is_array($arrLP)) {
  6826. reset($arrLP);
  6827. }
  6828. if ($action != 'move') {
  6829. $id_prerequisite = 0;
  6830. if (is_array($arrLP)) {
  6831. foreach ($arrLP as $key => $value) {
  6832. if ($value['id'] == $id) {
  6833. $id_prerequisite = $value['prerequisite'];
  6834. break;
  6835. }
  6836. }
  6837. }
  6838. $arrHide = array();
  6839. for ($i = 0; $i < count($arrLP); $i++) {
  6840. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6841. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6842. $s_selected_position = $arrLP[$i]['id'];
  6843. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6844. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6845. }
  6846. }
  6847. if (!$no_display_add) {
  6848. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6849. if (isset ($_POST['content']))
  6850. $content = stripslashes($_POST['content']);
  6851. elseif (is_array($extra_info)) {
  6852. //If it's an html document or a text file
  6853. if (!$no_display_edit_textarea) {
  6854. $content = $this->display_document($extra_info['path'], false, false);
  6855. }
  6856. } elseif (is_numeric($extra_info))
  6857. $content = $this->display_document($extra_info, false, false);
  6858. else
  6859. $content = '';
  6860. if (!$no_display_edit_textarea) {
  6861. // We need to calculate here some specific settings for the online editor.
  6862. // The calculated settings work for documents in the Documents tool
  6863. // (on the root or in subfolders).
  6864. // For documents in native scorm packages it is unclear whether the
  6865. // online editor should be activated or not.
  6866. // A new document, it is in the root of the repository.
  6867. $relative_path = '';
  6868. $relative_prefix = '';
  6869. if (is_array($extra_info) && $extra_info != 'new') {
  6870. // The document already exists. Whe have to determine its relative path towards the repository root.
  6871. $relative_path = explode('/', $extra_info['dir']);
  6872. $cnt = count($relative_path) - 2;
  6873. if ($cnt < 0) {
  6874. $cnt = 0;
  6875. }
  6876. $relative_prefix = str_repeat('../', $cnt);
  6877. $relative_path = array_slice($relative_path, 1, $cnt);
  6878. $relative_path = implode('/', $relative_path);
  6879. if (strlen($relative_path) > 0) {
  6880. $relative_path = $relative_path . '/';
  6881. }
  6882. } else {
  6883. $result = $this->generate_lp_folder($_course);
  6884. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6885. $relative_prefix = '../../';
  6886. }
  6887. $editor_config = array(
  6888. 'ToolbarSet'=> 'LearningPathDocuments',
  6889. 'Width' => '100%',
  6890. 'Height' => '500',
  6891. 'FullPage' => true,
  6892. 'CreateDocumentDir' => $relative_prefix,
  6893. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6894. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6895. );
  6896. if ($_GET['action'] == 'add_item') {
  6897. $class = 'add';
  6898. $text = get_lang('LPCreateDocument');
  6899. } else {
  6900. if ($_GET['action'] == 'edit_item') {
  6901. $class = 'save';
  6902. $text = get_lang('SaveDocument');
  6903. }
  6904. }
  6905. $form->addButtonSave($text, 'submit_button');
  6906. $renderer = $form->defaultRenderer();
  6907. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  6908. $form->addElement('html', '<div class="editor-lp">');
  6909. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  6910. $form->addElement('html', '</div>');
  6911. $defaults['content_lp'] = $content;
  6912. }
  6913. } elseif (is_numeric($extra_info)) {
  6914. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6915. $return = $this->display_document($extra_info, true, true, true);
  6916. $form->addElement('html', $return);
  6917. }
  6918. }
  6919. }
  6920. if ($action == 'move') {
  6921. $form->addElement('hidden', 'title', $item_title);
  6922. $form->addElement('hidden', 'description', $item_description);
  6923. }
  6924. if (is_numeric($extra_info)) {
  6925. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6926. $form->addElement('hidden', 'path', $extra_info);
  6927. } elseif (is_array($extra_info)) {
  6928. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  6929. $form->addElement('hidden', 'path', $extra_info['path']);
  6930. }
  6931. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6932. $form->addElement('hidden', 'post_time', time());
  6933. $form->setDefaults($defaults);
  6934. return $form->return_form();
  6935. }
  6936. /**
  6937. * Return HTML form to add/edit a link item
  6938. * @param string $action (add/edit)
  6939. * @param integer $id Item ID if exists
  6940. * @param mixed $extra_info
  6941. * @return string HTML form
  6942. */
  6943. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6944. {
  6945. $course_id = api_get_course_int_id();
  6946. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6947. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6948. if ($id != 0 && is_array($extra_info)) {
  6949. $item_title = stripslashes($extra_info['title']);
  6950. $item_description = stripslashes($extra_info['description']);
  6951. $item_url = stripslashes($extra_info['url']);
  6952. } elseif (is_numeric($extra_info)) {
  6953. $extra_info = intval($extra_info);
  6954. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  6955. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6956. $result = Database::query($sql);
  6957. $row = Database :: fetch_array($result);
  6958. $item_title = $row['title'];
  6959. $item_description = $row['description'];
  6960. $item_url = $row['url'];
  6961. } else {
  6962. $item_title = '';
  6963. $item_description = '';
  6964. $item_url = '';
  6965. }
  6966. $legend = '<legend>';
  6967. if ($id != 0 && is_array($extra_info)) {
  6968. $parent = $extra_info['parent_item_id'];
  6969. } else {
  6970. $parent = 0;
  6971. }
  6972. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6973. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6974. $result = Database::query($sql);
  6975. $arrLP = array();
  6976. while ($row = Database :: fetch_array($result)) {
  6977. $arrLP[] = array(
  6978. 'id' => $row['id'],
  6979. 'item_type' => $row['item_type'],
  6980. 'title' => $row['title'],
  6981. 'path' => $row['path'],
  6982. 'description' => $row['description'],
  6983. 'parent_item_id' => $row['parent_item_id'],
  6984. 'previous_item_id' => $row['previous_item_id'],
  6985. 'next_item_id' => $row['next_item_id'],
  6986. 'display_order' => $row['display_order'],
  6987. 'max_score' => $row['max_score'],
  6988. 'min_score' => $row['min_score'],
  6989. 'mastery_score' => $row['mastery_score'],
  6990. 'prerequisite' => $row['prerequisite']
  6991. );
  6992. }
  6993. $this->tree_array($arrLP);
  6994. $arrLP = $this->arrMenu;
  6995. unset ($this->arrMenu);
  6996. if ($action == 'add')
  6997. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6998. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6999. else
  7000. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  7001. $legend .= '</legend>';
  7002. $return = '<div class="sectioncomment">';
  7003. $return .= '<form method="POST">';
  7004. $return .= $legend;
  7005. $return .= '<table>';
  7006. if ($action != 'move') {
  7007. $return .= '<tr>';
  7008. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7009. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  7010. $return .= '</tr>';
  7011. }
  7012. $return .= '<tr>';
  7013. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7014. $return .= '<td class="input">';
  7015. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7016. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7017. $arrHide = array(
  7018. $id
  7019. );
  7020. $parent_item_id = $_SESSION['parent_item_id'];
  7021. for ($i = 0; $i < count($arrLP); $i++) {
  7022. if ($action != 'add') {
  7023. 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)) {
  7024. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7025. } else {
  7026. $arrHide[] = $arrLP[$i]['id'];
  7027. }
  7028. } else {
  7029. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7030. $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>';
  7031. }
  7032. }
  7033. if (is_array($arrLP)) {
  7034. reset($arrLP);
  7035. }
  7036. $return .= '</select>';
  7037. $return .= '</td>';
  7038. $return .= '</tr>';
  7039. $return .= '<tr>';
  7040. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7041. $return .= '<td class="input">';
  7042. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7043. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7044. for ($i = 0; $i < count($arrLP); $i++) {
  7045. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7046. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7047. $selected = 'selected="selected" ';
  7048. elseif ($action == 'add')
  7049. $selected = 'selected="selected" ';
  7050. else
  7051. $selected = '';
  7052. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7053. }
  7054. }
  7055. $return .= '</select>';
  7056. $return .= '</td>';
  7057. $return .= '</tr>';
  7058. if ($action != 'move') {
  7059. $return .= '<tr>';
  7060. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7061. $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>';
  7062. $return .= '</tr>';
  7063. $id_prerequisite = 0;
  7064. if (is_array($arrLP)) {
  7065. foreach ($arrLP as $key => $value) {
  7066. if ($value['id'] == $id) {
  7067. $id_prerequisite = $value['prerequisite'];
  7068. break;
  7069. }
  7070. }
  7071. }
  7072. $arrHide = array();
  7073. for ($i = 0; $i < count($arrLP); $i++) {
  7074. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7075. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7076. $s_selected_position = $arrLP[$i]['id'];
  7077. elseif ($action == 'add') $s_selected_position = 0;
  7078. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7079. }
  7080. }
  7081. $return .= '</tr>';
  7082. }
  7083. $return .= '<tr>';
  7084. if ($action == 'add') {
  7085. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7086. } else {
  7087. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7088. }
  7089. $return .= '</tr>';
  7090. $return .= '</table>';
  7091. if ($action == 'move') {
  7092. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7093. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7094. }
  7095. if (is_numeric($extra_info)) {
  7096. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7097. } elseif (is_array($extra_info)) {
  7098. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7099. }
  7100. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7101. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7102. $return .= '</form>';
  7103. $return .= '</div>';
  7104. return $return;
  7105. }
  7106. /**
  7107. * Return HTML form to add/edit a student publication (work)
  7108. * @param string Action (add/edit)
  7109. * @param integer Item ID if already exists
  7110. * @param mixed Extra info (work ID if integer)
  7111. * @return string HTML form
  7112. */
  7113. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7114. {
  7115. $course_id = api_get_course_int_id();
  7116. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7117. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7118. if ($id != 0 && is_array($extra_info)) {
  7119. $item_title = stripslashes($extra_info['title']);
  7120. $item_description = stripslashes($extra_info['description']);
  7121. } elseif (is_numeric($extra_info)) {
  7122. $extra_info = intval($extra_info);
  7123. $sql = "SELECT title, description
  7124. FROM " . $tbl_publication . "
  7125. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7126. $result = Database::query($sql);
  7127. $row = Database :: fetch_array($result);
  7128. $item_title = $row['title'];
  7129. } else {
  7130. $item_title = get_lang('Student_publication');
  7131. }
  7132. $legend = '<legend>';
  7133. if ($id != 0 && is_array($extra_info)) {
  7134. $parent = $extra_info['parent_item_id'];
  7135. } else {
  7136. $parent = 0;
  7137. }
  7138. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7139. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7140. $result = Database::query($sql);
  7141. $arrLP = array();
  7142. while ($row = Database :: fetch_array($result)) {
  7143. $arrLP[] = array (
  7144. 'id' => $row['id'],
  7145. 'item_type' => $row['item_type'],
  7146. 'title' => $row['title'],
  7147. 'path' => $row['path'],
  7148. 'description' => $row['description'],
  7149. 'parent_item_id' => $row['parent_item_id'],
  7150. 'previous_item_id' => $row['previous_item_id'],
  7151. 'next_item_id' => $row['next_item_id'],
  7152. 'display_order' => $row['display_order'],
  7153. 'max_score' => $row['max_score'],
  7154. 'min_score' => $row['min_score'],
  7155. 'mastery_score' => $row['mastery_score'],
  7156. 'prerequisite' => $row['prerequisite']
  7157. );
  7158. }
  7159. $this->tree_array($arrLP);
  7160. $arrLP = $this->arrMenu;
  7161. unset ($this->arrMenu);
  7162. if ($action == 'add') {
  7163. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7164. } elseif ($action == 'move') {
  7165. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7166. } else {
  7167. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7168. }
  7169. $legend .= '</legend>';
  7170. $return = '<div class="sectioncomment">';
  7171. $return .= '<form method="POST">';
  7172. $return .= $legend;
  7173. $return .= '<table class="lp_form">';
  7174. if ($action != 'move') {
  7175. $return .= '<tr>';
  7176. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7177. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7178. $return .= '</tr>';
  7179. }
  7180. $return .= '<tr>';
  7181. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7182. $return .= '<td class="input">';
  7183. $return .= "\t\t\t\t" . '<select id="idParent" name="parent" style="width:100%;" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7184. //$parent_item_id = $_SESSION['parent_item_id'];
  7185. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7186. $arrHide = array (
  7187. $id
  7188. );
  7189. for ($i = 0; $i < count($arrLP); $i++) {
  7190. if ($action != 'add') {
  7191. 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)) {
  7192. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7193. } else {
  7194. $arrHide[] = $arrLP[$i]['id'];
  7195. }
  7196. } else {
  7197. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7198. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7199. }
  7200. }
  7201. if (is_array($arrLP)) {
  7202. reset($arrLP);
  7203. }
  7204. $return .= '</select>';
  7205. $return .= '</td>';
  7206. $return .= '</tr>';
  7207. $return .= '<tr>';
  7208. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7209. $return .= '<td class="input">';
  7210. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7211. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7212. for ($i = 0; $i < count($arrLP); $i++) {
  7213. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7214. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7215. $selected = 'selected="selected" ';
  7216. elseif ($action == 'add') $selected = 'selected="selected" ';
  7217. else
  7218. $selected = '';
  7219. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7220. }
  7221. }
  7222. $return .= '</select>';
  7223. $return .= '</td>';
  7224. $return .= '</tr>';
  7225. if ($action != 'move') {
  7226. $id_prerequisite = 0;
  7227. if (is_array($arrLP)) {
  7228. foreach ($arrLP as $key => $value) {
  7229. if ($value['id'] == $id) {
  7230. $id_prerequisite = $value['prerequisite'];
  7231. break;
  7232. }
  7233. }
  7234. }
  7235. $arrHide = array ();
  7236. for ($i = 0; $i < count($arrLP); $i++) {
  7237. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7238. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7239. $s_selected_position = $arrLP[$i]['id'];
  7240. elseif ($action == 'add') $s_selected_position = 0;
  7241. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7242. }
  7243. }
  7244. // Commented the prerequisites, only visible in edit (work).
  7245. /*
  7246. $return .= '<tr>';
  7247. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7248. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7249. foreach($arrHide as $key => $value) {
  7250. if ($key == $s_selected_position && $action == 'add') {
  7251. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7252. }
  7253. elseif ($key == $id_prerequisite && $action == 'edit') {
  7254. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7255. }
  7256. else {
  7257. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7258. }
  7259. }
  7260. $return .= "</select></td>";
  7261. */
  7262. $return .= '</tr>';
  7263. }
  7264. $return .= '<tr>';
  7265. if ($action == 'add') {
  7266. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7267. } else {
  7268. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7269. }
  7270. $return .= '</tr>';
  7271. $return .= '</table>';
  7272. if ($action == 'move') {
  7273. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7274. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7275. }
  7276. if (is_numeric($extra_info)) {
  7277. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7278. } elseif (is_array($extra_info)) {
  7279. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7280. }
  7281. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7282. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7283. $return .= '</form>';
  7284. $return .= '</div>';
  7285. return $return;
  7286. }
  7287. /**
  7288. * Displays the menu for manipulating a step
  7289. *
  7290. * @param $item_id
  7291. * @param string $item_type
  7292. * @return string
  7293. */
  7294. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7295. {
  7296. $_course = api_get_course_info();
  7297. $course_id = api_get_course_int_id();
  7298. $course_code = api_get_course_id();
  7299. $return = '<div class="actions">';
  7300. switch ($item_type) {
  7301. case 'dokeos_chapter' :
  7302. case 'chapter' :
  7303. // Commented the message cause should not show it.
  7304. //$lang = get_lang('TitleManipulateChapter');
  7305. break;
  7306. case 'dokeos_module' :
  7307. case 'module' :
  7308. // Commented the message cause should not show it.
  7309. //$lang = get_lang('TitleManipulateModule');
  7310. break;
  7311. case TOOL_DOCUMENT :
  7312. // Commented the message cause should not show it.
  7313. //$lang = get_lang('TitleManipulateDocument');
  7314. break;
  7315. case TOOL_LINK :
  7316. case 'link' :
  7317. // Commented the message cause should not show it.
  7318. //$lang = get_lang('TitleManipulateLink');
  7319. break;
  7320. case TOOL_QUIZ :
  7321. // Commented the message cause should not show it.
  7322. //$lang = get_lang('TitleManipulateQuiz');
  7323. break;
  7324. case TOOL_STUDENTPUBLICATION :
  7325. // Commented the message cause should not show it.
  7326. //$lang = get_lang('TitleManipulateStudentPublication');
  7327. break;
  7328. }
  7329. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7330. $item_id = intval($item_id);
  7331. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7332. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7333. $result = Database::query($sql);
  7334. $row = Database::fetch_assoc($result);
  7335. $audio_player = null;
  7336. // We display an audio player if needed.
  7337. if (!empty($row['audio'])) {
  7338. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7339. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7340. </div>';
  7341. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7342. $audio_player .= '<script>
  7343. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7344. s1.addParam("allowscriptaccess","always");
  7345. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7346. s1.write("container");
  7347. </script>';
  7348. }
  7349. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7350. $return .= Display::url(
  7351. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7352. $url.'&action=edit_item&path_item=' . $row['path']
  7353. );
  7354. $return .= Display::url(
  7355. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7356. $url.'&action=move_item'
  7357. );
  7358. // Commented for now as prerequisites cannot be added to chapters.
  7359. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7360. $return .= Display::url(
  7361. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7362. $url.'&action=edit_item_prereq'
  7363. );
  7364. }
  7365. $return .= Display::url(
  7366. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7367. $url.'&action=delete_item'
  7368. );
  7369. if ($item_type == TOOL_HOTPOTATOES ) {
  7370. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7371. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7372. }
  7373. if ($item_type == TOOL_DOCUMENT ) {
  7374. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7375. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7376. }
  7377. $return .= '</div>';
  7378. if (!empty($audio_player)) {
  7379. $return .= '<br />'.$audio_player;
  7380. }
  7381. return $return;
  7382. }
  7383. /**
  7384. * Creates the javascript needed for filling up the checkboxes without page reload
  7385. * @return string
  7386. */
  7387. public function get_js_dropdown_array()
  7388. {
  7389. $course_id = api_get_course_int_id();
  7390. $return = 'var child_name = new Array();' . "\n";
  7391. $return .= 'var child_value = new Array();' . "\n\n";
  7392. $return .= 'child_name[0] = new Array();' . "\n";
  7393. $return .= 'child_value[0] = new Array();' . "\n\n";
  7394. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7395. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7396. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7397. ORDER BY display_order ASC";
  7398. $res_zero = Database::query($sql_zero);
  7399. $i = 0;
  7400. while ($row_zero = Database :: fetch_array($res_zero)) {
  7401. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7402. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7403. }
  7404. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7405. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7406. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7407. }
  7408. $return .= "\n";
  7409. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7410. $res = Database::query($sql);
  7411. while ($row = Database :: fetch_array($res)) {
  7412. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7413. WHERE
  7414. c_id = ".$course_id." AND
  7415. parent_item_id = " . $row['id'] . "
  7416. ORDER BY display_order ASC";
  7417. $res_parent = Database::query($sql_parent);
  7418. $i = 0;
  7419. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7420. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7421. while ($row_parent = Database :: fetch_array($res_parent)) {
  7422. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7423. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7424. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7425. }
  7426. $return .= "\n";
  7427. }
  7428. return $return;
  7429. }
  7430. /**
  7431. * Display the form to allow moving an item
  7432. * @param integer Item ID
  7433. * @return string HTML form
  7434. */
  7435. public function display_move_item($item_id)
  7436. {
  7437. $course_id = api_get_course_int_id();
  7438. $_course = api_get_course_info();
  7439. global $charset;
  7440. $return = '';
  7441. if (is_numeric($item_id)) {
  7442. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7443. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7444. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7445. $res = Database::query($sql);
  7446. $row = Database :: fetch_array($res);
  7447. switch ($row['item_type']) {
  7448. case 'dokeos_chapter' :
  7449. case 'dir' :
  7450. case 'asset' :
  7451. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7452. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7453. break;
  7454. case 'dokeos_module' :
  7455. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7456. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7457. break;
  7458. case TOOL_DOCUMENT :
  7459. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7460. $return .= $this->display_document_form('move', $item_id, $row);
  7461. break;
  7462. case TOOL_LINK :
  7463. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7464. $return .= $this->display_link_form('move', $item_id, $row);
  7465. break;
  7466. case TOOL_HOTPOTATOES :
  7467. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7468. $return .= $this->display_link_form('move', $item_id, $row);
  7469. break;
  7470. case TOOL_QUIZ :
  7471. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7472. $return .= $this->display_quiz_form('move', $item_id, $row);
  7473. break;
  7474. case TOOL_STUDENTPUBLICATION :
  7475. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7476. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7477. break;
  7478. case TOOL_FORUM :
  7479. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7480. $return .= $this->display_forum_form('move', $item_id, $row);
  7481. break;
  7482. case TOOL_THREAD :
  7483. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7484. $return .= $this->display_forum_form('move', $item_id, $row);
  7485. break;
  7486. }
  7487. }
  7488. return $return;
  7489. }
  7490. /**
  7491. * Displays a basic form on the overview page for changing the item title and the item description.
  7492. * @param string $item_type
  7493. * @param string $title
  7494. * @param array $data
  7495. * @return string
  7496. */
  7497. public function display_item_small_form($item_type, $title = '', $data = array())
  7498. {
  7499. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7500. $form = new FormValidator('small_form', 'post', $url);
  7501. $form->addElement('header', $title);
  7502. $form->addElement('text', 'title', get_lang('Title'));
  7503. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7504. $form->addElement('hidden', 'id', $data['id']);
  7505. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7506. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7507. $form->setDefaults(array('title' => $data['title']));
  7508. return $form->toHtml();
  7509. }
  7510. /**
  7511. * Return HTML form to allow prerequisites selection
  7512. * @todo use FormValidator
  7513. * @param integer Item ID
  7514. * @return string HTML form
  7515. */
  7516. public function display_item_prerequisites_form($item_id)
  7517. {
  7518. $course_id = api_get_course_int_id();
  7519. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7520. $item_id = intval($item_id);
  7521. /* Current prerequisite */
  7522. $sql = "SELECT * FROM $tbl_lp_item
  7523. WHERE c_id = $course_id AND id = " . $item_id;
  7524. $result = Database::query($sql);
  7525. $row = Database::fetch_array($result);
  7526. $prerequisiteId = $row['prerequisite'];
  7527. $return = '<legend>';
  7528. $return .= get_lang('AddEditPrerequisites');
  7529. $return .= '</legend>';
  7530. $return .= '<form method="POST">';
  7531. $return .= '<table class="data_table">';
  7532. $return .= '<tr>';
  7533. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7534. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7535. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7536. $return .= '</tr>';
  7537. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7538. $return .= '<tr >';
  7539. $return .= '<td colspan="3" class="radio">';
  7540. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7541. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7542. $return .= '</tr>';
  7543. $sql = "SELECT * FROM $tbl_lp_item
  7544. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7545. $result = Database::query($sql);
  7546. $arrLP = array();
  7547. $selectedMinScore = array();
  7548. $selectedMaxScore = array();
  7549. while ($row = Database :: fetch_array($result)) {
  7550. if ($row['id'] == $item_id) {
  7551. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7552. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7553. }
  7554. $arrLP[] = array(
  7555. 'id' => $row['id'],
  7556. 'item_type' => $row['item_type'],
  7557. 'title' => $row['title'],
  7558. 'ref' => $row['ref'],
  7559. 'description' => $row['description'],
  7560. 'parent_item_id' => $row['parent_item_id'],
  7561. 'previous_item_id' => $row['previous_item_id'],
  7562. 'next_item_id' => $row['next_item_id'],
  7563. 'max_score' => $row['max_score'],
  7564. 'min_score' => $row['min_score'],
  7565. 'mastery_score' => $row['mastery_score'],
  7566. 'prerequisite' => $row['prerequisite'],
  7567. 'next_item_id' => $row['next_item_id'],
  7568. 'display_order' => $row['display_order'],
  7569. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7570. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7571. );
  7572. }
  7573. $this->tree_array($arrLP);
  7574. $arrLP = $this->arrMenu;
  7575. unset($this->arrMenu);
  7576. for ($i = 0; $i < count($arrLP); $i++) {
  7577. $item = $arrLP[$i];
  7578. if ($item['id'] == $item_id) {
  7579. break;
  7580. }
  7581. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7582. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7583. $return .= '<tr>';
  7584. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7585. $return .= '<label for="id' . $item['id'] . '">';
  7586. $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'] . '" />';
  7587. $icon_name = str_replace(' ', '', $item['item_type']);
  7588. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7589. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7590. } else {
  7591. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7592. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7593. } else {
  7594. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7595. }
  7596. }
  7597. $return .= $item['title'] . '</label>';
  7598. $return .= '</td>';
  7599. if ($item['item_type'] == TOOL_QUIZ) {
  7600. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7601. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7602. $tmp_obj_exercice = new Exercise();
  7603. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7604. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7605. $tmp_obj_lp_item->update_in_bdd();
  7606. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7607. $return .= '<td class="exercise">';
  7608. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7609. $return .= '</td>';
  7610. $return .= '<td class="exercise">';
  7611. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7612. $return .= '</td>';
  7613. }
  7614. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7615. $return .= '<td class="exercise">';
  7616. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7617. $return .= '</td>';
  7618. $return .= '<td class="exercise"">';
  7619. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7620. $return .= '</td>';
  7621. }
  7622. $return .= '</tr>';
  7623. }
  7624. $return .= '<tr>';
  7625. $return .= '</tr>';
  7626. $return .= '</table>';
  7627. $return .= '<div style="padding-top:3px;">';
  7628. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7629. $return .= '</form>';
  7630. return $return;
  7631. }
  7632. /**
  7633. * Return HTML list to allow prerequisites selection for lp
  7634. * @param integer Item ID
  7635. * @return string HTML form
  7636. */
  7637. public function display_lp_prerequisites_list()
  7638. {
  7639. $course_id = api_get_course_int_id();
  7640. $lp_id = $this->lp_id;
  7641. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7642. // get current prerequisite
  7643. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7644. $result = Database::query($sql);
  7645. $row = Database :: fetch_array($result);
  7646. $prerequisiteId = $row['prerequisite'];
  7647. $session_id = api_get_session_id();
  7648. $session_condition = api_get_session_condition($session_id);
  7649. $sql = "SELECT * FROM $tbl_lp
  7650. WHERE c_id = $course_id $session_condition
  7651. ORDER BY display_order ";
  7652. $rs = Database::query($sql);
  7653. $return = '';
  7654. $return .= '<select name="prerequisites" class="form-control">';
  7655. $return .= '<option value="0">'.get_lang('None').'</option>';
  7656. if (Database::num_rows($rs) > 0) {
  7657. while ($row = Database::fetch_array($rs)) {
  7658. if ($row['id'] == $lp_id) {
  7659. continue;
  7660. }
  7661. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7662. }
  7663. }
  7664. $return .= '</select>';
  7665. return $return;
  7666. }
  7667. /**
  7668. * Creates a list with all the documents in it
  7669. * @param bool $showInvisibleFiles
  7670. * @return string
  7671. */
  7672. public function get_documents($showInvisibleFiles = false)
  7673. {
  7674. $course_info = api_get_course_info();
  7675. $sessionId = api_get_session_id();
  7676. $document_tree = DocumentManager::get_document_preview(
  7677. $course_info,
  7678. $this->lp_id,
  7679. null,
  7680. $sessionId,
  7681. true,
  7682. null,
  7683. null,
  7684. $showInvisibleFiles,
  7685. true
  7686. );
  7687. return $document_tree;
  7688. }
  7689. /**
  7690. * Creates a list with all the exercises (quiz) in it
  7691. * @return string
  7692. */
  7693. public function get_exercises()
  7694. {
  7695. $course_id = api_get_course_int_id();
  7696. // New for hotpotatoes.
  7697. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7698. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7699. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7700. $session_id = api_get_session_id();
  7701. $condition_session = api_get_session_condition($session_id);
  7702. $sql_quiz = "SELECT * FROM $tbl_quiz
  7703. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7704. ORDER BY title ASC";
  7705. $sql_hot = "SELECT * FROM $tbl_doc
  7706. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7707. ORDER BY id ASC";
  7708. $res_quiz = Database::query($sql_quiz);
  7709. $res_hot = Database::query($sql_hot);
  7710. $return = '<ul class="lp_resource">';
  7711. $return .= '<li class="lp_resource_element">';
  7712. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7713. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  7714. get_lang('NewExercise') . '</a>';
  7715. $return .= '</li>';
  7716. // Display hotpotatoes
  7717. while ($row_hot = Database :: fetch_array($res_hot)) {
  7718. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7719. $return .= '<a class="moved" href="#">';
  7720. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7721. $return .= '</a> ';
  7722. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7723. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  7724. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7725. $return .= '</li>';
  7726. }
  7727. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7728. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7729. $return .= '<a class="moved" href="#">';
  7730. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7731. $return .= '</a> ';
  7732. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7733. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  7734. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7735. '</a>';
  7736. $return .= '</li>';
  7737. }
  7738. $return .= '</ul>';
  7739. return $return;
  7740. }
  7741. /**
  7742. * Creates a list with all the links in it
  7743. * @return string
  7744. */
  7745. public function get_links()
  7746. {
  7747. $selfUrl = api_get_self();
  7748. $courseIdReq = api_get_cidreq();
  7749. $course = api_get_course_info();
  7750. $course_id = $course['real_id'];
  7751. $tbl_link = Database::get_course_table(TABLE_LINK);
  7752. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  7753. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7754. $session_id = api_get_session_id();
  7755. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  7756. $sql = "SELECT link.id as link_id,
  7757. link.title as link_title,
  7758. link.category_id as category_id,
  7759. link_category.category_title as category_title
  7760. FROM $tbl_link as link
  7761. LEFT JOIN $linkCategoryTable as link_category
  7762. ON link.category_id = link_category.id
  7763. WHERE link.c_id = ".$course_id." $condition_session
  7764. ORDER BY link_category.category_title ASC, link.title ASC";
  7765. $links = Database::query($sql);
  7766. $categorizedLinks = array();
  7767. $categories = array();
  7768. while ($link = Database :: fetch_array($links)) {
  7769. if (!$link['category_id']) {
  7770. $link['category_title'] = get_lang('Uncategorized');
  7771. }
  7772. $categories[$link['category_id']] = $link['category_title'];
  7773. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  7774. }
  7775. $linksHtmlCode =
  7776. '<script>
  7777. function toggle_tool(tool, id){
  7778. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  7779. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  7780. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7781. } else {
  7782. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  7783. document.getElementById(tool+"_"+id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7784. }
  7785. }
  7786. </script>
  7787. <ul class="lp_resource">
  7788. <li class="lp_resource_element">
  7789. <img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px"/>
  7790. <a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.$courseIdReq.
  7791. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  7792. </li>';
  7793. foreach ($categorizedLinks as $categoryId => $links) {
  7794. $linkNodes = null;
  7795. foreach ($links as $key => $title) {
  7796. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  7797. $linkNodes .=
  7798. '<li class="lp_resource_element" data_id="'.$key.
  7799. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  7800. <a class="moved" href="#">'.
  7801. $moveEverywhereIcon.
  7802. '</a>
  7803. <img alt="" src="../img/lp_link.gif" style="margin-right:5px;width:16px"/>
  7804. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  7805. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  7806. Security::remove_XSS($title).
  7807. '</a>
  7808. </li>';
  7809. }
  7810. }
  7811. $linksHtmlCode .=
  7812. '<li>
  7813. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')"
  7814. style="vertical-align:middle">
  7815. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  7816. align="absbottom" />
  7817. </a>
  7818. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  7819. </li>
  7820. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  7821. }
  7822. $linksHtmlCode .= '</ul>';
  7823. return $linksHtmlCode;
  7824. }
  7825. /**
  7826. * Creates a list with all the student publications in it
  7827. * @return unknown
  7828. */
  7829. public function get_student_publications()
  7830. {
  7831. $return = '<div class="lp_resource" >';
  7832. $return .= '<div class="lp_resource_element">';
  7833. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7834. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7835. $return .= '</div>';
  7836. $return .= '</div>';
  7837. return $return;
  7838. }
  7839. /**
  7840. * Creates a list with all the forums in it
  7841. * @return string
  7842. */
  7843. public function get_forums()
  7844. {
  7845. require_once '../forum/forumfunction.inc.php';
  7846. require_once '../forum/forumconfig.inc.php';
  7847. $a_forums = get_forums();
  7848. $return = '<ul class="lp_resource">';
  7849. //First add link
  7850. $return .= '<li class="lp_resource_element">';
  7851. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7852. $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>';
  7853. $return .= '</li>';
  7854. $return .= '<script>
  7855. function toggle_forum(forum_id){
  7856. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7857. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7858. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7859. } else {
  7860. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7861. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7862. }
  7863. }
  7864. </script>';
  7865. foreach ($a_forums as $forum) {
  7866. if (!empty($forum['forum_id'])) {
  7867. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7868. $return .= '<a class="moved" href="#">';
  7869. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7870. $return .= ' </a>';
  7871. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7872. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7873. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7874. </a>
  7875. <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">' .
  7876. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7877. $return .= '</li>';
  7878. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7879. $a_threads = get_threads($forum['forum_id']);
  7880. if (is_array($a_threads)) {
  7881. foreach ($a_threads as $thread) {
  7882. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7883. $return .= '&nbsp;<a class="moved" href="#">';
  7884. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7885. $return .= ' </a>';
  7886. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7887. $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 . '">' .
  7888. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7889. $return .= '</li>';
  7890. }
  7891. }
  7892. $return .= '</div>';
  7893. }
  7894. }
  7895. $return .= '</ul>';
  7896. return $return;
  7897. }
  7898. /**
  7899. * // TODO: The output encoding should be equal to the system encoding.
  7900. *
  7901. * Exports the learning path as a SCORM package. This is the main function that
  7902. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7903. * whole thing and returns the zip.
  7904. *
  7905. * This method needs to be called in PHP5, as it will fail with non-adequate
  7906. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7907. * you need to call it on a learnpath object.
  7908. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7909. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7910. * path has been modified, it should use the generic method here below.
  7911. * @TODO link this function with the export_lp() function in the same class
  7912. * @param string Optional name of zip file. If none, title of learnpath is
  7913. * domesticated and trailed with ".zip"
  7914. * @return string Returns the zip package string, or null if error
  7915. */
  7916. public function scorm_export()
  7917. {
  7918. $_course = api_get_course_info();
  7919. $course_id = $_course['real_id'];
  7920. // Remove memory and time limits as much as possible as this might be a long process...
  7921. if (function_exists('ini_set')) {
  7922. api_set_memory_limit('128M');
  7923. ini_set('max_execution_time', 600);
  7924. }
  7925. // Create the zip handler (this will remain available throughout the method).
  7926. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7927. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7928. $temp_dir_short = uniqid();
  7929. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7930. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7931. $zip_folder = new PclZip($temp_zip_file);
  7932. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7933. $root_path = $main_path = api_get_path(SYS_PATH);
  7934. $files_cleanup = array();
  7935. // Place to temporarily stash the zip file.
  7936. // create the temp dir if it doesn't exist
  7937. // or do a cleanup before creating the zip file.
  7938. if (!is_dir($temp_zip_dir)) {
  7939. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7940. } else {
  7941. // Cleanup: Check the temp dir for old files and delete them.
  7942. $handle = opendir($temp_zip_dir);
  7943. while (false !== ($file = readdir($handle))) {
  7944. if ($file != '.' && $file != '..') {
  7945. unlink("$temp_zip_dir/$file");
  7946. }
  7947. }
  7948. closedir($handle);
  7949. }
  7950. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7951. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7952. // Remove the possible . at the end of the path.
  7953. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7954. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7955. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7956. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7957. }
  7958. // Build a dummy imsmanifest structure.
  7959. // Do not add to the zip yet (we still need it).
  7960. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7961. // Aggregation Model official document, section "2.3 Content Packaging".
  7962. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7963. $xmldoc = new DOMDocument('1.0');
  7964. $root = $xmldoc->createElement('manifest');
  7965. $root->setAttribute('identifier', 'SingleCourseManifest');
  7966. $root->setAttribute('version', '1.1');
  7967. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7968. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7969. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7970. $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');
  7971. // Build mandatory sub-root container elements.
  7972. $metadata = $xmldoc->createElement('metadata');
  7973. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7974. $metadata->appendChild($md_schema);
  7975. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7976. $metadata->appendChild($md_schemaversion);
  7977. $root->appendChild($metadata);
  7978. $organizations = $xmldoc->createElement('organizations');
  7979. $resources = $xmldoc->createElement('resources');
  7980. // Build the only organization we will use in building our learnpaths.
  7981. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7982. $organization = $xmldoc->createElement('organization');
  7983. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7984. // To set the title of the SCORM entity (=organization), we take the name given
  7985. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7986. // learning path charset) as it is the encoding that defines how it is stored
  7987. // in the database. Then we convert it to HTML entities again as the "&" character
  7988. // alone is not authorized in XML (must be &amp;).
  7989. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7990. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7991. $organization->appendChild($org_title);
  7992. $folder_name = 'document';
  7993. // Removes the learning_path/scorm_folder path when exporting see #4841
  7994. $path_to_remove = null;
  7995. $result = $this->generate_lp_folder($_course);
  7996. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7997. $path_to_remove = 'document'.$result['dir'];
  7998. $path_to_replace = $folder_name.'/';
  7999. }
  8000. //Fixes chamilo scorm exports
  8001. if ($this->ref == 'chamilo_scorm_export') {
  8002. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8003. }
  8004. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8005. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8006. $link_updates = array();
  8007. $links_to_create = array();
  8008. foreach ($this->items as $index => $item) {
  8009. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8010. // Get included documents from this item.
  8011. if ($item->type == 'sco') {
  8012. $inc_docs = $item->get_resources_from_source(
  8013. null,
  8014. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8015. );
  8016. } else {
  8017. $inc_docs = $item->get_resources_from_source();
  8018. }
  8019. // Give a child element <item> to the <organization> element.
  8020. $my_item_id = $item->get_id();
  8021. $my_item = $xmldoc->createElement('item');
  8022. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8023. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8024. $my_item->setAttribute('isvisible', 'true');
  8025. // Give a child element <title> to the <item> element.
  8026. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8027. $my_item->appendChild($my_title);
  8028. // Give a child element <adlcp:prerequisites> to the <item> element.
  8029. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8030. $my_prereqs->setAttribute('type', 'aicc_script');
  8031. $my_item->appendChild($my_prereqs);
  8032. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8033. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8034. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8035. //$xmldoc->createElement('adlcp:timelimitaction','');
  8036. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8037. //$xmldoc->createElement('adlcp:datafromlms','');
  8038. // Give a child element <adlcp:masteryscore> to the <item> element.
  8039. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8040. $my_item->appendChild($my_masteryscore);
  8041. // Attach this item to the organization element or hits parent if there is one.
  8042. if (!empty($item->parent) && $item->parent != 0) {
  8043. $children = $organization->childNodes;
  8044. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8045. if (is_object($possible_parent)) {
  8046. $possible_parent->appendChild($my_item);
  8047. } else {
  8048. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8049. }
  8050. } else {
  8051. if ($this->debug > 0) { error_log('No parent'); }
  8052. $organization->appendChild($my_item);
  8053. }
  8054. // Get the path of the file(s) from the course directory root.
  8055. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8056. if (!empty($path_to_remove)) {
  8057. //From docs
  8058. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8059. //From quiz
  8060. if ($this->ref == 'chamilo_scorm_export') {
  8061. $path_to_remove = 'scorm/'.$this->path.'/';
  8062. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8063. }
  8064. } else {
  8065. $my_xml_file_path = $my_file_path;
  8066. }
  8067. $my_sub_dir = dirname($my_file_path);
  8068. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8069. $my_xml_sub_dir = $my_sub_dir;
  8070. // Give a <resource> child to the <resources> element
  8071. $my_resource = $xmldoc->createElement('resource');
  8072. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8073. $my_resource->setAttribute('type', 'webcontent');
  8074. $my_resource->setAttribute('href', $my_xml_file_path);
  8075. // adlcp:scormtype can be either 'sco' or 'asset'.
  8076. if ($item->type == 'sco') {
  8077. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8078. } else {
  8079. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8080. }
  8081. // xml:base is the base directory to find the files declared in this resource.
  8082. $my_resource->setAttribute('xml:base', '');
  8083. // Give a <file> child to the <resource> element.
  8084. $my_file = $xmldoc->createElement('file');
  8085. $my_file->setAttribute('href', $my_xml_file_path);
  8086. $my_resource->appendChild($my_file);
  8087. // Dependency to other files - not yet supported.
  8088. $i = 1;
  8089. foreach ($inc_docs as $doc_info) {
  8090. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8091. $my_dep = $xmldoc->createElement('resource');
  8092. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8093. $my_dep->setAttribute('identifier', $res_id);
  8094. $my_dep->setAttribute('type', 'webcontent');
  8095. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8096. $my_dep_file = $xmldoc->createElement('file');
  8097. // Check type of URL.
  8098. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8099. if ($doc_info[1] == 'remote') {
  8100. // Remote file. Save url as is.
  8101. $my_dep_file->setAttribute('href', $doc_info[0]);
  8102. $my_dep->setAttribute('xml:base', '');
  8103. } elseif ($doc_info[1] == 'local') {
  8104. switch ($doc_info[2]) {
  8105. case 'url': // Local URL - save path as url for now, don't zip file.
  8106. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8107. $current_dir = dirname($abs_path);
  8108. $current_dir = str_replace('\\', '/', $current_dir);
  8109. $file_path = realpath($abs_path);
  8110. $file_path = str_replace('\\', '/', $file_path);
  8111. $my_dep_file->setAttribute('href', $file_path);
  8112. $my_dep->setAttribute('xml:base', '');
  8113. if (strstr($file_path, $main_path) !== false) {
  8114. // The calculated real path is really inside Chamilo's root path.
  8115. // Reduce file path to what's under the DocumentRoot.
  8116. $file_path = substr($file_path, strlen($root_path) - 1);
  8117. //echo $file_path;echo '<br /><br />';
  8118. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8119. $zip_files_abs[] = $file_path;
  8120. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8121. $my_dep_file->setAttribute('href', $file_path);
  8122. $my_dep->setAttribute('xml:base', '');
  8123. } elseif (empty($file_path)) {
  8124. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8125. if (strpos($document_root, -1) == '/') {
  8126. $document_root = substr(0, -1, $document_root);
  8127. }*/
  8128. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8129. $file_path = str_replace('//', '/', $file_path);
  8130. if (file_exists($file_path)) {
  8131. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8132. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8133. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8134. $my_dep_file->setAttribute('href', $file_path);
  8135. $my_dep->setAttribute('xml:base', '');
  8136. }
  8137. }
  8138. break;
  8139. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8140. $my_dep_file->setAttribute('href', $doc_info[0]);
  8141. $my_dep->setAttribute('xml:base', '');
  8142. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8143. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8144. $abs_img_path_without_subdir = $doc_info[0];
  8145. $relp = api_get_path(REL_PATH); // The url-append config param.
  8146. $pos = strpos($abs_img_path_without_subdir, $relp);
  8147. if ($pos === 0) {
  8148. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8149. }
  8150. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8151. $file_path = str_replace('\\', '/', $file_path);
  8152. $file_path = str_replace('//', '/', $file_path);
  8153. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8154. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8155. // Check if the current document is in that path.
  8156. if (strstr($file_path, $cur_path) !== false) {
  8157. // The document is in that path, now get the relative path
  8158. // to the containing document.
  8159. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8160. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8161. $relative_path = '';
  8162. if (strstr($file_path, $cur_path) !== false) {
  8163. //$relative_path = substr($file_path, strlen($orig_file_path));
  8164. $relative_path = str_replace($cur_path, '', $file_path);
  8165. $file_path = substr($file_path, strlen($cur_path));
  8166. } else {
  8167. // This case is still a problem as it's difficult to calculate a relative path easily
  8168. // might still generate wrong links.
  8169. //$file_path = substr($file_path,strlen($cur_path));
  8170. // Calculate the directory path to the current file (without trailing slash).
  8171. $my_relative_path = dirname($file_path);
  8172. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8173. $my_relative_file = basename($file_path);
  8174. // Calculate the directory path to the containing file (without trailing slash).
  8175. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8176. $dotdot = '';
  8177. $subdir = '';
  8178. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8179. $my_relative_path2 = dirname($my_relative_path);
  8180. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8181. $my_orig_file_path = dirname($my_orig_file_path);
  8182. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8183. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8184. $dotdot += '../';
  8185. $my_relative_path = $my_relative_path2;
  8186. }
  8187. $relative_path = $dotdot.$subdir.$my_relative_file;
  8188. }
  8189. // Put the current document in the zip (this array is the array
  8190. // that will manage documents already in the course folder - relative).
  8191. $zip_files[] = $file_path;
  8192. // Update the links to the current document in the containing document (make them relative).
  8193. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8194. $my_dep_file->setAttribute('href', $file_path);
  8195. $my_dep->setAttribute('xml:base', '');
  8196. } elseif (strstr($file_path, $main_path) !== false) {
  8197. // The calculated real path is really inside Chamilo's root path.
  8198. // Reduce file path to what's under the DocumentRoot.
  8199. $file_path = substr($file_path, strlen($root_path));
  8200. //echo $file_path;echo '<br /><br />';
  8201. //error_log('Reduced path: '.$file_path, 0);
  8202. $zip_files_abs[] = $file_path;
  8203. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8204. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8205. $my_dep->setAttribute('xml:base', '');
  8206. } elseif (empty($file_path)) {
  8207. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8208. if(strpos($document_root,-1) == '/') {
  8209. $document_root = substr(0, -1, $document_root);
  8210. }*/
  8211. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8212. $file_path = str_replace('//', '/', $file_path);
  8213. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8214. $current_dir = dirname($abs_path);
  8215. $current_dir = str_replace('\\', '/', $current_dir);
  8216. if (file_exists($file_path)) {
  8217. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8218. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8219. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8220. $my_dep_file->setAttribute('href','document/'.$file_path);
  8221. $my_dep->setAttribute('xml:base', '');
  8222. }
  8223. }
  8224. break;
  8225. case 'rel':
  8226. // Path relative to the current document.
  8227. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8228. if (substr($doc_info[0], 0, 2) == '..') {
  8229. // Relative path going up.
  8230. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8231. $current_dir = str_replace('\\', '/', $current_dir);
  8232. $file_path = realpath($current_dir.$doc_info[0]);
  8233. $file_path = str_replace('\\', '/', $file_path);
  8234. //error_log($file_path.' <-> '.$main_path,0);
  8235. if (strstr($file_path, $main_path) !== false) {
  8236. // The calculated real path is really inside Chamilo's root path.
  8237. // Reduce file path to what's under the DocumentRoot.
  8238. $file_path = substr($file_path, strlen($root_path));
  8239. //error_log('Reduced path: '.$file_path, 0);
  8240. $zip_files_abs[] = $file_path;
  8241. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8242. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8243. $my_dep->setAttribute('xml:base', '');
  8244. }
  8245. } else {
  8246. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8247. $my_dep_file->setAttribute('href', $doc_info[0]);
  8248. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8249. }
  8250. break;
  8251. default:
  8252. $my_dep_file->setAttribute('href', $doc_info[0]);
  8253. $my_dep->setAttribute('xml:base', '');
  8254. break;
  8255. }
  8256. }
  8257. $my_dep->appendChild($my_dep_file);
  8258. $resources->appendChild($my_dep);
  8259. $dependency = $xmldoc->createElement('dependency');
  8260. $dependency->setAttribute('identifierref', $res_id);
  8261. $my_resource->appendChild($dependency);
  8262. $i++;
  8263. }
  8264. $resources->appendChild($my_resource);
  8265. $zip_files[] = $my_file_path;
  8266. } else {
  8267. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8268. switch ($item->type) {
  8269. case TOOL_LINK:
  8270. $my_item = $xmldoc->createElement('item');
  8271. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8272. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8273. $my_item->setAttribute('isvisible', 'true');
  8274. // Give a child element <title> to the <item> element.
  8275. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8276. $my_item->appendChild($my_title);
  8277. // Give a child element <adlcp:prerequisites> to the <item> element.
  8278. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8279. $my_prereqs->setAttribute('type', 'aicc_script');
  8280. $my_item->appendChild($my_prereqs);
  8281. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8282. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8283. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8284. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8285. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8286. //$xmldoc->createElement('adlcp:datafromlms', '');
  8287. // Give a child element <adlcp:masteryscore> to the <item> element.
  8288. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8289. $my_item->appendChild($my_masteryscore);
  8290. // Attach this item to the organization element or its parent if there is one.
  8291. if (!empty($item->parent) && $item->parent != 0) {
  8292. $children = $organization->childNodes;
  8293. for ($i = 0; $i < $children->length; $i++) {
  8294. $item_temp = $children->item($i);
  8295. if ($item_temp -> nodeName == 'item') {
  8296. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8297. $item_temp -> appendChild($my_item);
  8298. }
  8299. }
  8300. }
  8301. } else {
  8302. $organization->appendChild($my_item);
  8303. }
  8304. $my_file_path = 'link_'.$item->get_id().'.html';
  8305. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8306. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8307. $rs = Database::query($sql);
  8308. if ($link = Database :: fetch_array($rs)) {
  8309. $url = $link['url'];
  8310. $title = stripslashes($link['title']);
  8311. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8312. $my_xml_file_path = $my_file_path;
  8313. $my_sub_dir = dirname($my_file_path);
  8314. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8315. $my_xml_sub_dir = $my_sub_dir;
  8316. // Give a <resource> child to the <resources> element.
  8317. $my_resource = $xmldoc->createElement('resource');
  8318. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8319. $my_resource->setAttribute('type', 'webcontent');
  8320. $my_resource->setAttribute('href', $my_xml_file_path);
  8321. // adlcp:scormtype can be either 'sco' or 'asset'.
  8322. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8323. // xml:base is the base directory to find the files declared in this resource.
  8324. $my_resource->setAttribute('xml:base', '');
  8325. // give a <file> child to the <resource> element.
  8326. $my_file = $xmldoc->createElement('file');
  8327. $my_file->setAttribute('href', $my_xml_file_path);
  8328. $my_resource->appendChild($my_file);
  8329. $resources->appendChild($my_resource);
  8330. }
  8331. break;
  8332. case TOOL_QUIZ:
  8333. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8334. $exe = new Exercise();
  8335. $exe->read($exe_id);
  8336. $my_item = $xmldoc->createElement('item');
  8337. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8338. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8339. $my_item->setAttribute('isvisible', 'true');
  8340. // Give a child element <title> to the <item> element.
  8341. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8342. $my_item->appendChild($my_title);
  8343. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8344. //$my_item->appendChild($my_max_score);
  8345. // Give a child element <adlcp:prerequisites> to the <item> element.
  8346. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8347. $my_prereqs->setAttribute('type','aicc_script');
  8348. $my_item->appendChild($my_prereqs);
  8349. // Give a child element <adlcp:masteryscore> to the <item> element.
  8350. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8351. $my_item->appendChild($my_masteryscore);
  8352. // Attach this item to the organization element or hits parent if there is one.
  8353. if (!empty($item->parent) && $item->parent != 0) {
  8354. $children = $organization->childNodes;
  8355. for ($i = 0; $i < $children->length; $i++) {
  8356. $item_temp = $children->item($i);
  8357. if ($item_temp -> nodeName == 'item') {
  8358. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8359. $item_temp -> appendChild($my_item);
  8360. }
  8361. }
  8362. }
  8363. } else {
  8364. $organization->appendChild($my_item);
  8365. }
  8366. // Get the path of the file(s) from the course directory root
  8367. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8368. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8369. // Write the contents of the exported exercise into a (big) html file
  8370. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8371. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8372. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8373. $res = file_put_contents($tmp_file_path, $contents);
  8374. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8375. $files_cleanup[] = $tmp_file_path;
  8376. //error_log($tmp_path); die();
  8377. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8378. $my_xml_file_path = $my_file_path;
  8379. $my_sub_dir = dirname($my_file_path);
  8380. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8381. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8382. $my_xml_sub_dir = $my_sub_dir;
  8383. // Give a <resource> child to the <resources> element.
  8384. $my_resource = $xmldoc->createElement('resource');
  8385. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8386. $my_resource->setAttribute('type', 'webcontent');
  8387. $my_resource->setAttribute('href', $my_xml_file_path);
  8388. // adlcp:scormtype can be either 'sco' or 'asset'.
  8389. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8390. // xml:base is the base directory to find the files declared in this resource.
  8391. $my_resource->setAttribute('xml:base', '');
  8392. // Give a <file> child to the <resource> element.
  8393. $my_file = $xmldoc->createElement('file');
  8394. $my_file->setAttribute('href', $my_xml_file_path);
  8395. $my_resource->appendChild($my_file);
  8396. // Get included docs.
  8397. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8398. // Dependency to other files - not yet supported.
  8399. $i = 1;
  8400. foreach ($inc_docs as $doc_info) {
  8401. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8402. $my_dep = $xmldoc->createElement('resource');
  8403. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8404. $my_dep->setAttribute('identifier', $res_id);
  8405. $my_dep->setAttribute('type', 'webcontent');
  8406. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8407. $my_dep_file = $xmldoc->createElement('file');
  8408. // Check type of URL.
  8409. if ($doc_info[1] == 'remote') {
  8410. // Remote file. Save url as is.
  8411. $my_dep_file->setAttribute('href', $doc_info[0]);
  8412. $my_dep->setAttribute('xml:base', '');
  8413. } elseif ($doc_info[1] == 'local') {
  8414. switch ($doc_info[2]) {
  8415. case 'url': // Local URL - save path as url for now, don't zip file.
  8416. // Save file but as local file (retrieve from URL).
  8417. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8418. $current_dir = dirname($abs_path);
  8419. $current_dir = str_replace('\\', '/', $current_dir);
  8420. $file_path = realpath($abs_path);
  8421. $file_path = str_replace('\\', '/', $file_path);
  8422. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8423. $my_dep->setAttribute('xml:base', '');
  8424. if (strstr($file_path, $main_path) !== false) {
  8425. // The calculated real path is really inside the chamilo root path.
  8426. // Reduce file path to what's under the DocumentRoot.
  8427. $file_path = substr($file_path, strlen($root_path));
  8428. //echo $file_path;echo '<br /><br />';
  8429. //error_log('Reduced path: '.$file_path, 0);
  8430. $zip_files_abs[] = $file_path;
  8431. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8432. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8433. $my_dep->setAttribute('xml:base', '');
  8434. } elseif (empty($file_path)) {
  8435. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8436. if (strpos($document_root,-1) == '/') {
  8437. $document_root = substr(0, -1, $document_root);
  8438. }*/
  8439. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8440. $file_path = str_replace('//', '/', $file_path);
  8441. if (file_exists($file_path)) {
  8442. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8443. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8444. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8445. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8446. $my_dep->setAttribute('xml:base', '');
  8447. }
  8448. }
  8449. break;
  8450. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8451. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8452. $current_dir = str_replace('\\', '/', $current_dir);
  8453. $file_path = realpath($doc_info[0]);
  8454. $file_path = str_replace('\\', '/', $file_path);
  8455. $my_dep_file->setAttribute('href', $file_path);
  8456. $my_dep->setAttribute('xml:base', '');
  8457. if (strstr($file_path,$main_path) !== false) {
  8458. // The calculated real path is really inside the chamilo root path.
  8459. // Reduce file path to what's under the DocumentRoot.
  8460. $file_path = substr($file_path, strlen($root_path));
  8461. //echo $file_path;echo '<br /><br />';
  8462. //error_log('Reduced path: '.$file_path, 0);
  8463. $zip_files_abs[] = $file_path;
  8464. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8465. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8466. $my_dep->setAttribute('xml:base', '');
  8467. } elseif (empty($file_path)) {
  8468. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8469. if (strpos($document_root,-1) == '/') {
  8470. $document_root = substr(0, -1, $document_root);
  8471. }*/
  8472. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8473. $file_path = str_replace('//', '/', $file_path);
  8474. if (file_exists($file_path)) {
  8475. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8476. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8477. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8478. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8479. $my_dep->setAttribute('xml:base', '');
  8480. }
  8481. }
  8482. break;
  8483. 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
  8484. if (substr($doc_info[0], 0, 2) == '..') {
  8485. // Relative path going up.
  8486. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8487. $current_dir = str_replace('\\', '/', $current_dir);
  8488. $file_path = realpath($current_dir.$doc_info[0]);
  8489. $file_path = str_replace('\\', '/', $file_path);
  8490. //error_log($file_path.' <-> '.$main_path, 0);
  8491. if (strstr($file_path, $main_path) !== false) {
  8492. // The calculated real path is really inside Chamilo's root path.
  8493. // Reduce file path to what's under the DocumentRoot.
  8494. $file_path = substr($file_path, strlen($root_path));
  8495. $file_path_dest = $file_path;
  8496. // File path is courses/CHAMILO/document/....
  8497. $info_file_path = explode('/', $file_path);
  8498. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8499. $file_path_dest = 'document/'.$file_path;
  8500. }
  8501. //error_log('Reduced path: '.$file_path, 0);
  8502. $zip_files_abs[] = $file_path;
  8503. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8504. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8505. $my_dep->setAttribute('xml:base', '');
  8506. }
  8507. } else {
  8508. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8509. $my_dep_file->setAttribute('href', $doc_info[0]);
  8510. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8511. }
  8512. break;
  8513. default:
  8514. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8515. $my_dep->setAttribute('xml:base', '');
  8516. break;
  8517. }
  8518. }
  8519. $my_dep->appendChild($my_dep_file);
  8520. $resources->appendChild($my_dep);
  8521. $dependency = $xmldoc->createElement('dependency');
  8522. $dependency->setAttribute('identifierref', $res_id);
  8523. $my_resource->appendChild($dependency);
  8524. $i++;
  8525. }
  8526. $resources->appendChild($my_resource);
  8527. $zip_files[] = $my_file_path;
  8528. break;
  8529. default:
  8530. // Get the path of the file(s) from the course directory root
  8531. $my_file_path = 'non_exportable.html';
  8532. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8533. $my_xml_file_path = $my_file_path;
  8534. $my_sub_dir = dirname($my_file_path);
  8535. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8536. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8537. $my_xml_sub_dir = $my_sub_dir;
  8538. // Give a <resource> child to the <resources> element.
  8539. $my_resource = $xmldoc->createElement('resource');
  8540. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8541. $my_resource->setAttribute('type', 'webcontent');
  8542. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8543. // adlcp:scormtype can be either 'sco' or 'asset'.
  8544. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8545. // xml:base is the base directory to find the files declared in this resource.
  8546. $my_resource->setAttribute('xml:base', '');
  8547. // Give a <file> child to the <resource> element.
  8548. $my_file = $xmldoc->createElement('file');
  8549. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8550. $my_resource->appendChild($my_file);
  8551. $resources->appendChild($my_resource);
  8552. break;
  8553. }
  8554. }
  8555. }
  8556. $organizations->appendChild($organization);
  8557. $root->appendChild($organizations);
  8558. $root->appendChild($resources);
  8559. $xmldoc->appendChild($root);
  8560. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8561. // then add the file to the zip, then destroy the file (this is done automatically).
  8562. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8563. foreach ($zip_files as $file_path) {
  8564. if (empty($file_path)) {
  8565. continue;
  8566. }
  8567. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8568. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8569. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8570. }
  8571. $this->create_path($dest_file);
  8572. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8573. // Check if the file needs a link update.
  8574. if (in_array($file_path, array_keys($link_updates))) {
  8575. $string = file_get_contents($dest_file);
  8576. unlink($dest_file);
  8577. foreach ($link_updates[$file_path] as $old_new) {
  8578. // This is an ugly hack that allows .flv files to be found by the flv player that
  8579. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8580. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8581. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8582. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8583. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8584. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8585. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8586. }
  8587. //Fix to avoid problems with default_course_document
  8588. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8589. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8590. $newDestination = $old_new['dest'];
  8591. } else {
  8592. $newDestination = str_replace('document/', '', $old_new['dest']);
  8593. }
  8594. $string = str_replace($old_new['orig'], $newDestination, $string);
  8595. //Add files inside the HTMLs
  8596. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8597. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8598. if (file_exists($sys_course_path.$new_path)) {
  8599. copy($sys_course_path.$new_path, $destinationFile);
  8600. }
  8601. }
  8602. file_put_contents($dest_file, $string);
  8603. }
  8604. }
  8605. foreach ($zip_files_abs as $file_path) {
  8606. if (empty($file_path)) {
  8607. continue;
  8608. }
  8609. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8610. continue;
  8611. }
  8612. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8613. $this->create_path($dest_file);
  8614. copy($main_path.$file_path, $dest_file);
  8615. // Check if the file needs a link update.
  8616. if (in_array($file_path, array_keys($link_updates))) {
  8617. $string = file_get_contents($dest_file);
  8618. unlink($dest_file);
  8619. foreach ($link_updates[$file_path] as $old_new) {
  8620. // This is an ugly hack that allows .flv files to be found by the flv player that
  8621. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8622. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8623. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8624. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8625. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8626. }
  8627. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8628. }
  8629. file_put_contents($dest_file, $string);
  8630. }
  8631. }
  8632. if (is_array($links_to_create)) {
  8633. foreach ($links_to_create as $file => $link) {
  8634. $file_content = '<!DOCTYPE html><head>
  8635. <meta charset="'.api_get_language_isocode().'" />
  8636. <title>'.$link['title'].'</title>
  8637. </head>
  8638. <body dir="'.api_get_text_direction().'">
  8639. <div style="text-align:center">
  8640. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8641. </body>
  8642. </html>';
  8643. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8644. }
  8645. }
  8646. // Add non exportable message explanation.
  8647. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8648. $file_content = '<!DOCTYPE html><head>
  8649. <meta charset="'.api_get_language_isocode().'" />
  8650. <title>'.$lang_not_exportable.'</title>
  8651. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8652. </head>
  8653. <body dir="'.api_get_text_direction().'">';
  8654. $file_content .=
  8655. <<<EOD
  8656. <style>
  8657. .error-message {
  8658. font-family: arial, verdana, helvetica, sans-serif;
  8659. border-width: 1px;
  8660. border-style: solid;
  8661. left: 50%;
  8662. margin: 10px auto;
  8663. min-height: 30px;
  8664. padding: 5px;
  8665. right: 50%;
  8666. width: 500px;
  8667. background-color: #FFD1D1;
  8668. border-color: #FF0000;
  8669. color: #000;
  8670. }
  8671. </style>
  8672. <body>
  8673. <div class="error-message">
  8674. $lang_not_exportable
  8675. </div>
  8676. </body>
  8677. </html>
  8678. EOD;
  8679. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8680. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8681. }
  8682. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8683. // Add the extra files that go along with a SCORM package.
  8684. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8685. $extra_files = scandir($main_code_path);
  8686. foreach ($extra_files as $extra_file) {
  8687. if (strpos($extra_file, '.') === 0)
  8688. continue;
  8689. else {
  8690. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8691. $this->create_path($dest_file);
  8692. copy($main_code_path.$extra_file, $dest_file);
  8693. }
  8694. }
  8695. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8696. $manifest = @$xmldoc->saveXML();
  8697. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8698. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8699. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8700. // Clean possible temporary files.
  8701. foreach ($files_cleanup as $file) {
  8702. $res = unlink($file);
  8703. if ($res === false) {
  8704. error_log(
  8705. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  8706. 0
  8707. );
  8708. }
  8709. }
  8710. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  8711. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8712. }
  8713. /**
  8714. * @param int $lp_id
  8715. * @return bool
  8716. */
  8717. public function scorm_export_to_pdf($lp_id)
  8718. {
  8719. $lp_id = intval($lp_id);
  8720. $files_to_export = array();
  8721. $course_data = api_get_course_info($this->cc);
  8722. if (!empty($course_data)) {
  8723. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8724. $list = self::get_flat_ordered_items_list($lp_id);
  8725. if (!empty($list)) {
  8726. foreach ($list as $item_id) {
  8727. $item = $this->items[$item_id];
  8728. switch ($item->type) {
  8729. case 'document':
  8730. //Getting documents from a LP with chamilo documents
  8731. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8732. // Try loading document from the base course.
  8733. if (empty($file_data) && !empty($sessionId)) {
  8734. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  8735. }
  8736. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8737. if (file_exists($file_path)) {
  8738. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8739. }
  8740. break;
  8741. case 'asset': //commes from a scorm package generated by chamilo
  8742. case 'sco':
  8743. $file_path = $scorm_path.'/'.$item->path;
  8744. if (file_exists($file_path)) {
  8745. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8746. }
  8747. break;
  8748. case 'dokeos_chapter':
  8749. case 'dir':
  8750. case 'chapter':
  8751. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8752. break;
  8753. }
  8754. }
  8755. }
  8756. $pdf = new PDF();
  8757. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8758. return $result;
  8759. }
  8760. return false;
  8761. }
  8762. /**
  8763. * Temp function to be moved in main_api or the best place around for this.
  8764. * Creates a file path if it doesn't exist
  8765. * @param string $path
  8766. */
  8767. public function create_path($path)
  8768. {
  8769. $path_bits = explode('/', dirname($path));
  8770. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8771. $path_built = IS_WINDOWS_OS ? '' : '/';
  8772. foreach ($path_bits as $bit) {
  8773. if (!empty ($bit)) {
  8774. $new_path = $path_built . $bit;
  8775. if (is_dir($new_path)) {
  8776. $path_built = $new_path . '/';
  8777. } else {
  8778. mkdir($new_path, api_get_permissions_for_new_directories());
  8779. $path_built = $new_path . '/';
  8780. }
  8781. }
  8782. }
  8783. }
  8784. /**
  8785. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8786. * @return boolean The results of the unlink function, or false if there was no image to start with
  8787. */
  8788. public function delete_lp_image()
  8789. {
  8790. $img = $this->get_preview_image();
  8791. if ($img != '') {
  8792. $del_file = $this->get_preview_image_path(null, 'sys');
  8793. if (isset($del_file) && file_exists($del_file)) {
  8794. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8795. if (file_exists($del_file_2)) {
  8796. unlink($del_file_2);
  8797. }
  8798. $this->set_preview_image('');
  8799. return @unlink($del_file);
  8800. }
  8801. }
  8802. return false;
  8803. }
  8804. /**
  8805. * Uploads an author image to the upload/learning_path/images directory
  8806. * @param array The image array, coming from the $_FILES superglobal
  8807. * @return boolean True on success, false on error
  8808. */
  8809. public function upload_image($image_array)
  8810. {
  8811. $image_moved = false;
  8812. if (!empty ($image_array['name'])) {
  8813. $upload_ok = process_uploaded_file($image_array);
  8814. $has_attachment = true;
  8815. } else {
  8816. $image_moved = true;
  8817. }
  8818. if ($upload_ok) {
  8819. if ($has_attachment) {
  8820. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8821. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8822. $updir = $sys_course_path . $courseDir;
  8823. // Try to add an extension to the file if it hasn't one.
  8824. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8825. if (!filter_extension($new_file_name)) {
  8826. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8827. $image_moved = false;
  8828. } else {
  8829. $file_extension = explode('.', $image_array['name']);
  8830. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8831. $filename = uniqid('');
  8832. $new_file_name = $filename.'.'.$file_extension;
  8833. $new_path = $updir.'/'.$new_file_name;
  8834. // Resize the image.
  8835. $temp = new Image($image_array['tmp_name']);
  8836. $picture_infos = $temp->get_image_info();
  8837. if ($picture_infos['width'] > 104) {
  8838. $thumbwidth = 104;
  8839. } else {
  8840. $thumbwidth = $picture_infos['width'];
  8841. }
  8842. if ($picture_infos['height'] > 96) {
  8843. $new_height = 96;
  8844. } else {
  8845. $new_height = $picture_infos['height'];
  8846. }
  8847. $temp->resize($thumbwidth, $new_height, 0);
  8848. $result = $temp->send_image($new_path);
  8849. // Storing the image filename.
  8850. if ($result) {
  8851. $image_moved = true;
  8852. $this->set_preview_image($new_file_name);
  8853. //Resize to 64px to use on course homepage
  8854. $temp->resize(64, 64, 0);
  8855. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8856. return true;
  8857. }
  8858. }
  8859. }
  8860. }
  8861. return false;
  8862. }
  8863. /**
  8864. * @param int $lp_id
  8865. * @param string $status
  8866. */
  8867. public function set_autolaunch($lp_id, $status)
  8868. {
  8869. $course_id = api_get_course_int_id();
  8870. $lp_id = intval($lp_id);
  8871. $status = intval($status);
  8872. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8873. // Setting everything to autolaunch = 0
  8874. $attributes['autolaunch'] = 0;
  8875. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8876. Database::update($lp_table, $attributes, $where);
  8877. if ($status == 1) {
  8878. //Setting my lp_id to autolaunch = 1
  8879. $attributes['autolaunch'] = 1;
  8880. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8881. Database::update($lp_table, $attributes, $where );
  8882. }
  8883. }
  8884. /**
  8885. * Gets previous_item_id for the next element of the lp_item table
  8886. * @author Isaac flores paz
  8887. * @return integer Previous item ID
  8888. */
  8889. public function select_previous_item_id()
  8890. {
  8891. $course_id = api_get_course_int_id();
  8892. if ($this->debug > 0) {
  8893. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8894. }
  8895. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8896. // Get the max order of the items
  8897. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8898. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8899. $rs_max_order = Database::query($sql_max_order);
  8900. $row_max_order = Database::fetch_object($rs_max_order);
  8901. $max_order = $row_max_order->display_order;
  8902. // Get the previous item ID
  8903. $sql = "SELECT id as previous FROM $table_lp_item
  8904. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8905. $rs_max = Database::query($sql);
  8906. $row_max = Database::fetch_object($rs_max);
  8907. // Return the previous item ID
  8908. return $row_max->previous;
  8909. }
  8910. /**
  8911. * Copies an LP
  8912. */
  8913. public function copy()
  8914. {
  8915. $main_path = api_get_path(SYS_CODE_PATH);
  8916. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8917. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8918. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8919. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8920. //Course builder
  8921. $cb = new CourseBuilder();
  8922. //Setting tools that will be copied
  8923. $cb->set_tools_to_build(array('learnpaths'));
  8924. //Setting elements that will be copied
  8925. $cb->set_tools_specific_id_list(
  8926. array('learnpaths' => array($this->lp_id))
  8927. );
  8928. $course = $cb->build();
  8929. //Course restorer
  8930. $course_restorer = new CourseRestorer($course);
  8931. $course_restorer->set_add_text_in_items(true);
  8932. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8933. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8934. }
  8935. public function verify_document_size($s)
  8936. {
  8937. $post_max = ini_get('post_max_size');
  8938. if (substr($post_max, -1, 1) == 'M') {
  8939. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  8940. } elseif (substr($post_max, -1, 1) == 'G') {
  8941. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  8942. }
  8943. $upl_max = ini_get('upload_max_filesize');
  8944. if (substr($upl_max, -1, 1) == 'M') {
  8945. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  8946. } elseif (substr($upl_max, -1, 1) == 'G') {
  8947. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  8948. }
  8949. $documents_total_space = DocumentManager::documents_total_space();
  8950. $course_max_space = DocumentManager::get_course_quota();
  8951. $total_size = filesize($s) + $documents_total_space;
  8952. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8953. return true;
  8954. } else{
  8955. return false;
  8956. }
  8957. }
  8958. /**
  8959. * Clear LP prerequisites
  8960. */
  8961. public function clear_prerequisites()
  8962. {
  8963. $course_id = $this->get_course_int_id();
  8964. if ($this->debug > 0) {
  8965. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8966. }
  8967. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8968. $lp_id = $this->get_id();
  8969. //Cleaning prerequisites
  8970. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8971. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8972. Database::query($sql);
  8973. //Cleaning mastery score for exercises
  8974. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8975. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8976. Database::query($sql);
  8977. }
  8978. public function set_previous_step_as_prerequisite_for_all_items()
  8979. {
  8980. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8981. $course_id = $this->get_course_int_id();
  8982. $lp_id = $this->get_id();
  8983. if (!empty($this->items)) {
  8984. $previous_item_id = null;
  8985. $previous_item_max = 0;
  8986. $previous_item_type = null;
  8987. $last_item_not_chapter = null;
  8988. $last_item_not_chapter_type = null;
  8989. $last_item_not_chapter_max = null;
  8990. foreach ($this->items as $item) {
  8991. // if there was a previous item... (otherwise jump to set it)
  8992. if (!empty($previous_item_id)) {
  8993. $current_item_id = $item->get_id(); //save current id
  8994. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8995. // Current item is not a folder, so it qualifies to get a prerequisites
  8996. if ($last_item_not_chapter_type == 'quiz') {
  8997. // if previous is quiz, mark its max score as default score to be achieved
  8998. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  8999. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9000. Database::query($sql);
  9001. }
  9002. // now simply update the prerequisite to set it to the last non-chapter item
  9003. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9004. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9005. Database::query($sql);
  9006. // record item as 'non-chapter' reference
  9007. $last_item_not_chapter = $item->get_id();
  9008. $last_item_not_chapter_type = $item->get_type();
  9009. $last_item_not_chapter_max = $item->get_max();
  9010. }
  9011. } else {
  9012. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9013. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9014. $last_item_not_chapter = $item->get_id();
  9015. $last_item_not_chapter_type = $item->get_type();
  9016. $last_item_not_chapter_max = $item->get_max();
  9017. }
  9018. }
  9019. // Saving the item as "previous item" for the next loop
  9020. $previous_item_id = $item->get_id();
  9021. $previous_item_max = $item->get_max();
  9022. $previous_item_type = $item->get_type();
  9023. }
  9024. }
  9025. }
  9026. /**
  9027. * @param array $params
  9028. */
  9029. public static function createCategory($params)
  9030. {
  9031. $em = Database::getManager();
  9032. $item = new CLpCategory();
  9033. $item->setName($params['name']);
  9034. $item->setCId($params['c_id']);
  9035. $em->persist($item);
  9036. $em->flush();
  9037. }
  9038. /**
  9039. * @param array $params
  9040. */
  9041. public static function updateCategory($params)
  9042. {
  9043. $em = Database::getManager();
  9044. /** @var CLpCategory $item */
  9045. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9046. if ($item) {
  9047. $item->setName($params['name']);
  9048. $em->merge($item);
  9049. $em->flush();
  9050. }
  9051. }
  9052. /**
  9053. * @param int $id
  9054. */
  9055. public static function moveUpCategory($id)
  9056. {
  9057. $em = Database::getManager();
  9058. /** @var CLpCategory $item */
  9059. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9060. if ($item) {
  9061. $position = $item->getPosition() - 1;
  9062. $item->setPosition($position);
  9063. $em->persist($item);
  9064. $em->flush();
  9065. }
  9066. }
  9067. /**
  9068. * @param int $id
  9069. */
  9070. public static function moveDownCategory($id)
  9071. {
  9072. $em = Database::getManager();
  9073. /** @var CLpCategory $item */
  9074. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9075. if ($item) {
  9076. $position = $item->getPosition() + 1;
  9077. $item->setPosition($position);
  9078. $em->persist($item);
  9079. $em->flush();
  9080. }
  9081. }
  9082. /**
  9083. * @param int $courseId
  9084. * @return int|mixed
  9085. */
  9086. public static function getCountCategories($courseId)
  9087. {
  9088. if (empty($course_id)) {
  9089. return 0;
  9090. }
  9091. $em = Database::getManager();
  9092. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9093. $query->setParameter('id', $courseId);
  9094. return $query->getSingleScalarResult();
  9095. }
  9096. /**
  9097. * @param int $courseId
  9098. *
  9099. * @return mixed
  9100. */
  9101. public static function getCategories($courseId)
  9102. {
  9103. $em = Database::getManager();
  9104. //Default behaviour
  9105. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9106. array('cId' => $course_id),
  9107. array('name' => 'ASC')
  9108. );*/
  9109. // Using doctrine extensions
  9110. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9111. array('cId' => $courseId)
  9112. )->getResult();
  9113. return $items;
  9114. }
  9115. /**
  9116. * @param int $id
  9117. *
  9118. * @return mixed
  9119. */
  9120. public static function getCategory($id)
  9121. {
  9122. $em = Database::getManager();
  9123. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9124. return $item;
  9125. }
  9126. /**
  9127. * @param int $courseId
  9128. * @return array
  9129. */
  9130. public static function getCategoryByCourse($courseId)
  9131. {
  9132. $em = Database::getManager();
  9133. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9134. return $items;
  9135. }
  9136. /**
  9137. * @param int $id
  9138. *
  9139. * @return mixed
  9140. */
  9141. public static function deleteCategory($id)
  9142. {
  9143. $em = Database::getManager();
  9144. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9145. if ($item) {
  9146. $courseId = $item->getCId();
  9147. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9148. $query->setParameter('id', $courseId);
  9149. $query->setParameter('catId', $item->getId());
  9150. $lps = $query->getResult();
  9151. // Setting category = 0.
  9152. if ($lps) {
  9153. foreach ($lps as $lpItem) {
  9154. $lpItem->setCategoryId(0);
  9155. }
  9156. }
  9157. // Removing category.
  9158. $em->remove($item);
  9159. $em->flush();
  9160. }
  9161. }
  9162. /**
  9163. * @param int $courseId
  9164. * @param bool $addSelectOption
  9165. *
  9166. * @return mixed
  9167. */
  9168. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9169. {
  9170. $items = self::getCategoryByCourse($courseId);
  9171. $cats = array();
  9172. if ($addSelectOption) {
  9173. $cats = array(get_lang('SelectACategory'));
  9174. }
  9175. if (!empty($items)) {
  9176. foreach ($items as $cat) {
  9177. $cats[$cat->getId()] = $cat->getName();
  9178. }
  9179. }
  9180. return $cats;
  9181. }
  9182. /**
  9183. * Return the scorm item type object with spaces replaced with _
  9184. * The return result is use to build a css classname like scorm_type_$return
  9185. * @param $in_type
  9186. * @return mixed
  9187. */
  9188. private static function format_scorm_type_item($in_type)
  9189. {
  9190. return str_replace(' ', '_', $in_type);
  9191. }
  9192. /**
  9193. * @return \learnpath
  9194. */
  9195. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9196. {
  9197. $lpObject = Session::read('lpobject');
  9198. $learnPath = null;
  9199. if (isset($lpObject)) {
  9200. $learnPath = unserialize($lpObject);
  9201. }
  9202. if (!is_object($learnPath)) {
  9203. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9204. }
  9205. return $learnPath;
  9206. }
  9207. /**
  9208. * @param int $itemId
  9209. * @return learnpathItem|false
  9210. */
  9211. public function getItem($itemId)
  9212. {
  9213. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9214. return $this->items[$itemId];
  9215. }
  9216. return false;
  9217. }
  9218. /**
  9219. * @return int
  9220. */
  9221. public function getCategoryId()
  9222. {
  9223. return $this->categoryId;
  9224. }
  9225. /**
  9226. * @param int $categoryId
  9227. * @return bool
  9228. */
  9229. public function setCategoryId($categoryId)
  9230. {
  9231. $this->categoryId = intval($categoryId);
  9232. $courseId = api_get_course_int_id();
  9233. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9234. $lp_id = $this->get_id();
  9235. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9236. WHERE c_id = $courseId AND id = $lp_id";
  9237. Database::query($sql);
  9238. return true;
  9239. }
  9240. /**
  9241. * Get whether this is a learning path with the possibility to subscribe
  9242. * users or not
  9243. * @return int
  9244. */
  9245. public function getSubscribeUsers()
  9246. {
  9247. return $this->subscribeUsers;
  9248. }
  9249. /**
  9250. * Set whether this is a learning path with the possibility to subscribe
  9251. * users or not
  9252. * @param int $subscribeUsers (0 = false, 1 = true)
  9253. */
  9254. public function setSubscribeUsers($value)
  9255. {
  9256. if ($this->debug > 0) {
  9257. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9258. }
  9259. $this->subscribeUsers = intval($value);;
  9260. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9261. $lp_id = $this->get_id();
  9262. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9263. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9264. Database::query($sql);
  9265. return true;
  9266. }
  9267. /**
  9268. * Calculate the count of stars for a user in this LP
  9269. * @param int $sessionId Optional. The session ID
  9270. * @return int The count of stars
  9271. */
  9272. public function getCalculateStars($sessionId = 0)
  9273. {
  9274. $stars = 0;
  9275. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9276. if ($progress > 50) {
  9277. $stars++;
  9278. }
  9279. // Calculate stars chapters evaluation
  9280. $exercisesItems = $this->getExercisesItems();
  9281. if (!empty($exercisesItems)) {
  9282. $totalResult = 0;
  9283. foreach ($exercisesItems as $exerciseItem) {
  9284. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9285. $this->user_id,
  9286. $exerciseItem->path,
  9287. $this->course_int_id,
  9288. $sessionId,
  9289. $this->lp_id,
  9290. $exerciseItem->db_id
  9291. );
  9292. $exerciseResult = 0;
  9293. foreach ($exerciseResultInfo as $result) {
  9294. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9295. }
  9296. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9297. $totalResult += $exerciseAverage;
  9298. }
  9299. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9300. if ($totalExerciseAverage >= 50) {
  9301. $stars++;
  9302. }
  9303. if ($totalExerciseAverage >= 80) {
  9304. $stars++;
  9305. }
  9306. }
  9307. // Calculate star for final evaluation
  9308. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9309. if (!empty($finalEvaluationItem)) {
  9310. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9311. $this->user_id,
  9312. $finalEvaluationItem->path,
  9313. $this->course_int_id,
  9314. $sessionId,
  9315. $this->lp_id,
  9316. $finalEvaluationItem->db_id
  9317. );
  9318. $evaluationResult = 0;
  9319. foreach ($evaluationResultInfo as $result) {
  9320. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9321. }
  9322. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9323. $evaluationAverage = $evaluationResult / $averageDivisor;
  9324. if ($evaluationAverage >= 80) {
  9325. $stars++;
  9326. }
  9327. }
  9328. return $stars;
  9329. }
  9330. /**
  9331. * Get the items of exercise type
  9332. * @return array The items. Otherwise return false
  9333. */
  9334. public function getExercisesItems()
  9335. {
  9336. $exercises = [];
  9337. foreach ($this->items as $item) {
  9338. if ($item->type != 'quiz') {
  9339. continue;
  9340. }
  9341. $exercises[] = $item;
  9342. }
  9343. array_pop($exercises);
  9344. return $exercises;
  9345. }
  9346. /**
  9347. * Get the item of exercise type (evaluation type)
  9348. * @return array The final evaluation. Otherwise return false
  9349. */
  9350. public function getFinalEvaluationItem()
  9351. {
  9352. $exercises = [];
  9353. foreach ($this->items as $item) {
  9354. if ($item->type != 'quiz') {
  9355. continue;
  9356. }
  9357. $exercises[] = $item;
  9358. }
  9359. return array_pop($exercises);
  9360. }
  9361. /**
  9362. * Calculate the total points achieved for the current user in this learning path
  9363. * @param int $sessionId Optional. The session Id
  9364. * @return int
  9365. */
  9366. public function getCalculateScore($sessionId = 0)
  9367. {
  9368. // Calculate stars chapters evaluation
  9369. $exercisesItems = $this->getExercisesItems();
  9370. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9371. $totalExercisesResult = 0;
  9372. $totalEvaluationResult = 0;
  9373. if ($exercisesItems !== false) {
  9374. foreach ($exercisesItems as $exerciseItem) {
  9375. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9376. $this->user_id,
  9377. $exerciseItem->path,
  9378. $this->course_int_id,
  9379. $sessionId,
  9380. $this->lp_id,
  9381. $exerciseItem->db_id
  9382. );
  9383. $exerciseResult = 0;
  9384. foreach ($exerciseResultInfo as $result) {
  9385. $exerciseResult += $result['exe_result'];
  9386. }
  9387. $totalExercisesResult += $exerciseResult;
  9388. }
  9389. }
  9390. if (!empty($finalEvaluationItem)) {
  9391. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9392. $this->user_id,
  9393. $finalEvaluationItem->path,
  9394. $this->course_int_id,
  9395. $sessionId,
  9396. $this->lp_id,
  9397. $finalEvaluationItem->db_id
  9398. );
  9399. foreach ($evaluationResultInfo as $result) {
  9400. $totalEvaluationResult += $result['exe_result'];
  9401. }
  9402. }
  9403. return $totalExercisesResult + $totalEvaluationResult;
  9404. }
  9405. }
  9406. if (!function_exists('trim_value')) {
  9407. function trim_value(& $value) {
  9408. $value = trim($value);
  9409. }
  9410. }