learnpath.class.php 480 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use Gedmo\Sortable\Entity\Repository\SortableRepository;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Finder\Finder;
  10. use Chamilo\CourseBundle\Entity\CLp;
  11. /**
  12. * Class learnpath
  13. * This class defines the parent attributes and methods for Chamilo learnpaths
  14. * and SCORM learnpaths. It is used by the scorm class.
  15. *
  16. * @package chamilo.learnpath
  17. * @author Yannick Warnier <ywarnier@beeznest.org>
  18. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  19. */
  20. class learnpath
  21. {
  22. public $attempt = 0; // The number for the current ID view.
  23. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  24. public $current; // Id of the current item the user is viewing.
  25. public $current_score; // The score of the current item.
  26. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  27. public $current_time_stop; // The time the user closed this resource.
  28. public $default_status = 'not attempted';
  29. public $encoding = 'UTF-8';
  30. public $error = '';
  31. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  32. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  33. public $index; // The index of the active learnpath_item in $ordered_items array.
  34. public $items = array();
  35. public $last; // item_id of last item viewed in the learning path.
  36. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  37. public $license; // Which license this course has been given - not used yet on 20060522.
  38. public $lp_id; // DB ID for this learnpath.
  39. public $lp_view_id; // DB ID for lp_view
  40. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  41. public $message = '';
  42. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  43. public $name; // Learnpath name (they generally have one).
  44. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  45. public $path = ''; // Path inside the scorm directory (if scorm).
  46. public $theme; // The current theme of the learning path.
  47. public $preview_image; // The current image of the learning path.
  48. public $accumulateScormTime; // Flag to decide whether to accumulate SCORM time or not
  49. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  50. public $prevent_reinit = 1;
  51. // Describes the mode of progress bar display.
  52. public $seriousgame_mode = 0;
  53. public $progress_bar_mode = '%';
  54. // Percentage progress as saved in the db.
  55. public $progress_db = '0';
  56. public $proximity; // Wether the content is distant or local or unknown.
  57. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  58. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  59. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  60. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  61. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  62. public $user_id; //ID of the user that is viewing/using the course
  63. public $update_queue = array();
  64. public $scorm_debug = 0;
  65. public $arrMenu = array(); // Array for the menu items.
  66. public $debug = 0; // Logging level.
  67. public $lp_session_id = 0;
  68. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  69. public $prerequisite = 0;
  70. public $use_max_score = 1; // 1 or 0
  71. public $subscribeUsers = 0; // Subscribe users or not
  72. public $created_on = '';
  73. public $modified_on = '';
  74. public $publicated_on = '';
  75. public $expired_on = '';
  76. public $ref = null;
  77. public $course_int_id;
  78. public $course_info = array();
  79. public $categoryId;
  80. /**
  81. * Constructor.
  82. * Needs a database handler, a course code and a learnpath id from the database.
  83. * Also builds the list of items into $this->items.
  84. * @param string $course Course code
  85. * @param integer $lp_id
  86. * @param integer $user_id
  87. */
  88. public function __construct($course, $lp_id, $user_id)
  89. {
  90. $this->encoding = api_get_system_encoding();
  91. if ($this->debug > 0) {
  92. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  93. }
  94. if (empty($course)) {
  95. $course = api_get_course_id();
  96. }
  97. $course_info = api_get_course_info($course);
  98. if (!empty($course_info)) {
  99. $this->cc = $course_info['code'];
  100. $this->course_info = $course_info;
  101. $course_id = $course_info['real_id'];
  102. } else {
  103. $this->error = 'Course code does not exist in database.';
  104. }
  105. $this->set_course_int_id($course_id);
  106. $lp_id = (int) $lp_id;
  107. // Check learnpath ID.
  108. if (empty($lp_id)) {
  109. $this->error = 'Learnpath ID is empty';
  110. } else {
  111. // TODO: Make it flexible to use any course_code (still using env course code here).
  112. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  113. $sql = "SELECT * FROM $lp_table
  114. WHERE id = '$lp_id' AND c_id = $course_id";
  115. if ($this->debug > 2) {
  116. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  117. }
  118. $res = Database::query($sql);
  119. if (Database::num_rows($res) > 0) {
  120. $this->lp_id = $lp_id;
  121. $row = Database::fetch_array($res);
  122. $this->type = $row['lp_type'];
  123. $this->name = stripslashes($row['name']);
  124. $this->proximity = $row['content_local'];
  125. $this->theme = $row['theme'];
  126. $this->maker = $row['content_maker'];
  127. $this->prevent_reinit = $row['prevent_reinit'];
  128. $this->seriousgame_mode = $row['seriousgame_mode'];
  129. $this->license = $row['content_license'];
  130. $this->scorm_debug = $row['debug'];
  131. $this->js_lib = $row['js_lib'];
  132. $this->path = $row['path'];
  133. $this->preview_image = $row['preview_image'];
  134. $this->author = $row['author'];
  135. $this->hide_toc_frame = $row['hide_toc_frame'];
  136. $this->lp_session_id = $row['session_id'];
  137. $this->use_max_score = $row['use_max_score'];
  138. $this->subscribeUsers = $row['subscribe_users'];
  139. $this->created_on = $row['created_on'];
  140. $this->modified_on = $row['modified_on'];
  141. $this->ref = $row['ref'];
  142. $this->categoryId = $row['category_id'];
  143. $this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
  144. if (!empty($row['publicated_on'])) {
  145. $this->publicated_on = $row['publicated_on'];
  146. }
  147. if (!empty($row['expired_on'])) {
  148. $this->expired_on = $row['expired_on'];
  149. }
  150. if ($this->type == 2) {
  151. if ($row['force_commit'] == 1) {
  152. $this->force_commit = true;
  153. }
  154. }
  155. $this->mode = $row['default_view_mod'];
  156. // Check user ID.
  157. if (empty($user_id)) {
  158. $this->error = 'User ID is empty';
  159. } else {
  160. $user_info = api_get_user_info($user_id);
  161. if (!empty($user_info)) {
  162. $this->user_id = $user_info['user_id'];
  163. } else {
  164. $this->error = 'User ID does not exist in database ('.$sql.')';
  165. }
  166. }
  167. // End of variables checking.
  168. $session_id = api_get_session_id();
  169. // Get the session condition for learning paths of the base + session.
  170. $session = api_get_session_condition($session_id);
  171. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  172. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  173. // Selecting by view_count descending allows to get the highest view_count first.
  174. $sql = "SELECT * FROM $lp_table
  175. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  176. ORDER BY view_count DESC";
  177. $res = Database::query($sql);
  178. if ($this->debug > 2) {
  179. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  180. }
  181. if (Database::num_rows($res) > 0) {
  182. if ($this->debug > 2) {
  183. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  184. }
  185. $row = Database::fetch_array($res);
  186. $this->attempt = $row['view_count'];
  187. $this->lp_view_id = $row['id'];
  188. $this->last_item_seen = $row['last_item'];
  189. $this->progress_db = $row['progress'];
  190. $this->lp_view_session_id = $row['session_id'];
  191. } else if (!api_is_invitee()) {
  192. if ($this->debug > 2) {
  193. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  194. }
  195. $this->attempt = 1;
  196. $params = [
  197. 'c_id' => $course_id,
  198. 'lp_id' => $lp_id,
  199. 'user_id' => $user_id,
  200. 'view_count' => 1,
  201. 'session_id' => $session_id,
  202. 'last_item' => 0
  203. ];
  204. $this->lp_view_id = Database::insert($lp_table, $params);
  205. if (!empty($this->lp_view_id)) {
  206. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  207. Database::query($sql);
  208. }
  209. }
  210. // Initialise items.
  211. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  212. $sql = "SELECT * FROM $lp_item_table
  213. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  214. ORDER BY parent_item_id, display_order";
  215. $res = Database::query($sql);
  216. if ($this->debug > 2) {
  217. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  218. error_log('-- Start while--', 0);
  219. }
  220. $lp_item_id_list = array();
  221. while ($row = Database::fetch_array($res)) {
  222. $lp_item_id_list[] = $row['id'];
  223. switch ($this->type) {
  224. case 3: //aicc
  225. $oItem = new aiccItem('db', $row['id'], $course_id);
  226. if (is_object($oItem)) {
  227. $my_item_id = $oItem->get_id();
  228. $oItem->set_lp_view($this->lp_view_id, $course_id);
  229. $oItem->set_prevent_reinit($this->prevent_reinit);
  230. // Don't use reference here as the next loop will make the pointed object change.
  231. $this->items[$my_item_id] = $oItem;
  232. $this->refs_list[$oItem->ref] = $my_item_id;
  233. if ($this->debug > 2) {
  234. error_log(
  235. 'New LP - learnpath::__construct() - ' .
  236. 'aicc object with id ' . $my_item_id .
  237. ' set in items[]',
  238. 0
  239. );
  240. }
  241. }
  242. break;
  243. case 2:
  244. $oItem = new scormItem('db', $row['id'], $course_id);
  245. if (is_object($oItem)) {
  246. $my_item_id = $oItem->get_id();
  247. $oItem->set_lp_view($this->lp_view_id, $course_id);
  248. $oItem->set_prevent_reinit($this->prevent_reinit);
  249. // Don't use reference here as the next loop will make the pointed object change.
  250. $this->items[$my_item_id] = $oItem;
  251. $this->refs_list[$oItem->ref] = $my_item_id;
  252. if ($this->debug > 2) {
  253. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  254. }
  255. }
  256. break;
  257. case 1:
  258. default:
  259. if ($this->debug > 2) {
  260. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  261. }
  262. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  263. if ($this->debug > 2) {
  264. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  265. }
  266. if (is_object($oItem)) {
  267. $my_item_id = $oItem->get_id();
  268. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  269. $oItem->set_prevent_reinit($this->prevent_reinit);
  270. // Don't use reference here as the next loop will make the pointed object change.
  271. $this->items[$my_item_id] = $oItem;
  272. $this->refs_list[$my_item_id] = $my_item_id;
  273. if ($this->debug > 2) {
  274. error_log(
  275. 'New LP - learnpath::__construct() ' . __LINE__ .
  276. ' - object with id ' . $my_item_id . ' set in items[]',
  277. 0
  278. );
  279. }
  280. }
  281. break;
  282. }
  283. // Setting the object level with variable $this->items[$i][parent]
  284. foreach ($this->items as $itemLPObject) {
  285. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  286. $itemLPObject->level = $level;
  287. }
  288. // Setting the view in the item object.
  289. if (is_object($this->items[$row['id']])) {
  290. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  291. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  292. $this->items[$row['id']]->current_start_time = 0;
  293. $this->items[$row['id']]->current_stop_time = 0;
  294. }
  295. }
  296. }
  297. if ($this->debug > 2) {
  298. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  299. }
  300. if (!empty($lp_item_id_list)) {
  301. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  302. if (!empty($lp_item_id_list_to_string)) {
  303. // Get last viewing vars.
  304. $itemViewTable = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  305. // This query should only return one or zero result.
  306. $sql = "SELECT lp_item_id, status
  307. FROM $itemViewTable
  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. if (!empty($this->lp_view_id)) {
  343. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  344. $params = [
  345. 'c_id' => $course_id,
  346. 'lp_item_id' => $course_id,
  347. 'lp_view_id' => $this->lp_view_id,
  348. 'view_count' => 1,
  349. 'status' => 'not attempted',
  350. 'start_time' => api_get_utc_datetime(),
  351. 'total_time' => 0,
  352. 'score' => 0
  353. ];
  354. $insertId = Database::insert($itemViewTable, $params);
  355. if ($insertId) {
  356. $sql = "UPDATE $itemViewTable SET id = iid
  357. WHERE iid = $insertId";
  358. Database::query($sql);
  359. }
  360. $this->items[$item_id]->set_lp_view(
  361. $this->lp_view_id,
  362. $course_id
  363. );
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. $this->ordered_items = self::get_flat_ordered_items_list(
  371. $this->get_id(),
  372. 0,
  373. $course_id
  374. );
  375. $this->max_ordered_items = 0;
  376. foreach ($this->ordered_items as $index => $dummy) {
  377. if ($index > $this->max_ordered_items && !empty($dummy)) {
  378. $this->max_ordered_items = $index;
  379. }
  380. }
  381. // TODO: Define the current item better.
  382. $this->first();
  383. if ($this->debug > 2) {
  384. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  385. }
  386. } else {
  387. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  388. }
  389. }
  390. }
  391. /**
  392. * @return string
  393. */
  394. public function getCourseCode()
  395. {
  396. return $this->cc;
  397. }
  398. /**
  399. * @return int
  400. */
  401. public function get_course_int_id()
  402. {
  403. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  404. }
  405. /**
  406. * @param $course_id
  407. * @return int
  408. */
  409. public function set_course_int_id($course_id)
  410. {
  411. return $this->course_int_id = (int) $course_id;
  412. }
  413. /**
  414. * Get the depth level of LP item
  415. * @param array $items
  416. * @param int $currentItemId
  417. * @return int
  418. */
  419. private static function get_level_for_item($items, $currentItemId)
  420. {
  421. $parentItemId = $items[$currentItemId]->parent;
  422. if ($parentItemId == 0) {
  423. return 0;
  424. } else {
  425. return learnpath::get_level_for_item($items, $parentItemId) + 1;
  426. }
  427. }
  428. /**
  429. * Function rewritten based on old_add_item() from Yannick Warnier.
  430. * Due the fact that users can decide where the item should come, I had to overlook this function and
  431. * I found it better to rewrite it. Old function is still available.
  432. * Added also the possibility to add a description.
  433. *
  434. * @param int $parent
  435. * @param int $previous
  436. * @param string $type
  437. * @param int $id resource ID (ref)
  438. * @param string $title
  439. * @param string $description
  440. * @param int $prerequisites
  441. * @param int $max_time_allowed
  442. * @param int $userId
  443. *
  444. * @return int
  445. */
  446. public function add_item(
  447. $parent,
  448. $previous,
  449. $type = 'dir',
  450. $id,
  451. $title,
  452. $description,
  453. $prerequisites = 0,
  454. $max_time_allowed = 0,
  455. $userId = 0
  456. ) {
  457. $course_id = $this->course_info['real_id'];
  458. if ($this->debug > 0) {
  459. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  460. }
  461. if (empty($course_id)) {
  462. // Sometimes Oogie doesn't catch the course info but sets $this->cc
  463. $this->course_info = api_get_course_info($this->cc);
  464. $course_id = $this->course_info['real_id'];
  465. }
  466. $userId = empty($userId) ? api_get_user_id() : $userId;
  467. $sessionId = api_get_session_id();
  468. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  469. $_course = $this->course_info;
  470. $parent = intval($parent);
  471. $previous = intval($previous);
  472. $id = intval($id);
  473. $max_time_allowed = htmlentities($max_time_allowed);
  474. if (empty($max_time_allowed)) {
  475. $max_time_allowed = 0;
  476. }
  477. $sql = "SELECT COUNT(id) AS num
  478. FROM $tbl_lp_item
  479. WHERE
  480. c_id = $course_id AND
  481. lp_id = " . $this->get_id() . " AND
  482. parent_item_id = " . $parent;
  483. $res_count = Database::query($sql);
  484. $row = Database::fetch_array($res_count);
  485. $num = $row['num'];
  486. if ($num > 0) {
  487. if (empty($previous)) {
  488. $sql = "SELECT id, next_item_id, display_order
  489. FROM " . $tbl_lp_item . "
  490. WHERE
  491. c_id = $course_id AND
  492. lp_id = " . $this->get_id() . " AND
  493. parent_item_id = " . $parent . " AND
  494. previous_item_id = 0 OR
  495. previous_item_id=" . $parent;
  496. $result = Database::query($sql);
  497. $row = Database::fetch_array($result);
  498. $tmp_previous = 0;
  499. $next = $row['id'];
  500. $display_order = 0;
  501. } else {
  502. $previous = (int) $previous;
  503. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  504. FROM $tbl_lp_item
  505. WHERE
  506. c_id = $course_id AND
  507. lp_id = " . $this->get_id() . " AND
  508. id = " . $previous;
  509. $result = Database::query($sql);
  510. $row = Database:: fetch_array($result);
  511. $tmp_previous = $row['id'];
  512. $next = $row['next_item_id'];
  513. $display_order = $row['display_order'];
  514. }
  515. } else {
  516. $tmp_previous = 0;
  517. $next = 0;
  518. $display_order = 0;
  519. }
  520. $id = intval($id);
  521. $typeCleaned = Database::escape_string($type);
  522. if ($type == 'quiz') {
  523. $sql = 'SELECT SUM(ponderation)
  524. FROM ' . Database::get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  525. INNER JOIN ' . Database::get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  526. ON
  527. quiz_question.id = quiz_rel_question.question_id AND
  528. quiz_question.c_id = quiz_rel_question.c_id
  529. WHERE
  530. quiz_rel_question.exercice_id = '.$id." AND
  531. quiz_question.c_id = $course_id AND
  532. quiz_rel_question.c_id = $course_id ";
  533. $rsQuiz = Database::query($sql);
  534. $max_score = Database::result($rsQuiz, 0, 0);
  535. // Disabling the exercise if we add it inside a LP
  536. $exercise = new Exercise($course_id);
  537. $exercise->read($id);
  538. $exercise->disable();
  539. $exercise->save();
  540. } else {
  541. $max_score = 100;
  542. }
  543. $params = array(
  544. "c_id" => $course_id,
  545. "lp_id" => $this->get_id(),
  546. "item_type" => $typeCleaned,
  547. "ref" => '',
  548. "title" => $title,
  549. "description" => $description,
  550. "path" => $id,
  551. "max_score" => $max_score,
  552. "parent_item_id" => $parent,
  553. "previous_item_id" => $previous,
  554. "next_item_id" => intval($next),
  555. "display_order" => $display_order + 1,
  556. "prerequisite" => $prerequisites,
  557. "max_time_allowed" => $max_time_allowed,
  558. 'min_score' => 0,
  559. 'launch_data' => ''
  560. );
  561. if ($prerequisites != 0) {
  562. $params['prerequisite'] = $prerequisites;
  563. }
  564. $new_item_id = Database::insert($tbl_lp_item, $params);
  565. if ($this->debug > 2) {
  566. error_log('New LP - Inserting dir/chapter: ' . $new_item_id, 0);
  567. }
  568. if ($new_item_id) {
  569. $sql = "UPDATE $tbl_lp_item SET id = iid WHERE iid = $new_item_id";
  570. Database::query($sql);
  571. $sql = "UPDATE $tbl_lp_item
  572. SET previous_item_id = $new_item_id
  573. WHERE c_id = $course_id AND id = $next";
  574. Database::query($sql);
  575. // Update the item that should be before the new item.
  576. $sql = "UPDATE $tbl_lp_item
  577. SET next_item_id = $new_item_id
  578. WHERE c_id = $course_id AND id = $tmp_previous";
  579. Database::query($sql);
  580. // Update all the items after the new item.
  581. $sql = "UPDATE " . $tbl_lp_item . "
  582. SET display_order = display_order + 1
  583. WHERE
  584. c_id = $course_id AND
  585. lp_id = " . $this->get_id() . " AND
  586. id <> " . $new_item_id . " AND
  587. parent_item_id = " . $parent . " AND
  588. display_order > " . $display_order;
  589. Database::query($sql);
  590. // Update the item that should come after the new item.
  591. $sql = "UPDATE " . $tbl_lp_item . "
  592. SET ref = " . $new_item_id . "
  593. WHERE c_id = $course_id AND id = " . $new_item_id;
  594. Database::query($sql);
  595. // Upload audio.
  596. if (!empty($_FILES['mp3']['name'])) {
  597. // Create the audio folder if it does not exist yet.
  598. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  599. if (!is_dir($filepath . 'audio')) {
  600. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  601. $audio_id = add_document(
  602. $_course,
  603. '/audio',
  604. 'folder',
  605. 0,
  606. 'audio',
  607. '',
  608. 0,
  609. true,
  610. null,
  611. $sessionId,
  612. $userId
  613. );
  614. api_item_property_update(
  615. $_course,
  616. TOOL_DOCUMENT,
  617. $audio_id,
  618. 'FolderCreated',
  619. $userId,
  620. null,
  621. null,
  622. null,
  623. null,
  624. $sessionId
  625. );
  626. api_item_property_update(
  627. $_course,
  628. TOOL_DOCUMENT,
  629. $audio_id,
  630. 'invisible',
  631. $userId,
  632. null,
  633. null,
  634. null,
  635. null,
  636. $sessionId
  637. );
  638. }
  639. $file_path = handle_uploaded_document(
  640. $_course,
  641. $_FILES['mp3'],
  642. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  643. '/audio',
  644. $userId,
  645. '',
  646. '',
  647. '',
  648. '',
  649. false
  650. );
  651. // Getting the filename only.
  652. $file_components = explode('/', $file_path);
  653. $file = $file_components[count($file_components) - 1];
  654. // Store the mp3 file in the lp_item table.
  655. $sql = "UPDATE $tbl_lp_item SET
  656. audio = '" . Database::escape_string($file) . "'
  657. WHERE id = '" . intval($new_item_id) . "'";
  658. Database::query($sql);
  659. }
  660. }
  661. return $new_item_id;
  662. }
  663. /**
  664. * Static admin function allowing addition of a learnpath to a course.
  665. * @param string Course code
  666. * @param string Learnpath name
  667. * @param string Learnpath description string, if provided
  668. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  669. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  670. * @param string Zip file containing the learnpath or directory containing the learnpath
  671. * @return integer The new learnpath ID on success, 0 on failure
  672. */
  673. public static function add_lp(
  674. $courseCode,
  675. $name,
  676. $description = '',
  677. $learnpath = 'guess',
  678. $origin = 'zip',
  679. $zipname = '',
  680. $publicated_on = '',
  681. $expired_on = '',
  682. $categoryId = 0,
  683. $userId = 0
  684. ) {
  685. global $charset;
  686. if (!empty($courseCode)) {
  687. $courseInfo = api_get_course_info($courseCode);
  688. $course_id = $courseInfo['real_id'];
  689. } else {
  690. $course_id = api_get_course_int_id();
  691. $courseInfo = api_get_course_info();
  692. }
  693. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  694. // Check course code exists.
  695. // Check lp_name doesn't exist, otherwise append something.
  696. $i = 0;
  697. $name = Database::escape_string($name);
  698. $categoryId = intval($categoryId);
  699. // Session id.
  700. $session_id = api_get_session_id();
  701. $userId = empty($userId) ? api_get_user_id() : $userId;
  702. $check_name = "SELECT * FROM $tbl_lp
  703. WHERE c_id = $course_id AND name = '$name'";
  704. $res_name = Database::query($check_name);
  705. if (empty($publicated_on)) {
  706. //by default the publication date is the same that the creation date
  707. //The behaviour above was changed due BT#2800
  708. // global $_custom;
  709. // if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  710. // $publicated_on = null;
  711. // } else {
  712. // $publicated_on = null;
  713. // }
  714. $publicated_on = null;
  715. } else {
  716. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  717. }
  718. if (empty($expired_on)) {
  719. $expired_on = null;
  720. } else {
  721. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  722. }
  723. while (Database::num_rows($res_name)) {
  724. // There is already one such name, update the current one a bit.
  725. $i++;
  726. $name = $name . ' - ' . $i;
  727. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  728. $res_name = Database::query($check_name);
  729. }
  730. // New name does not exist yet; keep it.
  731. // Escape description.
  732. // Kevin: added htmlentities().
  733. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  734. $type = 1;
  735. switch ($learnpath) {
  736. case 'guess':
  737. break;
  738. case 'dokeos':
  739. case 'chamilo':
  740. $type = 1;
  741. break;
  742. case 'aicc':
  743. break;
  744. }
  745. switch ($origin) {
  746. case 'zip':
  747. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  748. break;
  749. case 'manual':
  750. default:
  751. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  752. $res_max = Database::query($get_max);
  753. if (Database::num_rows($res_max) < 1) {
  754. $dsp = 1;
  755. } else {
  756. $row = Database::fetch_array($res_max);
  757. $dsp = $row[0] + 1;
  758. }
  759. $params = [
  760. 'c_id' => $course_id,
  761. 'lp_type' => $type,
  762. 'name' => $name,
  763. 'description' => $description,
  764. 'path' => '',
  765. 'default_view_mod' => 'embedded',
  766. 'default_encoding' => 'UTF-8',
  767. 'display_order' => $dsp,
  768. 'content_maker' => 'Chamilo',
  769. 'content_local' => 'local',
  770. 'js_lib' => '',
  771. 'session_id' => $session_id,
  772. 'created_on' => api_get_utc_datetime(),
  773. 'modified_on' => api_get_utc_datetime(),
  774. 'publicated_on' => $publicated_on,
  775. 'expired_on' => $expired_on,
  776. 'category_id' => $categoryId,
  777. 'force_commit' => 0,
  778. 'content_license' => '',
  779. 'debug' => 0,
  780. 'theme' => '',
  781. 'preview_image' => '',
  782. 'author' => '',
  783. 'prerequisite' => 0,
  784. 'hide_toc_frame' => 0,
  785. 'seriousgame_mode' => 0,
  786. 'autolaunch' => 0,
  787. 'max_attempts' => 0,
  788. 'subscribe_users' => 0,
  789. 'accumulate_scorm_time' => 1
  790. ];
  791. $id = Database::insert($tbl_lp, $params);
  792. if ($id > 0) {
  793. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  794. Database::query($sql);
  795. // Insert into item_property.
  796. api_item_property_update(
  797. $courseInfo,
  798. TOOL_LEARNPATH,
  799. $id,
  800. 'LearnpathAdded',
  801. $userId
  802. );
  803. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  804. return $id;
  805. }
  806. break;
  807. }
  808. }
  809. /**
  810. * Auto completes the parents of an item in case it's been completed or passed
  811. * @param int $item Optional ID of the item from which to look for parents
  812. */
  813. public function autocomplete_parents($item)
  814. {
  815. $debug = $this->debug;
  816. if ($debug) {
  817. error_log('Learnpath::autocomplete_parents()', 0);
  818. }
  819. if (empty($item)) {
  820. $item = $this->current;
  821. }
  822. $currentItem = $this->getItem($item);
  823. if ($currentItem) {
  824. $parent_id = $currentItem->get_parent();
  825. $parent = $this->getItem($parent_id);
  826. if ($parent) {
  827. // if $item points to an object and there is a parent.
  828. if ($debug) {
  829. error_log(
  830. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  831. 0
  832. );
  833. }
  834. // New experiment including failed and browsed in completed status.
  835. //$current_status = $currentItem->get_status();
  836. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  837. // Fixes chapter auto complete
  838. if (true) {
  839. // If the current item is completed or passes or succeeded.
  840. $updateParentStatus = true;
  841. if ($debug) {
  842. error_log('Status of current item is alright', 0);
  843. }
  844. foreach ($parent->get_children() as $childItemId) {
  845. $childItem = $this->getItem($childItemId);
  846. // If children was not set try to get the info
  847. if (empty($childItem->db_item_view_id)) {
  848. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  849. }
  850. // Check all his brothers (parent's children) for completion status.
  851. if ($childItemId != $item) {
  852. if ($debug) {
  853. error_log(
  854. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  855. 0
  856. );
  857. }
  858. // Trying completing parents of failed and browsed items as well.
  859. if ($childItem->status_is(
  860. array(
  861. 'completed',
  862. 'passed',
  863. 'succeeded',
  864. 'browsed',
  865. 'failed'
  866. )
  867. )
  868. ) {
  869. // Keep completion status to true.
  870. continue;
  871. } else {
  872. if ($debug > 2) {
  873. error_log(
  874. '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,
  875. 0
  876. );
  877. }
  878. $updateParentStatus = false;
  879. break;
  880. }
  881. }
  882. }
  883. if ($updateParentStatus) {
  884. // If all the children were completed:
  885. $parent->set_status('completed');
  886. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  887. // Force the status to "completed"
  888. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  889. $this->update_queue[$parent->get_id()] = 'completed';
  890. if ($debug) {
  891. error_log(
  892. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  893. print_r($this->update_queue, 1),
  894. 0
  895. );
  896. }
  897. // Recursive call.
  898. $this->autocomplete_parents($parent->get_id());
  899. }
  900. }
  901. } else {
  902. if ($debug) {
  903. error_log("Parent #$parent_id does not exists");
  904. }
  905. }
  906. } else {
  907. if ($debug) {
  908. error_log("#$item is an item that doesn't have parents");
  909. }
  910. }
  911. }
  912. /**
  913. * Auto saves the current results into the database for the whole learnpath
  914. * @todo: Add save operations for the learnpath itself.
  915. */
  916. public function autosave()
  917. {
  918. if ($this->debug > 0) {
  919. error_log('New LP - In learnpath::autosave()', 0);
  920. }
  921. }
  922. /**
  923. * Closes the current resource
  924. *
  925. * Stops the timer
  926. * Saves into the database if required
  927. * Clears the current resource data from this object
  928. * @return boolean True on success, false on failure
  929. */
  930. public function close()
  931. {
  932. if ($this->debug > 0) {
  933. error_log('New LP - In learnpath::close()', 0);
  934. }
  935. if (empty($this->lp_id)) {
  936. $this->error = 'Trying to close this learnpath but no ID is set';
  937. return false;
  938. }
  939. $this->current_time_stop = time();
  940. $this->ordered_items = array();
  941. $this->index = 0;
  942. unset($this->lp_id);
  943. //unset other stuff
  944. return true;
  945. }
  946. /**
  947. * Static admin function allowing removal of a learnpath
  948. * @param array $courseInfo
  949. * @param integer Learnpath ID
  950. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  951. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  952. */
  953. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  954. {
  955. $course_id = api_get_course_int_id();
  956. if (!empty($courseInfo)) {
  957. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  958. }
  959. // TODO: Implement a way of getting this to work when the current object is not set.
  960. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  961. // If an ID is specifically given and the current LP is not the same, prevent delete.
  962. if (!empty($id) && ($id != $this->lp_id)) {
  963. return false;
  964. }
  965. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  966. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  967. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  968. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  969. // Delete lp item id.
  970. foreach ($this->items as $id => $dummy) {
  971. $sql = "DELETE FROM $lp_item_view
  972. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  973. Database::query($sql);
  974. }
  975. // Proposed by Christophe (nickname: clefevre)
  976. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  977. Database::query($sql);
  978. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  979. Database::query($sql);
  980. self::toggle_publish($this->lp_id, 'i');
  981. if ($this->type == 2 || $this->type == 3) {
  982. // This is a scorm learning path, delete the files as well.
  983. $sql = "SELECT path FROM $lp
  984. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  985. $res = Database::query($sql);
  986. if (Database::num_rows($res) > 0) {
  987. $row = Database::fetch_array($res);
  988. $path = $row['path'];
  989. $sql = "SELECT id FROM $lp
  990. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  991. $res = Database::query($sql);
  992. if (Database::num_rows($res) > 0) {
  993. // Another learning path uses this directory, so don't delete it.
  994. if ($this->debug > 2) {
  995. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  996. }
  997. } else {
  998. // No other LP uses that directory, delete it.
  999. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  1000. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  1001. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  1002. if ($this->debug > 2) {
  1003. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  1004. }
  1005. // Proposed by Christophe (clefevre).
  1006. if (strcmp(substr($path, -2), "/.") == 0) {
  1007. $path = substr($path, 0, -1); // Remove "." at the end.
  1008. }
  1009. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1010. rmdirr($course_scorm_dir . $path);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1016. $link = 'lp/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1017. // Delete tools
  1018. $sql = "DELETE FROM $tbl_tool
  1019. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1020. Database::query($sql);
  1021. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1022. Database::query($sql);
  1023. // Updates the display order of all lps.
  1024. $this->update_display_order();
  1025. api_item_property_update(
  1026. api_get_course_info(),
  1027. TOOL_LEARNPATH,
  1028. $this->lp_id,
  1029. 'delete',
  1030. api_get_user_id()
  1031. );
  1032. $link_info = GradebookUtils::isResourceInCourseGradebook(
  1033. api_get_course_id(),
  1034. 4,
  1035. $id,
  1036. api_get_session_id()
  1037. );
  1038. if ($link_info !== false) {
  1039. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  1040. }
  1041. if (api_get_setting('search_enabled') == 'true') {
  1042. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1043. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1044. }
  1045. }
  1046. /**
  1047. * Removes all the children of one item - dangerous!
  1048. * @param integer $id Element ID of which children have to be removed
  1049. * @return integer Total number of children removed
  1050. */
  1051. public function delete_children_items($id)
  1052. {
  1053. $course_id = $this->course_info['real_id'];
  1054. if ($this->debug > 0) {
  1055. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1056. }
  1057. $num = 0;
  1058. if (empty ($id) || $id != strval(intval($id))) {
  1059. return false;
  1060. }
  1061. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1062. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1063. $res = Database::query($sql);
  1064. while ($row = Database::fetch_array($res)) {
  1065. $num += $this->delete_children_items($row['id']);
  1066. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1067. Database::query($sql_del);
  1068. $num++;
  1069. }
  1070. return $num;
  1071. }
  1072. /**
  1073. * Removes an item from the current learnpath
  1074. * @param integer $id Elem ID (0 if first)
  1075. * @param integer $remove Whether to remove the resource/data from the
  1076. * system or leave it (default: 'keep', others 'remove')
  1077. * @return integer Number of elements moved
  1078. * @todo implement resource removal
  1079. */
  1080. public function delete_item($id, $remove = 'keep')
  1081. {
  1082. $course_id = api_get_course_int_id();
  1083. if ($this->debug > 0) {
  1084. error_log('New LP - In learnpath::delete_item()', 0);
  1085. }
  1086. // TODO: Implement the resource removal.
  1087. if (empty ($id) || $id != strval(intval($id))) {
  1088. return false;
  1089. }
  1090. // First select item to get previous, next, and display order.
  1091. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1092. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1093. $res_sel = Database::query($sql_sel);
  1094. if (Database::num_rows($res_sel) < 1) {
  1095. return false;
  1096. }
  1097. $row = Database::fetch_array($res_sel);
  1098. $previous = $row['previous_item_id'];
  1099. $next = $row['next_item_id'];
  1100. $display = $row['display_order'];
  1101. $parent = $row['parent_item_id'];
  1102. $lp = $row['lp_id'];
  1103. // Delete children items.
  1104. $num = $this->delete_children_items($id);
  1105. if ($this->debug > 2) {
  1106. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1107. }
  1108. // Now delete the item.
  1109. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1110. if ($this->debug > 2) {
  1111. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1112. }
  1113. Database::query($sql_del);
  1114. // Now update surrounding items.
  1115. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1116. WHERE c_id = ".$course_id." AND id = $previous";
  1117. Database::query($sql_upd);
  1118. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1119. WHERE c_id = ".$course_id." AND id = $next";
  1120. Database::query($sql_upd);
  1121. // Now update all following items with new display order.
  1122. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1123. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1124. Database::query($sql_all);
  1125. //Removing prerequisites since the item will not longer exist
  1126. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1127. Database::query($sql_all);
  1128. // Remove from search engine if enabled.
  1129. if (api_get_setting('search_enabled') == 'true') {
  1130. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1131. $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';
  1132. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1133. $res = Database::query($sql);
  1134. if (Database::num_rows($res) > 0) {
  1135. $row2 = Database::fetch_array($res);
  1136. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1137. $di = new ChamiloIndexer();
  1138. $di->remove_document((int) $row2['search_did']);
  1139. }
  1140. $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';
  1141. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1142. Database::query($sql);
  1143. }
  1144. }
  1145. /**
  1146. * Updates an item's content in place
  1147. * @param integer $id Element ID
  1148. * @param integer $parent Parent item ID
  1149. * @param integer $previous Previous item ID
  1150. * @param string $title Item title
  1151. * @param string $description Item description
  1152. * @param string $prerequisites Prerequisites (optional)
  1153. * @param array $audio The array resulting of the $_FILES[mp3] element
  1154. * @param int $max_time_allowed
  1155. * @param string $url
  1156. * @return boolean True on success, false on error
  1157. */
  1158. public function edit_item(
  1159. $id,
  1160. $parent,
  1161. $previous,
  1162. $title,
  1163. $description,
  1164. $prerequisites = '0',
  1165. $audio = array(),
  1166. $max_time_allowed = 0,
  1167. $url = ''
  1168. ) {
  1169. $course_id = api_get_course_int_id();
  1170. $_course = api_get_course_info();
  1171. if ($this->debug > 0) {
  1172. error_log('New LP - In learnpath::edit_item()', 0);
  1173. }
  1174. if (empty ($max_time_allowed)) {
  1175. $max_time_allowed = 0;
  1176. }
  1177. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1178. return false;
  1179. }
  1180. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1181. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1182. $res_select = Database::query($sql_select);
  1183. $row_select = Database::fetch_array($res_select);
  1184. $audio_update_sql = '';
  1185. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1186. // Create the audio folder if it does not exist yet.
  1187. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1188. if (!is_dir($filepath . 'audio')) {
  1189. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1190. $audio_id = add_document(
  1191. $_course,
  1192. '/audio',
  1193. 'folder',
  1194. 0,
  1195. 'audio'
  1196. );
  1197. api_item_property_update(
  1198. $_course,
  1199. TOOL_DOCUMENT,
  1200. $audio_id,
  1201. 'FolderCreated',
  1202. api_get_user_id(),
  1203. null,
  1204. null,
  1205. null,
  1206. null,
  1207. api_get_session_id()
  1208. );
  1209. api_item_property_update(
  1210. $_course,
  1211. TOOL_DOCUMENT,
  1212. $audio_id,
  1213. 'invisible',
  1214. api_get_user_id(),
  1215. null,
  1216. null,
  1217. null,
  1218. null,
  1219. api_get_session_id()
  1220. );
  1221. }
  1222. // Upload file in documents.
  1223. $pi = pathinfo($audio['name']);
  1224. if ($pi['extension'] == 'mp3') {
  1225. $c_det = api_get_course_info($this->cc);
  1226. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1227. $path = handle_uploaded_document(
  1228. $c_det,
  1229. $audio,
  1230. $bp,
  1231. '/audio',
  1232. api_get_user_id(),
  1233. 0,
  1234. null,
  1235. 0,
  1236. 'rename',
  1237. false,
  1238. 0
  1239. );
  1240. $path = substr($path, 7);
  1241. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1242. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1243. }
  1244. }
  1245. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1246. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1247. // TODO: htmlspecialchars to be checked for encoding related problems.
  1248. if ($same_parent && $same_previous) {
  1249. // Only update title and description.
  1250. $sql = "UPDATE " . $tbl_lp_item . "
  1251. SET title = '" . Database::escape_string($title) . "',
  1252. prerequisite = '" . $prerequisites . "',
  1253. description = '" . Database::escape_string($description) . "'
  1254. " . $audio_update_sql . ",
  1255. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1256. WHERE c_id = ".$course_id." AND id = " . $id;
  1257. Database::query($sql);
  1258. } else {
  1259. $old_parent = $row_select['parent_item_id'];
  1260. $old_previous = $row_select['previous_item_id'];
  1261. $old_next = $row_select['next_item_id'];
  1262. $old_order = $row_select['display_order'];
  1263. $old_prerequisite = $row_select['prerequisite'];
  1264. $old_max_time_allowed = $row_select['max_time_allowed'];
  1265. /* BEGIN -- virtually remove the current item id */
  1266. /* for the next and previous item it is like the current item doesn't exist anymore */
  1267. if ($old_previous != 0) {
  1268. // Next
  1269. $sql = "UPDATE " . $tbl_lp_item . "
  1270. SET next_item_id = " . $old_next . "
  1271. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1272. Database::query($sql);
  1273. }
  1274. if ($old_next != 0) {
  1275. // Previous
  1276. $sql = "UPDATE " . $tbl_lp_item . "
  1277. SET previous_item_id = " . $old_previous . "
  1278. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1279. Database::query($sql);
  1280. }
  1281. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1282. $sql = "UPDATE " . $tbl_lp_item . "
  1283. SET display_order = display_order - 1
  1284. WHERE
  1285. c_id = ".$course_id." AND
  1286. display_order > " . $old_order . " AND
  1287. lp_id = " . $this->lp_id . " AND
  1288. parent_item_id = " . $old_parent;
  1289. Database::query($sql);
  1290. /* END -- virtually remove the current item id */
  1291. /* BEGIN -- update the current item id to his new location */
  1292. if ($previous == 0) {
  1293. // Select the data of the item that should come after the current item.
  1294. $sql = "SELECT id, display_order
  1295. FROM " . $tbl_lp_item . "
  1296. WHERE
  1297. c_id = ".$course_id." AND
  1298. lp_id = " . $this->lp_id . " AND
  1299. parent_item_id = " . $parent . " AND
  1300. previous_item_id = " . $previous;
  1301. $res_select_old = Database::query($sql);
  1302. $row_select_old = Database::fetch_array($res_select_old);
  1303. // If the new parent didn't have children before.
  1304. if (Database::num_rows($res_select_old) == 0) {
  1305. $new_next = 0;
  1306. $new_order = 1;
  1307. } else {
  1308. $new_next = $row_select_old['id'];
  1309. $new_order = $row_select_old['display_order'];
  1310. }
  1311. } else {
  1312. // Select the data of the item that should come before the current item.
  1313. $sql = "SELECT next_item_id, display_order
  1314. FROM " . $tbl_lp_item . "
  1315. WHERE c_id = ".$course_id." AND id = " . $previous;
  1316. $res_select_old = Database::query($sql);
  1317. $row_select_old = Database::fetch_array($res_select_old);
  1318. $new_next = $row_select_old['next_item_id'];
  1319. $new_order = $row_select_old['display_order'] + 1;
  1320. }
  1321. // TODO: htmlspecialchars to be checked for encoding related problems.
  1322. // Update the current item with the new data.
  1323. $sql = "UPDATE " . $tbl_lp_item . "
  1324. SET
  1325. title = '" . Database::escape_string($title) . "',
  1326. description = '" . Database::escape_string($description) . "',
  1327. parent_item_id = " . $parent . ",
  1328. previous_item_id = " . $previous . ",
  1329. next_item_id = " . $new_next . ",
  1330. display_order = " . $new_order . "
  1331. " . $audio_update_sql . "
  1332. WHERE c_id = ".$course_id." AND id = " . $id;
  1333. Database::query($sql);
  1334. if ($previous != 0) {
  1335. // Update the previous item's next_item_id.
  1336. $sql = "UPDATE " . $tbl_lp_item . "
  1337. SET next_item_id = " . $id . "
  1338. WHERE c_id = ".$course_id." AND id = " . $previous;
  1339. Database::query($sql);
  1340. }
  1341. if ($new_next != 0) {
  1342. // Update the next item's previous_item_id.
  1343. $sql = "UPDATE " . $tbl_lp_item . "
  1344. SET previous_item_id = " . $id . "
  1345. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1346. Database::query($sql);
  1347. }
  1348. if ($old_prerequisite != $prerequisites) {
  1349. $sql = "UPDATE " . $tbl_lp_item . "
  1350. SET prerequisite = '" . $prerequisites . "'
  1351. WHERE c_id = ".$course_id." AND id = " . $id;
  1352. Database::query($sql);
  1353. }
  1354. if ($old_max_time_allowed != $max_time_allowed) {
  1355. // update max time allowed
  1356. $sql = "UPDATE " . $tbl_lp_item . "
  1357. SET max_time_allowed = " . $max_time_allowed . "
  1358. WHERE c_id = ".$course_id." AND id = " . $id;
  1359. Database::query($sql);
  1360. }
  1361. // Update all the items with the same or a bigger display_order than the current item.
  1362. $sql = "UPDATE " . $tbl_lp_item . "
  1363. SET display_order = display_order + 1
  1364. WHERE
  1365. c_id = ".$course_id." AND
  1366. lp_id = " . $this->get_id() . " AND
  1367. id <> " . $id . " AND
  1368. parent_item_id = " . $parent . " AND
  1369. display_order >= " . $new_order;
  1370. Database::query($sql);
  1371. }
  1372. if ($row_select['item_type'] == 'link') {
  1373. $link = new Link();
  1374. $linkId = $row_select['path'];
  1375. $link->updateLink($linkId, $url);
  1376. }
  1377. }
  1378. /**
  1379. * Updates an item's prereq in place
  1380. * @param integer $id Element ID
  1381. * @param string $prerequisite_id Prerequisite Element ID
  1382. * @param int $mastery_score Prerequisite min score
  1383. * @param int $max_score Prerequisite max score
  1384. *
  1385. * @return boolean True on success, false on error
  1386. */
  1387. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1388. {
  1389. $course_id = api_get_course_int_id();
  1390. if ($this->debug > 0) {
  1391. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1392. }
  1393. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1394. return false;
  1395. }
  1396. $prerequisite_id = intval($prerequisite_id);
  1397. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1398. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1399. $mastery_score = 0;
  1400. }
  1401. if (!is_numeric($max_score) || $max_score < 0) {
  1402. $max_score = 100;
  1403. }
  1404. /*if ($mastery_score > $max_score) {
  1405. $max_score = $mastery_score;
  1406. }*/
  1407. if (!is_numeric($prerequisite_id)) {
  1408. $prerequisite_id = 'NULL';
  1409. }
  1410. $mastery_score = floatval($mastery_score);
  1411. $max_score = floatval($max_score);
  1412. $sql = " UPDATE $tbl_lp_item
  1413. SET
  1414. prerequisite = $prerequisite_id ,
  1415. prerequisite_min_score = $mastery_score ,
  1416. prerequisite_max_score = $max_score
  1417. WHERE c_id = $course_id AND id = $id";
  1418. Database::query($sql);
  1419. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1420. // Will this be enough to ensure unicity?
  1421. /*$sql = " UPDATE $tbl_lp_item
  1422. SET mastery_score = $mastery_score
  1423. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1424. Database::query($sql);*/
  1425. }
  1426. // TODO: Update the item object (can be ignored for now because refreshed).
  1427. return true;
  1428. }
  1429. /**
  1430. * Static admin function exporting a learnpath into a zip file
  1431. * @param string Export type (scorm, zip, cd)
  1432. * @param string Course code
  1433. * @param integer Learnpath ID
  1434. * @param string Zip file name
  1435. * @return string Zip file path (or false on error)
  1436. */
  1437. public function export_lp($type, $course, $id, $zipname)
  1438. {
  1439. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1440. return false;
  1441. }
  1442. $url = '';
  1443. switch ($type) {
  1444. case 'scorm':
  1445. break;
  1446. case 'zip':
  1447. break;
  1448. case 'cdrom':
  1449. break;
  1450. }
  1451. return $url;
  1452. }
  1453. /**
  1454. * Gets all the chapters belonging to the same parent as the item/chapter given
  1455. * Can also be called as abstract method
  1456. * @param integer $id Item ID
  1457. * @return array A list of all the "brother items" (or an empty array on failure)
  1458. */
  1459. public function getSiblingDirectories($id)
  1460. {
  1461. $course_id = api_get_course_int_id();
  1462. if ($this->debug > 0) {
  1463. error_log('New LP - In learnpath::getSiblingDirectories()', 0);
  1464. }
  1465. if (empty($id) || $id != strval(intval($id))) {
  1466. return array ();
  1467. }
  1468. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1469. $sql_parent = "SELECT * FROM $lp_item
  1470. WHERE c_id = ".$course_id." AND id = $id AND item_type='dir'";
  1471. $res_parent = Database::query($sql_parent);
  1472. if (Database::num_rows($res_parent) > 0) {
  1473. $row_parent = Database::fetch_array($res_parent);
  1474. $parent = $row_parent['parent_item_id'];
  1475. $sql = "SELECT * FROM $lp_item
  1476. WHERE
  1477. c_id = ".$course_id." AND
  1478. parent_item_id = $parent AND
  1479. id = $id AND
  1480. item_type='dir'
  1481. ORDER BY display_order";
  1482. $res_bros = Database::query($sql);
  1483. $list = array();
  1484. while ($row_bro = Database::fetch_array($res_bros)) {
  1485. $list[] = $row_bro;
  1486. }
  1487. return $list;
  1488. }
  1489. return array();
  1490. }
  1491. /**
  1492. * Gets all the items belonging to the same parent as the item given
  1493. * Can also be called as abstract method
  1494. * @param integer $id Item ID
  1495. * @return array A list of all the "brother items" (or an empty array on failure)
  1496. */
  1497. public function get_brother_items($id)
  1498. {
  1499. $course_id = api_get_course_int_id();
  1500. if ($this->debug > 0) {
  1501. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1502. }
  1503. if (empty ($id) || $id != strval(intval($id))) {
  1504. return array();
  1505. }
  1506. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1507. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1508. $res_parent = Database::query($sql_parent);
  1509. if (Database::num_rows($res_parent) > 0) {
  1510. $row_parent = Database::fetch_array($res_parent);
  1511. $parent = $row_parent['parent_item_id'];
  1512. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1513. ORDER BY display_order";
  1514. $res_bros = Database::query($sql_bros);
  1515. $list = [];
  1516. while ($row_bro = Database::fetch_array($res_bros)) {
  1517. $list[] = $row_bro;
  1518. }
  1519. return $list;
  1520. }
  1521. return [];
  1522. }
  1523. /**
  1524. * Get the specific prefix index terms of this learning path
  1525. * @param string $prefix
  1526. * @return array Array of terms
  1527. */
  1528. public function get_common_index_terms_by_prefix($prefix)
  1529. {
  1530. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1531. $terms = get_specific_field_values_list_by_prefix(
  1532. $prefix,
  1533. $this->cc,
  1534. TOOL_LEARNPATH,
  1535. $this->lp_id
  1536. );
  1537. $prefix_terms = array();
  1538. if (!empty($terms)) {
  1539. foreach ($terms as $term) {
  1540. $prefix_terms[] = $term['value'];
  1541. }
  1542. }
  1543. return $prefix_terms;
  1544. }
  1545. /**
  1546. * Gets the number of items currently completed
  1547. * @param bool $failedStatusException flag to determine the failed status is not considered progressed
  1548. * @return integer The number of items currently completed
  1549. */
  1550. public function get_complete_items_count($failedStatusException = false)
  1551. {
  1552. if ($this->debug > 0) {
  1553. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1554. }
  1555. $i = 0;
  1556. $completedStatusList = array(
  1557. 'completed',
  1558. 'passed',
  1559. 'succeeded',
  1560. 'browsed'
  1561. );
  1562. if (!$failedStatusException) {
  1563. $completedStatusList[] = 'failed';
  1564. }
  1565. foreach ($this->items as $id => $dummy) {
  1566. // Trying failed and browsed considered "progressed" as well.
  1567. if ($this->items[$id]->status_is($completedStatusList) &&
  1568. $this->items[$id]->get_type() != 'dir'
  1569. ) {
  1570. $i++;
  1571. }
  1572. }
  1573. return $i;
  1574. }
  1575. /**
  1576. * Gets the current item ID
  1577. * @return integer The current learnpath item id
  1578. */
  1579. public function get_current_item_id()
  1580. {
  1581. $current = 0;
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1584. }
  1585. if (!empty($this->current)) {
  1586. $current = $this->current;
  1587. }
  1588. if ($this->debug > 2) {
  1589. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1590. }
  1591. return $current;
  1592. }
  1593. /**
  1594. * Force to get the first learnpath item id
  1595. * @return integer The current learnpath item id
  1596. */
  1597. public function get_first_item_id()
  1598. {
  1599. $current = 0;
  1600. if (is_array($this->ordered_items)) {
  1601. $current = $this->ordered_items[0];
  1602. }
  1603. return $current;
  1604. }
  1605. /**
  1606. * Gets the total number of items available for viewing in this SCORM
  1607. * @return integer The total number of items
  1608. */
  1609. public function get_total_items_count()
  1610. {
  1611. if ($this->debug > 0) {
  1612. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1613. }
  1614. return count($this->items);
  1615. }
  1616. /**
  1617. * Gets the total number of items available for viewing in this SCORM but without chapters
  1618. * @return integer The total no-chapters number of items
  1619. */
  1620. public function getTotalItemsCountWithoutDirs()
  1621. {
  1622. if ($this->debug > 0) {
  1623. error_log('New LP - In learnpath::getTotalItemsCountWithoutDirs()', 0);
  1624. }
  1625. $total = 0;
  1626. $typeListNotToCount = self::getChapterTypes();
  1627. foreach ($this->items as $temp2) {
  1628. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1629. $total++;
  1630. }
  1631. }
  1632. return $total;
  1633. }
  1634. /**
  1635. * Gets the first element URL.
  1636. * @return string URL to load into the viewer
  1637. */
  1638. public function first()
  1639. {
  1640. if ($this->debug > 0) {
  1641. error_log('New LP - In learnpath::first()', 0);
  1642. error_log('$this->last_item_seen '.$this->last_item_seen);
  1643. }
  1644. // Test if the last_item_seen exists and is not a dir.
  1645. if (count($this->ordered_items) == 0) {
  1646. $this->index = 0;
  1647. }
  1648. if ($this->debug > 0) {
  1649. if (isset($this->items[$this->last_item_seen])) {
  1650. $status = $this->items[$this->last_item_seen]->get_status();
  1651. }
  1652. error_log('status '.$status);
  1653. }
  1654. if (!empty($this->last_item_seen) &&
  1655. !empty($this->items[$this->last_item_seen]) &&
  1656. $this->items[$this->last_item_seen]->get_type() != 'dir'
  1657. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1658. //&& !$this->items[$this->last_item_seen]->is_done()
  1659. ) {
  1660. if ($this->debug > 2) {
  1661. 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);
  1662. }
  1663. $index = -1;
  1664. foreach ($this->ordered_items as $myindex => $item_id) {
  1665. if ($item_id == $this->last_item_seen) {
  1666. $index = $myindex;
  1667. break;
  1668. }
  1669. }
  1670. if ($index == -1) {
  1671. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1672. if ($this->debug > 2) {
  1673. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1674. }
  1675. return false;
  1676. } else {
  1677. $this->last = $this->last_item_seen;
  1678. $this->current = $this->last_item_seen;
  1679. $this->index = $index;
  1680. }
  1681. } else {
  1682. if ($this->debug > 2) {
  1683. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1684. }
  1685. $index = 0;
  1686. // Loop through all ordered items and stop at the first item that is
  1687. // not a directory *and* that has not been completed yet.
  1688. while ( !empty($this->ordered_items[$index]) AND
  1689. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1690. (
  1691. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1692. $this->items[$this->ordered_items[$index]]->is_done() === true
  1693. ) AND $index < $this->max_ordered_items) {
  1694. $index++;
  1695. }
  1696. $this->last = $this->current;
  1697. // current is
  1698. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1699. $this->index = $index;
  1700. if ($this->debug > 2) {
  1701. error_log('$index ' . $index);
  1702. }
  1703. if ($this->debug > 2) {
  1704. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1705. }
  1706. }
  1707. if ($this->debug > 2) {
  1708. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1709. }
  1710. }
  1711. /**
  1712. * Gets the information about an item in a format usable as JavaScript to update
  1713. * the JS API by just printing this content into the <head> section of the message frame
  1714. * @param int $item_id
  1715. * @return string
  1716. */
  1717. public function get_js_info($item_id = 0)
  1718. {
  1719. if ($this->debug > 0) {
  1720. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1721. }
  1722. $info = '';
  1723. $item_id = intval($item_id);
  1724. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1725. //if item is defined, return values from DB
  1726. $oItem = $this->items[$item_id];
  1727. $info .= '<script language="javascript">';
  1728. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1729. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1730. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1731. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1732. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1733. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1734. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1735. $info .= "top.set_flag_synchronized();";
  1736. $info .= '</script>';
  1737. if ($this->debug > 2) {
  1738. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1739. }
  1740. return $info;
  1741. } else {
  1742. // If item_id is empty, just update to default SCORM data.
  1743. $info .= '<script language="javascript">';
  1744. $info .= "top.set_score(" . learnpathItem::get_score() . ");\n";
  1745. $info .= "top.set_max(" . learnpathItem::get_max() . ");\n";
  1746. $info .= "top.set_min(" . learnpathItem::get_min() . ");\n";
  1747. $info .= "top.set_lesson_status('" . learnpathItem::get_status() . "');";
  1748. $info .= "top.set_session_time('" . learnpathItem::getScormTimeFromParameter('js') . "');";
  1749. $info .= "top.set_suspend_data('" . learnpathItem::get_suspend_data() . "');";
  1750. $info .= "top.set_saved_lesson_status('" . learnpathItem::get_status() . "');";
  1751. $info .= "top.set_flag_synchronized();";
  1752. $info .= '</script>';
  1753. if ($this->debug > 2) {
  1754. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1755. }
  1756. return $info;
  1757. }
  1758. }
  1759. /**
  1760. * Gets the js library from the database
  1761. * @return string The name of the javascript library to be used
  1762. */
  1763. public function get_js_lib()
  1764. {
  1765. $lib = '';
  1766. if (!empty ($this->js_lib)) {
  1767. $lib = $this->js_lib;
  1768. }
  1769. return $lib;
  1770. }
  1771. /**
  1772. * Gets the learnpath database ID
  1773. * @return integer Learnpath ID in the lp table
  1774. */
  1775. public function get_id()
  1776. {
  1777. if (!empty ($this->lp_id)) {
  1778. return $this->lp_id;
  1779. } else {
  1780. return 0;
  1781. }
  1782. }
  1783. /**
  1784. * Gets the last element URL.
  1785. * @return string URL to load into the viewer
  1786. */
  1787. public function get_last()
  1788. {
  1789. if ($this->debug > 0) {
  1790. error_log('New LP - In learnpath::get_last()', 0);
  1791. }
  1792. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1793. if (count($this->ordered_items) > 0) {
  1794. $this->index = count($this->ordered_items) - 1;
  1795. return $this->ordered_items[$this->index];
  1796. }
  1797. return false;
  1798. }
  1799. /**
  1800. * Gets the navigation bar for the learnpath display screen
  1801. * @return string The HTML string to use as a navigation bar
  1802. */
  1803. public function get_navigation_bar($idBar = null, $display = null)
  1804. {
  1805. if ($this->debug > 0) {
  1806. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1807. }
  1808. if (empty($idBar)) {
  1809. $idBar = 'control-top';
  1810. }
  1811. $navbar = null;
  1812. $lp_id = $this->lp_id;
  1813. $mycurrentitemid = $this->get_current_item_id();
  1814. $reportingText = get_lang('Reporting');
  1815. $previousText = get_lang('ScormPrevious');
  1816. $nextText = get_lang('ScormNext');
  1817. $fullScreenText = get_lang('ScormExitFullScreen');
  1818. if ($this->mode == 'fullscreen') {
  1819. $navbar = '
  1820. <span id="'.$idBar.'" class="buttons">
  1821. <a class="icon-toolbar" 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="'.$reportingText.'" id="stats_link">
  1822. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText . '</span>
  1823. </a>
  1824. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1825. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText . '</span>
  1826. </a>
  1827. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1828. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText. '</span>
  1829. </a>
  1830. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="'.$fullScreenText.'">
  1831. <span class="fa fa-columns"></span><span class="sr-only">' . $fullScreenText . '</span>
  1832. </a>
  1833. </span>';
  1834. } else {
  1835. $navbar = '
  1836. <span id="'.$idBar.'" class="buttons text-right">
  1837. <a class="icon-toolbar" 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="'.$reportingText.'" id="stats_link">
  1838. <span class="fa fa-info"></span><span class="sr-only">' . $reportingText. '</span>
  1839. </a>
  1840. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="'.$previousText.'">
  1841. <span class="fa fa-chevron-left"></span><span class="sr-only">' . $previousText. '</span>
  1842. </a>
  1843. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="'.$nextText.'">
  1844. <span class="fa fa-chevron-right"></span><span class="sr-only">' . $nextText . '</span>
  1845. </a>
  1846. </span>';
  1847. }
  1848. return $navbar;
  1849. }
  1850. /**
  1851. * Gets the next resource in queue (url).
  1852. * @return string URL to load into the viewer
  1853. */
  1854. public function get_next_index()
  1855. {
  1856. if ($this->debug > 0) {
  1857. error_log('New LP - In learnpath::get_next_index()', 0);
  1858. }
  1859. // TODO
  1860. $index = $this->index;
  1861. $index++;
  1862. if ($this->debug > 2) {
  1863. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1864. }
  1865. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') AND $index < $this->max_ordered_items) {
  1866. $index++;
  1867. if ($index == $this->max_ordered_items){
  1868. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir') {
  1869. return $this->index;
  1870. } else {
  1871. return $index;
  1872. }
  1873. }
  1874. }
  1875. if (empty ($this->ordered_items[$index])) {
  1876. return $this->index;
  1877. }
  1878. if ($this->debug > 2) {
  1879. error_log('New LP - index is now ' . $index, 0);
  1880. }
  1881. return $index;
  1882. }
  1883. /**
  1884. * Gets item_id for the next element
  1885. * @return integer Next item (DB) ID
  1886. */
  1887. public function get_next_item_id()
  1888. {
  1889. if ($this->debug > 0) {
  1890. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1891. }
  1892. $new_index = $this->get_next_index();
  1893. if (!empty ($new_index)) {
  1894. if (isset ($this->ordered_items[$new_index])) {
  1895. if ($this->debug > 2) {
  1896. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1897. }
  1898. return $this->ordered_items[$new_index];
  1899. }
  1900. }
  1901. if ($this->debug > 2) {
  1902. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1908. *
  1909. * Generally, the package provided is in the form of a zip file, so the function
  1910. * has been written to test a zip file. If not a zip, the function will return the
  1911. * default return value: ''
  1912. * @param string the path to the file
  1913. * @param string the original name of the file
  1914. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1915. */
  1916. public static function get_package_type($file_path, $file_name)
  1917. {
  1918. // Get name of the zip file without the extension.
  1919. $file_info = pathinfo($file_name);
  1920. $filename = $file_info['basename']; // Name including extension.
  1921. $extension = $file_info['extension']; // Extension only.
  1922. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1923. 'dll',
  1924. 'exe'
  1925. ))) {
  1926. return 'oogie';
  1927. }
  1928. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1929. 'dll',
  1930. 'exe'
  1931. ))) {
  1932. return 'woogie';
  1933. }
  1934. $zipFile = new PclZip($file_path);
  1935. // Check the zip content (real size and file extension).
  1936. $zipContentArray = $zipFile->listContent();
  1937. $package_type = '';
  1938. $at_root = false;
  1939. $manifest = '';
  1940. $aicc_match_crs = 0;
  1941. $aicc_match_au = 0;
  1942. $aicc_match_des = 0;
  1943. $aicc_match_cst = 0;
  1944. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1945. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1946. foreach ($zipContentArray as $thisContent) {
  1947. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1948. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1949. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1950. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1951. $package_type = 'scorm';
  1952. break; // Exit the foreach loop.
  1953. } elseif (
  1954. preg_match('/aicc\//i', $thisContent['filename']) ||
  1955. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1956. ) {
  1957. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1958. switch ($ext) {
  1959. case 'crs':
  1960. $aicc_match_crs = 1;
  1961. break;
  1962. case 'au':
  1963. $aicc_match_au = 1;
  1964. break;
  1965. case 'des':
  1966. $aicc_match_des = 1;
  1967. break;
  1968. case 'cst':
  1969. $aicc_match_cst = 1;
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1975. } else {
  1976. $package_type = '';
  1977. }
  1978. }
  1979. }
  1980. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1981. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1982. $package_type = 'aicc';
  1983. }
  1984. return $package_type;
  1985. }
  1986. /**
  1987. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1988. * @return string URL to load into the viewer
  1989. */
  1990. public function get_previous_index()
  1991. {
  1992. if ($this->debug > 0) {
  1993. error_log('New LP - In learnpath::get_previous_index()', 0);
  1994. }
  1995. $index = $this->index;
  1996. if (isset ($this->ordered_items[$index -1])) {
  1997. $index--;
  1998. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir')) {
  1999. $index--;
  2000. if ($index < 0) {
  2001. return $this->index;
  2002. }
  2003. }
  2004. } else {
  2005. if ($this->debug > 2) {
  2006. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2007. }
  2008. // There is no previous item.
  2009. }
  2010. return $index;
  2011. }
  2012. /**
  2013. * Gets item_id for the next element
  2014. * @return integer Previous item (DB) ID
  2015. */
  2016. public function get_previous_item_id()
  2017. {
  2018. if ($this->debug > 0) {
  2019. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2020. }
  2021. $new_index = $this->get_previous_index();
  2022. return $this->ordered_items[$new_index];
  2023. }
  2024. /**
  2025. * Gets the progress value from the progress_db attribute
  2026. * @return integer Current progress value
  2027. */
  2028. public function get_progress()
  2029. {
  2030. if ($this->debug > 0) {
  2031. error_log('New LP - In learnpath::get_progress()', 0);
  2032. }
  2033. if (!empty ($this->progress_db)) {
  2034. return $this->progress_db;
  2035. }
  2036. return 0;
  2037. }
  2038. /**
  2039. * Returns the HTML necessary to print a mediaplayer block inside a page
  2040. * @return string The mediaplayer HTML
  2041. */
  2042. public function get_mediaplayer($autostart = 'true')
  2043. {
  2044. $course_id = api_get_course_int_id();
  2045. $_course = api_get_course_info();
  2046. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2047. $tbl_lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2048. // Getting all the information about the item.
  2049. $sql = "SELECT * FROM $tbl_lp_item as lp
  2050. INNER JOIN $tbl_lp_item_view as lp_view
  2051. ON (lp.id = lp_view.lp_item_id AND lp.c_id = lp_view.c_id)
  2052. WHERE
  2053. lp.id = '".$_SESSION['oLP']->current."' AND
  2054. lp.c_id = $course_id AND
  2055. lp_view.c_id = $course_id";
  2056. $result = Database::query($sql);
  2057. $row = Database::fetch_assoc($result);
  2058. $output = '';
  2059. if (!empty ($row['audio'])) {
  2060. $list = $_SESSION['oLP']->get_toc();
  2061. $type_quiz = false;
  2062. foreach($list as $toc) {
  2063. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2064. $type_quiz = true;
  2065. }
  2066. }
  2067. if ($type_quiz) {
  2068. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2069. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2070. } else {
  2071. $autostart_audio = $autostart;
  2072. }
  2073. } else {
  2074. $autostart_audio = 'true';
  2075. }
  2076. $courseInfo = api_get_course_info();
  2077. $audio = $row['audio'];
  2078. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2079. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2080. if (!file_exists($file)) {
  2081. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2082. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2083. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2084. }
  2085. $player = Display::getMediaPlayer(
  2086. $file,
  2087. array(
  2088. 'id' => 'lp_audio_media_player',
  2089. 'url' => $url,
  2090. 'autoplay' => $autostart_audio,
  2091. 'width' => '100%'
  2092. )
  2093. );
  2094. // The mp3 player.
  2095. $output = '<div id="container">';
  2096. $output .= $player;
  2097. $output .= '</div>';
  2098. }
  2099. return $output;
  2100. }
  2101. /**
  2102. * @param int $studentId
  2103. * @param int $prerequisite
  2104. * @param array $courseInfo
  2105. * @param int $sessionId
  2106. *
  2107. * @return bool
  2108. *
  2109. */
  2110. public static function isBlockedByPrerequisite(
  2111. $studentId,
  2112. $prerequisite,
  2113. $courseInfo,
  2114. $sessionId
  2115. ) {
  2116. $isBlocked = false;
  2117. if (!empty($prerequisite)) {
  2118. $progress = self::getProgress(
  2119. $prerequisite,
  2120. $studentId,
  2121. $courseInfo['real_id'],
  2122. $sessionId
  2123. );
  2124. if ($progress < 100) {
  2125. $isBlocked = true;
  2126. }
  2127. }
  2128. return $isBlocked;
  2129. }
  2130. /**
  2131. * Checks if the learning path is visible for student after the progress
  2132. * of its prerequisite is completed, considering the time availability and
  2133. * the LP visibility.
  2134. * @param int $lp_id
  2135. * @param int $student_id
  2136. * @param string Course code (optional)
  2137. * @param int $sessionId
  2138. * @return bool
  2139. */
  2140. public static function is_lp_visible_for_student(
  2141. $lp_id,
  2142. $student_id,
  2143. $courseCode = null,
  2144. $sessionId = 0
  2145. ) {
  2146. $courseInfo = api_get_course_info($courseCode);
  2147. $lp_id = (int) $lp_id;
  2148. $sessionId = (int) $sessionId;
  2149. if (empty($sessionId)) {
  2150. $sessionId = api_get_session_id();
  2151. }
  2152. if (empty($courseInfo)) {
  2153. return false;
  2154. }
  2155. $itemInfo = api_get_item_property_info(
  2156. $courseInfo['real_id'],
  2157. TOOL_LEARNPATH,
  2158. $lp_id,
  2159. $sessionId
  2160. );
  2161. // If the item was deleted.
  2162. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2163. return false;
  2164. }
  2165. // @todo remove this query and load the row info as a parameter
  2166. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2167. // Get current prerequisite
  2168. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2169. FROM $tbl_learnpath
  2170. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2171. $rs = Database::query($sql);
  2172. $now = time();
  2173. if (Database::num_rows($rs) > 0) {
  2174. $row = Database::fetch_array($rs, 'ASSOC');
  2175. $prerequisite = $row['prerequisite'];
  2176. $is_visible = true;
  2177. $isBlocked = self::isBlockedByPrerequisite(
  2178. $student_id,
  2179. $prerequisite,
  2180. $courseInfo,
  2181. $sessionId
  2182. );
  2183. if ($isBlocked) {
  2184. $is_visible = false;
  2185. }
  2186. // Also check the time availability of the LP
  2187. if ($is_visible) {
  2188. // Adding visibility restrictions
  2189. if (!empty($row['publicated_on'])) {
  2190. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2191. $is_visible = false;
  2192. }
  2193. }
  2194. // Blocking empty start times see BT#2800
  2195. global $_custom;
  2196. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2197. $_custom['lps_hidden_when_no_start_date']
  2198. ) {
  2199. if (empty($row['publicated_on'])) {
  2200. $is_visible = false;
  2201. }
  2202. }
  2203. if (!empty($row['expired_on'])) {
  2204. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2205. $is_visible = false;
  2206. }
  2207. }
  2208. }
  2209. // Check if the subscription users/group to a LP is ON
  2210. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2211. // Try group
  2212. $is_visible = false;
  2213. // Checking only the user visibility
  2214. $userVisibility = api_get_item_visibility(
  2215. $courseInfo,
  2216. 'learnpath',
  2217. $row['id'],
  2218. $sessionId,
  2219. $student_id,
  2220. 'LearnpathSubscription'
  2221. );
  2222. if ($userVisibility == 1) {
  2223. $is_visible = true;
  2224. } else {
  2225. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2226. if (!empty($userGroups)) {
  2227. foreach ($userGroups as $groupInfo) {
  2228. $groupId = $groupInfo['iid'];
  2229. $userVisibility = api_get_item_visibility(
  2230. $courseInfo,
  2231. 'learnpath',
  2232. $row['id'],
  2233. $sessionId,
  2234. null,
  2235. 'LearnpathSubscription',
  2236. $groupId
  2237. );
  2238. if ($userVisibility == 1) {
  2239. $is_visible = true;
  2240. break;
  2241. }
  2242. }
  2243. }
  2244. }
  2245. }
  2246. return $is_visible;
  2247. }
  2248. return false;
  2249. }
  2250. /**
  2251. * @param int $lpId
  2252. * @param int $userId
  2253. * @param int $courseId
  2254. * @param int $sessionId
  2255. * @return int
  2256. */
  2257. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2258. {
  2259. $lpId = intval($lpId);
  2260. $userId = intval($userId);
  2261. $courseId = intval($courseId);
  2262. $sessionId = intval($sessionId);
  2263. $progress = 0;
  2264. $sessionCondition = api_get_session_condition($sessionId);
  2265. $table = Database::get_course_table(TABLE_LP_VIEW);
  2266. $sql = "SELECT * FROM $table
  2267. WHERE
  2268. c_id = ".$courseId." AND
  2269. lp_id = $lpId AND
  2270. user_id = $userId $sessionCondition";
  2271. $res = Database::query($sql);
  2272. if (Database::num_rows($res) > 0) {
  2273. $row = Database:: fetch_array($res);
  2274. $progress = $row['progress'];
  2275. }
  2276. return (int) $progress;
  2277. }
  2278. /**
  2279. * Displays a progress bar
  2280. * completed so far.
  2281. * @param integer $percentage Progress value to display
  2282. * @param string $text_add Text to display near the progress value
  2283. * @return string HTML string containing the progress bar
  2284. */
  2285. public static function get_progress_bar($percentage = -1, $text_add = '')
  2286. {
  2287. $text = $percentage . $text_add;
  2288. $output = '<div class="progress">
  2289. <div id="progress_bar_value" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2290. '. $text .'
  2291. </div>
  2292. </div>';
  2293. return $output;
  2294. }
  2295. /**
  2296. * @param string $mode can be '%' or 'abs'
  2297. * otherwise this value will be used $this->progress_bar_mode
  2298. * @return string
  2299. */
  2300. public function getProgressBar($mode = null)
  2301. {
  2302. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2303. return self::get_progress_bar($percentage, $text_add);
  2304. }
  2305. /**
  2306. * Gets the progress bar info to display inside the progress bar.
  2307. * Also used by scorm_api.php
  2308. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2309. * we display a number of completed elements per total elements
  2310. * @param integer $add Additional steps to fake as completed
  2311. * @return list array Percentage or number and symbol (% or /xx)
  2312. */
  2313. public function get_progress_bar_text($mode = '', $add = 0)
  2314. {
  2315. if ($this->debug > 0) {
  2316. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2317. }
  2318. if (empty($mode)) {
  2319. $mode = $this->progress_bar_mode;
  2320. }
  2321. $total_items = $this->getTotalItemsCountWithoutDirs();
  2322. if ($this->debug > 2) {
  2323. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2324. }
  2325. $completeItems = $this->get_complete_items_count();
  2326. if ($this->debug > 2) {
  2327. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2328. }
  2329. if ($add != 0) {
  2330. $completeItems += $add;
  2331. if ($this->debug > 2) {
  2332. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2333. }
  2334. }
  2335. $text = '';
  2336. if ($completeItems > $total_items) {
  2337. $completeItems = $total_items;
  2338. }
  2339. $percentage = 0;
  2340. if ($mode == '%') {
  2341. if ($total_items > 0) {
  2342. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2343. } else {
  2344. $percentage = 0;
  2345. }
  2346. $percentage = number_format($percentage, 0);
  2347. $text = '%';
  2348. } elseif ($mode == 'abs') {
  2349. $percentage = $completeItems;
  2350. $text = '/' . $total_items;
  2351. }
  2352. return array(
  2353. $percentage,
  2354. $text
  2355. );
  2356. }
  2357. /**
  2358. * Gets the progress bar mode
  2359. * @return string The progress bar mode attribute
  2360. */
  2361. public function get_progress_bar_mode()
  2362. {
  2363. if ($this->debug > 0) {
  2364. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2365. }
  2366. if (!empty ($this->progress_bar_mode)) {
  2367. return $this->progress_bar_mode;
  2368. } else {
  2369. return '%';
  2370. }
  2371. }
  2372. /**
  2373. * Gets the learnpath proximity (remote or local)
  2374. * @return string Learnpath proximity
  2375. */
  2376. public function get_proximity()
  2377. {
  2378. if ($this->debug > 0) {
  2379. error_log('New LP - In learnpath::get_proximity()', 0);
  2380. }
  2381. if (!empty ($this->proximity)) {
  2382. return $this->proximity;
  2383. } else {
  2384. return '';
  2385. }
  2386. }
  2387. /**
  2388. * Gets the learnpath theme (remote or local)
  2389. * @return string Learnpath theme
  2390. */
  2391. public function get_theme()
  2392. {
  2393. if ($this->debug > 0) {
  2394. error_log('New LP - In learnpath::get_theme()', 0);
  2395. }
  2396. if (!empty ($this->theme)) {
  2397. return $this->theme;
  2398. } else {
  2399. return '';
  2400. }
  2401. }
  2402. /**
  2403. * Gets the learnpath session id
  2404. * @return int
  2405. */
  2406. public function get_lp_session_id()
  2407. {
  2408. if ($this->debug > 0) {
  2409. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2410. }
  2411. if (!empty ($this->lp_session_id)) {
  2412. return (int) $this->lp_session_id;
  2413. } else {
  2414. return 0;
  2415. }
  2416. }
  2417. /**
  2418. * Gets the learnpath image
  2419. * @return string Web URL of the LP image
  2420. */
  2421. public function get_preview_image()
  2422. {
  2423. if ($this->debug > 0) {
  2424. error_log('New LP - In learnpath::get_preview_image()', 0);
  2425. }
  2426. if (!empty($this->preview_image)) {
  2427. return $this->preview_image;
  2428. } else {
  2429. return '';
  2430. }
  2431. }
  2432. /**
  2433. * @param string $size
  2434. * @param string $path_type
  2435. * @return bool|string
  2436. */
  2437. public function get_preview_image_path($size = null, $path_type = 'web')
  2438. {
  2439. $preview_image = $this->get_preview_image();
  2440. if (isset($preview_image) && !empty($preview_image)) {
  2441. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2442. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2443. if (isset($size)) {
  2444. $info = pathinfo($preview_image);
  2445. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2446. if (file_exists($image_sys_path.$image_custom_size)) {
  2447. if ($path_type == 'web') {
  2448. return $image_path.$image_custom_size;
  2449. } else {
  2450. return $image_sys_path.$image_custom_size;
  2451. }
  2452. }
  2453. } else {
  2454. if ($path_type == 'web') {
  2455. return $image_path.$preview_image;
  2456. } else {
  2457. return $image_sys_path.$preview_image;
  2458. }
  2459. }
  2460. }
  2461. return false;
  2462. }
  2463. /**
  2464. * Gets the learnpath author
  2465. * @return string LP's author
  2466. */
  2467. public function get_author()
  2468. {
  2469. if ($this->debug > 0) {
  2470. error_log('New LP - In learnpath::get_author()', 0);
  2471. }
  2472. if (!empty ($this->author)) {
  2473. return $this->author;
  2474. } else {
  2475. return '';
  2476. }
  2477. }
  2478. /**
  2479. * Gets the learnpath author
  2480. * @return string LP's author
  2481. */
  2482. public function get_hide_toc_frame()
  2483. {
  2484. if ($this->debug > 0) {
  2485. error_log('New LP - In learnpath::get_author()', 0);
  2486. }
  2487. if (!empty ($this->hide_toc_frame)) {
  2488. return $this->hide_toc_frame;
  2489. } else {
  2490. return '';
  2491. }
  2492. }
  2493. /**
  2494. * Generate a new prerequisites string for a given item. If this item was a sco and
  2495. * its prerequisites were strings (instead of IDs), then transform those strings into
  2496. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2497. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2498. * same rule as the scorm_export() method
  2499. * @param integer Item ID
  2500. * @return string Prerequisites string ready for the export as SCORM
  2501. */
  2502. public function get_scorm_prereq_string($item_id)
  2503. {
  2504. if ($this->debug > 0) {
  2505. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2506. }
  2507. if (!is_object($this->items[$item_id])) {
  2508. return false;
  2509. }
  2510. /** @var learnpathItem $oItem */
  2511. $oItem = $this->items[$item_id];
  2512. $prereq = $oItem->get_prereq_string();
  2513. if (empty($prereq)) {
  2514. return '';
  2515. }
  2516. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2517. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2518. // then simply return it (with the ITEM_ prefix).
  2519. //return 'ITEM_' . $prereq;
  2520. return $this->items[$prereq]->ref;
  2521. } else {
  2522. if (isset($this->refs_list[$prereq])) {
  2523. // It's a simple string item from which the ID can be found in the refs list,
  2524. // so we can transform it directly to an ID for export.
  2525. return $this->items[$this->refs_list[$prereq]]->ref;
  2526. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2527. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2528. } else {
  2529. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2530. // and replace them, one by one, by the internal IDs (chamilo db)
  2531. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2532. // by a space as well.
  2533. $find = array (
  2534. '&',
  2535. '|',
  2536. '~',
  2537. '=',
  2538. '<>',
  2539. '{',
  2540. '}',
  2541. '*',
  2542. '(',
  2543. ')'
  2544. );
  2545. $replace = array (
  2546. ' ',
  2547. ' ',
  2548. ' ',
  2549. ' ',
  2550. ' ',
  2551. ' ',
  2552. ' ',
  2553. ' ',
  2554. ' ',
  2555. ' '
  2556. );
  2557. $prereq_mod = str_replace($find, $replace, $prereq);
  2558. $ids = explode(' ', $prereq_mod);
  2559. foreach ($ids as $id) {
  2560. $id = trim($id);
  2561. if (isset ($this->refs_list[$id])) {
  2562. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2563. }
  2564. }
  2565. return $prereq;
  2566. }
  2567. }
  2568. }
  2569. /**
  2570. * Returns the XML DOM document's node
  2571. * @param resource Reference to a list of objects to search for the given ITEM_*
  2572. * @param string The identifier to look for
  2573. * @return mixed The reference to the element found with that identifier. False if not found
  2574. */
  2575. public function get_scorm_xml_node(& $children, $id)
  2576. {
  2577. for ($i = 0; $i < $children->length; $i++) {
  2578. $item_temp = $children->item($i);
  2579. if ($item_temp->nodeName == 'item') {
  2580. if ($item_temp->getAttribute('identifier') == $id) {
  2581. return $item_temp;
  2582. }
  2583. }
  2584. $subchildren = $item_temp->childNodes;
  2585. if ($subchildren && $subchildren->length > 0) {
  2586. $val = $this->get_scorm_xml_node($subchildren, $id);
  2587. if (is_object($val)) {
  2588. return $val;
  2589. }
  2590. }
  2591. }
  2592. return false;
  2593. }
  2594. /**
  2595. * Gets the status list for all LP's items
  2596. * @return array Array of [index] => [item ID => current status]
  2597. */
  2598. public function get_items_status_list()
  2599. {
  2600. if ($this->debug > 0) {
  2601. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2602. }
  2603. $list = array ();
  2604. foreach ($this->ordered_items as $item_id) {
  2605. $list[] = array (
  2606. $item_id => $this->items[$item_id]->get_status()
  2607. );
  2608. }
  2609. return $list;
  2610. }
  2611. /**
  2612. * Return the number of interactions for the given learnpath Item View ID.
  2613. * This method can be used as static.
  2614. * @param integer Item View ID
  2615. * @param integer course id
  2616. * @return integer Number of interactions
  2617. */
  2618. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2619. {
  2620. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2621. $lp_iv_id = intval($lp_iv_id);
  2622. $course_id = intval($course_id);
  2623. $sql = "SELECT count(*) FROM $table
  2624. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2625. $res = Database::query($sql);
  2626. $num = 0;
  2627. if (Database::num_rows($res)) {
  2628. $row = Database::fetch_array($res);
  2629. $num = $row[0];
  2630. }
  2631. return $num;
  2632. }
  2633. /**
  2634. * Return the interactions as an array for the given lp_iv_id.
  2635. * This method can be used as static.
  2636. * @param integer Learnpath Item View ID
  2637. * @return array
  2638. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2639. */
  2640. public static function get_iv_interactions_array($lp_iv_id)
  2641. {
  2642. $course_id = api_get_course_int_id();
  2643. $list = array();
  2644. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2645. if (empty($lp_iv_id)) {
  2646. return array();
  2647. }
  2648. $sql = "SELECT * FROM $table
  2649. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2650. ORDER BY order_id ASC";
  2651. $res = Database::query($sql);
  2652. $num = Database::num_rows($res);
  2653. if ($num > 0) {
  2654. $list[] = array (
  2655. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2656. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2657. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2658. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2659. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2660. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2661. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2662. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2663. );
  2664. while ($row = Database::fetch_array($res)) {
  2665. $list[] = array (
  2666. 'order_id' => ($row['order_id'] + 1),
  2667. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2668. 'type' => $row['interaction_type'],
  2669. 'time' => $row['completion_time'],
  2670. //'correct_responses' => $row['correct_responses'],
  2671. 'correct_responses' => '', // Hide correct responses from students.
  2672. 'student_response' => $row['student_response'],
  2673. 'result' => $row['result'],
  2674. 'latency' => $row['latency']
  2675. );
  2676. }
  2677. }
  2678. return $list;
  2679. }
  2680. /**
  2681. * Return the number of objectives for the given learnpath Item View ID.
  2682. * This method can be used as static.
  2683. * @param integer Item View ID
  2684. * @return integer Number of objectives
  2685. */
  2686. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2687. {
  2688. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2689. $course_id = intval($course_id);
  2690. $lp_iv_id = intval($lp_iv_id);
  2691. $sql = "SELECT count(*) FROM $table
  2692. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2693. //@todo seems that this always returns 0
  2694. $res = Database::query($sql);
  2695. $num = 0;
  2696. if (Database::num_rows($res)) {
  2697. $row = Database::fetch_array($res);
  2698. $num = $row[0];
  2699. }
  2700. return $num;
  2701. }
  2702. /**
  2703. * Return the objectives as an array for the given lp_iv_id.
  2704. * This method can be used as static.
  2705. * @param integer Learnpath Item View ID
  2706. * @return array
  2707. * @todo Translate labels
  2708. */
  2709. public static function get_iv_objectives_array($lp_iv_id = 0)
  2710. {
  2711. $course_id = api_get_course_int_id();
  2712. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2713. $sql = "SELECT * FROM $table
  2714. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2715. ORDER BY order_id ASC";
  2716. $res = Database::query($sql);
  2717. $num = Database::num_rows($res);
  2718. $list = array();
  2719. if ($num > 0) {
  2720. $list[] = array(
  2721. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2722. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2723. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2724. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2725. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2726. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2727. );
  2728. while ($row = Database::fetch_array($res)) {
  2729. $list[] = array (
  2730. 'order_id' => ($row['order_id'] + 1),
  2731. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2732. 'score_raw' => $row['score_raw'],
  2733. 'score_max' => $row['score_max'],
  2734. 'score_min' => $row['score_min'],
  2735. 'status' => $row['status']
  2736. );
  2737. }
  2738. }
  2739. return $list;
  2740. }
  2741. /**
  2742. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2743. * used by get_html_toc() to be ready to display
  2744. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2745. */
  2746. public function get_toc()
  2747. {
  2748. if ($this->debug > 0) {
  2749. error_log('learnpath::get_toc()', 0);
  2750. }
  2751. $toc = array();
  2752. foreach ($this->ordered_items as $item_id) {
  2753. if ($this->debug > 2) {
  2754. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2755. }
  2756. // TODO: Change this link generation and use new function instead.
  2757. $toc[] = array(
  2758. 'id' => $item_id,
  2759. 'title' => $this->items[$item_id]->get_title(),
  2760. 'status' => $this->items[$item_id]->get_status(),
  2761. 'level' => $this->items[$item_id]->get_level(),
  2762. 'type' => $this->items[$item_id]->get_type(),
  2763. 'description' => $this->items[$item_id]->get_description(),
  2764. 'path' => $this->items[$item_id]->get_path(),
  2765. );
  2766. }
  2767. if ($this->debug > 2) {
  2768. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2769. }
  2770. return $toc;
  2771. }
  2772. /**
  2773. * Generate and return the table of contents for this learnpath. The JS
  2774. * table returned is used inside of scorm_api.php
  2775. * @return string A JS array vairiable construction
  2776. */
  2777. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2778. {
  2779. if ($this->debug > 0) {
  2780. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2781. }
  2782. $toc = $varname.' = new Array();';
  2783. foreach ($this->ordered_items as $item_id) {
  2784. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2785. }
  2786. if ($this->debug > 2) {
  2787. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2788. }
  2789. return $toc;
  2790. }
  2791. /**
  2792. * Gets the learning path type
  2793. * @param boolean Return the name? If false, return the ID. Default is false.
  2794. * @return mixed Type ID or name, depending on the parameter
  2795. */
  2796. public function get_type($get_name = false)
  2797. {
  2798. $res = false;
  2799. if ($this->debug > 0) {
  2800. error_log('New LP - In learnpath::get_type()', 0);
  2801. }
  2802. if (!empty ($this->type)) {
  2803. if ($get_name) {
  2804. // Get it from the lp_type table in main db.
  2805. } else {
  2806. $res = $this->type;
  2807. }
  2808. }
  2809. if ($this->debug > 2) {
  2810. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2811. }
  2812. return $res;
  2813. }
  2814. /**
  2815. * Gets the learning path type as static method
  2816. * @param boolean Return the name? If false, return the ID. Default is false.
  2817. * @return mixed Type ID or name, depending on the parameter
  2818. */
  2819. public static function get_type_static($lp_id = 0)
  2820. {
  2821. $course_id = api_get_course_int_id();
  2822. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  2823. $lp_id = intval($lp_id);
  2824. $sql = "SELECT lp_type FROM $tbl_lp
  2825. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2826. $res = Database::query($sql);
  2827. if ($res === false) {
  2828. return null;
  2829. }
  2830. if (Database::num_rows($res) <= 0) {
  2831. return null;
  2832. }
  2833. $row = Database::fetch_array($res);
  2834. return $row['lp_type'];
  2835. }
  2836. /**
  2837. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2838. * This method can be used as abstract and is recursive
  2839. * @param integer Learnpath ID
  2840. * @param integer Parent ID of the items to look for
  2841. * @return mixed Ordered list of item IDs or false on error
  2842. */
  2843. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2844. {
  2845. if (empty($course_id)) {
  2846. $course_id = api_get_course_int_id();
  2847. } else {
  2848. $course_id = intval($course_id);
  2849. }
  2850. $list = array();
  2851. if (empty($lp)) {
  2852. return false;
  2853. }
  2854. $lp = intval($lp);
  2855. $parent = intval($parent);
  2856. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  2857. $sql = "SELECT id FROM $tbl_lp_item
  2858. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2859. ORDER BY display_order";
  2860. $res = Database::query($sql);
  2861. while ($row = Database::fetch_array($res)) {
  2862. $sublist = learnpath::get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2863. $list[] = $row['id'];
  2864. foreach ($sublist as $item) {
  2865. $list[] = $item;
  2866. }
  2867. }
  2868. return $list;
  2869. }
  2870. /**
  2871. * @return array
  2872. */
  2873. public static function getChapterTypes()
  2874. {
  2875. return array(
  2876. 'dir'
  2877. );
  2878. }
  2879. /**
  2880. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2881. * @return string HTML TOC ready to display
  2882. */
  2883. public function get_html_toc($toc_list = null)
  2884. {
  2885. if ($this->debug > 0) {
  2886. error_log('In learnpath::get_html_toc()', 0);
  2887. }
  2888. if (empty($toc_list)) {
  2889. $toc_list = $this->get_toc();
  2890. }
  2891. $html = '<div class="scorm-body">';
  2892. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2893. // Temporary variables.
  2894. $mycurrentitemid = $this->get_current_item_id();
  2895. $color_counter = 0;
  2896. $i = 0;
  2897. foreach ($toc_list as $item) {
  2898. // Style Status
  2899. $class_name = [
  2900. 'not attempted' => 'scorm_not_attempted',
  2901. 'incomplete' => 'scorm_not_attempted',
  2902. 'failed' => 'scorm_failed',
  2903. 'completed' => 'scorm_completed',
  2904. 'passed' => 'scorm_completed',
  2905. 'succeeded' => 'scorm_completed',
  2906. 'browsed' => 'scorm_completed',
  2907. ];
  2908. $rowColor = ' ';
  2909. $dirTypes = self::getChapterTypes();
  2910. if (in_array($item['type'], $dirTypes)) {
  2911. $rowColor ='scorm_item_section ';
  2912. }
  2913. if ($item['id'] == $this->current) {
  2914. $rowColor = 'scorm_item_normal '.$rowColor.' scorm_highlight';
  2915. } elseif (!in_array($item['type'], $dirTypes)) {
  2916. $rowColor = 'scorm_item_normal '.$rowColor.' ';
  2917. }
  2918. $html .= '<div id="toc_' . $item['id'] . '" class="' . $rowColor . ' ' . $class_name[$item['status']] . '">';
  2919. // Learning path title
  2920. $title = $item['title'];
  2921. if (empty ($title)) {
  2922. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2923. }
  2924. $title = Security::remove_XSS($title);
  2925. // Learning path personalization
  2926. // build the LP tree
  2927. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2928. $description = $item['description'];
  2929. if (empty($description)) {
  2930. $description = $title;
  2931. }
  2932. if (in_array($item['type'], $dirTypes)) {
  2933. // Chapters
  2934. $html .= '<div class="section level_'.$item['level'].'" title="'.$description.'" >';
  2935. } else {
  2936. $html .= '<div class="item level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2937. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2938. }
  2939. if (in_array($item['type'], $dirTypes)) {
  2940. // Chapter
  2941. // if you want to put an image before, you should use css
  2942. $html .= stripslashes($title);
  2943. } else {
  2944. $this->get_link('http', $item['id'], $toc_list);
  2945. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2946. }
  2947. $html .= "</div>";
  2948. if ($rowColor != '') {
  2949. $html .= '</div>';
  2950. }
  2951. $color_counter++;
  2952. }
  2953. $html .= "</div>";
  2954. $html .= "</div>";
  2955. return $html;
  2956. }
  2957. /**
  2958. * Returns an HTML-formatted string ready to display with teacher buttons
  2959. * in LP view menu
  2960. * @return string HTML TOC ready to display
  2961. */
  2962. public function get_teacher_toc_buttons()
  2963. {
  2964. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2965. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  2966. $html = '';
  2967. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2968. $gradebook = '';
  2969. if (!empty($_GET['gradebook'])) {
  2970. $gradebook = Security:: remove_XSS($_GET['gradebook']);
  2971. }
  2972. if ($this->get_lp_session_id() == api_get_session_id()) {
  2973. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  2974. $html .= '<div class="btn-group">';
  2975. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&gradebook=$gradebook&action=build&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  2976. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  2977. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "&isStudentView=false' target='_parent'>" .
  2978. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  2979. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&gradebook=$gradebook&action=edit&lp_id=" . $this->lp_id.'&isStudentView=false">' .
  2980. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  2981. $html .= '</div>';
  2982. $html .= '</div>';
  2983. }
  2984. }
  2985. return $html;
  2986. }
  2987. /**
  2988. * Gets the learnpath maker name - generally the editor's name
  2989. * @return string Learnpath maker name
  2990. */
  2991. public function get_maker()
  2992. {
  2993. if ($this->debug > 0) {
  2994. error_log('New LP - In learnpath::get_maker()', 0);
  2995. }
  2996. if (!empty ($this->maker)) {
  2997. return $this->maker;
  2998. } else {
  2999. return '';
  3000. }
  3001. }
  3002. /**
  3003. * Gets the learnpath name/title
  3004. * @return string Learnpath name/title
  3005. */
  3006. public function get_name()
  3007. {
  3008. if ($this->debug > 0) {
  3009. error_log('New LP - In learnpath::get_name()', 0);
  3010. }
  3011. if (!empty ($this->name)) {
  3012. return $this->name;
  3013. } else {
  3014. return 'N/A';
  3015. }
  3016. }
  3017. /**
  3018. * Gets a link to the resource from the present location, depending on item ID.
  3019. * @param string $type Type of link expected
  3020. * @param integer $item_id Learnpath item ID
  3021. * @return string $provided_toc Link to the lp_item resource
  3022. */
  3023. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3024. {
  3025. $course_id = $this->get_course_int_id();
  3026. if ($this->debug > 0) {
  3027. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3028. }
  3029. if (empty($item_id)) {
  3030. if ($this->debug > 2) {
  3031. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3032. }
  3033. $item_id = $this->get_current_item_id();
  3034. }
  3035. if (empty($item_id)) {
  3036. if ($this->debug > 2) {
  3037. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3038. }
  3039. //still empty, this means there was no item_id given and we are not in an object context or
  3040. //the object property is empty, return empty link
  3041. $item_id = $this->first();
  3042. return '';
  3043. }
  3044. $file = '';
  3045. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3046. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3047. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3048. $item_id = intval($item_id);
  3049. $sql = "SELECT
  3050. l.lp_type as ltype,
  3051. l.path as lpath,
  3052. li.item_type as litype,
  3053. li.path as lipath,
  3054. li.parameters as liparams
  3055. FROM $lp_table l
  3056. INNER JOIN $lp_item_table li
  3057. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3058. WHERE li.id = $item_id ";
  3059. if ($this->debug > 2) {
  3060. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3061. }
  3062. $res = Database::query($sql);
  3063. if (Database::num_rows($res) > 0) {
  3064. $row = Database::fetch_array($res);
  3065. $lp_type = $row['ltype'];
  3066. $lp_path = $row['lpath'];
  3067. $lp_item_type = $row['litype'];
  3068. $lp_item_path = $row['lipath'];
  3069. $lp_item_params = $row['liparams'];
  3070. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3071. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3072. }
  3073. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3074. if ($type == 'http') {
  3075. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3076. } else {
  3077. $course_path = $sys_course_path; //system path
  3078. }
  3079. // 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.
  3080. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3081. $lp_type = 1;
  3082. }
  3083. if ($this->debug > 2) {
  3084. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3085. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3086. }
  3087. // Now go through the specific cases to get the end of the path
  3088. // @todo Use constants instead of int values.
  3089. switch ($lp_type) {
  3090. case 1 :
  3091. $file = self::rl_get_resource_link_for_learnpath(
  3092. $course_id,
  3093. $this->get_id(),
  3094. $item_id,
  3095. $this->get_view_id()
  3096. );
  3097. if ($this->debug > 0) {
  3098. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3099. }
  3100. switch ($lp_item_type) {
  3101. case 'dir':
  3102. $file = 'lp_content.php?type=dir';
  3103. break;
  3104. case 'link':
  3105. if (Link::is_youtube_link($file)) {
  3106. $src = Link::get_youtube_video_id($file);
  3107. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=youtube&source='.$src;
  3108. } elseif (Link::isVimeoLink($file)) {
  3109. $src = Link::getVimeoLinkId($file);
  3110. $file = api_get_path(WEB_CODE_PATH) . 'lp/embed.php?type=vimeo&source='.$src;
  3111. } else {
  3112. // If the current site is HTTPS and the link is
  3113. // HTTP, browsers will refuse opening the link
  3114. $urlId = api_get_current_access_url_id();
  3115. $url = api_get_access_url($urlId, false);
  3116. $protocol = substr($url['url'], 0, 5);
  3117. if ($protocol === 'https') {
  3118. $linkProtocol = substr($file, 0, 5);
  3119. if ($linkProtocol === 'http:') {
  3120. //this is the special intervention case
  3121. $file = api_get_path(WEB_CODE_PATH).'lp/embed.php?type=nonhttps&source='.urlencode($file);
  3122. }
  3123. }
  3124. }
  3125. break;
  3126. case 'quiz':
  3127. // Check how much attempts of a exercise exits in lp
  3128. $lp_item_id = $this->get_current_item_id();
  3129. $lp_view_id = $this->get_view_id();
  3130. $prevent_reinit = null;
  3131. if (isset($this->items[$this->current])) {
  3132. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3133. }
  3134. if (empty($provided_toc)) {
  3135. if ($this->debug > 0) {
  3136. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3137. }
  3138. $list = $this->get_toc();
  3139. } else {
  3140. if ($this->debug > 0) {
  3141. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3142. }
  3143. $list = $provided_toc;
  3144. }
  3145. $type_quiz = false;
  3146. foreach ($list as $toc) {
  3147. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3148. $type_quiz = true;
  3149. }
  3150. }
  3151. if ($type_quiz) {
  3152. $lp_item_id = intval($lp_item_id);
  3153. $lp_view_id = intval($lp_view_id);
  3154. $sql = "SELECT count(*) FROM $lp_item_view_table
  3155. WHERE
  3156. c_id = $course_id AND
  3157. lp_item_id='".$lp_item_id."' AND
  3158. lp_view_id ='".$lp_view_id."' AND
  3159. status='completed'";
  3160. $result = Database::query($sql);
  3161. $row_count = Database:: fetch_row($result);
  3162. $count_item_view = (int)$row_count[0];
  3163. $not_multiple_attempt = 0;
  3164. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3165. $not_multiple_attempt = 1;
  3166. }
  3167. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3168. }
  3169. break;
  3170. }
  3171. $tmp_array = explode('/', $file);
  3172. $document_name = $tmp_array[count($tmp_array) - 1];
  3173. if (strpos($document_name, '_DELETED_')) {
  3174. $file = 'blank.php?error=document_deleted';
  3175. }
  3176. break;
  3177. case 2 :
  3178. if ($this->debug > 2) {
  3179. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3180. }
  3181. if ($lp_item_type != 'dir') {
  3182. // Quite complex here:
  3183. // We want to make sure 'http://' (and similar) links can
  3184. // be loaded as is (withouth the Chamilo path in front) but
  3185. // some contents use this form: resource.htm?resource=http://blablabla
  3186. // which means we have to find a protocol at the path's start, otherwise
  3187. // it should not be considered as an external URL.
  3188. //if ($this->prerequisites_match($item_id)) {
  3189. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3190. if ($this->debug > 2) {
  3191. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3192. }
  3193. // Distant url, return as is.
  3194. $file = $lp_item_path;
  3195. } else {
  3196. if ($this->debug > 2) {
  3197. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3198. }
  3199. // Prevent getting untranslatable urls.
  3200. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3201. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3202. // Prepare the path.
  3203. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3204. // TODO: Fix this for urls with protocol header.
  3205. $file = str_replace('//', '/', $file);
  3206. $file = str_replace(':/', '://', $file);
  3207. if (substr($lp_path, -1) == '/') {
  3208. $lp_path = substr($lp_path, 0, -1);
  3209. }
  3210. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3211. // if file not found.
  3212. $decoded = html_entity_decode($lp_item_path);
  3213. list ($decoded) = explode('?', $decoded);
  3214. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3215. $file = self::rl_get_resource_link_for_learnpath(
  3216. $course_id,
  3217. $this->get_id(),
  3218. $item_id,
  3219. $this->get_view_id()
  3220. );
  3221. if (empty($file)) {
  3222. $file = 'blank.php?error=document_not_found';
  3223. } else {
  3224. $tmp_array = explode('/', $file);
  3225. $document_name = $tmp_array[count($tmp_array) - 1];
  3226. if (strpos($document_name, '_DELETED_')) {
  3227. $file = 'blank.php?error=document_deleted';
  3228. } else {
  3229. $file = 'blank.php?error=document_not_found';
  3230. }
  3231. }
  3232. } else {
  3233. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3234. }
  3235. }
  3236. }
  3237. // We want to use parameters if they were defined in the imsmanifest
  3238. if (strpos($file, 'blank.php') === false) {
  3239. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3240. }
  3241. } else {
  3242. $file = 'lp_content.php?type=dir';
  3243. }
  3244. break;
  3245. case 3 :
  3246. if ($this->debug > 2) {
  3247. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3248. }
  3249. // Formatting AICC HACP append URL.
  3250. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'lp/aicc_hacp.php') . '&';
  3251. if (!empty($lp_item_params)) {
  3252. $aicc_append .= $lp_item_params . '&';
  3253. }
  3254. if ($lp_item_type != 'dir') {
  3255. // Quite complex here:
  3256. // We want to make sure 'http://' (and similar) links can
  3257. // be loaded as is (withouth the Chamilo path in front) but
  3258. // some contents use this form: resource.htm?resource=http://blablabla
  3259. // which means we have to find a protocol at the path's start, otherwise
  3260. // it should not be considered as an external URL.
  3261. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3262. if ($this->debug > 2) {
  3263. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3264. }
  3265. // Distant url, return as is.
  3266. $file = $lp_item_path;
  3267. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3268. /*
  3269. if (stristr($file,'<servername>') !== false) {
  3270. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3271. }
  3272. */
  3273. if (stripos($file, '<servername>') !== false) {
  3274. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3275. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3276. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3277. }
  3278. //
  3279. $file .= $aicc_append;
  3280. } else {
  3281. if ($this->debug > 2) {
  3282. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3283. }
  3284. // Prevent getting untranslatable urls.
  3285. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3286. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3287. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3288. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3289. // TODO: Fix this for urls with protocol header.
  3290. $file = str_replace('//', '/', $file);
  3291. $file = str_replace(':/', '://', $file);
  3292. $file .= $aicc_append;
  3293. }
  3294. } else {
  3295. $file = 'lp_content.php?type=dir';
  3296. }
  3297. break;
  3298. case 4 :
  3299. break;
  3300. default :
  3301. break;
  3302. }
  3303. // Replace &amp; by & because &amp; will break URL with params
  3304. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3305. }
  3306. if ($this->debug > 2) {
  3307. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3308. }
  3309. return $file;
  3310. }
  3311. /**
  3312. * Gets the latest usable view or generate a new one
  3313. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3314. * @return integer DB lp_view id
  3315. */
  3316. public function get_view($attempt_num = 0)
  3317. {
  3318. if ($this->debug > 0) {
  3319. error_log('New LP - In learnpath::get_view()', 0);
  3320. }
  3321. $search = '';
  3322. // Use $attempt_num to enable multi-views management (disabled so far).
  3323. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3324. $search = 'AND view_count = ' . $attempt_num;
  3325. }
  3326. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3327. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3328. $course_id = api_get_course_int_id();
  3329. $sessionId = api_get_session_id();
  3330. $sql = "SELECT id, view_count FROM $lp_view_table
  3331. WHERE
  3332. c_id = " . $course_id . " AND
  3333. lp_id = " . $this->get_id() . " AND
  3334. user_id = " . $this->get_user_id() . " AND
  3335. session_id = $sessionId
  3336. $search
  3337. ORDER BY view_count DESC";
  3338. $res = Database::query($sql);
  3339. if (Database::num_rows($res) > 0) {
  3340. $row = Database::fetch_array($res);
  3341. $this->lp_view_id = $row['id'];
  3342. } else if (!api_is_invitee()) {
  3343. // There is no database record, create one.
  3344. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3345. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3346. Database::query($sql);
  3347. $id = Database::insert_id();
  3348. $this->lp_view_id = $id;
  3349. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3350. Database::query($sql);
  3351. }
  3352. return $this->lp_view_id;
  3353. }
  3354. /**
  3355. * Gets the current view id
  3356. * @return integer View ID (from lp_view)
  3357. */
  3358. public function get_view_id()
  3359. {
  3360. if ($this->debug > 0) {
  3361. error_log('New LP - In learnpath::get_view_id()', 0);
  3362. }
  3363. if (!empty ($this->lp_view_id)) {
  3364. return $this->lp_view_id;
  3365. } else {
  3366. return 0;
  3367. }
  3368. }
  3369. /**
  3370. * Gets the update queue
  3371. * @return array Array containing IDs of items to be updated by JavaScript
  3372. */
  3373. public function get_update_queue()
  3374. {
  3375. if ($this->debug > 0) {
  3376. error_log('New LP - In learnpath::get_update_queue()', 0);
  3377. }
  3378. return $this->update_queue;
  3379. }
  3380. /**
  3381. * Gets the user ID
  3382. * @return integer User ID
  3383. */
  3384. public function get_user_id()
  3385. {
  3386. if ($this->debug > 0) {
  3387. error_log('New LP - In learnpath::get_user_id()', 0);
  3388. }
  3389. if (!empty ($this->user_id)) {
  3390. return $this->user_id;
  3391. } else {
  3392. return false;
  3393. }
  3394. }
  3395. /**
  3396. * Checks if any of the items has an audio element attached
  3397. * @return bool True or false
  3398. */
  3399. public function has_audio()
  3400. {
  3401. if ($this->debug > 1) {
  3402. error_log('New LP - In learnpath::has_audio()', 0);
  3403. }
  3404. $has = false;
  3405. foreach ($this->items as $i => $item) {
  3406. if (!empty ($this->items[$i]->audio)) {
  3407. $has = true;
  3408. break;
  3409. }
  3410. }
  3411. return $has;
  3412. }
  3413. /**
  3414. * Logs a message into a file
  3415. * @param string Message to log
  3416. * @return boolean True on success, false on error or if msg empty
  3417. */
  3418. public function log($msg)
  3419. {
  3420. if ($this->debug > 0) {
  3421. error_log('New LP - In learnpath::log()', 0);
  3422. }
  3423. // TODO
  3424. $this->error .= $msg;
  3425. return true;
  3426. }
  3427. /**
  3428. * Moves an item up and down at its level
  3429. * @param integer Item to move up and down
  3430. * @param string Direction 'up' or 'down'
  3431. * @return integer New display order, or false on error
  3432. */
  3433. public function move_item($id, $direction)
  3434. {
  3435. $course_id = api_get_course_int_id();
  3436. if ($this->debug > 0) {
  3437. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3438. }
  3439. if (empty($id) || empty($direction)) {
  3440. return false;
  3441. }
  3442. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3443. $sql_sel = "SELECT *
  3444. FROM " . $tbl_lp_item . "
  3445. WHERE c_id = ".$course_id." AND id = " . $id;
  3446. $res_sel = Database::query($sql_sel);
  3447. // Check if elem exists.
  3448. if (Database::num_rows($res_sel) < 1) {
  3449. return false;
  3450. }
  3451. // Gather data.
  3452. $row = Database::fetch_array($res_sel);
  3453. $previous = $row['previous_item_id'];
  3454. $next = $row['next_item_id'];
  3455. $display = $row['display_order'];
  3456. $parent = $row['parent_item_id'];
  3457. $lp = $row['lp_id'];
  3458. // Update the item (switch with previous/next one).
  3459. switch ($direction) {
  3460. case 'up':
  3461. if ($this->debug > 2) {
  3462. error_log('Movement up detected', 0);
  3463. }
  3464. if ($display <= 1) { /*do nothing*/
  3465. } else {
  3466. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3467. WHERE c_id = ".$course_id." AND id = $previous";
  3468. if ($this->debug > 2) {
  3469. error_log('Selecting previous: ' . $sql_sel2, 0);
  3470. }
  3471. $res_sel2 = Database::query($sql_sel2);
  3472. if (Database::num_rows($res_sel2) < 1) {
  3473. $previous_previous = 0;
  3474. }
  3475. // Gather data.
  3476. $row2 = Database::fetch_array($res_sel2);
  3477. $previous_previous = $row2['previous_item_id'];
  3478. // Update previous_previous item (switch "next" with current).
  3479. if ($previous_previous != 0) {
  3480. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3481. next_item_id = $id
  3482. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3483. if ($this->debug > 2) {
  3484. error_log($sql_upd2, 0);
  3485. }
  3486. Database::query($sql_upd2);
  3487. }
  3488. // Update previous item (switch with current).
  3489. if ($previous != 0) {
  3490. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3491. next_item_id = $next,
  3492. previous_item_id = $id,
  3493. display_order = display_order +1
  3494. WHERE c_id = ".$course_id." AND id = $previous";
  3495. if ($this->debug > 2) {
  3496. error_log($sql_upd2, 0);
  3497. }
  3498. Database::query($sql_upd2);
  3499. }
  3500. // Update current item (switch with previous).
  3501. if ($id != 0) {
  3502. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3503. next_item_id = $previous,
  3504. previous_item_id = $previous_previous,
  3505. display_order = display_order-1
  3506. WHERE c_id = ".$course_id." AND id = $id";
  3507. if ($this->debug > 2) {
  3508. error_log($sql_upd2, 0);
  3509. }
  3510. Database::query($sql_upd2);
  3511. }
  3512. // Update next item (new previous item).
  3513. if ($next != 0) {
  3514. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3515. WHERE c_id = ".$course_id." AND id = $next";
  3516. if ($this->debug > 2) {
  3517. error_log($sql_upd2, 0);
  3518. }
  3519. Database::query($sql_upd2);
  3520. }
  3521. $display = $display -1;
  3522. }
  3523. break;
  3524. case 'down':
  3525. if ($this->debug > 2) {
  3526. error_log('Movement down detected', 0);
  3527. }
  3528. if ($next == 0) { /* Do nothing. */
  3529. } else {
  3530. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3531. if ($this->debug > 2) {
  3532. error_log('Selecting next: ' . $sql_sel2, 0);
  3533. }
  3534. $res_sel2 = Database::query($sql_sel2);
  3535. if (Database::num_rows($res_sel2) < 1) {
  3536. $next_next = 0;
  3537. }
  3538. // Gather data.
  3539. $row2 = Database::fetch_array($res_sel2);
  3540. $next_next = $row2['next_item_id'];
  3541. // Update previous item (switch with current).
  3542. if ($previous != 0) {
  3543. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3544. WHERE c_id = ".$course_id." AND id = $previous";
  3545. Database::query($sql_upd2);
  3546. }
  3547. // Update current item (switch with previous).
  3548. if ($id != 0) {
  3549. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3550. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3551. WHERE c_id = ".$course_id." AND id = $id";
  3552. Database::query($sql_upd2);
  3553. }
  3554. // Update next item (new previous item).
  3555. if ($next != 0) {
  3556. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3557. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3558. WHERE c_id = ".$course_id." AND id = $next";
  3559. Database::query($sql_upd2);
  3560. }
  3561. // Update next_next item (switch "previous" with current).
  3562. if ($next_next != 0) {
  3563. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3564. previous_item_id = $id
  3565. WHERE c_id = ".$course_id." AND id = $next_next";
  3566. Database::query($sql_upd2);
  3567. }
  3568. $display = $display +1;
  3569. }
  3570. break;
  3571. default :
  3572. return false;
  3573. }
  3574. return $display;
  3575. }
  3576. /**
  3577. * Move a learnpath up (display_order)
  3578. * @param integer $lp_id Learnpath ID
  3579. */
  3580. public static function move_up($lp_id)
  3581. {
  3582. $course_id = api_get_course_int_id();
  3583. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3584. $sql = "SELECT * FROM $lp_table
  3585. WHERE c_id = $course_id
  3586. ORDER BY display_order";
  3587. $res = Database::query($sql);
  3588. if ($res === false) {
  3589. return false;
  3590. }
  3591. $lps = array ();
  3592. $lp_order = array();
  3593. $num = Database::num_rows($res);
  3594. // First check the order is correct, globally (might be wrong because
  3595. // of versions < 1.8.4)
  3596. if ($num > 0) {
  3597. $i = 1;
  3598. while ($row = Database::fetch_array($res)) {
  3599. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3600. $need_fix = true;
  3601. $sql_u = "UPDATE $lp_table SET display_order = $i
  3602. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3603. Database::query($sql_u);
  3604. }
  3605. $row['display_order'] = $i;
  3606. $lps[$row['id']] = $row;
  3607. $lp_order[$i] = $row['id'];
  3608. $i++;
  3609. }
  3610. }
  3611. if ($num > 1) { // If there's only one element, no need to sort.
  3612. $order = $lps[$lp_id]['display_order'];
  3613. if ($order > 1) { // If it's the first element, no need to move up.
  3614. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3615. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3616. Database::query($sql_u1);
  3617. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3618. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3619. Database::query($sql_u2);
  3620. }
  3621. }
  3622. }
  3623. /**
  3624. * Move a learnpath down (display_order)
  3625. * @param integer $lp_id Learnpath ID
  3626. */
  3627. public static function move_down($lp_id)
  3628. {
  3629. $course_id = api_get_course_int_id();
  3630. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3631. $sql = "SELECT * FROM $lp_table
  3632. WHERE c_id = $course_id
  3633. ORDER BY display_order";
  3634. $res = Database::query($sql);
  3635. if ($res === false) {
  3636. return false;
  3637. }
  3638. $lps = array();
  3639. $lp_order = array();
  3640. $num = Database::num_rows($res);
  3641. $max = 0;
  3642. // First check the order is correct, globally (might be wrong because
  3643. // of versions < 1.8.4).
  3644. if ($num > 0) {
  3645. $i = 1;
  3646. while ($row = Database::fetch_array($res)) {
  3647. $max = $i;
  3648. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3649. $need_fix = true;
  3650. $sql_u = "UPDATE $lp_table SET display_order = $i
  3651. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3652. Database::query($sql_u);
  3653. }
  3654. $row['display_order'] = $i;
  3655. $lps[$row['id']] = $row;
  3656. $lp_order[$i] = $row['id'];
  3657. $i++;
  3658. }
  3659. }
  3660. if ($num > 1) { // If there's only one element, no need to sort.
  3661. $order = $lps[$lp_id]['display_order'];
  3662. if ($order < $max) { // If it's the first element, no need to move up.
  3663. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3664. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3665. Database::query($sql_u1);
  3666. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3667. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3668. Database::query($sql_u2);
  3669. }
  3670. }
  3671. }
  3672. /**
  3673. * Updates learnpath attributes to point to the next element
  3674. * The last part is similar to set_current_item but processing the other way around
  3675. */
  3676. public function next()
  3677. {
  3678. if ($this->debug > 0) {
  3679. error_log('New LP - In learnpath::next()', 0);
  3680. }
  3681. $this->last = $this->get_current_item_id();
  3682. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3683. $this->autocomplete_parents($this->last);
  3684. $new_index = $this->get_next_index();
  3685. if ($this->debug > 2) {
  3686. error_log('New LP - New index: ' . $new_index, 0);
  3687. }
  3688. $this->index = $new_index;
  3689. if ($this->debug > 2) {
  3690. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3691. }
  3692. $this->current = $this->ordered_items[$new_index];
  3693. if ($this->debug > 2) {
  3694. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3695. }
  3696. }
  3697. /**
  3698. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3699. * class, this might be redefined to allow several behaviours depending on the document type.
  3700. * @param integer Resource ID
  3701. * @return boolean True on success, false otherwise
  3702. */
  3703. public function open($id)
  3704. {
  3705. if ($this->debug > 0) {
  3706. error_log('New LP - In learnpath::open()', 0);
  3707. }
  3708. // TODO:
  3709. // set the current resource attribute to this resource
  3710. // switch on element type (redefine in child class?)
  3711. // set status for this item to "opened"
  3712. // start timer
  3713. // initialise score
  3714. $this->index = 0; //or = the last item seen (see $this->last)
  3715. }
  3716. /**
  3717. * Check that all prerequisites are fulfilled. Returns true and an
  3718. * empty string on success, returns false
  3719. * and the prerequisite string on error.
  3720. * This function is based on the rules for aicc_script language as
  3721. * described in the SCORM 1.2 CAM documentation page 108.
  3722. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3723. * @return boolean True if prerequisites are matched, false otherwise -
  3724. * Empty string if true returned, prerequisites string otherwise.
  3725. */
  3726. public function prerequisites_match($itemId = null)
  3727. {
  3728. $debug = $this->debug;
  3729. if ($debug > 0) {
  3730. error_log('In learnpath::prerequisites_match()', 0);
  3731. }
  3732. if (empty($itemId)) {
  3733. $itemId = $this->current;
  3734. }
  3735. $currentItem = $this->getItem($itemId);
  3736. if ($currentItem) {
  3737. if ($this->type == 2) {
  3738. // Getting prereq from scorm
  3739. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3740. } else {
  3741. $prereq_string = $currentItem->get_prereq_string();
  3742. }
  3743. if (empty($prereq_string)) {
  3744. if ($debug > 0) {
  3745. error_log('Found prereq_string is empty return true');
  3746. }
  3747. return true;
  3748. }
  3749. // Clean spaces.
  3750. $prereq_string = str_replace(' ', '', $prereq_string);
  3751. if ($debug > 0) {
  3752. error_log('Found prereq_string: ' . $prereq_string, 0);
  3753. }
  3754. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3755. $result = $currentItem->parse_prereq(
  3756. $prereq_string,
  3757. $this->items,
  3758. $this->refs_list,
  3759. $this->get_user_id()
  3760. );
  3761. if ($result === false) {
  3762. $this->set_error_msg($currentItem->prereq_alert);
  3763. }
  3764. } else {
  3765. $result = true;
  3766. if ($debug > 1) {
  3767. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3768. }
  3769. }
  3770. if ($debug > 1) {
  3771. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3772. }
  3773. return $result;
  3774. }
  3775. /**
  3776. * Updates learnpath attributes to point to the previous element
  3777. * The last part is similar to set_current_item but processing the other way around
  3778. */
  3779. public function previous()
  3780. {
  3781. if ($this->debug > 0) {
  3782. error_log('New LP - In learnpath::previous()', 0);
  3783. }
  3784. $this->last = $this->get_current_item_id();
  3785. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3786. $this->autocomplete_parents($this->last);
  3787. $new_index = $this->get_previous_index();
  3788. $this->index = $new_index;
  3789. $this->current = $this->ordered_items[$new_index];
  3790. }
  3791. /**
  3792. * Publishes a learnpath. This basically means show or hide the learnpath
  3793. * to normal users.
  3794. * Can be used as abstract
  3795. * @param integer Learnpath ID
  3796. * @param string New visibility
  3797. */
  3798. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3799. {
  3800. $action = 'visible';
  3801. if ($set_visibility != 1) {
  3802. $action = 'invisible';
  3803. self::toggle_publish($lp_id, 'i');
  3804. }
  3805. return api_item_property_update(
  3806. api_get_course_info(),
  3807. TOOL_LEARNPATH,
  3808. $lp_id,
  3809. $action,
  3810. api_get_user_id()
  3811. );
  3812. }
  3813. /**
  3814. * Publishes a learnpath. This basically means show or hide the learnpath
  3815. * on the course homepage
  3816. * Can be used as abstract
  3817. * @param integer $lp_id Learnpath id
  3818. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3819. * @return bool
  3820. */
  3821. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3822. {
  3823. $course_id = api_get_course_int_id();
  3824. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  3825. $lp_id = intval($lp_id);
  3826. $sql = "SELECT * FROM $tbl_lp
  3827. WHERE c_id = $course_id AND id = $lp_id";
  3828. $result = Database::query($sql);
  3829. if (Database::num_rows($result)) {
  3830. $row = Database::fetch_array($result);
  3831. $name = Database::escape_string($row['name']);
  3832. if ($set_visibility == 'i') {
  3833. $v = 0;
  3834. }
  3835. if ($set_visibility == 'v') {
  3836. $v = 1;
  3837. }
  3838. $session_id = api_get_session_id();
  3839. $session_condition = api_get_session_condition($session_id);
  3840. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  3841. $link = 'lp/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3842. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3843. $sql = "SELECT * FROM $tbl_tool
  3844. WHERE
  3845. c_id = $course_id AND
  3846. (link = '$link' OR link = '$oldLink') AND
  3847. image = 'scormbuilder.gif' AND
  3848. (
  3849. link LIKE '$link%' OR
  3850. link LIKE '$oldLink%'
  3851. )
  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
  3859. c_id = $course_id AND
  3860. (link = '$link' OR link = '$oldLink') AND
  3861. image='scormbuilder.gif'
  3862. $session_condition";
  3863. Database::query($sql);
  3864. } elseif ($set_visibility == 'v' && $num == 0) {
  3865. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3866. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3867. Database::query($sql);
  3868. $insertId = Database::insert_id();
  3869. if ($insertId) {
  3870. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3871. Database::query($sql);
  3872. }
  3873. } elseif ($set_visibility == 'v' && $num > 0) {
  3874. $sql = "UPDATE $tbl_tool SET
  3875. c_id = $course_id,
  3876. name = '$name',
  3877. link = '$link',
  3878. image = 'scormbuilder.gif',
  3879. visibility = '$v',
  3880. admin = '0',
  3881. address = 'pastillegris.gif',
  3882. added_tool = 0,
  3883. session_id = $session_id
  3884. WHERE
  3885. c_id = ".$course_id." AND
  3886. (link = '$link' OR link = '$oldLink') AND
  3887. image='scormbuilder.gif'
  3888. $session_condition
  3889. ";
  3890. Database::query($sql);
  3891. } else {
  3892. // Parameter and database incompatible, do nothing, exit.
  3893. return false;
  3894. }
  3895. } else {
  3896. return false;
  3897. }
  3898. }
  3899. /**
  3900. * Restart the whole learnpath. Return the URL of the first element.
  3901. * Make sure the results are saved with anoter method. This method should probably be
  3902. * redefined in children classes.
  3903. * To use a similar method statically, use the create_new_attempt() method
  3904. * @return string URL to load in the viewer
  3905. */
  3906. public function restart()
  3907. {
  3908. if ($this->debug > 0) {
  3909. error_log('New LP - In learnpath::restart()', 0);
  3910. }
  3911. // TODO
  3912. // Call autosave method to save the current progress.
  3913. //$this->index = 0;
  3914. if (api_is_invitee()) {
  3915. return false;
  3916. }
  3917. $session_id = api_get_session_id();
  3918. $course_id = api_get_course_int_id();
  3919. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  3920. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3921. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3922. if ($this->debug > 2) {
  3923. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3924. }
  3925. Database::query($sql);
  3926. $view_id = Database::insert_id();
  3927. if ($view_id) {
  3928. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3929. Database::query($sql);
  3930. $this->lp_view_id = $view_id;
  3931. $this->attempt = $this->attempt + 1;
  3932. } else {
  3933. $this->error = 'Could not insert into item_view table...';
  3934. return false;
  3935. }
  3936. $this->autocomplete_parents($this->current);
  3937. foreach ($this->items as $index => $dummy) {
  3938. $this->items[$index]->restart();
  3939. $this->items[$index]->set_lp_view($this->lp_view_id);
  3940. }
  3941. $this->first();
  3942. return true;
  3943. }
  3944. /**
  3945. * Saves the current item
  3946. * @return boolean
  3947. */
  3948. public function save_current()
  3949. {
  3950. if ($this->debug > 0) {
  3951. error_log('learnpath::save_current()', 0);
  3952. }
  3953. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3954. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3955. if ($this->debug > 2) {
  3956. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3957. }
  3958. if ($this->debug > 2) {
  3959. error_log('' . print_r($this->items, true), 0);
  3960. }
  3961. if (isset($this->items[$this->current]) &&
  3962. is_object($this->items[$this->current])
  3963. ) {
  3964. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3965. $this->autocomplete_parents($this->current);
  3966. $status = $this->items[$this->current]->get_status();
  3967. $this->update_queue[$this->current] = $status;
  3968. return $res;
  3969. }
  3970. return false;
  3971. }
  3972. /**
  3973. * Saves the given item
  3974. * @param integer $item_id Optional (will take from $_REQUEST if null)
  3975. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3976. * @return boolean
  3977. */
  3978. public function save_item($item_id = null, $from_outside = true)
  3979. {
  3980. $debug = $this->debug;
  3981. if ($debug) {
  3982. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3983. }
  3984. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3985. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3986. if (empty($item_id)) {
  3987. $item_id = intval($_REQUEST['id']);
  3988. }
  3989. if (empty($item_id)) {
  3990. $item_id = $this->get_current_item_id();
  3991. }
  3992. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3993. if ($debug) {
  3994. error_log('Object exists');
  3995. }
  3996. // Saving the item.
  3997. $res = $this->items[$item_id]->save(
  3998. $from_outside,
  3999. $this->prerequisites_match($item_id)
  4000. );
  4001. if ($debug) {
  4002. error_log('update_queue before:');
  4003. error_log(print_r($this->update_queue,1));
  4004. }
  4005. $this->autocomplete_parents($item_id);
  4006. $status = $this->items[$item_id]->get_status();
  4007. $this->update_queue[$item_id] = $status;
  4008. if ($debug) {
  4009. error_log('get_status(): ' . $status);
  4010. error_log('update_queue after:');
  4011. error_log(print_r($this->update_queue,1));
  4012. }
  4013. return $res;
  4014. }
  4015. return false;
  4016. }
  4017. /**
  4018. * Saves the last item seen's ID only in case
  4019. */
  4020. public function save_last()
  4021. {
  4022. $course_id = api_get_course_int_id();
  4023. if ($this->debug > 0) {
  4024. error_log('New LP - In learnpath::save_last()', 0);
  4025. }
  4026. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4027. $table = Database::get_course_table(TABLE_LP_VIEW);
  4028. if (isset($this->current) && !api_is_invitee()) {
  4029. if ($this->debug > 2) {
  4030. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4031. }
  4032. $sql = "UPDATE $table SET
  4033. last_item = " . intval($this->get_current_item_id()). "
  4034. WHERE
  4035. c_id = $course_id AND
  4036. lp_id = " . $this->get_id() . " AND
  4037. user_id = " . $this->get_user_id()." ".$session_condition;
  4038. if ($this->debug > 2) {
  4039. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4040. }
  4041. Database::query($sql);
  4042. }
  4043. if (!api_is_invitee()) {
  4044. // Save progress.
  4045. list($progress, $text) = $this->get_progress_bar_text('%');
  4046. if ($progress >= 0 && $progress <= 100) {
  4047. $progress = (int) $progress;
  4048. $sql = "UPDATE $table SET
  4049. progress = $progress
  4050. WHERE
  4051. c_id = ".$course_id." AND
  4052. lp_id = " . $this->get_id() . " AND
  4053. user_id = " . $this->get_user_id()." ".$session_condition;
  4054. // Ignore errors as some tables might not have the progress field just yet.
  4055. Database::query($sql);
  4056. $this->progress_db = $progress;
  4057. }
  4058. }
  4059. }
  4060. /**
  4061. * Sets the current item ID (checks if valid and authorized first)
  4062. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4063. */
  4064. public function set_current_item($item_id = null)
  4065. {
  4066. if ($this->debug > 0) {
  4067. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4068. }
  4069. if (empty ($item_id)) {
  4070. if ($this->debug > 2) {
  4071. error_log('New LP - No new current item given, ignore...', 0);
  4072. }
  4073. // Do nothing.
  4074. } else {
  4075. if ($this->debug > 2) {
  4076. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4077. }
  4078. if (is_numeric($item_id)) {
  4079. $item_id = intval($item_id);
  4080. // TODO: Check in database here.
  4081. $this->last = $this->current;
  4082. $this->current = $item_id;
  4083. // TODO: Update $this->index as well.
  4084. foreach ($this->ordered_items as $index => $item) {
  4085. if ($item == $this->current) {
  4086. $this->index = $index;
  4087. break;
  4088. }
  4089. }
  4090. if ($this->debug > 2) {
  4091. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4092. }
  4093. } else {
  4094. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4095. }
  4096. }
  4097. }
  4098. /**
  4099. * Sets the encoding
  4100. * @param string New encoding
  4101. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4102. */
  4103. public function set_encoding($enc = 'UTF-8')
  4104. {
  4105. if ($this->debug > 0) {
  4106. error_log('New LP - In learnpath::set_encoding()', 0);
  4107. }
  4108. $course_id = api_get_course_int_id();
  4109. $enc = api_refine_encoding_id($enc);
  4110. if (empty($enc)) {
  4111. $enc = api_get_system_encoding();
  4112. }
  4113. if (api_is_encoding_supported($enc)) {
  4114. $lp = $this->get_id();
  4115. if ($lp != 0) {
  4116. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4117. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
  4118. WHERE c_id = ".$course_id." AND id = " . $lp;
  4119. $res = Database::query($sql);
  4120. return $res;
  4121. }
  4122. }
  4123. return false;
  4124. }
  4125. /**
  4126. * Sets the JS lib setting in the database directly.
  4127. * This is the JavaScript library file this lp needs to load on startup
  4128. * @param string Proximity setting
  4129. * @return boolean True on update success. False otherwise.
  4130. */
  4131. public function set_jslib($lib = '')
  4132. {
  4133. if ($this->debug > 0) {
  4134. error_log('New LP - In learnpath::set_jslib()', 0);
  4135. }
  4136. $lp = $this->get_id();
  4137. $course_id = api_get_course_int_id();
  4138. if ($lp != 0) {
  4139. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  4140. $sql = "UPDATE $tbl_lp SET js_lib = '$lib'
  4141. WHERE c_id = ".$course_id." AND id = " . $lp;
  4142. $res = Database::query($sql);
  4143. return $res;
  4144. } else {
  4145. return false;
  4146. }
  4147. }
  4148. /**
  4149. * Sets the name of the LP maker (publisher) (and save)
  4150. * @param string Optional string giving the new content_maker of this learnpath
  4151. * @return boolean True
  4152. */
  4153. public function set_maker($name = '')
  4154. {
  4155. if ($this->debug > 0) {
  4156. error_log('New LP - In learnpath::set_maker()', 0);
  4157. }
  4158. if (empty ($name))
  4159. return false;
  4160. $this->maker = $name;
  4161. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4162. $course_id = api_get_course_int_id();
  4163. $lp_id = $this->get_id();
  4164. $sql = "UPDATE $lp_table SET
  4165. content_maker = '" . Database::escape_string($this->maker) . "'
  4166. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4167. if ($this->debug > 2) {
  4168. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4169. }
  4170. Database::query($sql);
  4171. return true;
  4172. }
  4173. /**
  4174. * Sets the name of the current learnpath (and save)
  4175. * @param string $name Optional string giving the new name of this learnpath
  4176. * @return boolean True/False
  4177. */
  4178. public function set_name($name = null)
  4179. {
  4180. if ($this->debug > 0) {
  4181. error_log('New LP - In learnpath::set_name()', 0);
  4182. }
  4183. if (empty($name)) {
  4184. return false;
  4185. }
  4186. $this->name = Database::escape_string($name);
  4187. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4188. $lp_id = $this->get_id();
  4189. $course_id = $this->course_info['real_id'];
  4190. $sql = "UPDATE $lp_table SET
  4191. name = '" . Database::escape_string($this->name). "'
  4192. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4193. if ($this->debug > 2) {
  4194. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4195. }
  4196. $result = Database::query($sql);
  4197. // If the lp is visible on the homepage, change his name there.
  4198. if (Database::affected_rows($result)) {
  4199. $session_id = api_get_session_id();
  4200. $session_condition = api_get_session_condition($session_id);
  4201. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  4202. $link = 'lp/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4203. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4204. WHERE
  4205. c_id = $course_id AND
  4206. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4207. Database::query($sql);
  4208. return true;
  4209. } else {
  4210. return false;
  4211. }
  4212. }
  4213. /**
  4214. * Set index specified prefix terms for all items in this path
  4215. * @param string Comma-separated list of terms
  4216. * @param char Xapian term prefix
  4217. * @return boolean False on error, true otherwise
  4218. */
  4219. public function set_terms_by_prefix($terms_string, $prefix)
  4220. {
  4221. $course_id = api_get_course_int_id();
  4222. if (api_get_setting('search_enabled') !== 'true')
  4223. return false;
  4224. if (!extension_loaded('xapian')) {
  4225. return false;
  4226. }
  4227. $terms_string = trim($terms_string);
  4228. $terms = explode(',', $terms_string);
  4229. array_walk($terms, 'trim_value');
  4230. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4231. // Don't do anything if no change, verify only at DB, not the search engine.
  4232. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4233. return false;
  4234. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4235. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4236. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4237. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4238. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  4239. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4240. $lp_id = intval($_POST['lp_id']);
  4241. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4242. $result = Database::query($sql);
  4243. $di = new ChamiloIndexer();
  4244. while ($lp_item = Database::fetch_array($result)) {
  4245. // Get search_did.
  4246. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4247. $sql = 'SELECT * FROM %s
  4248. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d
  4249. LIMIT 1';
  4250. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4251. //echo $sql; echo '<br>';
  4252. $res = Database::query($sql);
  4253. if (Database::num_rows($res) > 0) {
  4254. $se_ref = Database::fetch_array($res);
  4255. // Compare terms.
  4256. $doc = $di->get_document($se_ref['search_did']);
  4257. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4258. $xterms = array();
  4259. foreach ($xapian_terms as $xapian_term) {
  4260. $xterms[] = substr($xapian_term['name'], 1);
  4261. }
  4262. $dterms = $terms;
  4263. $missing_terms = array_diff($dterms, $xterms);
  4264. $deprecated_terms = array_diff($xterms, $dterms);
  4265. // Save it to search engine.
  4266. foreach ($missing_terms as $term) {
  4267. $doc->add_term($prefix . $term, 1);
  4268. }
  4269. foreach ($deprecated_terms as $term) {
  4270. $doc->remove_term($prefix . $term);
  4271. }
  4272. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4273. $di->getDb()->flush();
  4274. }
  4275. }
  4276. return true;
  4277. }
  4278. /**
  4279. * Sets the theme of the LP (local/remote) (and save)
  4280. * @param string Optional string giving the new theme of this learnpath
  4281. * @return bool Returns true if theme name is not empty
  4282. */
  4283. public function set_theme($name = '')
  4284. {
  4285. $course_id = api_get_course_int_id();
  4286. if ($this->debug > 0) {
  4287. error_log('New LP - In learnpath::set_theme()', 0);
  4288. }
  4289. $this->theme = $name;
  4290. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4291. $lp_id = $this->get_id();
  4292. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4293. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4294. if ($this->debug > 2) {
  4295. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4296. }
  4297. Database::query($sql);
  4298. return true;
  4299. }
  4300. /**
  4301. * Sets the image of an LP (and save)
  4302. * @param string Optional string giving the new image of this learnpath
  4303. * @return bool Returns true if theme name is not empty
  4304. */
  4305. public function set_preview_image($name = '')
  4306. {
  4307. $course_id = api_get_course_int_id();
  4308. if ($this->debug > 0) {
  4309. error_log('New LP - In learnpath::set_preview_image()', 0);
  4310. }
  4311. $this->preview_image = $name;
  4312. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4313. $lp_id = $this->get_id();
  4314. $sql = "UPDATE $lp_table SET
  4315. preview_image = '" . Database::escape_string($this->preview_image). "'
  4316. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4317. if ($this->debug > 2) {
  4318. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4319. }
  4320. Database::query($sql);
  4321. return true;
  4322. }
  4323. /**
  4324. * Sets the author of a LP (and save)
  4325. * @param string Optional string giving the new author of this learnpath
  4326. * @return bool Returns true if author's name is not empty
  4327. */
  4328. public function set_author($name = '')
  4329. {
  4330. $course_id = api_get_course_int_id();
  4331. if ($this->debug > 0) {
  4332. error_log('New LP - In learnpath::set_author()', 0);
  4333. }
  4334. $this->author = $name;
  4335. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4336. $lp_id = $this->get_id();
  4337. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4338. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4339. if ($this->debug > 2) {
  4340. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4341. }
  4342. Database::query($sql);
  4343. return true;
  4344. }
  4345. /**
  4346. * Sets the hide_toc_frame parameter of a LP (and save)
  4347. * @param int 1 if frame is hidden 0 then else
  4348. * @return bool Returns true if author's name is not empty
  4349. */
  4350. public function set_hide_toc_frame($hide)
  4351. {
  4352. $course_id = api_get_course_int_id();
  4353. if ($this->debug > 0) {
  4354. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4355. }
  4356. if (intval($hide) == $hide) {
  4357. $this->hide_toc_frame = $hide;
  4358. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4359. $lp_id = $this->get_id();
  4360. $sql = "UPDATE $lp_table SET
  4361. hide_toc_frame = '" . (int) $this->hide_toc_frame . "'
  4362. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4363. if ($this->debug > 2) {
  4364. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4365. }
  4366. Database::query($sql);
  4367. return true;
  4368. } else {
  4369. return false;
  4370. }
  4371. }
  4372. /**
  4373. * Sets the prerequisite of a LP (and save)
  4374. * @param int integer giving the new prerequisite of this learnpath
  4375. * @return bool returns true if prerequisite is not empty
  4376. */
  4377. public function set_prerequisite($prerequisite)
  4378. {
  4379. $course_id = api_get_course_int_id();
  4380. if ($this->debug > 0) {
  4381. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4382. }
  4383. $this->prerequisite = intval($prerequisite);
  4384. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4385. $lp_id = $this->get_id();
  4386. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4387. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4388. if ($this->debug > 2) {
  4389. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4390. }
  4391. Database::query($sql);
  4392. return true;
  4393. }
  4394. /**
  4395. * Sets the location/proximity of the LP (local/remote) (and save)
  4396. * @param string Optional string giving the new location of this learnpath
  4397. * @return boolean True on success / False on error
  4398. */
  4399. public function set_proximity($name = '')
  4400. {
  4401. $course_id = api_get_course_int_id();
  4402. if ($this->debug > 0) {
  4403. error_log('New LP - In learnpath::set_proximity()', 0);
  4404. }
  4405. if (empty ($name))
  4406. return false;
  4407. $this->proximity = $name;
  4408. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4409. $lp_id = $this->get_id();
  4410. $sql = "UPDATE $lp_table SET
  4411. content_local = '" . Database::escape_string($name) . "'
  4412. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4413. if ($this->debug > 2) {
  4414. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4415. }
  4416. Database::query($sql);
  4417. return true;
  4418. }
  4419. /**
  4420. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4421. * @param integer DB ID of the item
  4422. */
  4423. public function set_previous_item($id)
  4424. {
  4425. if ($this->debug > 0) {
  4426. error_log('New LP - In learnpath::set_previous_item()', 0);
  4427. }
  4428. $this->last = $id;
  4429. }
  4430. /**
  4431. * Sets use_max_score
  4432. * @param string $use_max_score Optional string giving the new location of this learnpath
  4433. * @return boolean True on success / False on error
  4434. */
  4435. public function set_use_max_score($use_max_score = 1)
  4436. {
  4437. $course_id = api_get_course_int_id();
  4438. if ($this->debug > 0) {
  4439. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4440. }
  4441. $use_max_score = intval($use_max_score);
  4442. $this->use_max_score = $use_max_score;
  4443. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4444. $lp_id = $this->get_id();
  4445. $sql = "UPDATE $lp_table SET
  4446. use_max_score = '" . $this->use_max_score . "'
  4447. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4448. if ($this->debug > 2) {
  4449. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4450. }
  4451. Database::query($sql);
  4452. return true;
  4453. }
  4454. /**
  4455. * Sets and saves the expired_on date
  4456. * @param string $expired_on Optional string giving the new author of this learnpath
  4457. * @return bool Returns true if author's name is not empty
  4458. */
  4459. public function set_expired_on($expired_on)
  4460. {
  4461. if ($this->debug > 0) {
  4462. error_log('New LP - In learnpath::set_expired_on()', 0);
  4463. }
  4464. $em = Database::getManager();
  4465. /** @var CLp $lp */
  4466. $lp = $em
  4467. ->getRepository('ChamiloCourseBundle:CLp')
  4468. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4469. if (!$lp) {
  4470. return false;
  4471. }
  4472. $this->expired_on = !empty($expired_on) ? api_get_utc_datetime($expired_on, false, true) : null;
  4473. $lp->setExpiredOn($this->expired_on);
  4474. $em->persist($lp);
  4475. $em->flush();
  4476. if ($this->debug > 2) {
  4477. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4478. }
  4479. return true;
  4480. }
  4481. /**
  4482. * Sets and saves the publicated_on date
  4483. * @param string $publicated_on Optional string giving the new author of this learnpath
  4484. * @return bool Returns true if author's name is not empty
  4485. */
  4486. public function set_publicated_on($publicated_on)
  4487. {
  4488. if ($this->debug > 0) {
  4489. error_log('New LP - In learnpath::set_expired_on()', 0);
  4490. }
  4491. $em = Database::getManager();
  4492. /** @var CLp $lp */
  4493. $lp = $em
  4494. ->getRepository('ChamiloCourseBundle:CLp')
  4495. ->findOneBy(['id' => $this->get_id(), 'cId' => api_get_course_int_id()]);
  4496. if (!$lp) {
  4497. return false;
  4498. }
  4499. $this->publicated_on = !empty($publicated_on) ? api_get_utc_datetime($publicated_on, false, true) : null;
  4500. $lp->setPublicatedOn($this->publicated_on);
  4501. $em->persist($lp);
  4502. $em->flush();
  4503. if ($this->debug > 2) {
  4504. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4505. }
  4506. return true;
  4507. }
  4508. /**
  4509. * Sets and saves the expired_on date
  4510. * @return bool Returns true if author's name is not empty
  4511. */
  4512. public function set_modified_on()
  4513. {
  4514. $course_id = api_get_course_int_id();
  4515. if ($this->debug > 0) {
  4516. error_log('New LP - In learnpath::set_expired_on()', 0);
  4517. }
  4518. $this->modified_on = api_get_utc_datetime();
  4519. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4520. $lp_id = $this->get_id();
  4521. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4522. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4523. if ($this->debug > 2) {
  4524. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4525. }
  4526. Database::query($sql);
  4527. return true;
  4528. }
  4529. /**
  4530. * Sets the object's error message
  4531. * @param string Error message. If empty, reinits the error string
  4532. * @return void
  4533. */
  4534. public function set_error_msg($error = '')
  4535. {
  4536. if ($this->debug > 0) {
  4537. error_log('New LP - In learnpath::set_error_msg()', 0);
  4538. }
  4539. if (empty ($error)) {
  4540. $this->error = '';
  4541. } else {
  4542. $this->error .= $error;
  4543. }
  4544. }
  4545. /**
  4546. * Launches the current item if not 'sco'
  4547. * (starts timer and make sure there is a record ready in the DB)
  4548. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4549. * @return boolean
  4550. */
  4551. public function start_current_item($allow_new_attempt = false)
  4552. {
  4553. if ($this->debug > 0) {
  4554. error_log('New LP - In learnpath::start_current_item()', 0);
  4555. }
  4556. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4557. $type = $this->get_type();
  4558. $item_type = $this->items[$this->current]->get_type();
  4559. if (($type == 2 && $item_type != 'sco') ||
  4560. ($type == 3 && $item_type != 'au') ||
  4561. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4562. ) {
  4563. $this->items[$this->current]->open($allow_new_attempt);
  4564. $this->autocomplete_parents($this->current);
  4565. $prereq_check = $this->prerequisites_match($this->current);
  4566. $this->items[$this->current]->save(false, $prereq_check);
  4567. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4568. } else {
  4569. // If sco, then it is supposed to have been updated by some other call.
  4570. }
  4571. if ($item_type == 'sco') {
  4572. $this->items[$this->current]->restart();
  4573. }
  4574. }
  4575. if ($this->debug > 0) {
  4576. error_log('New LP - End of learnpath::start_current_item()', 0);
  4577. }
  4578. return true;
  4579. }
  4580. /**
  4581. * Stops the processing and counters for the old item (as held in $this->last)
  4582. * @return boolean True/False
  4583. */
  4584. public function stop_previous_item()
  4585. {
  4586. if ($this->debug > 0) {
  4587. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4588. }
  4589. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4590. if ($this->debug > 2) {
  4591. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4592. }
  4593. switch ($this->get_type()) {
  4594. case '3':
  4595. if ($this->items[$this->last]->get_type() != 'au') {
  4596. if ($this->debug > 2) {
  4597. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4598. }
  4599. $this->items[$this->last]->close();
  4600. //$this->autocomplete_parents($this->last);
  4601. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4602. } else {
  4603. if ($this->debug > 2) {
  4604. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4605. }
  4606. }
  4607. case '2':
  4608. if ($this->items[$this->last]->get_type() != 'sco') {
  4609. if ($this->debug > 2) {
  4610. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4611. }
  4612. $this->items[$this->last]->close();
  4613. //$this->autocomplete_parents($this->last);
  4614. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4615. } else {
  4616. if ($this->debug > 2) {
  4617. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4618. }
  4619. }
  4620. break;
  4621. case '1':
  4622. default:
  4623. if ($this->debug > 2) {
  4624. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4625. }
  4626. $this->items[$this->last]->close();
  4627. break;
  4628. }
  4629. } else {
  4630. if ($this->debug > 2) {
  4631. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4632. }
  4633. return false;
  4634. }
  4635. return true;
  4636. }
  4637. /**
  4638. * Updates the default view mode from fullscreen to embedded and inversely
  4639. * @return string The current default view mode ('fullscreen' or 'embedded')
  4640. */
  4641. public function update_default_view_mode()
  4642. {
  4643. $course_id = api_get_course_int_id();
  4644. if ($this->debug > 0) {
  4645. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4646. }
  4647. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4648. $sql = "SELECT * FROM $lp_table
  4649. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4650. $res = Database::query($sql);
  4651. if (Database::num_rows($res) > 0) {
  4652. $row = Database::fetch_array($res);
  4653. $default_view_mode = $row['default_view_mod'];
  4654. $view_mode = $default_view_mode;
  4655. switch ($default_view_mode) {
  4656. case 'fullscreen': // default with popup
  4657. $view_mode = 'embedded';
  4658. break;
  4659. case 'embedded': // default view with left menu
  4660. $view_mode = 'embedframe';
  4661. break;
  4662. case 'embedframe': //folded menu
  4663. $view_mode = 'impress';
  4664. break;
  4665. case 'impress':
  4666. $view_mode = 'fullscreen';
  4667. break;
  4668. }
  4669. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4670. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4671. Database::query($sql);
  4672. $this->mode = $view_mode;
  4673. return $view_mode;
  4674. } else {
  4675. if ($this->debug > 2) {
  4676. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4677. }
  4678. }
  4679. return -1;
  4680. }
  4681. /**
  4682. * Updates the default behaviour about auto-commiting SCORM updates
  4683. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4684. */
  4685. public function update_default_scorm_commit()
  4686. {
  4687. $course_id = api_get_course_int_id();
  4688. if ($this->debug > 0) {
  4689. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4690. }
  4691. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4692. $sql = "SELECT * FROM $lp_table
  4693. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4694. $res = Database::query($sql);
  4695. if (Database::num_rows($res) > 0) {
  4696. $row = Database::fetch_array($res);
  4697. $force = $row['force_commit'];
  4698. if ($force == 1) {
  4699. $force = 0;
  4700. $force_return = false;
  4701. } elseif ($force == 0) {
  4702. $force = 1;
  4703. $force_return = true;
  4704. }
  4705. $sql = "UPDATE $lp_table SET force_commit = $force
  4706. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4707. Database::query($sql);
  4708. $this->force_commit = $force_return;
  4709. return $force_return;
  4710. } else {
  4711. if ($this->debug > 2) {
  4712. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4713. }
  4714. }
  4715. return -1;
  4716. }
  4717. /**
  4718. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4719. * @return bool True on success, false on failure
  4720. */
  4721. public function update_display_order()
  4722. {
  4723. $course_id = api_get_course_int_id();
  4724. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4725. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4726. $res = Database::query($sql);
  4727. if ($res === false) {
  4728. return false;
  4729. }
  4730. $num = Database::num_rows($res);
  4731. // First check the order is correct, globally (might be wrong because
  4732. // of versions < 1.8.4).
  4733. if ($num > 0) {
  4734. $i = 1;
  4735. while ($row = Database::fetch_array($res)) {
  4736. if ($row['display_order'] != $i) {
  4737. // If we find a gap in the order, we need to fix it.
  4738. $sql = "UPDATE $lp_table SET display_order = $i
  4739. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4740. Database::query($sql);
  4741. }
  4742. $i++;
  4743. }
  4744. }
  4745. return true;
  4746. }
  4747. /**
  4748. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4749. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4750. */
  4751. public function update_reinit()
  4752. {
  4753. $course_id = api_get_course_int_id();
  4754. if ($this->debug > 0) {
  4755. error_log('New LP - In learnpath::update_reinit()', 0);
  4756. }
  4757. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4758. $sql = "SELECT * FROM $lp_table
  4759. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4760. $res = Database::query($sql);
  4761. if (Database::num_rows($res) > 0) {
  4762. $row = Database::fetch_array($res);
  4763. $force = $row['prevent_reinit'];
  4764. if ($force == 1) {
  4765. $force = 0;
  4766. } elseif ($force == 0) {
  4767. $force = 1;
  4768. }
  4769. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4770. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4771. Database::query($sql);
  4772. $this->prevent_reinit = $force;
  4773. return $force;
  4774. } else {
  4775. if ($this->debug > 2) {
  4776. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4777. }
  4778. }
  4779. return -1;
  4780. }
  4781. /**
  4782. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4783. *
  4784. * @return string 'single', 'multi' or 'seriousgame'
  4785. * @author ndiechburg <noel@cblue.be>
  4786. **/
  4787. public function get_attempt_mode()
  4788. {
  4789. //Set default value for seriousgame_mode
  4790. if (!isset($this->seriousgame_mode)) {
  4791. $this->seriousgame_mode=0;
  4792. }
  4793. // Set default value for prevent_reinit
  4794. if (!isset($this->prevent_reinit)) {
  4795. $this->prevent_reinit =1;
  4796. }
  4797. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4798. return 'seriousgame';
  4799. }
  4800. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4801. return 'single';
  4802. }
  4803. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4804. return 'multiple';
  4805. }
  4806. return 'single';
  4807. }
  4808. /**
  4809. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4810. *
  4811. * @param string 'seriousgame', 'single' or 'multiple'
  4812. * @return boolean
  4813. * @author ndiechburg <noel@cblue.be>
  4814. **/
  4815. public function set_attempt_mode($mode)
  4816. {
  4817. $course_id = api_get_course_int_id();
  4818. switch ($mode) {
  4819. case 'seriousgame':
  4820. $sg_mode = 1;
  4821. $prevent_reinit = 1;
  4822. break;
  4823. case 'single':
  4824. $sg_mode = 0;
  4825. $prevent_reinit = 1;
  4826. break;
  4827. case 'multiple':
  4828. $sg_mode = 0;
  4829. $prevent_reinit = 0;
  4830. break;
  4831. default:
  4832. $sg_mode = 0;
  4833. $prevent_reinit = 0;
  4834. break;
  4835. }
  4836. $this->prevent_reinit = $prevent_reinit;
  4837. $this->seriousgame_mode = $sg_mode;
  4838. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4839. $sql = "UPDATE $lp_table SET
  4840. prevent_reinit = $prevent_reinit ,
  4841. seriousgame_mode = $sg_mode
  4842. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4843. $res = Database::query($sql);
  4844. if ($res) {
  4845. return true;
  4846. } else {
  4847. return false;
  4848. }
  4849. }
  4850. /**
  4851. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4852. *
  4853. * @return boolean
  4854. * @author ndiechburg <noel@cblue.be>
  4855. **/
  4856. public function switch_attempt_mode()
  4857. {
  4858. if ($this->debug > 0) {
  4859. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4860. }
  4861. $mode = $this->get_attempt_mode();
  4862. switch ($mode) {
  4863. case 'single':
  4864. $next_mode = 'multiple';
  4865. break;
  4866. case 'multiple':
  4867. $next_mode = 'seriousgame';
  4868. break;
  4869. case 'seriousgame':
  4870. $next_mode = 'single';
  4871. break;
  4872. default:
  4873. $next_mode = 'single';
  4874. break;
  4875. }
  4876. $this->set_attempt_mode($next_mode);
  4877. }
  4878. /**
  4879. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4880. * but possibility to do again a completed item.
  4881. *
  4882. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4883. * @author ndiechburg <noel@cblue.be>
  4884. **/
  4885. public function set_seriousgame_mode()
  4886. {
  4887. $course_id = api_get_course_int_id();
  4888. if ($this->debug > 0) {
  4889. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4890. }
  4891. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4892. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4893. $res = Database::query($sql);
  4894. if (Database::num_rows($res) > 0) {
  4895. $row = Database::fetch_array($res);
  4896. $force = $row['seriousgame_mode'];
  4897. if ($force == 1) {
  4898. $force = 0;
  4899. } elseif ($force == 0) {
  4900. $force = 1;
  4901. }
  4902. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4903. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4904. Database::query($sql);
  4905. $this->seriousgame_mode = $force;
  4906. return $force;
  4907. } else {
  4908. if ($this->debug > 2) {
  4909. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4910. }
  4911. }
  4912. return -1;
  4913. }
  4914. /**
  4915. * Updates the "scorm_debug" value that shows or hide the debug window
  4916. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4917. */
  4918. public function update_scorm_debug()
  4919. {
  4920. $course_id = api_get_course_int_id();
  4921. if ($this->debug > 0) {
  4922. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4923. }
  4924. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  4925. $sql = "SELECT * FROM $lp_table
  4926. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4927. $res = Database::query($sql);
  4928. if (Database::num_rows($res) > 0) {
  4929. $row = Database::fetch_array($res);
  4930. $force = $row['debug'];
  4931. if ($force == 1) {
  4932. $force = 0;
  4933. } elseif ($force == 0) {
  4934. $force = 1;
  4935. }
  4936. $sql = "UPDATE $lp_table SET debug = $force
  4937. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4938. Database::query($sql);
  4939. $this->scorm_debug = $force;
  4940. return $force;
  4941. } else {
  4942. if ($this->debug > 2) {
  4943. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4944. }
  4945. }
  4946. return -1;
  4947. }
  4948. /**
  4949. * Function that makes a call to the function sort_tree_array and create_tree_array
  4950. * @author Kevin Van Den Haute
  4951. * @param array
  4952. */
  4953. public function tree_array($array)
  4954. {
  4955. if ($this->debug > 1) {
  4956. error_log('New LP - In learnpath::tree_array()', 0);
  4957. }
  4958. $array = $this->sort_tree_array($array);
  4959. $this->create_tree_array($array);
  4960. }
  4961. /**
  4962. * Creates an array with the elements of the learning path tree in it
  4963. *
  4964. * @author Kevin Van Den Haute
  4965. * @param array $array
  4966. * @param int $parent
  4967. * @param int $depth
  4968. * @param array $tmp
  4969. */
  4970. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  4971. {
  4972. if ($this->debug > 1) {
  4973. error_log('New LP - In learnpath::create_tree_array())', 0);
  4974. }
  4975. if (is_array($array)) {
  4976. for ($i = 0; $i < count($array); $i++) {
  4977. if ($array[$i]['parent_item_id'] == $parent) {
  4978. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4979. $tmp[] = $array[$i]['parent_item_id'];
  4980. $depth++;
  4981. }
  4982. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4983. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4984. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4985. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  4986. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  4987. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  4988. $this->arrMenu[] = array(
  4989. 'id' => $array[$i]['id'],
  4990. 'ref' => $ref,
  4991. 'item_type' => $array[$i]['item_type'],
  4992. 'title' => $array[$i]['title'],
  4993. 'path' => $path,
  4994. 'description' => $array[$i]['description'],
  4995. 'parent_item_id' => $array[$i]['parent_item_id'],
  4996. 'previous_item_id' => $array[$i]['previous_item_id'],
  4997. 'next_item_id' => $array[$i]['next_item_id'],
  4998. 'min_score' => $array[$i]['min_score'],
  4999. 'max_score' => $array[$i]['max_score'],
  5000. 'mastery_score' => $array[$i]['mastery_score'],
  5001. 'display_order' => $array[$i]['display_order'],
  5002. 'prerequisite' => $preq,
  5003. 'depth' => $depth,
  5004. 'audio' => $audio,
  5005. 'prerequisite_min_score' => $prerequisiteMinScore,
  5006. 'prerequisite_max_score' => $prerequisiteMaxScore
  5007. );
  5008. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5009. }
  5010. }
  5011. }
  5012. }
  5013. /**
  5014. * Sorts a multi dimensional array by parent id and display order
  5015. * @author Kevin Van Den Haute
  5016. *
  5017. * @param array $array (array with al the learning path items in it)
  5018. *
  5019. * @return array
  5020. */
  5021. public function sort_tree_array($array)
  5022. {
  5023. foreach ($array as $key => $row) {
  5024. $parent[$key] = $row['parent_item_id'];
  5025. $position[$key] = $row['display_order'];
  5026. }
  5027. if (count($array) > 0) {
  5028. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5029. }
  5030. return $array;
  5031. }
  5032. /**
  5033. * Function that creates a html list of learning path items so that we can add audio files to them
  5034. * @author Kevin Van Den Haute
  5035. * @return string
  5036. */
  5037. public function overview()
  5038. {
  5039. if ($this->debug > 0) {
  5040. error_log('New LP - In learnpath::overview()', 0);
  5041. }
  5042. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5043. $return = '';
  5044. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5045. // we need to start a form when we want to update all the mp3 files
  5046. if ($update_audio == 'true') {
  5047. $return .= '<form action="' . api_get_self() . '?'.api_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">';
  5048. }
  5049. $return .= '<div id="message"></div>';
  5050. if (count($this->items) == 0) {
  5051. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5052. } else {
  5053. $return_audio = '<table class="data_table">';
  5054. $return_audio .= '<tr>';
  5055. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5056. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5057. $return_audio .= '</tr>';
  5058. if ($update_audio != 'true') {
  5059. $return .= '<div class="col-md-12">';
  5060. $return .= self::return_new_tree($update_audio);
  5061. $return .='</div>';
  5062. $return .= Display::div(
  5063. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn btn-primary')),
  5064. array('style' => 'float:left; margin-top:15px;width:100%')
  5065. );
  5066. } else {
  5067. $return_audio .= self::return_new_tree($update_audio);
  5068. $return .= $return_audio.'</table>';
  5069. }
  5070. // We need to close the form when we are updating the mp3 files.
  5071. if ($update_audio == 'true') {
  5072. $return .= '<div class="footer-audio">';
  5073. $return .= Display::button(
  5074. 'save_audio',
  5075. '<em class="fa fa-file-audio-o"></em> '.get_lang('SaveAudioAndOrganization'),
  5076. array('class' => 'btn btn-primary', 'type' => 'submit')
  5077. );
  5078. $return .= '</div>';
  5079. }
  5080. }
  5081. // We need to close the form when we are updating the mp3 files.
  5082. if ($update_audio == 'true' && isset($this->arrMenu) && count($this->arrMenu) != 0) {
  5083. $return .= '</form>';
  5084. }
  5085. return $return;
  5086. }
  5087. /**
  5088. * @param string string $update_audio
  5089. * @param bool $drop_element_here
  5090. * @return string
  5091. */
  5092. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5093. {
  5094. $return = '';
  5095. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5096. $course_id = api_get_course_int_id();
  5097. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5098. $sql = "SELECT * FROM $tbl_lp_item
  5099. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5100. $result = Database::query($sql);
  5101. $arrLP = array();
  5102. while ($row = Database::fetch_array($result)) {
  5103. $arrLP[] = array(
  5104. 'id' => $row['id'],
  5105. 'item_type' => $row['item_type'],
  5106. 'title' => Security::remove_XSS($row['title']),
  5107. 'path' => $row['path'],
  5108. 'description' => Security::remove_XSS($row['description']),
  5109. 'parent_item_id' => $row['parent_item_id'],
  5110. 'previous_item_id' => $row['previous_item_id'],
  5111. 'next_item_id' => $row['next_item_id'],
  5112. 'max_score' => $row['max_score'],
  5113. 'min_score' => $row['min_score'],
  5114. 'mastery_score' => $row['mastery_score'],
  5115. 'prerequisite' => $row['prerequisite'],
  5116. 'display_order' => $row['display_order'],
  5117. 'audio' => $row['audio'],
  5118. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5119. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5120. );
  5121. }
  5122. $this->tree_array($arrLP);
  5123. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5124. unset ($this->arrMenu);
  5125. $default_data = null;
  5126. $default_content = null;
  5127. $elements = array();
  5128. $return_audio = null;
  5129. for ($i = 0; $i < count($arrLP); $i++) {
  5130. $title = $arrLP[$i]['title'];
  5131. $title_cut = cut($arrLP[$i]['title'], 25);
  5132. // Link for the documents
  5133. if ($arrLP[$i]['item_type'] == 'document') {
  5134. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&mode=preview_document&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5135. $title_cut = Display::url(
  5136. $title_cut,
  5137. $url,
  5138. array(
  5139. 'class' => 'ajax moved',
  5140. 'data-title' => $title_cut
  5141. )
  5142. );
  5143. }
  5144. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5145. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5146. Session::write('pathItem', $arrLP[$i]['path']);
  5147. }
  5148. if (($i % 2) == 0) {
  5149. $oddClass = 'row_odd';
  5150. } else {
  5151. $oddClass = 'row_even';
  5152. }
  5153. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5154. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5155. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5156. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5157. } else {
  5158. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5159. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5160. } else {
  5161. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5162. $icon = Display::return_icon('certificate.png');
  5163. } else {
  5164. $icon = Display::return_icon('folder_document.gif');
  5165. }
  5166. }
  5167. }
  5168. // The audio column.
  5169. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5170. $audio = '';
  5171. if (!$update_audio || $update_audio <> 'true') {
  5172. if (!empty($arrLP[$i]['audio'])) {
  5173. } else {
  5174. $audio .= '';
  5175. }
  5176. } else {
  5177. $types = self::getChapterTypes();
  5178. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5179. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5180. if (!empty ($arrLP[$i]['audio'])) {
  5181. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5182. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5183. }
  5184. }
  5185. }
  5186. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5187. $return_audio .= '</td>';
  5188. $move_icon = '';
  5189. $move_item_icon = '';
  5190. $edit_icon = '';
  5191. $delete_icon = '';
  5192. $audio_icon = '';
  5193. $prerequisities_icon = '';
  5194. $forumIcon = '';
  5195. $previewIcon = '';
  5196. if ($is_allowed_to_edit) {
  5197. if (!$update_audio || $update_audio <> 'true') {
  5198. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5199. $move_icon .= '<a class="moved" href="#">';
  5200. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5201. $move_icon .= '</a>';
  5202. }
  5203. }
  5204. // No edit for this item types
  5205. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5206. if ($arrLP[$i]['item_type'] != 'dir') {
  5207. $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'] . '" class="btn btn-default">';
  5208. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5209. $edit_icon .= '</a>';
  5210. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5211. $forumThread = $this->items[$arrLP[$i]['id']]->getForumThread(
  5212. $this->course_int_id,
  5213. $this->lp_session_id
  5214. );
  5215. if ($forumThread) {
  5216. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5217. 'action' => 'dissociate_forum',
  5218. 'id' => $arrLP[$i]['id'],
  5219. 'lp_id' => $this->lp_id
  5220. ]);
  5221. $forumIcon = Display::url(
  5222. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5223. $forumIconUrl,
  5224. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5225. );
  5226. } else {
  5227. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5228. 'action' => 'create_forum',
  5229. 'id' => $arrLP[$i]['id'],
  5230. 'lp_id' => $this->lp_id
  5231. ]);
  5232. $forumIcon = Display::url(
  5233. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5234. $forumIconUrl,
  5235. ['class' => "btn btn-default lp-btn-associate-forum"]
  5236. );
  5237. }
  5238. }
  5239. } else {
  5240. $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'] . '" class="btn btn-default">';
  5241. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5242. $edit_icon .= '</a>';
  5243. }
  5244. } else {
  5245. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5246. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id.'" class="btn btn-default">';
  5247. $edit_icon .= Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_TINY);
  5248. $edit_icon .= '</a>';
  5249. }
  5250. }
  5251. $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) . '\');" class="btn btn-default">';
  5252. $delete_icon .= Display::return_icon(
  5253. 'delete.png',
  5254. get_lang('LearnpathDeleteModule'),
  5255. [],
  5256. ICON_SIZE_TINY
  5257. );
  5258. $delete_icon .= '</a>';
  5259. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5260. $previewImage = Display::return_icon(
  5261. 'preview_view.png',
  5262. get_lang('Preview'),
  5263. [],
  5264. ICON_SIZE_TINY
  5265. );
  5266. switch ($arrLP[$i]['item_type']) {
  5267. case TOOL_DOCUMENT:
  5268. case TOOL_LP_FINAL_ITEM:
  5269. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5270. $previewIcon = Display::url(
  5271. $previewImage,
  5272. $urlPreviewLink,
  5273. array(
  5274. 'class' => 'btn btn-default ajax',
  5275. 'data-title' => $arrLP[$i]['title']
  5276. )
  5277. );
  5278. break;
  5279. case TOOL_THREAD:
  5280. case TOOL_FORUM:
  5281. case TOOL_LP_FINAL_ITEM:
  5282. case TOOL_LINK:
  5283. $target = '';
  5284. $class = 'btn btn-default ajax';
  5285. if ($arrLP[$i]['item_type'] == TOOL_LINK) {
  5286. $class = 'btn btn-default';
  5287. $target = '_blank';
  5288. }
  5289. $link = self::rl_get_resource_link_for_learnpath(
  5290. $this->course_int_id,
  5291. $this->lp_id,
  5292. $arrLP[$i]['id'],
  5293. 0
  5294. );
  5295. $previewIcon = Display::url(
  5296. $previewImage,
  5297. $link,
  5298. [
  5299. 'class' => $class,
  5300. 'data-title' => $arrLP[$i]['title'],
  5301. 'target' => $target
  5302. ]
  5303. );
  5304. break;
  5305. default:
  5306. $previewIcon = Display::url(
  5307. $previewImage,
  5308. $url.'&action=view_item', ['class' => 'btn btn-default']
  5309. );
  5310. break;
  5311. }
  5312. if ($arrLP[$i]['item_type'] != 'dir') {
  5313. $prerequisities_icon = Display::url(
  5314. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5315. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5316. );
  5317. if ($arrLP[$i]['item_type'] != 'final_item') {
  5318. $move_item_icon = Display::url(
  5319. Display::return_icon(
  5320. 'move.png',
  5321. get_lang('Move'),
  5322. array(),
  5323. ICON_SIZE_TINY
  5324. ),
  5325. $url.'&action=move_item',
  5326. ['class' => 'btn btn-default']
  5327. );
  5328. }
  5329. $audio_icon = Display::url(
  5330. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5331. $url.'&action=add_audio',
  5332. ['class' => 'btn btn-default']
  5333. );
  5334. }
  5335. }
  5336. if ($update_audio != 'true') {
  5337. $row = $move_icon . ' ' . $icon .
  5338. Display::span($title_cut) .
  5339. Display::tag(
  5340. 'div',
  5341. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5342. array('class'=>'btn-toolbar button_actions')
  5343. );
  5344. } else {
  5345. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5346. }
  5347. $parent_id = $arrLP[$i]['parent_item_id'];
  5348. $default_data[$arrLP[$i]['id']] = $row;
  5349. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5350. if (empty($parent_id)) {
  5351. $elements[$arrLP[$i]['id']]['data'] = $row;
  5352. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5353. } else {
  5354. $parent_arrays = array();
  5355. if ($arrLP[$i]['depth'] > 1) {
  5356. //Getting list of parents
  5357. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5358. foreach($arrLP as $item) {
  5359. if ($item['id'] == $parent_id) {
  5360. if ($item['parent_item_id'] == 0) {
  5361. $parent_id = $item['id'];
  5362. break;
  5363. } else {
  5364. $parent_id = $item['parent_item_id'];
  5365. if (empty($parent_arrays)) {
  5366. $parent_arrays[] = intval($item['id']);
  5367. }
  5368. $parent_arrays[] = $parent_id;
  5369. break;
  5370. }
  5371. }
  5372. }
  5373. }
  5374. }
  5375. if (!empty($parent_arrays)) {
  5376. $parent_arrays = array_reverse($parent_arrays);
  5377. $val = '$elements';
  5378. $x = 0;
  5379. foreach($parent_arrays as $item) {
  5380. if ($x != count($parent_arrays) -1) {
  5381. $val .= '["'.$item.'"]["children"]';
  5382. } else {
  5383. $val .= '["'.$item.'"]["children"]';
  5384. }
  5385. $x++;
  5386. }
  5387. $val .= "";
  5388. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5389. eval($code_str);
  5390. } else {
  5391. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5392. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5393. }
  5394. }
  5395. }
  5396. $list = '<ul id="lp_item_list">';
  5397. $tree = self::print_recursive($elements, $default_data, $default_content);
  5398. if (!empty($tree)) {
  5399. $list .= $tree;
  5400. } else {
  5401. if ($drop_element_here) {
  5402. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5403. }
  5404. }
  5405. $list .= '</ul>';
  5406. $return .= Display::panelCollapse(
  5407. $this->name,
  5408. $list,
  5409. 'scorm-list',
  5410. null,
  5411. 'scorm-list-accordion',
  5412. 'scorm-list-collapse'
  5413. );
  5414. if ($update_audio == 'true') {
  5415. $return = $return_audio;
  5416. }
  5417. return $return;
  5418. }
  5419. /**
  5420. * @param array $elements
  5421. * @param array $default_data
  5422. * @param array $default_content
  5423. * @return string
  5424. */
  5425. public function print_recursive($elements, $default_data, $default_content)
  5426. {
  5427. $return = '';
  5428. foreach ($elements as $key => $item) {
  5429. if (isset($item['load_data']) || empty($item['data'])) {
  5430. $item['data'] = $default_data[$item['load_data']];
  5431. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5432. }
  5433. $sub_list = '';
  5434. if (isset($item['type']) && $item['type'] == 'dir') {
  5435. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5436. }
  5437. if (empty($item['children'])) {
  5438. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5439. $active = null;
  5440. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5441. $active = 'active';
  5442. }
  5443. $return .= Display::tag(
  5444. 'li',
  5445. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5446. array('id'=>$key, 'class'=>'record li_container')
  5447. );
  5448. } else {
  5449. // Sections
  5450. if (isset($item['children'])) {
  5451. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5452. }
  5453. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5454. $return .= Display::tag(
  5455. 'li',
  5456. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5457. array('id'=>$key, 'class'=>'record li_container')
  5458. );
  5459. }
  5460. }
  5461. return $return;
  5462. }
  5463. /**
  5464. * This function builds the action menu
  5465. * @param bool $returnContent Optional
  5466. * @param bool $showRequirementButtons Optional. Allow show the requirements button
  5467. * @param bool $isConfigPage Optional. If is the config page, show the edit button
  5468. * @param bool $allowExpand Optional. Allow show the expand/contract button
  5469. * @return string
  5470. */
  5471. public function build_action_menu($returnContent = false, $showRequirementButtons = true, $isConfigPage = false, $allowExpand = true)
  5472. {
  5473. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  5474. $actionsLeft = '';
  5475. $actionsRight = '';
  5476. $actionsLeft .= Display::url(
  5477. Display:: return_icon('preview_view.png', get_lang('Display'), '', ICON_SIZE_MEDIUM),
  5478. 'lp_controller.php?'.api_get_cidreq().'&' . http_build_query([
  5479. 'gradebook' => $gradebook,
  5480. 'action' => 'view',
  5481. 'lp_id' => $_SESSION['oLP']->lp_id,
  5482. 'isStudentView' => 'true'
  5483. ])
  5484. );
  5485. $actionsLeft .= Display::url(
  5486. Display:: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'), '', ICON_SIZE_MEDIUM),
  5487. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5488. 'action' => 'admin_view',
  5489. 'lp_id' => $_SESSION['oLP']->lp_id,
  5490. 'updateaudio' => 'true'
  5491. ])
  5492. );
  5493. if (!$isConfigPage) {
  5494. $actionsLeft .= Display::url(
  5495. Display::return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM),
  5496. 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5497. 'action' => 'edit',
  5498. 'lp_id' => $_SESSION['oLP']->lp_id
  5499. ])
  5500. );
  5501. } else {
  5502. $actionsLeft .= Display::url(
  5503. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM),
  5504. 'lp_controller.php?' . http_build_query([
  5505. 'action' => 'build',
  5506. 'lp_id' => $_SESSION['oLP']->lp_id
  5507. ]) . '&' . api_get_cidreq()
  5508. );
  5509. }
  5510. if ($allowExpand) {
  5511. $actionsLeft .= Display::url(
  5512. Display::return_icon('expand.png', get_lang('Expand'), array('id' => 'expand'), ICON_SIZE_MEDIUM) .
  5513. Display::return_icon('contract.png', get_lang('Collapse'), array('id' => 'contract', 'class' => 'hide'), ICON_SIZE_MEDIUM),
  5514. '#',
  5515. ['role' => 'button', 'id' => 'hide_bar_template']
  5516. );
  5517. }
  5518. if ($showRequirementButtons) {
  5519. $buttons = array(
  5520. array(
  5521. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5522. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5523. 'action' => 'set_previous_step_as_prerequisite',
  5524. 'lp_id' => $_SESSION['oLP']->lp_id
  5525. ])
  5526. ),
  5527. array(
  5528. 'title' => get_lang('ClearAllPrerequisites'),
  5529. 'href' => 'lp_controller.php?' . api_get_cidreq() . '&' . http_build_query([
  5530. 'action' => 'clear_prerequisites',
  5531. 'lp_id' => $_SESSION['oLP']->lp_id
  5532. ])
  5533. ),
  5534. );
  5535. $actionsRight = Display::groupButtonWithDropDown(get_lang('PrerequisitesOptions'), $buttons, true);
  5536. }
  5537. $toolbar = Display::toolbarAction('actions-lp-controller', array($actionsLeft, $actionsRight));
  5538. if ($returnContent) {
  5539. return $toolbar;
  5540. }
  5541. echo $toolbar;
  5542. }
  5543. /**
  5544. * Creates the default learning path folder
  5545. * @param array $course
  5546. * @param int $creatorId
  5547. *
  5548. * @return bool
  5549. */
  5550. public static function generate_learning_path_folder($course, $creatorId = 0)
  5551. {
  5552. // Creating learning_path folder
  5553. $dir = '/learning_path';
  5554. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5555. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5556. $folder = false;
  5557. if (!is_dir($filepath.'/'.$dir)) {
  5558. $folderData = create_unexisting_directory(
  5559. $course,
  5560. $creatorId,
  5561. 0,
  5562. 0,
  5563. 0,
  5564. $filepath,
  5565. $dir,
  5566. get_lang('LearningPaths'),
  5567. 0
  5568. );
  5569. if (!empty($folderData)) {
  5570. $folder = true;
  5571. }
  5572. } else {
  5573. $folder = true;
  5574. }
  5575. return $folder;
  5576. }
  5577. /**
  5578. * @param array $course
  5579. * @param string $lp_name
  5580. * @param int $creatorId
  5581. *
  5582. * @return array
  5583. */
  5584. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5585. {
  5586. $filepath = '';
  5587. $dir = '/learning_path/';
  5588. if (empty($lp_name)) {
  5589. $lp_name = $this->name;
  5590. }
  5591. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5592. $folder = self::generate_learning_path_folder($course, $creatorId);
  5593. // Limits title size
  5594. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5595. $dir = $dir.$title;
  5596. // Creating LP folder
  5597. $documentId = null;
  5598. if ($folder) {
  5599. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5600. if (!is_dir($filepath.'/'.$dir)) {
  5601. $folderData = create_unexisting_directory(
  5602. $course,
  5603. $creatorId,
  5604. 0,
  5605. 0,
  5606. 0,
  5607. $filepath,
  5608. $dir,
  5609. $lp_name
  5610. );
  5611. if (!empty($folderData)) {
  5612. $folder = true;
  5613. }
  5614. $documentId = $folderData['id'];
  5615. } else {
  5616. $folder = true;
  5617. }
  5618. $dir = $dir.'/';
  5619. if ($folder) {
  5620. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5621. }
  5622. }
  5623. if (empty($documentId)) {
  5624. $dir = api_remove_trailing_slash($dir);
  5625. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5626. }
  5627. $array = array(
  5628. 'dir' => $dir,
  5629. 'filepath' => $filepath,
  5630. 'folder' => $folder,
  5631. 'id' => $documentId
  5632. );
  5633. return $array;
  5634. }
  5635. /**
  5636. * Create a new document //still needs some finetuning
  5637. * @param array $courseInfo
  5638. * @param string $content
  5639. * @param string $title
  5640. * @param string $extension
  5641. * @param int $parentId
  5642. * @param int $creatorId creator id
  5643. *
  5644. * @return string
  5645. */
  5646. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5647. {
  5648. if (!empty($courseInfo)) {
  5649. $course_id = $courseInfo['real_id'];
  5650. } else {
  5651. $course_id = api_get_course_int_id();
  5652. }
  5653. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5654. $sessionId = api_get_session_id();
  5655. // Generates folder
  5656. $result = $this->generate_lp_folder($courseInfo);
  5657. $dir = $result['dir'];
  5658. if (empty($parentId)) {
  5659. $postDir = isset($_POST['dir']) ? $_POST['dir'] : $dir;
  5660. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5661. // Please, do not modify this dirname formatting.
  5662. if (strstr($dir, '..')) {
  5663. $dir = '/';
  5664. }
  5665. if (!empty($dir[0]) && $dir[0] == '.') {
  5666. $dir = substr($dir, 1);
  5667. }
  5668. if (!empty($dir[0]) && $dir[0] != '/') {
  5669. $dir = '/'.$dir;
  5670. }
  5671. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5672. $dir .= '/';
  5673. }
  5674. } else {
  5675. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5676. if (!empty($parentInfo)) {
  5677. $dir = $parentInfo['path'].'/';
  5678. }
  5679. }
  5680. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5681. if (!is_dir($filepath)) {
  5682. $dir = '/';
  5683. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document/'.$dir;
  5684. }
  5685. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5686. // is already escaped twice when it gets here.
  5687. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5688. if (!empty($title)) {
  5689. $title = api_replace_dangerous_char(stripslashes($title));
  5690. } else {
  5691. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5692. }
  5693. $title = disable_dangerous_file($title);
  5694. $filename = $title;
  5695. $content = !empty($content) ? $content : $_POST['content_lp'];
  5696. $tmp_filename = $filename;
  5697. $i = 0;
  5698. while (file_exists($filepath . $tmp_filename . '.'.$extension)){
  5699. $tmp_filename = $filename . '_' . ++ $i;
  5700. }
  5701. $filename = $tmp_filename . '.'.$extension;
  5702. if ($extension == 'html') {
  5703. $content = stripslashes($content);
  5704. $content = str_replace(
  5705. api_get_path(WEB_COURSE_PATH),
  5706. api_get_path(REL_PATH).'courses/',
  5707. $content
  5708. );
  5709. // Change the path of mp3 to absolute.
  5710. // The first regexp deals with :// urls.
  5711. $content = preg_replace(
  5712. "|(flashvars=\"file=)([^:/]+)/|",
  5713. "$1".api_get_path(
  5714. REL_COURSE_PATH
  5715. ).$courseInfo['path'].'/document/',
  5716. $content
  5717. );
  5718. // The second regexp deals with audio/ urls.
  5719. $content = preg_replace(
  5720. "|(flashvars=\"file=)([^/]+)/|",
  5721. "$1".api_get_path(
  5722. REL_COURSE_PATH
  5723. ).$courseInfo['path'].'/document/$2/',
  5724. $content
  5725. );
  5726. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5727. $content = str_replace(
  5728. '</body>',
  5729. '<style type="text/css">body{}</style></body>',
  5730. $content
  5731. );
  5732. }
  5733. if (!file_exists($filepath . $filename)) {
  5734. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5735. fputs($fp, $content);
  5736. fclose($fp);
  5737. $file_size = filesize($filepath . $filename);
  5738. $save_file_path = $dir.$filename;
  5739. $document_id = add_document(
  5740. $courseInfo,
  5741. $save_file_path,
  5742. 'file',
  5743. $file_size,
  5744. $tmp_filename,
  5745. '',
  5746. 0, //readonly
  5747. true,
  5748. null,
  5749. $sessionId,
  5750. $creatorId
  5751. );
  5752. if ($document_id) {
  5753. api_item_property_update(
  5754. $courseInfo,
  5755. TOOL_DOCUMENT,
  5756. $document_id,
  5757. 'DocumentAdded',
  5758. $creatorId,
  5759. null,
  5760. null,
  5761. null,
  5762. null,
  5763. $sessionId
  5764. );
  5765. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5766. $new_title = $originalTitle;
  5767. if ($new_comment || $new_title) {
  5768. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5769. $ct = '';
  5770. if ($new_comment)
  5771. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5772. if ($new_title)
  5773. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5774. $sql = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)."
  5775. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5776. Database::query($sql);
  5777. }
  5778. }
  5779. return $document_id;
  5780. }
  5781. }
  5782. }
  5783. /**
  5784. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5785. * @param array $_course array
  5786. * @return void
  5787. */
  5788. public function edit_document($_course)
  5789. {
  5790. $course_id = api_get_course_int_id();
  5791. $urlAppend = api_get_configuration_value('url_append');
  5792. // Please, do not modify this dirname formatting.
  5793. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5794. $dir = isset($_GET['dir']) ? $_GET['dir'] : $postDir;
  5795. if (strstr($dir, '..')) {
  5796. $dir = '/';
  5797. }
  5798. if (isset($dir[0]) && $dir[0] == '.') {
  5799. $dir = substr($dir, 1);
  5800. }
  5801. if (isset($dir[0]) && $dir[0] != '/') {
  5802. $dir = '/'.$dir;
  5803. }
  5804. if (isset($dir[strlen($dir) - 1] ) && $dir[strlen($dir) - 1] != '/') {
  5805. $dir .= '/';
  5806. }
  5807. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5808. if (!is_dir($filepath)) {
  5809. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5810. }
  5811. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  5812. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5813. $document_id = intval($_POST['path']);
  5814. $sql = "SELECT path FROM " . $table_doc . "
  5815. WHERE c_id = $course_id AND id = " . $document_id;
  5816. $res = Database::query($sql);
  5817. $row = Database::fetch_array($res);
  5818. $content = stripslashes($_POST['content_lp']);
  5819. $file = $filepath . $row['path'];
  5820. if ($fp = @ fopen($file, 'w')) {
  5821. $content = str_replace(api_get_path(WEB_COURSE_PATH), $urlAppend.api_get_path(REL_COURSE_PATH), $content);
  5822. // Change the path of mp3 to absolute.
  5823. // The first regexp deals with :// urls.
  5824. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5825. // The second regexp deals with audio/ urls.
  5826. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5827. fputs($fp, $content);
  5828. fclose($fp);
  5829. $sql = "UPDATE " . $table_doc ." SET
  5830. title='".Database::escape_string($_POST['title'])."'
  5831. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5832. Database::query($sql);
  5833. }
  5834. }
  5835. }
  5836. /**
  5837. * Displays the selected item, with a panel for manipulating the item
  5838. * @param int $item_id
  5839. * @param string $msg
  5840. * @return string
  5841. */
  5842. public function display_item($item_id, $msg = null, $show_actions = true)
  5843. {
  5844. $course_id = api_get_course_int_id();
  5845. $return = '';
  5846. if (is_numeric($item_id)) {
  5847. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5848. $sql = "SELECT lp.* FROM $tbl_lp_item as lp
  5849. WHERE
  5850. c_id = $course_id AND
  5851. lp.id = " . intval($item_id);
  5852. $result = Database::query($sql);
  5853. while ($row = Database::fetch_array($result,'ASSOC')) {
  5854. $_SESSION['parent_item_id'] = $row['item_type'] == 'dir' ? $item_id : 0;
  5855. // Prevents wrong parent selection for document, see Bug#1251.
  5856. if ($row['item_type'] != 'dir') {
  5857. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5858. }
  5859. if ($show_actions) {
  5860. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5861. }
  5862. $return .= '<div style="padding:10px;">';
  5863. if ($msg != '') {
  5864. $return .= $msg;
  5865. }
  5866. $return .= '<h3>'.$row['title'].'</h3>';
  5867. switch ($row['item_type']) {
  5868. case TOOL_THREAD:
  5869. $link = $this->rl_get_resource_link_for_learnpath(
  5870. $course_id,
  5871. $row['lp_id'],
  5872. $item_id,
  5873. 0
  5874. );
  5875. $return .= Display::url(
  5876. get_lang('GoToThread'),
  5877. $link,
  5878. ['class' => 'btn btn-primary']
  5879. );
  5880. break;
  5881. case TOOL_FORUM:
  5882. $return .= Display::url(
  5883. get_lang('GoToForum'),
  5884. api_get_path(WEB_CODE_PATH) . 'forum/viewforum.php?' . api_get_cidreq() . '&forum=' . $row['path'],
  5885. ['class' => 'btn btn-primary']
  5886. );
  5887. break;
  5888. case TOOL_QUIZ:
  5889. if (!empty($row['path'])) {
  5890. $exercise = new Exercise();
  5891. $exercise->read($row['path']);
  5892. $return .= $exercise->description.'<br />';
  5893. $return .= Display::url(
  5894. get_lang('GoToExercise'),
  5895. api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . api_get_cidreq() . '&exerciseId=' . $exercise->id,
  5896. ['class' => 'btn btn-primary']
  5897. );
  5898. }
  5899. break;
  5900. case TOOL_LP_FINAL_ITEM:
  5901. $return .= $this->getSavedFinalItem();
  5902. break;
  5903. case TOOL_DOCUMENT:
  5904. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5905. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5906. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5907. $result = Database::query($sql_doc);
  5908. $path_file = Database::result($result, 0, 0);
  5909. $path_parts = pathinfo($path_file);
  5910. // TODO: Correct the following naive comparisons.
  5911. if (in_array($path_parts['extension'], array(
  5912. 'html',
  5913. 'txt',
  5914. 'png',
  5915. 'jpg',
  5916. 'JPG',
  5917. 'jpeg',
  5918. 'JPEG',
  5919. 'gif',
  5920. 'swf',
  5921. 'pdf',
  5922. 'htm'
  5923. ))) {
  5924. $return .= $this->display_document($row['path'], true, true);
  5925. }
  5926. break;
  5927. case TOOL_HOTPOTATOES:
  5928. $return .= $this->display_document($row['path'], false, true);
  5929. break;
  5930. }
  5931. $return .= '</div>';
  5932. }
  5933. }
  5934. return $return;
  5935. }
  5936. /**
  5937. * Shows the needed forms for editing a specific item
  5938. * @param int $item_id
  5939. * @return string
  5940. */
  5941. public function display_edit_item($item_id)
  5942. {
  5943. $course_id = api_get_course_int_id();
  5944. $return = '';
  5945. if (is_numeric($item_id)) {
  5946. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  5947. $sql = "SELECT * FROM $tbl_lp_item
  5948. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5949. $res = Database::query($sql);
  5950. $row = Database::fetch_array($res);
  5951. switch ($row['item_type']) {
  5952. case 'dir':
  5953. case 'asset':
  5954. case 'sco':
  5955. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  5956. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5957. $return .= $this->display_item_form(
  5958. $row['item_type'],
  5959. get_lang('EditCurrentChapter').' :',
  5960. 'edit',
  5961. $item_id,
  5962. $row
  5963. );
  5964. } else {
  5965. $return .= $this->display_item_small_form(
  5966. $row['item_type'],
  5967. get_lang('EditCurrentChapter').' :',
  5968. $row
  5969. );
  5970. }
  5971. break;
  5972. case TOOL_DOCUMENT:
  5973. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  5974. $sql = "SELECT lp.*, doc.path as dir
  5975. FROM $tbl_lp_item as lp
  5976. LEFT JOIN $tbl_doc as doc
  5977. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  5978. WHERE
  5979. lp.c_id = $course_id AND
  5980. doc.c_id = $course_id AND
  5981. lp.id = " . intval($item_id);
  5982. $res_step = Database::query($sql);
  5983. $row_step = Database::fetch_array($res_step, 'ASSOC');
  5984. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5985. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5986. break;
  5987. case TOOL_LINK:
  5988. $link_id = (string) $row['path'];
  5989. if (ctype_digit($link_id)) {
  5990. $tbl_link = Database::get_course_table(TABLE_LINK);
  5991. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5992. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5993. $res_link = Database::query($sql_select);
  5994. $row_link = Database::fetch_array($res_link);
  5995. if (is_array($row_link)) {
  5996. $row['url'] = $row_link['url'];
  5997. }
  5998. }
  5999. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6000. $return .= $this->display_link_form('edit', $item_id, $row);
  6001. break;
  6002. case TOOL_LP_FINAL_ITEM:
  6003. Session::write('finalItem', true);
  6004. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6005. $sql = "SELECT lp.*, doc.path as dir
  6006. FROM $tbl_lp_item as lp
  6007. LEFT JOIN $tbl_doc as doc
  6008. ON (doc.id = lp.path AND lp.c_id = doc.c_id)
  6009. WHERE
  6010. lp.c_id = $course_id AND
  6011. doc.c_id = $course_id AND
  6012. lp.id = " . intval($item_id);
  6013. $res_step = Database::query($sql);
  6014. $row_step = Database::fetch_array($res_step, 'ASSOC');
  6015. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6016. $return .= $this->display_document_form('edit', $item_id, $row_step);
  6017. break;
  6018. case TOOL_QUIZ:
  6019. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6020. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6021. break;
  6022. case TOOL_HOTPOTATOES:
  6023. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6024. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6025. break;
  6026. case TOOL_STUDENTPUBLICATION:
  6027. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6028. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6029. break;
  6030. case TOOL_FORUM:
  6031. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6032. $return .= $this->display_forum_form('edit', $item_id, $row);
  6033. break;
  6034. case TOOL_THREAD:
  6035. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6036. $return .= $this->display_thread_form('edit', $item_id, $row);
  6037. break;
  6038. }
  6039. }
  6040. return $return;
  6041. }
  6042. /**
  6043. * Function that displays a list with al the resources that
  6044. * could be added to the learning path
  6045. * @return string
  6046. */
  6047. public function display_resources()
  6048. {
  6049. $course_code = api_get_course_id();
  6050. // Get all the docs.
  6051. $documents = $this->get_documents(true);
  6052. // Get all the exercises.
  6053. $exercises = $this->get_exercises();
  6054. // Get all the links.
  6055. $links = $this->get_links();
  6056. // Get all the student publications.
  6057. $works = $this->get_student_publications();
  6058. // Get all the forums.
  6059. $forums = $this->get_forums(null, $course_code);
  6060. // Get the final item form (see BT#11048) .
  6061. $finish = $this->getFinalItemForm();
  6062. $headers = array(
  6063. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  6064. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  6065. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  6066. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  6067. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  6068. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  6069. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  6070. );
  6071. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6072. $dir = $_SESSION['oLP']->display_item_form('dir', get_lang('EnterDataNewChapter'), 'add_item');
  6073. echo Display::tabs(
  6074. $headers,
  6075. array(
  6076. $documents,
  6077. $exercises,
  6078. $links,
  6079. $works,
  6080. $forums,
  6081. $dir,
  6082. $finish,
  6083. ),
  6084. 'resource_tab'
  6085. );
  6086. return true;
  6087. }
  6088. /**
  6089. * Returns the extension of a document
  6090. * @param string $filename
  6091. * @return string Extension (part after the last dot)
  6092. */
  6093. public function get_extension($filename)
  6094. {
  6095. $explode = explode('.', $filename);
  6096. return $explode[count($explode) - 1];
  6097. }
  6098. /**
  6099. * Displays a document by id
  6100. *
  6101. * @param int $id
  6102. * @return string
  6103. */
  6104. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6105. {
  6106. $_course = api_get_course_info();
  6107. $course_id = api_get_course_int_id();
  6108. $return = '';
  6109. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  6110. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  6111. WHERE c_id = ".$course_id." AND id = " . $id;
  6112. $res_doc = Database::query($sql_doc);
  6113. $row_doc = Database::fetch_array($res_doc);
  6114. // TODO: Add a path filter.
  6115. if ($iframe) {
  6116. $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>';
  6117. } else {
  6118. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/' . $row_doc['path']);
  6119. }
  6120. return $return;
  6121. }
  6122. /**
  6123. * Return HTML form to add/edit a quiz
  6124. * @param string $action Action (add/edit)
  6125. * @param integer $id Item ID if already exists
  6126. * @param mixed $extra_info Extra information (quiz ID if integer)
  6127. * @return string HTML form
  6128. */
  6129. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6130. {
  6131. $course_id = api_get_course_int_id();
  6132. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6133. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6134. if ($id != 0 && is_array($extra_info)) {
  6135. $item_title = $extra_info['title'];
  6136. $item_description = $extra_info['description'];
  6137. } elseif (is_numeric($extra_info)) {
  6138. $sql = "SELECT title, description
  6139. FROM $tbl_quiz
  6140. WHERE c_id = $course_id AND id = " . $extra_info;
  6141. $result = Database::query($sql);
  6142. $row = Database::fetch_array($result);
  6143. $item_title = $row['title'];
  6144. $item_description = $row['description'];
  6145. } else {
  6146. $item_title = '';
  6147. $item_description = '';
  6148. }
  6149. $item_title = Security::remove_XSS($item_title);
  6150. $item_description = Security::remove_XSS($item_description);
  6151. if ($id != 0 && is_array($extra_info)) {
  6152. $parent = $extra_info['parent_item_id'];
  6153. } else {
  6154. $parent = 0;
  6155. }
  6156. $sql = "SELECT * FROM $tbl_lp_item
  6157. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  6158. $result = Database::query($sql);
  6159. $arrLP = array ();
  6160. while ($row = Database::fetch_array($result)) {
  6161. $arrLP[] = array(
  6162. 'id' => $row['id'],
  6163. 'item_type' => $row['item_type'],
  6164. 'title' => $row['title'],
  6165. 'path' => $row['path'],
  6166. 'description' => $row['description'],
  6167. 'parent_item_id' => $row['parent_item_id'],
  6168. 'previous_item_id' => $row['previous_item_id'],
  6169. 'next_item_id' => $row['next_item_id'],
  6170. 'display_order' => $row['display_order'],
  6171. 'max_score' => $row['max_score'],
  6172. 'min_score' => $row['min_score'],
  6173. 'mastery_score' => $row['mastery_score'],
  6174. 'prerequisite' => $row['prerequisite'],
  6175. 'max_time_allowed' => $row['max_time_allowed']
  6176. );
  6177. }
  6178. $this->tree_array($arrLP);
  6179. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6180. unset ($this->arrMenu);
  6181. $form = new FormValidator(
  6182. 'quiz_form',
  6183. 'POST',
  6184. $this->getCurrentBuildingModeURL()
  6185. );
  6186. $defaults = [];
  6187. if ($action == 'add') {
  6188. $legend = get_lang('CreateTheExercise');
  6189. } elseif ($action == 'move') {
  6190. $legend = get_lang('MoveTheCurrentExercise');
  6191. } else {
  6192. $legend = get_lang('EditCurrentExecice');
  6193. }
  6194. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6195. $legend .= Display::return_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6196. }
  6197. $form->addHeader($legend);
  6198. if ($action != 'move') {
  6199. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6200. $defaults['title'] = $item_title;
  6201. }
  6202. // Select for Parent item, root or chapter
  6203. $selectParent = $form->addSelect(
  6204. 'parent',
  6205. get_lang('Parent'),
  6206. [],
  6207. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6208. );
  6209. $selectParent->addOption($this->name, 0);
  6210. $arrHide = array (
  6211. $id
  6212. );
  6213. for ($i = 0; $i < count($arrLP); $i++) {
  6214. if ($action != 'add') {
  6215. if (
  6216. ($arrLP[$i]['item_type'] == 'dir') &&
  6217. !in_array($arrLP[$i]['id'], $arrHide) &&
  6218. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6219. ) {
  6220. $selectParent->addOption(
  6221. $arrLP[$i]['title'],
  6222. $arrLP[$i]['id'],
  6223. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6224. );
  6225. if ($parent == $arrLP[$i]['id']) {
  6226. $selectParent->setSelected($arrLP[$i]['id']);
  6227. }
  6228. } else {
  6229. $arrHide[] = $arrLP[$i]['id'];
  6230. }
  6231. } else {
  6232. if ($arrLP[$i]['item_type'] == 'dir') {
  6233. $selectParent->addOption(
  6234. $arrLP[$i]['title'],
  6235. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6236. );
  6237. if ($parent == $arrLP[$i]['id']) {
  6238. $selectParent->setSelected($arrLP[$i]['id']);
  6239. }
  6240. }
  6241. }
  6242. }
  6243. if (is_array($arrLP)) {
  6244. reset($arrLP);
  6245. }
  6246. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6247. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6248. for ($i = 0; $i < count($arrLP); $i++) {
  6249. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6250. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6251. if (is_array($extra_info)) {
  6252. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6253. $selectPrevious->setSelected($arrLP[$i]['id']);
  6254. }
  6255. } elseif ($action == 'add') {
  6256. $selectPrevious->setSelected($arrLP[$i]['id']);
  6257. }
  6258. }
  6259. }
  6260. if ($action != 'move') {
  6261. $id_prerequisite = 0;
  6262. if (is_array($arrLP)) {
  6263. foreach ($arrLP as $key => $value) {
  6264. if ($value['id'] == $id) {
  6265. $id_prerequisite = $value['prerequisite'];
  6266. break;
  6267. }
  6268. }
  6269. }
  6270. $arrHide = array ();
  6271. for ($i = 0; $i < count($arrLP); $i++) {
  6272. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6273. if (is_array($extra_info)) {
  6274. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6275. $s_selected_position = $arrLP[$i]['id'];
  6276. }
  6277. } elseif ($action == 'add') {
  6278. $s_selected_position = 0;
  6279. }
  6280. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6281. }
  6282. }
  6283. /*// Commented the prerequisites, only visible in edit (exercise).
  6284. $return .= '<tr>';
  6285. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6286. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6287. foreach($arrHide as $key => $value){
  6288. if($key==$s_selected_position && $action == 'add'){
  6289. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6290. }
  6291. elseif($key==$id_prerequisite && $action == 'edit'){
  6292. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6293. }
  6294. else{
  6295. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6296. }
  6297. }
  6298. $return .= "</select></td>";
  6299. */
  6300. /*$return .= '<tr>';
  6301. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6302. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6303. // Remove temporarily the test description.
  6304. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6305. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6306. $return .= '</tr>'; */
  6307. }
  6308. if ($action == 'add') {
  6309. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6310. } else {
  6311. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6312. }
  6313. if ($action == 'move') {
  6314. $form->addHidden('title', $item_title);
  6315. $form->addHidden('description', $item_description);
  6316. }
  6317. if (is_numeric($extra_info)) {
  6318. $form->addHidden('path', $extra_info);
  6319. } elseif (is_array($extra_info)) {
  6320. $form->addHidden('path', $extra_info['path']);
  6321. }
  6322. $form->addHidden('type', TOOL_QUIZ);
  6323. $form->addHidden('post_time', time());
  6324. $form->setDefaults($defaults);
  6325. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6326. }
  6327. /**
  6328. * Addition of Hotpotatoes tests
  6329. * @param string Action
  6330. * @param integer Internal ID of the item
  6331. * @param mixed Extra information - can be an array with title and description indexes
  6332. * @return string HTML structure to display the hotpotatoes addition formular
  6333. */
  6334. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6335. {
  6336. $course_id = api_get_course_int_id();
  6337. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6338. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6339. if ($id != 0 && is_array($extra_info)) {
  6340. $item_title = stripslashes($extra_info['title']);
  6341. $item_description = stripslashes($extra_info['description']);
  6342. } elseif (is_numeric($extra_info)) {
  6343. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  6344. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6345. WHERE
  6346. c_id = ".$course_id." AND
  6347. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6348. id = " . (int) $extra_info . "
  6349. ORDER BY id ASC";
  6350. $res_hot = Database::query($sql);
  6351. $row = Database::fetch_array($res_hot);
  6352. $item_title = $row['title'];
  6353. $item_description = $row['description'];
  6354. if (!empty ($row['comment'])) {
  6355. $item_title = $row['comment'];
  6356. }
  6357. } else {
  6358. $item_title = '';
  6359. $item_description = '';
  6360. }
  6361. if ($id != 0 && is_array($extra_info)) {
  6362. $parent = $extra_info['parent_item_id'];
  6363. } else {
  6364. $parent = 0;
  6365. }
  6366. $sql = "SELECT * FROM $tbl_lp_item
  6367. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6368. $result = Database::query($sql);
  6369. $arrLP = array ();
  6370. while ($row = Database::fetch_array($result)) {
  6371. $arrLP[] = array(
  6372. 'id' => $row['id'],
  6373. 'item_type' => $row['item_type'],
  6374. 'title' => $row['title'],
  6375. 'path' => $row['path'],
  6376. 'description' => $row['description'],
  6377. 'parent_item_id' => $row['parent_item_id'],
  6378. 'previous_item_id' => $row['previous_item_id'],
  6379. 'next_item_id' => $row['next_item_id'],
  6380. 'display_order' => $row['display_order'],
  6381. 'max_score' => $row['max_score'],
  6382. 'min_score' => $row['min_score'],
  6383. 'mastery_score' => $row['mastery_score'],
  6384. 'prerequisite' => $row['prerequisite'],
  6385. 'max_time_allowed' => $row['max_time_allowed']
  6386. );
  6387. }
  6388. $legend = '<legend>';
  6389. if ($action == 'add') {
  6390. $legend .= get_lang('CreateTheExercise');
  6391. } elseif ($action == 'move') {
  6392. $legend .= get_lang('MoveTheCurrentExercise');
  6393. } else {
  6394. $legend .= get_lang('EditCurrentExecice');
  6395. }
  6396. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6397. $legend .= Display:: return_message(
  6398. get_lang('Warning').' ! '.get_lang('WarningEditingDocument')
  6399. );
  6400. }
  6401. $legend .= '</legend>';
  6402. $return = '<form method="POST">';
  6403. $return .= $legend;
  6404. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6405. $return .= '<tr>';
  6406. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6407. $return .= '<td class="input">';
  6408. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6409. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6410. $arrHide = array(
  6411. $id
  6412. );
  6413. if (count($arrLP) > 0) {
  6414. for ($i = 0; $i < count($arrLP); $i++) {
  6415. if ($action != 'add') {
  6416. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6417. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6418. } else {
  6419. $arrHide[] = $arrLP[$i]['id'];
  6420. }
  6421. } else {
  6422. if ($arrLP[$i]['item_type'] == 'dir')
  6423. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6424. }
  6425. }
  6426. reset($arrLP);
  6427. }
  6428. $return .= '</select>';
  6429. $return .= '</td>';
  6430. $return .= '</tr>';
  6431. $return .= '<tr>';
  6432. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6433. $return .= '<td class="input">';
  6434. $return .= '<select id="previous" name="previous" size="1">';
  6435. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6436. for ($i = 0; $i < count($arrLP); $i++) {
  6437. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6438. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6439. $selected = 'selected="selected" ';
  6440. elseif ($action == 'add') $selected = 'selected="selected" ';
  6441. else
  6442. $selected = '';
  6443. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6444. }
  6445. }
  6446. $return .= '</select>';
  6447. $return .= '</td>';
  6448. $return .= '</tr>';
  6449. if ($action != 'move') {
  6450. $return .= '<tr>';
  6451. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6452. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6453. $return .= '</tr>';
  6454. $id_prerequisite = 0;
  6455. if (is_array($arrLP) && count($arrLP) > 0) {
  6456. foreach ($arrLP as $key => $value) {
  6457. if ($value['id'] == $id) {
  6458. $id_prerequisite = $value['prerequisite'];
  6459. break;
  6460. }
  6461. }
  6462. $arrHide = array ();
  6463. for ($i = 0; $i < count($arrLP); $i++) {
  6464. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6465. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6466. $s_selected_position = $arrLP[$i]['id'];
  6467. elseif ($action == 'add') $s_selected_position = 0;
  6468. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6469. }
  6470. }
  6471. }
  6472. }
  6473. $return .= '<tr>';
  6474. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6475. $return .= '</tr>';
  6476. $return .= '</table>';
  6477. if ($action == 'move') {
  6478. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6479. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6480. }
  6481. if (is_numeric($extra_info)) {
  6482. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6483. } elseif (is_array($extra_info)) {
  6484. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6485. }
  6486. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6487. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6488. $return .= '</form>';
  6489. return $return;
  6490. }
  6491. /**
  6492. * Return the form to display the forum edit/add option
  6493. * @param string Action (add/edit)
  6494. * @param integer ID of the lp_item if already exists
  6495. * @param mixed Forum ID or title
  6496. * @return string HTML form
  6497. */
  6498. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6499. {
  6500. $course_id = api_get_course_int_id();
  6501. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6502. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  6503. if ($id != 0 && is_array($extra_info)) {
  6504. $item_title = stripslashes($extra_info['title']);
  6505. } elseif (is_numeric($extra_info)) {
  6506. $sql = "SELECT forum_title as title, forum_comment as comment
  6507. FROM " . $tbl_forum . "
  6508. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6509. $result = Database::query($sql);
  6510. $row = Database::fetch_array($result);
  6511. $item_title = $row['title'];
  6512. $item_description = $row['comment'];
  6513. } else {
  6514. $item_title = '';
  6515. $item_description = '';
  6516. }
  6517. if ($id != 0 && is_array($extra_info)) {
  6518. $parent = $extra_info['parent_item_id'];
  6519. } else {
  6520. $parent = 0;
  6521. }
  6522. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6523. WHERE
  6524. c_id = ".$course_id." AND
  6525. lp_id = " . $this->lp_id;
  6526. $result = Database::query($sql);
  6527. $arrLP = array();
  6528. while ($row = Database::fetch_array($result)) {
  6529. $arrLP[] = array(
  6530. 'id' => $row['id'],
  6531. 'item_type' => $row['item_type'],
  6532. 'title' => $row['title'],
  6533. 'path' => $row['path'],
  6534. 'description' => $row['description'],
  6535. 'parent_item_id' => $row['parent_item_id'],
  6536. 'previous_item_id' => $row['previous_item_id'],
  6537. 'next_item_id' => $row['next_item_id'],
  6538. 'display_order' => $row['display_order'],
  6539. 'max_score' => $row['max_score'],
  6540. 'min_score' => $row['min_score'],
  6541. 'mastery_score' => $row['mastery_score'],
  6542. 'prerequisite' => $row['prerequisite']
  6543. );
  6544. }
  6545. $this->tree_array($arrLP);
  6546. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6547. unset($this->arrMenu);
  6548. if ($action == 'add') {
  6549. $legend = get_lang('CreateTheForum');
  6550. } elseif ($action == 'move') {
  6551. $legend = get_lang('MoveTheCurrentForum');
  6552. } else {
  6553. $legend = get_lang('EditCurrentForum');
  6554. }
  6555. $form = new FormValidator(
  6556. 'forum_form',
  6557. 'POST',
  6558. $this->getCurrentBuildingModeURL()
  6559. );
  6560. $defaults = [];
  6561. $form->addHeader($legend);
  6562. if ($action != 'move') {
  6563. $form->addText(
  6564. 'title',
  6565. get_lang('Title'),
  6566. true,
  6567. ['id' => 'idTitle', 'class' => 'learnpath_item_form']
  6568. );
  6569. $defaults['title'] = $item_title;
  6570. }
  6571. $selectParent = $form->addSelect(
  6572. 'parent',
  6573. get_lang('Parent'),
  6574. [],
  6575. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6576. );
  6577. $selectParent->addOption($this->name, 0);
  6578. $arrHide = array(
  6579. $id
  6580. );
  6581. //$parent_item_id = $_SESSION['parent_item_id'];
  6582. for ($i = 0; $i < count($arrLP); $i++) {
  6583. if ($action != 'add') {
  6584. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6585. $selectParent->addOption(
  6586. $arrLP[$i]['title'],
  6587. $arrLP[$i]['id'],
  6588. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6589. );
  6590. if ($parent == $arrLP[$i]['id']) {
  6591. $selectParent->setSelected($arrLP[$i]['id']);
  6592. }
  6593. } else {
  6594. $arrHide[] = $arrLP[$i]['id'];
  6595. }
  6596. } else {
  6597. if ($arrLP[$i]['item_type'] == 'dir') {
  6598. $selectParent->addOption(
  6599. $arrLP[$i]['title'],
  6600. $arrLP[$i]['id'],
  6601. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6602. );
  6603. if ($parent == $arrLP[$i]['id']) {
  6604. $selectParent->setSelected($arrLP[$i]['id']);
  6605. }
  6606. }
  6607. }
  6608. }
  6609. if (is_array($arrLP)) {
  6610. reset($arrLP);
  6611. }
  6612. $selectPrevious = $form->addSelect(
  6613. 'previous',
  6614. get_lang('Position'),
  6615. [],
  6616. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6617. );
  6618. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6619. for ($i = 0; $i < count($arrLP); $i++) {
  6620. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6621. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6622. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6623. $selectPrevious->setSelected($arrLP[$i]['id']);
  6624. } elseif ($action == 'add') {
  6625. $selectPrevious->setSelected($arrLP[$i]['id']);
  6626. }
  6627. }
  6628. }
  6629. if ($action != 'move') {
  6630. $id_prerequisite = 0;
  6631. if (is_array($arrLP)) {
  6632. foreach ($arrLP as $key => $value) {
  6633. if ($value['id'] == $id) {
  6634. $id_prerequisite = $value['prerequisite'];
  6635. break;
  6636. }
  6637. }
  6638. }
  6639. $arrHide = array();
  6640. for ($i = 0; $i < count($arrLP); $i++) {
  6641. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6642. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6643. $s_selected_position = $arrLP[$i]['id'];
  6644. elseif ($action == 'add') $s_selected_position = 0;
  6645. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6646. }
  6647. }
  6648. }
  6649. if ($action == 'add') {
  6650. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6651. } else {
  6652. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6653. }
  6654. if ($action == 'move') {
  6655. $form->addHidden('title', $item_title);
  6656. $form->addHidden('description', $item_description);
  6657. }
  6658. if (is_numeric($extra_info)) {
  6659. $form->addHidden('path', $extra_info);
  6660. } elseif (is_array($extra_info)) {
  6661. $form->addHidden('path', $extra_info['path']);
  6662. }
  6663. $form->addHidden('type', TOOL_FORUM);
  6664. $form->addHidden('post_time', time());
  6665. $form->setDefaults($defaults);
  6666. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6667. }
  6668. /**
  6669. * Return HTML form to add/edit forum threads
  6670. * @param string Action (add/edit)
  6671. * @param integer Item ID if already exists in learning path
  6672. * @param mixed Extra information (thread ID if integer)
  6673. * @return string HTML form
  6674. */
  6675. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6676. {
  6677. $course_id = api_get_course_int_id();
  6678. if (empty($course_id)) {
  6679. return null;
  6680. }
  6681. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6682. $tbl_forum = Database::get_course_table(TABLE_FORUM_THREAD);
  6683. if ($id != 0 && is_array($extra_info)) {
  6684. $item_title = stripslashes($extra_info['title']);
  6685. } elseif (is_numeric($extra_info)) {
  6686. $sql = "SELECT thread_title as title FROM $tbl_forum
  6687. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6688. $result = Database::query($sql);
  6689. $row = Database::fetch_array($result);
  6690. $item_title = $row['title'];
  6691. $item_description = '';
  6692. } else {
  6693. $item_title = '';
  6694. $item_description = '';
  6695. }
  6696. if ($id != 0 && is_array($extra_info)) {
  6697. $parent = $extra_info['parent_item_id'];
  6698. } else {
  6699. $parent = 0;
  6700. }
  6701. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6702. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6703. $result = Database::query($sql);
  6704. $arrLP = array();
  6705. while ($row = Database::fetch_array($result)) {
  6706. $arrLP[] = array (
  6707. 'id' => $row['id'],
  6708. 'item_type' => $row['item_type'],
  6709. 'title' => $row['title'],
  6710. 'path' => $row['path'],
  6711. 'description' => $row['description'],
  6712. 'parent_item_id' => $row['parent_item_id'],
  6713. 'previous_item_id' => $row['previous_item_id'],
  6714. 'next_item_id' => $row['next_item_id'],
  6715. 'display_order' => $row['display_order'],
  6716. 'max_score' => $row['max_score'],
  6717. 'min_score' => $row['min_score'],
  6718. 'mastery_score' => $row['mastery_score'],
  6719. 'prerequisite' => $row['prerequisite']
  6720. );
  6721. }
  6722. $this->tree_array($arrLP);
  6723. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6724. unset ($this->arrMenu);
  6725. $form = new FormValidator(
  6726. 'thread_form',
  6727. 'POST',
  6728. $this->getCurrentBuildingModeURL()
  6729. );
  6730. $defaults = [];
  6731. if ($action == 'add') {
  6732. $legend = get_lang('CreateTheForum');
  6733. } elseif ($action == 'move') {
  6734. $legend = get_lang('MoveTheCurrentForum');
  6735. } else {
  6736. $legend = get_lang('EditCurrentForum');
  6737. }
  6738. $form->addHeader($legend);
  6739. $selectParent = $form->addSelect(
  6740. 'parent',
  6741. get_lang('Parent'),
  6742. [],
  6743. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6744. );
  6745. $selectParent->addOption($this->name, 0);
  6746. $arrHide = array (
  6747. $id
  6748. );
  6749. for ($i = 0; $i < count($arrLP); $i++) {
  6750. if ($action != 'add') {
  6751. if (
  6752. ($arrLP[$i]['item_type'] == 'dir') &&
  6753. !in_array($arrLP[$i]['id'], $arrHide) &&
  6754. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6755. ) {
  6756. $selectParent->addOption(
  6757. $arrLP[$i]['title'],
  6758. $arrLP[$i]['id'],
  6759. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6760. );
  6761. if ($parent == $arrLP[$i]['id']) {
  6762. $selectParent->setSelected($arrLP[$i]['id']);
  6763. }
  6764. } else {
  6765. $arrHide[] = $arrLP[$i]['id'];
  6766. }
  6767. } else {
  6768. if ($arrLP[$i]['item_type'] == 'dir') {
  6769. $selectParent->addOption(
  6770. $arrLP[$i]['title'],
  6771. $arrLP[$i]['id'],
  6772. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6773. );
  6774. if ($parent == $arrLP[$i]['id']) {
  6775. $selectParent->setSelected($arrLP[$i]['id']);
  6776. }
  6777. }
  6778. }
  6779. }
  6780. if ($arrLP != null) {
  6781. reset($arrLP);
  6782. }
  6783. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6784. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6785. for ($i = 0; $i < count($arrLP); $i++) {
  6786. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6787. $selectPrevious->addOption(
  6788. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6789. $arrLP[$i]['id']
  6790. );
  6791. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6792. $selectPrevious->setSelected($arrLP[$i]['id']);
  6793. } elseif ($action == 'add') {
  6794. $selectPrevious->setSelected($arrLP[$i]['id']);
  6795. }
  6796. }
  6797. }
  6798. if ($action != 'move') {
  6799. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6800. $defaults['title'] = $item_title;
  6801. $id_prerequisite = 0;
  6802. if ($arrLP != null) {
  6803. foreach ($arrLP as $key => $value) {
  6804. if ($value['id'] == $id) {
  6805. $id_prerequisite = $value['prerequisite'];
  6806. break;
  6807. }
  6808. }
  6809. }
  6810. $arrHide = array();
  6811. $s_selected_position = 0;
  6812. for ($i = 0; $i < count($arrLP); $i++) {
  6813. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  6814. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6815. $s_selected_position = $arrLP[$i]['id'];
  6816. elseif ($action == 'add') $s_selected_position = 0;
  6817. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6818. }
  6819. }
  6820. $selectPrerequisites = $form->addSelect(
  6821. 'prerequisites',
  6822. get_lang('LearnpathPrerequisites'),
  6823. [],
  6824. ['id' => 'prerequisites']
  6825. );
  6826. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6827. foreach ($arrHide as $key => $value) {
  6828. $selectPrerequisites->addOption($value['value'], $key);
  6829. if ($key == $s_selected_position && $action == 'add') {
  6830. $selectPrerequisites->setSelected($key);
  6831. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6832. $selectPrerequisites->setSelected($key);
  6833. }
  6834. }
  6835. }
  6836. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6837. if ($action == 'move') {
  6838. $form->addHidden('title', $item_title);
  6839. $form->addHidden('description', $item_description);
  6840. }
  6841. if (is_numeric($extra_info)) {
  6842. $form->addHidden('path', $extra_info);
  6843. }
  6844. elseif (is_array($extra_info)) {
  6845. $form->addHidden('path', $extra_info['path']);
  6846. }
  6847. $form->addHidden('type', TOOL_THREAD);
  6848. $form->addHidden('post_time', time());
  6849. $form->setDefaults($defaults);
  6850. return $form->returnForm();
  6851. }
  6852. /**
  6853. * Return the HTML form to display an item (generally a dir item)
  6854. * @param string Item type (dir)
  6855. * @param string Title (optional, only when creating)
  6856. * @param string Action ('add'/'edit')
  6857. * @param integer lp_item ID
  6858. * @param mixed Extra info
  6859. * @return string HTML form
  6860. */
  6861. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6862. {
  6863. $course_id = api_get_course_int_id();
  6864. $_course = api_get_course_info();
  6865. global $charset;
  6866. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  6867. if ($id != 0 && is_array($extra_info)) {
  6868. $item_title = $extra_info['title'];
  6869. $item_description = $extra_info['description'];
  6870. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6871. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6872. } else {
  6873. $item_title = '';
  6874. $item_description = '';
  6875. $item_path_fck = '';
  6876. }
  6877. if ($id != 0 && is_array($extra_info)) {
  6878. $parent = $extra_info['parent_item_id'];
  6879. } else {
  6880. $parent = 0;
  6881. }
  6882. $id = intval($id);
  6883. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6884. WHERE
  6885. c_id = ".$course_id." AND
  6886. lp_id = " . $this->lp_id . " AND
  6887. id != $id";
  6888. if ($item_type == 'dir') {
  6889. $sql .= " AND parent_item_id = 0";
  6890. }
  6891. $result = Database::query($sql);
  6892. $arrLP = [];
  6893. while ($row = Database::fetch_array($result)) {
  6894. $arrLP[] = array(
  6895. 'id' => $row['id'],
  6896. 'item_type' => $row['item_type'],
  6897. 'title' => $row['title'],
  6898. 'path' => $row['path'],
  6899. 'description' => $row['description'],
  6900. 'parent_item_id' => $row['parent_item_id'],
  6901. 'previous_item_id' => $row['previous_item_id'],
  6902. 'next_item_id' => $row['next_item_id'],
  6903. 'max_score' => $row['max_score'],
  6904. 'min_score' => $row['min_score'],
  6905. 'mastery_score' => $row['mastery_score'],
  6906. 'prerequisite' => $row['prerequisite'],
  6907. 'display_order' => $row['display_order']
  6908. );
  6909. }
  6910. $this->tree_array($arrLP);
  6911. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6912. unset($this->arrMenu);
  6913. $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
  6914. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6915. $form = new FormValidator('form', 'POST', $url);
  6916. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6917. $defaults['description'] = $item_description;
  6918. $form->addElement('header', $title);
  6919. //$arrHide = array($id);
  6920. $arrHide[0]['value'] = Security::remove_XSS($this->name);
  6921. $arrHide[0]['padding'] = 20;
  6922. $charset = api_get_system_encoding();
  6923. for ($i = 0; $i < count($arrLP); $i++) {
  6924. if ($action != 'add') {
  6925. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6926. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6927. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6928. if ($parent == $arrLP[$i]['id']) {
  6929. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6930. }
  6931. }
  6932. } else {
  6933. if ($arrLP[$i]['item_type'] == 'dir') {
  6934. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6935. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6936. if ($parent == $arrLP[$i]['id']) {
  6937. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6938. }
  6939. }
  6940. }
  6941. }
  6942. if ($action != 'move') {
  6943. $form->addElement('text', 'title', get_lang('Title'));
  6944. $form->applyFilter('title', 'html_filter');
  6945. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6946. } else {
  6947. $form->addElement('hidden', 'title');
  6948. }
  6949. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', array('id' => 'idParent', 'onchange' => "javascript: load_cbo(this.value);"));
  6950. foreach ($arrHide as $key => $value) {
  6951. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6952. }
  6953. if (!empty($s_selected_parent)) {
  6954. $parent_select->setSelected($s_selected_parent);
  6955. }
  6956. if (is_array($arrLP)) {
  6957. reset($arrLP);
  6958. }
  6959. $arrHide = array();
  6960. // POSITION
  6961. for ($i = 0; $i < count($arrLP); $i++) {
  6962. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6963. //this is the same!
  6964. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6965. $s_selected_position = $arrLP[$i]['id'];
  6966. } elseif ($action == 'add') {
  6967. $s_selected_position = $arrLP[$i]['id'];
  6968. }
  6969. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6970. }
  6971. }
  6972. $position = $form->addElement(
  6973. 'select',
  6974. 'previous',
  6975. get_lang('Position'),
  6976. '',
  6977. array('id' => 'previous')
  6978. );
  6979. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6980. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6981. foreach ($arrHide as $key => $value) {
  6982. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6983. }
  6984. if (!empty ($s_selected_position)) {
  6985. $position->setSelected($s_selected_position);
  6986. }
  6987. if (is_array($arrLP)) {
  6988. reset($arrLP);
  6989. }
  6990. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6991. //fix in order to use the tab
  6992. if ($item_type == 'dir') {
  6993. $form->addElement('hidden', 'type', 'dir');
  6994. }
  6995. $extension = null;
  6996. if (!empty($item_path)) {
  6997. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6998. }
  6999. //assets can't be modified
  7000. //$item_type == 'asset' ||
  7001. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7002. if ($item_type == 'sco') {
  7003. $form->addElement('html', '<script>alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  7004. }
  7005. $renderer = $form->defaultRenderer();
  7006. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  7007. $relative_prefix = '';
  7008. $editor_config = array(
  7009. 'ToolbarSet' => 'LearningPathDocuments',
  7010. 'Width' => '100%',
  7011. 'Height' => '500',
  7012. 'FullPage' => true,
  7013. 'CreateDocumentDir' => $relative_prefix,
  7014. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7015. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7016. );
  7017. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7018. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  7019. $defaults['content_lp'] = file_get_contents($content_path);
  7020. }
  7021. $form->addElement('hidden', 'type', $item_type);
  7022. $form->addElement('hidden', 'post_time', time());
  7023. $form->setDefaults($defaults);
  7024. return $form->return_form();
  7025. }
  7026. /**
  7027. * @return string
  7028. */
  7029. public function getCurrentBuildingModeURL()
  7030. {
  7031. $pathItem = isset($_GET['path_item']) ? (int) $_GET['path_item'] : '';
  7032. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  7033. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  7034. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : '';
  7035. $currentUrl = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&lp_id='.$this->lp_id.'&path_item='.$pathItem.'&view='.$view.'&id='.$id;
  7036. return $currentUrl;
  7037. }
  7038. /**
  7039. * Returns the form to update or create a document
  7040. * @param string $action (add/edit)
  7041. * @param integer $id ID of the lp_item (if already exists)
  7042. * @param mixed $extra_info Integer if document ID, string if info ('new')
  7043. * @return string HTML form
  7044. */
  7045. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7046. {
  7047. $course_id = api_get_course_int_id();
  7048. $_course = api_get_course_info();
  7049. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7050. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  7051. $no_display_edit_textarea = false;
  7052. $item_description = '';
  7053. //If action==edit document
  7054. //We don't display the document form if it's not an editable document (html or txt file)
  7055. if ($action == 'edit') {
  7056. if (is_array($extra_info)) {
  7057. $path_parts = pathinfo($extra_info['dir']);
  7058. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7059. $no_display_edit_textarea = true;
  7060. }
  7061. }
  7062. }
  7063. $no_display_add = false;
  7064. // If action==add an existing document
  7065. // We don't display the document form if it's not an editable document (html or txt file).
  7066. if ($action == 'add') {
  7067. if (is_numeric($extra_info)) {
  7068. $sql_doc = "SELECT path FROM $tbl_doc
  7069. WHERE c_id = $course_id AND id = " . intval($extra_info);
  7070. $result = Database::query($sql_doc);
  7071. $path_file = Database::result($result, 0, 0);
  7072. $path_parts = pathinfo($path_file);
  7073. if ($path_parts['extension'] != 'txt' && $path_parts['extension'] != 'html') {
  7074. $no_display_add = true;
  7075. }
  7076. }
  7077. }
  7078. if ($id != 0 && is_array($extra_info)) {
  7079. $item_title = stripslashes($extra_info['title']);
  7080. $item_description = stripslashes($extra_info['description']);
  7081. $item_terms = stripslashes($extra_info['terms']);
  7082. if (empty ($item_title)) {
  7083. $path_parts = pathinfo($extra_info['path']);
  7084. $item_title = stripslashes($path_parts['filename']);
  7085. }
  7086. } elseif (is_numeric($extra_info)) {
  7087. $sql = "SELECT path, title FROM $tbl_doc
  7088. WHERE
  7089. c_id = ".$course_id." AND
  7090. id = " . intval($extra_info);
  7091. $result = Database::query($sql);
  7092. $row = Database::fetch_array($result);
  7093. $item_title = $row['title'];
  7094. $item_title = str_replace('_', ' ', $item_title);
  7095. if (empty ($item_title)) {
  7096. $path_parts = pathinfo($row['path']);
  7097. $item_title = stripslashes($path_parts['filename']);
  7098. }
  7099. } else {
  7100. $item_title = '';
  7101. $item_description = '';
  7102. }
  7103. $return = '<legend>';
  7104. if ($id != 0 && is_array($extra_info)) {
  7105. $parent = $extra_info['parent_item_id'];
  7106. } else {
  7107. $parent = 0;
  7108. }
  7109. $sql = "SELECT * FROM $tbl_lp_item
  7110. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  7111. $result = Database::query($sql);
  7112. $arrLP = array();
  7113. while ($row = Database::fetch_array($result)) {
  7114. $arrLP[] = array(
  7115. 'id' => $row['id'],
  7116. 'item_type' => $row['item_type'],
  7117. 'title' => $row['title'],
  7118. 'path' => $row['path'],
  7119. 'description' => $row['description'],
  7120. 'parent_item_id' => $row['parent_item_id'],
  7121. 'previous_item_id' => $row['previous_item_id'],
  7122. 'next_item_id' => $row['next_item_id'],
  7123. 'display_order' => $row['display_order'],
  7124. 'max_score' => $row['max_score'],
  7125. 'min_score' => $row['min_score'],
  7126. 'mastery_score' => $row['mastery_score'],
  7127. 'prerequisite' => $row['prerequisite']
  7128. );
  7129. }
  7130. $this->tree_array($arrLP);
  7131. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7132. unset ($this->arrMenu);
  7133. if ($action == 'add') {
  7134. $return .= get_lang('CreateTheDocument');
  7135. } elseif ($action == 'move') {
  7136. $return .= get_lang('MoveTheCurrentDocument');
  7137. } else {
  7138. $return .= get_lang('EditTheCurrentDocument');
  7139. }
  7140. $return .= '</legend>';
  7141. if (isset($_GET['edit']) && $_GET['edit'] == 'true') {
  7142. $return .= Display::return_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7143. }
  7144. $form = new FormValidator(
  7145. 'form',
  7146. 'POST',
  7147. $this->getCurrentBuildingModeURL(),
  7148. '',
  7149. array('enctype' => 'multipart/form-data')
  7150. );
  7151. $defaults['title'] = Security::remove_XSS($item_title);
  7152. if (empty($item_title)) {
  7153. $defaults['title'] = Security::remove_XSS($item_title);
  7154. }
  7155. $defaults['description'] = $item_description;
  7156. $form->addElement('html', $return);
  7157. if ($action != 'move') {
  7158. $data = $this->generate_lp_folder($_course);
  7159. if ($action != 'edit') {
  7160. $folders = DocumentManager::get_all_document_folders(
  7161. $_course,
  7162. 0,
  7163. true
  7164. );
  7165. DocumentManager::build_directory_selector(
  7166. $folders,
  7167. '',
  7168. array(),
  7169. true,
  7170. $form,
  7171. 'directory_parent_id'
  7172. );
  7173. }
  7174. if (isset($data['id'])) {
  7175. $defaults['directory_parent_id'] = $data['id'];
  7176. }
  7177. $form->addElement(
  7178. 'text',
  7179. 'title',
  7180. get_lang('Title'),
  7181. array('id' => 'idTitle', 'class' => 'col-md-4')
  7182. );
  7183. $form->applyFilter('title', 'html_filter');
  7184. }
  7185. $arrHide[0]['value'] = $this->name;
  7186. $arrHide[0]['padding'] = 20;
  7187. for ($i = 0; $i < count($arrLP); $i++) {
  7188. if ($action != 'add') {
  7189. if ($arrLP[$i]['item_type'] == 'dir' && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7190. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7191. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7192. if ($parent == $arrLP[$i]['id']) {
  7193. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7194. }
  7195. }
  7196. } else {
  7197. if ($arrLP[$i]['item_type'] == 'dir') {
  7198. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7199. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7200. if ($parent == $arrLP[$i]['id']) {
  7201. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7202. }
  7203. }
  7204. }
  7205. }
  7206. $parent_select = $form->addSelect(
  7207. 'parent',
  7208. get_lang('Parent'),
  7209. [],
  7210. [
  7211. 'id' => 'idParent',
  7212. 'onchange' => 'javascript: load_cbo(this.value);',
  7213. ]
  7214. );
  7215. $my_count = 0;
  7216. foreach ($arrHide as $key => $value) {
  7217. if ($my_count != 0) {
  7218. // The LP name is also the first section and is not in the same charset like the other sections.
  7219. $value['value'] = Security::remove_XSS($value['value']);
  7220. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7221. } else {
  7222. $value['value'] = Security::remove_XSS($value['value']);
  7223. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7224. }
  7225. $my_count++;
  7226. }
  7227. if (!empty($id)) {
  7228. $parent_select->setSelected($parent);
  7229. } else {
  7230. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0 ;
  7231. $parent_select->setSelected($parent_item_id);
  7232. }
  7233. if (is_array($arrLP)) {
  7234. reset($arrLP);
  7235. }
  7236. $arrHide = array();
  7237. $s_selected_position = null;
  7238. // POSITION
  7239. $lastPosition = null;
  7240. for ($i = 0; $i < count($arrLP); $i++) {
  7241. if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) ||
  7242. $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM
  7243. ) {
  7244. if ((isset($extra_info['previous_item_id']) &&
  7245. $extra_info['previous_item_id'] == $arrLP[$i]['id']) || $action == 'add'
  7246. ) {
  7247. $s_selected_position = $arrLP[$i]['id'];
  7248. }
  7249. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7250. }
  7251. $lastPosition = $arrLP[$i]['id'];
  7252. }
  7253. if (empty($s_selected_position)) {
  7254. $s_selected_position = $lastPosition;
  7255. }
  7256. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7257. $position->addOption(get_lang('FirstPosition'), 0);
  7258. foreach ($arrHide as $key => $value) {
  7259. $padding = isset($value['padding']) ? $value['padding']: 20;
  7260. $position->addOption(
  7261. $value['value'],
  7262. $key,
  7263. 'style="padding-left:'.$padding.'px;"'
  7264. );
  7265. }
  7266. $position->setSelected($s_selected_position);
  7267. if (is_array($arrLP)) {
  7268. reset($arrLP);
  7269. }
  7270. if ($action != 'move') {
  7271. $id_prerequisite = 0;
  7272. if (is_array($arrLP)) {
  7273. foreach ($arrLP as $key => $value) {
  7274. if ($value['id'] == $id) {
  7275. $id_prerequisite = $value['prerequisite'];
  7276. break;
  7277. }
  7278. }
  7279. }
  7280. $arrHide = array();
  7281. for ($i = 0; $i < count($arrLP); $i++) {
  7282. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir' && $arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7283. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7284. $s_selected_position = $arrLP[$i]['id'];
  7285. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7286. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7287. }
  7288. }
  7289. if (!$no_display_add) {
  7290. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7291. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7292. if ($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM || $edit == 'true') {
  7293. if (isset ($_POST['content'])) {
  7294. $content = stripslashes($_POST['content']);
  7295. } elseif (is_array($extra_info)) {
  7296. //If it's an html document or a text file
  7297. if (!$no_display_edit_textarea) {
  7298. $content = $this->display_document($extra_info['path'], false, false);
  7299. }
  7300. } elseif (is_numeric($extra_info)) {
  7301. $content = $this->display_document(
  7302. $extra_info,
  7303. false,
  7304. false
  7305. );
  7306. } else {
  7307. $content = '';
  7308. }
  7309. if (!$no_display_edit_textarea) {
  7310. // We need to calculate here some specific settings for the online editor.
  7311. // The calculated settings work for documents in the Documents tool
  7312. // (on the root or in subfolders).
  7313. // For documents in native scorm packages it is unclear whether the
  7314. // online editor should be activated or not.
  7315. // A new document, it is in the root of the repository.
  7316. $relative_path = '';
  7317. $relative_prefix = '';
  7318. if (is_array($extra_info) && $extra_info != 'new') {
  7319. // The document already exists. Whe have to determine its relative path towards the repository root.
  7320. $relative_path = explode('/', $extra_info['dir']);
  7321. $cnt = count($relative_path) - 2;
  7322. if ($cnt < 0) {
  7323. $cnt = 0;
  7324. }
  7325. $relative_prefix = str_repeat('../', $cnt);
  7326. $relative_path = array_slice($relative_path, 1, $cnt);
  7327. $relative_path = implode('/', $relative_path);
  7328. if (strlen($relative_path) > 0) {
  7329. $relative_path = $relative_path . '/';
  7330. }
  7331. } else {
  7332. $result = $this->generate_lp_folder($_course);
  7333. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7334. $relative_prefix = '../../';
  7335. }
  7336. $editor_config = array(
  7337. 'ToolbarSet' => 'LearningPathDocuments',
  7338. 'Width' => '100%',
  7339. 'Height' => '500',
  7340. 'FullPage' => true,
  7341. 'CreateDocumentDir' => $relative_prefix,
  7342. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7343. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'.$relative_path
  7344. );
  7345. if ($_GET['action'] == 'add_item') {
  7346. $class = 'add';
  7347. $text = get_lang('LPCreateDocument');
  7348. } else {
  7349. if ($_GET['action'] == 'edit_item') {
  7350. $class = 'save';
  7351. $text = get_lang('SaveDocument');
  7352. }
  7353. }
  7354. $form->addButtonSave($text, 'submit_button');
  7355. $renderer = $form->defaultRenderer();
  7356. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7357. $form->addElement('html', '<div class="editor-lp">');
  7358. $form->addHtmlEditor('content_lp', null, null, true, $editor_config, true);
  7359. $form->addElement('html', '</div>');
  7360. $defaults['content_lp'] = $content;
  7361. }
  7362. } elseif (is_numeric($extra_info)) {
  7363. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7364. $return = $this->display_document($extra_info, true, true, true);
  7365. $form->addElement('html', $return);
  7366. }
  7367. }
  7368. }
  7369. if (isset($extra_info['item_type'] ) &&
  7370. $extra_info['item_type'] == TOOL_LP_FINAL_ITEM
  7371. ) {
  7372. $parent_select->freeze();
  7373. $position->freeze();
  7374. }
  7375. if ($action == 'move') {
  7376. $form->addElement('hidden', 'title', $item_title);
  7377. $form->addElement('hidden', 'description', $item_description);
  7378. }
  7379. if (is_numeric($extra_info)) {
  7380. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7381. $form->addElement('hidden', 'path', $extra_info);
  7382. } elseif (is_array($extra_info)) {
  7383. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7384. $form->addElement('hidden', 'path', $extra_info['path']);
  7385. }
  7386. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7387. $form->addElement('hidden', 'post_time', time());
  7388. $form->setDefaults($defaults);
  7389. return $form->returnForm();
  7390. }
  7391. /**
  7392. * Return HTML form to add/edit a link item
  7393. * @param string $action (add/edit)
  7394. * @param integer $id Item ID if exists
  7395. * @param mixed $extra_info
  7396. * @return string HTML form
  7397. */
  7398. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7399. {
  7400. $course_id = api_get_course_int_id();
  7401. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7402. $tbl_link = Database::get_course_table(TABLE_LINK);
  7403. if ($id != 0 && is_array($extra_info)) {
  7404. $item_title = stripslashes($extra_info['title']);
  7405. $item_description = stripslashes($extra_info['description']);
  7406. $item_url = stripslashes($extra_info['url']);
  7407. } elseif (is_numeric($extra_info)) {
  7408. $extra_info = intval($extra_info);
  7409. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7410. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7411. $result = Database::query($sql);
  7412. $row = Database::fetch_array($result);
  7413. $item_title = $row['title'];
  7414. $item_description = $row['description'];
  7415. $item_url = $row['url'];
  7416. } else {
  7417. $item_title = '';
  7418. $item_description = '';
  7419. $item_url = '';
  7420. }
  7421. $form = new FormValidator(
  7422. 'edit_link',
  7423. 'POST',
  7424. $this->getCurrentBuildingModeURL()
  7425. );
  7426. $defaults = [];
  7427. if ($id != 0 && is_array($extra_info)) {
  7428. $parent = $extra_info['parent_item_id'];
  7429. } else {
  7430. $parent = 0;
  7431. }
  7432. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7433. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7434. $result = Database::query($sql);
  7435. $arrLP = array();
  7436. while ($row = Database::fetch_array($result)) {
  7437. $arrLP[] = array(
  7438. 'id' => $row['id'],
  7439. 'item_type' => $row['item_type'],
  7440. 'title' => $row['title'],
  7441. 'path' => $row['path'],
  7442. 'description' => $row['description'],
  7443. 'parent_item_id' => $row['parent_item_id'],
  7444. 'previous_item_id' => $row['previous_item_id'],
  7445. 'next_item_id' => $row['next_item_id'],
  7446. 'display_order' => $row['display_order'],
  7447. 'max_score' => $row['max_score'],
  7448. 'min_score' => $row['min_score'],
  7449. 'mastery_score' => $row['mastery_score'],
  7450. 'prerequisite' => $row['prerequisite']
  7451. );
  7452. }
  7453. $this->tree_array($arrLP);
  7454. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7455. unset ($this->arrMenu);
  7456. if ($action == 'add') {
  7457. $legend = get_lang('CreateTheLink');
  7458. } elseif ($action == 'move') {
  7459. $legend = get_lang('MoveCurrentLink');
  7460. } else {
  7461. $legend = get_lang('EditCurrentLink');
  7462. }
  7463. $form->addHeader($legend);
  7464. if ($action != 'move') {
  7465. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7466. $defaults['title'] = $item_title;
  7467. }
  7468. $selectParent = $form->addSelect(
  7469. 'parent',
  7470. get_lang('Parent'),
  7471. [],
  7472. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7473. );
  7474. $selectParent->addOption($this->name, 0);
  7475. $arrHide = array(
  7476. $id
  7477. );
  7478. $parent_item_id = isset($_SESSION['parent_item_id']) ? $_SESSION['parent_item_id'] : 0;
  7479. for ($i = 0; $i < count($arrLP); $i++) {
  7480. if ($action != 'add') {
  7481. if (
  7482. ($arrLP[$i]['item_type'] == 'dir') &&
  7483. !in_array($arrLP[$i]['id'], $arrHide) &&
  7484. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7485. ) {
  7486. $selectParent->addOption(
  7487. $arrLP[$i]['title'],
  7488. $arrLP[$i]['id'],
  7489. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7490. );
  7491. if ($parent == $arrLP[$i]['id']) {
  7492. $selectParent->setSelected($arrLP[$i]['id']);
  7493. }
  7494. } else {
  7495. $arrHide[] = $arrLP[$i]['id'];
  7496. }
  7497. } else {
  7498. if ($arrLP[$i]['item_type'] == 'dir') {
  7499. $selectParent->addOption(
  7500. $arrLP[$i]['title'],
  7501. $arrLP[$i]['id'],
  7502. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7503. );
  7504. if ($parent_item_id == $arrLP[$i]['id']) {
  7505. $selectParent->setSelected($arrLP[$i]['id']);
  7506. }
  7507. }
  7508. }
  7509. }
  7510. if (is_array($arrLP)) {
  7511. reset($arrLP);
  7512. }
  7513. $selectPrevious = $form->addSelect(
  7514. 'previous',
  7515. get_lang('Position'),
  7516. [],
  7517. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7518. );
  7519. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7520. for ($i = 0; $i < count($arrLP); $i++) {
  7521. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7522. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7523. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7524. $selectPrevious->setSelected($arrLP[$i]['id']);
  7525. } elseif ($action == 'add') {
  7526. $selectPrevious->setSelected($arrLP[$i]['id']);
  7527. }
  7528. }
  7529. }
  7530. if ($action != 'move') {
  7531. $urlAttributes = ['class' => 'learnpath_item_form'];
  7532. if (is_numeric($extra_info)) {
  7533. $urlAttributes['disabled'] = 'disabled';
  7534. }
  7535. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7536. $defaults['url'] = $item_url;
  7537. $id_prerequisite = 0;
  7538. if (is_array($arrLP)) {
  7539. foreach ($arrLP as $key => $value) {
  7540. if ($value['id'] == $id) {
  7541. $id_prerequisite = $value['prerequisite'];
  7542. break;
  7543. }
  7544. }
  7545. }
  7546. $arrHide = array();
  7547. for ($i = 0; $i < count($arrLP); $i++) {
  7548. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7549. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7550. $s_selected_position = $arrLP[$i]['id'];
  7551. elseif ($action == 'add') $s_selected_position = 0;
  7552. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7553. }
  7554. }
  7555. }
  7556. if ($action == 'add') {
  7557. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7558. } else {
  7559. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7560. }
  7561. if ($action == 'move') {
  7562. $form->addHidden('title', $item_title);
  7563. $form->addHidden('description', $item_description);
  7564. }
  7565. if (is_numeric($extra_info)) {
  7566. $form->addHidden('path', $extra_info);
  7567. } elseif (is_array($extra_info)) {
  7568. $form->addHidden('path', $extra_info['path']);
  7569. }
  7570. $form->addHidden('type', TOOL_LINK);
  7571. $form->addHidden('post_time', time());
  7572. $form->setDefaults($defaults);
  7573. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7574. }
  7575. /**
  7576. * Return HTML form to add/edit a student publication (work)
  7577. * @param string Action (add/edit)
  7578. * @param integer Item ID if already exists
  7579. * @param mixed Extra info (work ID if integer)
  7580. * @return string HTML form
  7581. */
  7582. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7583. {
  7584. $course_id = api_get_course_int_id();
  7585. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7586. $tbl_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  7587. if ($id != 0 && is_array($extra_info)) {
  7588. $item_title = stripslashes($extra_info['title']);
  7589. $item_description = stripslashes($extra_info['description']);
  7590. } elseif (is_numeric($extra_info)) {
  7591. $extra_info = intval($extra_info);
  7592. $sql = "SELECT title, description
  7593. FROM $tbl_publication
  7594. WHERE c_id = $course_id AND id = " . $extra_info;
  7595. $result = Database::query($sql);
  7596. $row = Database::fetch_array($result);
  7597. $item_title = $row['title'];
  7598. } else {
  7599. $item_title = get_lang('Student_publication');
  7600. }
  7601. if ($id != 0 && is_array($extra_info)) {
  7602. $parent = $extra_info['parent_item_id'];
  7603. } else {
  7604. $parent = 0;
  7605. }
  7606. $sql = "SELECT * FROM $tbl_lp_item
  7607. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7608. $result = Database::query($sql);
  7609. $arrLP = array();
  7610. while ($row = Database::fetch_array($result)) {
  7611. $arrLP[] = array(
  7612. 'id' => $row['id'],
  7613. 'item_type' => $row['item_type'],
  7614. 'title' => $row['title'],
  7615. 'path' => $row['path'],
  7616. 'description' => $row['description'],
  7617. 'parent_item_id' => $row['parent_item_id'],
  7618. 'previous_item_id' => $row['previous_item_id'],
  7619. 'next_item_id' => $row['next_item_id'],
  7620. 'display_order' => $row['display_order'],
  7621. 'max_score' => $row['max_score'],
  7622. 'min_score' => $row['min_score'],
  7623. 'mastery_score' => $row['mastery_score'],
  7624. 'prerequisite' => $row['prerequisite']
  7625. );
  7626. }
  7627. $this->tree_array($arrLP);
  7628. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7629. unset($this->arrMenu);
  7630. $form = new FormValidator('frm_student_publication', 'post', '#');
  7631. if ($action == 'add') {
  7632. $form->addHeader(get_lang('Student_publication'));
  7633. } elseif ($action == 'move') {
  7634. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7635. } else {
  7636. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7637. }
  7638. if ($action != 'move') {
  7639. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7640. }
  7641. $parentSelect = $form->addSelect(
  7642. 'parent',
  7643. get_lang('Parent'),
  7644. ['0' => $this->name],
  7645. [
  7646. 'onchange' => 'javascript: load_cbo(this.value);',
  7647. 'class' => 'learnpath_item_form',
  7648. 'id' => 'idParent'
  7649. ]
  7650. );
  7651. $arrHide = array($id);
  7652. for ($i = 0; $i < count($arrLP); $i++) {
  7653. if ($action != 'add') {
  7654. if (
  7655. ($arrLP[$i]['item_type'] == 'dir') &&
  7656. !in_array($arrLP[$i]['id'], $arrHide) &&
  7657. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7658. ) {
  7659. $parentSelect->addOption(
  7660. $arrLP[$i]['title'],
  7661. $arrLP[$i]['id'],
  7662. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7663. );
  7664. if ($parent == $arrLP[$i]['id']) {
  7665. $parentSelect->setSelected($arrLP[$i]['id']);
  7666. }
  7667. } else {
  7668. $arrHide[] = $arrLP[$i]['id'];
  7669. }
  7670. } else {
  7671. if ($arrLP[$i]['item_type'] == 'dir') {
  7672. $parentSelect->addOption(
  7673. $arrLP[$i]['title'],
  7674. $arrLP[$i]['id'],
  7675. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7676. );
  7677. if ($parent == $arrLP[$i]['id']) {
  7678. $parentSelect->setSelected($arrLP[$i]['id']);
  7679. }
  7680. }
  7681. }
  7682. }
  7683. if (is_array($arrLP)) {
  7684. reset($arrLP);
  7685. }
  7686. $previousSelect = $form->addSelect(
  7687. 'previous',
  7688. get_lang('Position'),
  7689. ['0' => get_lang('FirstPosition')],
  7690. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7691. );
  7692. for ($i = 0; $i < count($arrLP); $i++) {
  7693. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7694. $previousSelect->addOption(
  7695. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7696. $arrLP[$i]['id']
  7697. );
  7698. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7699. $previousSelect->setSelected($arrLP[$i]['id']);
  7700. } elseif ($action == 'add') {
  7701. $previousSelect->setSelected($arrLP[$i]['id']);
  7702. }
  7703. }
  7704. }
  7705. if ($action != 'move') {
  7706. $id_prerequisite = 0;
  7707. if (is_array($arrLP)) {
  7708. foreach ($arrLP as $key => $value) {
  7709. if ($value['id'] == $id) {
  7710. $id_prerequisite = $value['prerequisite'];
  7711. break;
  7712. }
  7713. }
  7714. }
  7715. $arrHide = array ();
  7716. for ($i = 0; $i < count($arrLP); $i++) {
  7717. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dir') {
  7718. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7719. $s_selected_position = $arrLP[$i]['id'];
  7720. elseif ($action == 'add') $s_selected_position = 0;
  7721. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7722. }
  7723. }
  7724. }
  7725. if ($action == 'add') {
  7726. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7727. } else {
  7728. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7729. }
  7730. if ($action == 'move') {
  7731. $form->addHidden('title', $item_title);
  7732. $form->addHidden('description', $item_description);
  7733. }
  7734. if (is_numeric($extra_info)) {
  7735. $form->addHidden('path', $extra_info);
  7736. } elseif (is_array($extra_info)) {
  7737. $form->addHidden('path', $extra_info['path']);
  7738. }
  7739. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7740. $form->addHidden('post_time', time());
  7741. $form->setDefaults(['title' => $item_title]);
  7742. $return = '<div class="sectioncomment">';
  7743. $return .= $form->returnForm();
  7744. $return .= '</div>';
  7745. return $return;
  7746. }
  7747. /**
  7748. * Displays the menu for manipulating a step
  7749. *
  7750. * @param $item_id
  7751. * @param string $item_type
  7752. * @return string
  7753. */
  7754. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7755. {
  7756. $_course = api_get_course_info();
  7757. $course_id = api_get_course_int_id();
  7758. $course_code = api_get_course_id();
  7759. $return = '<div class="actions">';
  7760. switch ($item_type) {
  7761. case 'dir':
  7762. // Commented the message cause should not show it.
  7763. //$lang = get_lang('TitleManipulateChapter');
  7764. break;
  7765. case TOOL_LP_FINAL_ITEM:
  7766. case TOOL_DOCUMENT:
  7767. // Commented the message cause should not show it.
  7768. //$lang = get_lang('TitleManipulateDocument');
  7769. break;
  7770. case TOOL_LINK:
  7771. case 'link' :
  7772. // Commented the message cause should not show it.
  7773. //$lang = get_lang('TitleManipulateLink');
  7774. break;
  7775. case TOOL_QUIZ:
  7776. // Commented the message cause should not show it.
  7777. //$lang = get_lang('TitleManipulateQuiz');
  7778. break;
  7779. case TOOL_STUDENTPUBLICATION:
  7780. // Commented the message cause should not show it.
  7781. //$lang = get_lang('TitleManipulateStudentPublication');
  7782. break;
  7783. }
  7784. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7785. $item_id = intval($item_id);
  7786. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7787. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7788. $result = Database::query($sql);
  7789. $row = Database::fetch_assoc($result);
  7790. $audio_player = null;
  7791. // We display an audio player if needed.
  7792. if (!empty($row['audio'])) {
  7793. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7794. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7795. </div>';
  7796. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7797. $audio_player .= '<script>
  7798. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7799. s1.addParam("allowscriptaccess","always");
  7800. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7801. s1.write("container");
  7802. </script>';
  7803. }
  7804. $url = api_get_self().'?cidReq='.api_get_cidreq().'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7805. $return .= Display::url(
  7806. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7807. $url.'&action=edit_item&path_item=' . $row['path']
  7808. );
  7809. $return .= Display::url(
  7810. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7811. $url.'&action=move_item'
  7812. );
  7813. // Commented for now as prerequisites cannot be added to chapters.
  7814. if ($item_type != 'dir') {
  7815. $return .= Display::url(
  7816. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7817. $url.'&action=edit_item_prereq'
  7818. );
  7819. }
  7820. $return .= Display::url(
  7821. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7822. $url.'&action=delete_item'
  7823. );
  7824. if ($item_type == TOOL_HOTPOTATOES) {
  7825. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7826. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7827. }
  7828. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7829. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7830. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7831. }
  7832. $return .= '</div>';
  7833. if (!empty($audio_player)) {
  7834. $return .= '<br />'.$audio_player;
  7835. }
  7836. return $return;
  7837. }
  7838. /**
  7839. * Creates the javascript needed for filling up the checkboxes without page reload
  7840. * @return string
  7841. */
  7842. public function get_js_dropdown_array()
  7843. {
  7844. $course_id = api_get_course_int_id();
  7845. $return = 'var child_name = new Array();' . "\n";
  7846. $return .= 'var child_value = new Array();' . "\n\n";
  7847. $return .= 'child_name[0] = new Array();' . "\n";
  7848. $return .= 'child_value[0] = new Array();' . "\n\n";
  7849. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7850. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7851. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7852. ORDER BY display_order ASC";
  7853. $res_zero = Database::query($sql);
  7854. $i = 0;
  7855. while ($row_zero = Database::fetch_array($res_zero)) {
  7856. if ($row_zero['item_type'] !== TOOL_LP_FINAL_ITEM) {
  7857. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7858. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7859. }
  7860. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7861. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7862. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7863. }
  7864. }
  7865. $return .= "\n";
  7866. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7867. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7868. $res = Database::query($sql);
  7869. while ($row = Database::fetch_array($res)) {
  7870. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7871. WHERE
  7872. c_id = ".$course_id." AND
  7873. parent_item_id = " . $row['id'] . "
  7874. ORDER BY display_order ASC";
  7875. $res_parent = Database::query($sql_parent);
  7876. $i = 0;
  7877. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7878. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7879. while ($row_parent = Database::fetch_array($res_parent)) {
  7880. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7881. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7882. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7883. }
  7884. $return .= "\n";
  7885. }
  7886. return $return;
  7887. }
  7888. /**
  7889. * Display the form to allow moving an item
  7890. * @param integer Item ID
  7891. * @return string HTML form
  7892. */
  7893. public function display_move_item($item_id)
  7894. {
  7895. $course_id = api_get_course_int_id();
  7896. $return = '';
  7897. if (is_numeric($item_id)) {
  7898. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7899. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7900. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7901. $res = Database::query($sql);
  7902. $row = Database::fetch_array($res);
  7903. switch ($row['item_type']) {
  7904. case 'dir':
  7905. case 'asset':
  7906. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7907. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7908. break;
  7909. case TOOL_DOCUMENT:
  7910. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7911. $return .= $this->display_document_form('move', $item_id, $row);
  7912. break;
  7913. case TOOL_LINK:
  7914. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7915. $return .= $this->display_link_form('move', $item_id, $row);
  7916. break;
  7917. case TOOL_HOTPOTATOES:
  7918. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7919. $return .= $this->display_link_form('move', $item_id, $row);
  7920. break;
  7921. case TOOL_QUIZ:
  7922. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7923. $return .= $this->display_quiz_form('move', $item_id, $row);
  7924. break;
  7925. case TOOL_STUDENTPUBLICATION:
  7926. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7927. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7928. break;
  7929. case TOOL_FORUM:
  7930. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7931. $return .= $this->display_forum_form('move', $item_id, $row);
  7932. break;
  7933. case TOOL_THREAD:
  7934. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7935. $return .= $this->display_forum_form('move', $item_id, $row);
  7936. break;
  7937. }
  7938. }
  7939. return $return;
  7940. }
  7941. /**
  7942. * Displays a basic form on the overview page for changing the item title and the item description.
  7943. * @param string $item_type
  7944. * @param string $title
  7945. * @param array $data
  7946. * @return string
  7947. */
  7948. public function display_item_small_form($item_type, $title = '', $data = array())
  7949. {
  7950. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7951. $form = new FormValidator('small_form', 'post', $url);
  7952. $form->addElement('header', $title);
  7953. $form->addElement('text', 'title', get_lang('Title'));
  7954. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7955. $form->addElement('hidden', 'id', $data['id']);
  7956. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7957. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7958. $form->setDefaults(array('title' => $data['title']));
  7959. return $form->toHtml();
  7960. }
  7961. /**
  7962. * Return HTML form to allow prerequisites selection
  7963. * @todo use FormValidator
  7964. * @param integer Item ID
  7965. * @return string HTML form
  7966. */
  7967. public function display_item_prerequisites_form($item_id)
  7968. {
  7969. $course_id = api_get_course_int_id();
  7970. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  7971. $item_id = intval($item_id);
  7972. /* Current prerequisite */
  7973. $sql = "SELECT * FROM $tbl_lp_item
  7974. WHERE c_id = $course_id AND id = " . $item_id;
  7975. $result = Database::query($sql);
  7976. $row = Database::fetch_array($result);
  7977. $prerequisiteId = $row['prerequisite'];
  7978. $return = '<legend>';
  7979. $return .= get_lang('AddEditPrerequisites');
  7980. $return .= '</legend>';
  7981. $return .= '<form method="POST">';
  7982. $return .= '<table class="data_table">';
  7983. $return .= '<tr>';
  7984. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7985. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7986. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7987. $return .= '</tr>';
  7988. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7989. $return .= '<tr >';
  7990. $return .= '<td colspan="3" class="radio">';
  7991. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7992. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7993. $return .= '</tr>';
  7994. $sql = "SELECT * FROM $tbl_lp_item
  7995. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7996. $result = Database::query($sql);
  7997. $arrLP = array();
  7998. $selectedMinScore = array();
  7999. $selectedMaxScore = array();
  8000. while ($row = Database::fetch_array($result)) {
  8001. if ($row['id'] == $item_id) {
  8002. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  8003. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  8004. }
  8005. $arrLP[] = array(
  8006. 'id' => $row['id'],
  8007. 'item_type' => $row['item_type'],
  8008. 'title' => $row['title'],
  8009. 'ref' => $row['ref'],
  8010. 'description' => $row['description'],
  8011. 'parent_item_id' => $row['parent_item_id'],
  8012. 'previous_item_id' => $row['previous_item_id'],
  8013. 'next_item_id' => $row['next_item_id'],
  8014. 'max_score' => $row['max_score'],
  8015. 'min_score' => $row['min_score'],
  8016. 'mastery_score' => $row['mastery_score'],
  8017. 'prerequisite' => $row['prerequisite'],
  8018. 'next_item_id' => $row['next_item_id'],
  8019. 'display_order' => $row['display_order'],
  8020. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  8021. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  8022. );
  8023. }
  8024. $this->tree_array($arrLP);
  8025. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  8026. unset($this->arrMenu);
  8027. for ($i = 0; $i < count($arrLP); $i++) {
  8028. $item = $arrLP[$i];
  8029. if ($item['id'] == $item_id) {
  8030. break;
  8031. }
  8032. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  8033. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  8034. $return .= '<tr>';
  8035. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  8036. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . ($item['item_type'] == 'dir' ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  8037. $return .= '<label for="id' . $item['id'] . '">';
  8038. $icon_name = str_replace(' ', '', $item['item_type']);
  8039. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  8040. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  8041. } else {
  8042. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  8043. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  8044. } else {
  8045. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  8046. }
  8047. }
  8048. $return .= $item['title'] . '</label>';
  8049. $return .= '</td>';
  8050. if ($item['item_type'] == TOOL_QUIZ) {
  8051. // lets update max_score Quiz information depending of the Quiz Advanced properties
  8052. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  8053. $tmp_obj_exercice = new Exercise();
  8054. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  8055. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  8056. $tmp_obj_lp_item->update_in_bdd();
  8057. $item['max_score'] = $tmp_obj_lp_item->max_score;
  8058. $return .= '<td class="exercise">';
  8059. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  8060. $return .= '</td>';
  8061. $return .= '<td class="exercise">';
  8062. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  8063. $return .= '</td>';
  8064. }
  8065. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  8066. $return .= '<td class="exercise">';
  8067. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  8068. $return .= '</td>';
  8069. $return .= '<td class="exercise"">';
  8070. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  8071. $return .= '</td>';
  8072. }
  8073. $return .= '</tr>';
  8074. }
  8075. $return .= '<tr>';
  8076. $return .= '</tr>';
  8077. $return .= '</table>';
  8078. $return .= '<div style="padding-top:3px;">';
  8079. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  8080. $return .= '</form>';
  8081. return $return;
  8082. }
  8083. /**
  8084. * Return HTML list to allow prerequisites selection for lp
  8085. * @param integer Item ID
  8086. * @return string HTML form
  8087. */
  8088. public function display_lp_prerequisites_list()
  8089. {
  8090. $course_id = api_get_course_int_id();
  8091. $lp_id = $this->lp_id;
  8092. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  8093. // get current prerequisite
  8094. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8095. $result = Database::query($sql);
  8096. $row = Database::fetch_array($result);
  8097. $prerequisiteId = $row['prerequisite'];
  8098. $session_id = api_get_session_id();
  8099. $session_condition = api_get_session_condition($session_id, true, true);
  8100. $sql = "SELECT * FROM $tbl_lp
  8101. WHERE c_id = $course_id $session_condition
  8102. ORDER BY display_order ";
  8103. $rs = Database::query($sql);
  8104. $return = '';
  8105. $return .= '<select name="prerequisites" class="form-control">';
  8106. $return .= '<option value="0">'.get_lang('None').'</option>';
  8107. if (Database::num_rows($rs) > 0) {
  8108. while ($row = Database::fetch_array($rs)) {
  8109. if ($row['id'] == $lp_id) {
  8110. continue;
  8111. }
  8112. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  8113. }
  8114. }
  8115. $return .= '</select>';
  8116. return $return;
  8117. }
  8118. /**
  8119. * Creates a list with all the documents in it
  8120. * @param bool $showInvisibleFiles
  8121. * @return string
  8122. */
  8123. public function get_documents($showInvisibleFiles = false)
  8124. {
  8125. $course_info = api_get_course_info();
  8126. $sessionId = api_get_session_id();
  8127. $documentTree = DocumentManager::get_document_preview(
  8128. $course_info,
  8129. $this->lp_id,
  8130. null,
  8131. $sessionId,
  8132. true,
  8133. null,
  8134. null,
  8135. $showInvisibleFiles,
  8136. true
  8137. );
  8138. $headers = array(
  8139. get_lang('Files'),
  8140. get_lang('CreateTheDocument'),
  8141. get_lang('Upload')
  8142. );
  8143. $form = new FormValidator(
  8144. 'form_upload',
  8145. 'POST',
  8146. $this->getCurrentBuildingModeURL(),
  8147. '',
  8148. array('enctype' => 'multipart/form-data')
  8149. );
  8150. $folders = DocumentManager::get_all_document_folders(
  8151. api_get_course_info(),
  8152. 0,
  8153. true
  8154. );
  8155. DocumentManager::build_directory_selector(
  8156. $folders,
  8157. '',
  8158. array(),
  8159. true,
  8160. $form,
  8161. 'directory_parent_id'
  8162. );
  8163. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8164. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8165. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  8166. $form->setDefaults(['if_exists' => 'rename']);
  8167. // Check box options
  8168. $form->addElement(
  8169. 'checkbox',
  8170. 'unzip',
  8171. get_lang('Options'),
  8172. get_lang('Uncompress')
  8173. );
  8174. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8175. $form->addMultipleUpload($url);
  8176. $new = $this->display_document_form('add', 0);
  8177. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8178. return $tabs;
  8179. }
  8180. /**
  8181. * Creates a list with all the exercises (quiz) in it
  8182. * @return string
  8183. */
  8184. public function get_exercises()
  8185. {
  8186. $course_id = api_get_course_int_id();
  8187. $session_id = api_get_session_id();
  8188. $userInfo = api_get_user_info();
  8189. // New for hotpotatoes.
  8190. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8191. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  8192. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  8193. $condition_session = api_get_session_condition($session_id, true, true);
  8194. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8195. $activeCondition = ' active <> -1 ';
  8196. if ($setting) {
  8197. $activeCondition = ' active = 1 ';
  8198. }
  8199. $sql_quiz = "SELECT * FROM $tbl_quiz
  8200. WHERE c_id = $course_id AND $activeCondition $condition_session
  8201. ORDER BY title ASC";
  8202. $sql_hot = "SELECT * FROM $tbl_doc
  8203. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8204. ORDER BY id ASC";
  8205. $res_quiz = Database::query($sql_quiz);
  8206. $res_hot = Database::query($sql_hot);
  8207. $return = '<ul class="lp_resource">';
  8208. $return .= '<li class="lp_resource_element">';
  8209. $return .= Display::return_icon('new_exercice.png');
  8210. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercise/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8211. get_lang('NewExercise') . '</a>';
  8212. $return .= '</li>';
  8213. $previewIcon = Display::return_icon('preview_view.png', get_lang('Preview'));
  8214. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/showinframes.php?'.api_get_cidreq();
  8215. // Display hotpotatoes
  8216. while ($row_hot = Database::fetch_array($res_hot)) {
  8217. $link = Display::url(
  8218. $previewIcon,
  8219. $exerciseUrl.'&file='.$row_hot['path'],
  8220. ['target' => '_blank']
  8221. );
  8222. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8223. $return .= '<a class="moved" href="#">';
  8224. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8225. $return .= '</a> ';
  8226. $return .= Display::return_icon('hotpotatoes_s.png');
  8227. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8228. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security::remove_XSS($row_hot['title'])) .$link. '</a>';
  8229. $return .= '</li>';
  8230. }
  8231. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq();
  8232. while ($row_quiz = Database::fetch_array($res_quiz)) {
  8233. $link = Display::url(
  8234. $previewIcon,
  8235. $exerciseUrl.'&exerciseId='.$row_quiz['id'],
  8236. ['target' => '_blank']
  8237. );
  8238. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8239. $return .= '<a class="moved" href="#">';
  8240. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8241. $return .= '</a> ';
  8242. $return .= Display::return_icon('quizz_small.gif', '', array(), ICON_SIZE_TINY);
  8243. $sessionStar = api_get_session_image($row_quiz['session_id'], $userInfo['status']);
  8244. $return .= '<a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8245. Security::remove_XSS(cut($row_quiz['title'], 80)).$link.$sessionStar.
  8246. '</a>';
  8247. $return .= '</li>';
  8248. }
  8249. $return .= '</ul>';
  8250. return $return;
  8251. }
  8252. /**
  8253. * Creates a list with all the links in it
  8254. * @return string
  8255. */
  8256. public function get_links()
  8257. {
  8258. $selfUrl = api_get_self();
  8259. $courseIdReq = api_get_cidreq();
  8260. $course = api_get_course_info();
  8261. $userInfo = api_get_user_info();
  8262. $course_id = $course['real_id'];
  8263. $tbl_link = Database::get_course_table(TABLE_LINK);
  8264. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8265. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8266. $session_id = api_get_session_id();
  8267. $condition_session = api_get_session_condition(
  8268. $session_id,
  8269. true,
  8270. true,
  8271. "link.session_id"
  8272. );
  8273. $sql = "SELECT
  8274. link.id as link_id,
  8275. link.title as link_title,
  8276. link.session_id as link_session_id,
  8277. link.category_id as category_id,
  8278. link_category.category_title as category_title
  8279. FROM $tbl_link as link
  8280. LEFT JOIN $linkCategoryTable as link_category
  8281. ON (link.category_id = link_category.id AND link.c_id = link_category.c_id)
  8282. WHERE link.c_id = ".$course_id." $condition_session
  8283. ORDER BY link_category.category_title ASC, link.title ASC";
  8284. $result = Database::query($sql);
  8285. $categorizedLinks = array();
  8286. $categories = array();
  8287. while ($link = Database::fetch_array($result)) {
  8288. if (!$link['category_id']) {
  8289. $link['category_title'] = get_lang('Uncategorized');
  8290. }
  8291. $categories[$link['category_id']] = $link['category_title'];
  8292. $categorizedLinks[$link['category_id']][$link['link_id']] = $link;
  8293. }
  8294. $linksHtmlCode =
  8295. '<script>
  8296. function toggle_tool(tool, id){
  8297. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8298. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8299. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8300. } else {
  8301. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8302. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8303. }
  8304. }
  8305. </script>
  8306. <ul class="lp_resource">
  8307. <li class="lp_resource_element">
  8308. '.Display::return_icon('linksnew.gif').'
  8309. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.'&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.
  8310. get_lang('LinkAdd').'
  8311. </a>
  8312. </li>';
  8313. foreach ($categorizedLinks as $categoryId => $links) {
  8314. $linkNodes = null;
  8315. foreach ($links as $key => $linkInfo) {
  8316. $title = $linkInfo['link_title'];
  8317. $linkSessionId = $linkInfo['link_session_id'];
  8318. $link = Display::url(
  8319. Display::return_icon('preview_view.png', get_lang('Preview')),
  8320. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8321. ['target' => '_blank']
  8322. );
  8323. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8324. $sessionStar = api_get_session_image($linkSessionId, $userInfo['status']);
  8325. $linkNodes .=
  8326. '<li class="lp_resource_element" data_id="'.$key.'" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8327. <a class="moved" href="#">'.
  8328. $moveEverywhereIcon.
  8329. '</a>
  8330. '.Display::return_icon('lp_link.png').'
  8331. <a class="moved" href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8332. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8333. Security::remove_XSS($title).$sessionStar.$link.
  8334. '</a>
  8335. </li>';
  8336. }
  8337. }
  8338. $linksHtmlCode .=
  8339. '<li>
  8340. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8341. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8342. align="absbottom" />
  8343. </a>
  8344. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8345. </li>
  8346. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8347. }
  8348. $linksHtmlCode .= '</ul>';
  8349. return $linksHtmlCode;
  8350. }
  8351. /**
  8352. * Creates a list with all the student publications in it
  8353. * @return string
  8354. */
  8355. public function get_student_publications()
  8356. {
  8357. $return = '<ul class="lp_resource">';
  8358. $return .= '<li class="lp_resource_element">';
  8359. $return .= Display::return_icon('works_new.gif');
  8360. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8361. get_lang('AddAssignmentPage') . '</a>';
  8362. $return .= '</li>';
  8363. $sessionId = api_get_session_id();
  8364. if (empty($sessionId)) {
  8365. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8366. $works = getWorkListTeacher(0, 100, null, null, null);
  8367. if (!empty($works)) {
  8368. foreach ($works as $work) {
  8369. $link = Display::url(
  8370. Display::return_icon('preview_view.png', get_lang('Preview')),
  8371. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8372. ['target' => '_blank']
  8373. );
  8374. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security::remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8375. $return .= '<a class="moved" href="#">';
  8376. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8377. $return .= '</a> ';
  8378. $return .= Display::return_icon('works.gif');
  8379. $return .= ' <a class="moved" href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&file=' . $work['iid'] . '&lp_id=' . $this->lp_id . '">' .
  8380. Security::remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8381. </a>';
  8382. $return .= '</li>';
  8383. }
  8384. }
  8385. }
  8386. $return .= '</ul>';
  8387. return $return;
  8388. }
  8389. /**
  8390. * Creates a list with all the forums in it
  8391. * @return string
  8392. */
  8393. public function get_forums()
  8394. {
  8395. require_once '../forum/forumfunction.inc.php';
  8396. require_once '../forum/forumconfig.inc.php';
  8397. $forumCategories = get_forum_categories();
  8398. $forumsInNoCategory = get_forums_in_category(0);
  8399. if (!empty($forumsInNoCategory)) {
  8400. $forumCategories = array_merge(
  8401. $forumCategories,
  8402. array(
  8403. array(
  8404. 'cat_id' => 0,
  8405. 'session_id' => 0,
  8406. 'visibility' => 1,
  8407. 'cat_comment' => null,
  8408. ),
  8409. )
  8410. );
  8411. }
  8412. $forumList = get_forums();
  8413. $a_forums = [];
  8414. foreach ($forumCategories as $forumCategory) {
  8415. // The forums in this category.
  8416. $forumsInCategory = get_forums_in_category($forumCategory['cat_id']);
  8417. if (!empty($forumsInCategory)) {
  8418. foreach ($forumList as $forum) {
  8419. if (isset($forum['forum_category']) && $forum['forum_category'] == $forumCategory['cat_id']) {
  8420. $a_forums[] = $forum;
  8421. }
  8422. }
  8423. }
  8424. }
  8425. $return = '<ul class="lp_resource">';
  8426. // First add link
  8427. $return .= '<li class="lp_resource_element">';
  8428. $return .= Display::return_icon('new_forum.png');
  8429. $return .= Display::url(
  8430. get_lang('CreateANewForum'),
  8431. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8432. 'action' => 'add',
  8433. 'content' => 'forum',
  8434. 'lp_id' => $this->lp_id
  8435. ]),
  8436. ['title' => get_lang('CreateANewForum')]
  8437. );
  8438. $return .= '</li>';
  8439. $return .= '<script>
  8440. function toggle_forum(forum_id) {
  8441. if (document.getElementById("forum_"+forum_id+"_content").style.display == "none") {
  8442. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8443. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8444. } else {
  8445. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8446. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8447. }
  8448. }
  8449. </script>';
  8450. foreach ($a_forums as $forum) {
  8451. if (!empty($forum['forum_id'])) {
  8452. $link = Display::url(
  8453. Display::return_icon('preview_view.png', get_lang('Preview')),
  8454. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8455. ['target' => '_blank']
  8456. );
  8457. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8458. $return .= '<a class="moved" href="#">';
  8459. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8460. $return .= ' </a>';
  8461. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8462. $return .= '<a onclick="javascript:toggle_forum(' . $forum['forum_id'] . ');" style="cursor:hand; vertical-align:middle">
  8463. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8464. </a>
  8465. <a class="moved" 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">' .
  8466. Security::remove_XSS($forum['forum_title']) . ' '.$link.'</a>';
  8467. $return .= '</li>';
  8468. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8469. $a_threads = get_threads($forum['forum_id']);
  8470. if (is_array($a_threads)) {
  8471. foreach ($a_threads as $thread) {
  8472. $link = Display::url(
  8473. Display::return_icon('preview_view.png', get_lang('Preview')),
  8474. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8475. ['target' => '_blank']
  8476. );
  8477. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8478. $return .= '&nbsp;<a class="moved" href="#">';
  8479. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8480. $return .= ' </a>';
  8481. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8482. $return .= '<a class="moved" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type=' . TOOL_THREAD . '&thread_id=' . $thread['thread_id'] . '&lp_id=' . $this->lp_id . '">' .
  8483. Security::remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8484. $return .= '</li>';
  8485. }
  8486. }
  8487. $return .= '</div>';
  8488. }
  8489. }
  8490. $return .= '</ul>';
  8491. return $return;
  8492. }
  8493. /**
  8494. * // TODO: The output encoding should be equal to the system encoding.
  8495. *
  8496. * Exports the learning path as a SCORM package. This is the main function that
  8497. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8498. * whole thing and returns the zip.
  8499. *
  8500. * This method needs to be called in PHP5, as it will fail with non-adequate
  8501. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8502. * you need to call it on a learnpath object.
  8503. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8504. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8505. * path has been modified, it should use the generic method here below.
  8506. * @TODO link this function with the export_lp() function in the same class
  8507. * @param string Optional name of zip file. If none, title of learnpath is
  8508. * domesticated and trailed with ".zip"
  8509. * @return string Returns the zip package string, or null if error
  8510. */
  8511. public function scorm_export()
  8512. {
  8513. $_course = api_get_course_info();
  8514. $course_id = $_course['real_id'];
  8515. // Remove memory and time limits as much as possible as this might be a long process...
  8516. if (function_exists('ini_set')) {
  8517. api_set_memory_limit('256M');
  8518. ini_set('max_execution_time', 600);
  8519. }
  8520. // Create the zip handler (this will remain available throughout the method).
  8521. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8522. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8523. $temp_dir_short = uniqid();
  8524. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8525. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8526. $zip_folder = new PclZip($temp_zip_file);
  8527. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8528. $root_path = $main_path = api_get_path(SYS_PATH);
  8529. $files_cleanup = array();
  8530. // Place to temporarily stash the zip file.
  8531. // create the temp dir if it doesn't exist
  8532. // or do a cleanup before creating the zip file.
  8533. if (!is_dir($temp_zip_dir)) {
  8534. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8535. } else {
  8536. // Cleanup: Check the temp dir for old files and delete them.
  8537. $handle = opendir($temp_zip_dir);
  8538. while (false !== ($file = readdir($handle))) {
  8539. if ($file != '.' && $file != '..') {
  8540. unlink("$temp_zip_dir/$file");
  8541. }
  8542. }
  8543. closedir($handle);
  8544. }
  8545. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8546. if (is_dir($current_course_path.'/scorm/'.$this->path) &&
  8547. is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')
  8548. ) {
  8549. // Remove the possible . at the end of the path.
  8550. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8551. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8552. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8553. copyr(
  8554. $current_course_path.'/scorm/'.$this->path,
  8555. $dest_path_to_scorm_folder,
  8556. array('imsmanifest'),
  8557. $zip_files
  8558. );
  8559. }
  8560. // Build a dummy imsmanifest structure.
  8561. // Do not add to the zip yet (we still need it).
  8562. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8563. // Aggregation Model official document, section "2.3 Content Packaging".
  8564. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8565. $xmldoc = new DOMDocument('1.0');
  8566. $root = $xmldoc->createElement('manifest');
  8567. $root->setAttribute('identifier', 'SingleCourseManifest');
  8568. $root->setAttribute('version', '1.1');
  8569. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8570. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8571. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8572. $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');
  8573. // Build mandatory sub-root container elements.
  8574. $metadata = $xmldoc->createElement('metadata');
  8575. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8576. $metadata->appendChild($md_schema);
  8577. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8578. $metadata->appendChild($md_schemaversion);
  8579. $root->appendChild($metadata);
  8580. $organizations = $xmldoc->createElement('organizations');
  8581. $resources = $xmldoc->createElement('resources');
  8582. // Build the only organization we will use in building our learnpaths.
  8583. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8584. $organization = $xmldoc->createElement('organization');
  8585. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8586. // To set the title of the SCORM entity (=organization), we take the name given
  8587. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8588. // learning path charset) as it is the encoding that defines how it is stored
  8589. // in the database. Then we convert it to HTML entities again as the "&" character
  8590. // alone is not authorized in XML (must be &amp;).
  8591. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8592. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8593. $organization->appendChild($org_title);
  8594. $folder_name = 'document';
  8595. // Removes the learning_path/scorm_folder path when exporting see #4841
  8596. $path_to_remove = null;
  8597. $result = $this->generate_lp_folder($_course);
  8598. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8599. $path_to_remove = 'document'.$result['dir'];
  8600. $path_to_replace = $folder_name.'/';
  8601. }
  8602. // Fixes chamilo scorm exports
  8603. if ($this->ref === 'chamilo_scorm_export') {
  8604. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8605. }
  8606. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8607. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8608. $link_updates = array();
  8609. $links_to_create = array();
  8610. //foreach ($this->items as $index => $item) {
  8611. foreach ($this->ordered_items as $index => $itemId) {
  8612. $item = $this->items[$itemId];
  8613. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8614. // Get included documents from this item.
  8615. if ($item->type === 'sco') {
  8616. $inc_docs = $item->get_resources_from_source(
  8617. null,
  8618. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8619. );
  8620. } else {
  8621. $inc_docs = $item->get_resources_from_source();
  8622. }
  8623. // Give a child element <item> to the <organization> element.
  8624. $my_item_id = $item->get_id();
  8625. $my_item = $xmldoc->createElement('item');
  8626. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8627. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8628. $my_item->setAttribute('isvisible', 'true');
  8629. // Give a child element <title> to the <item> element.
  8630. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8631. $my_item->appendChild($my_title);
  8632. // Give a child element <adlcp:prerequisites> to the <item> element.
  8633. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8634. $my_prereqs->setAttribute('type', 'aicc_script');
  8635. $my_item->appendChild($my_prereqs);
  8636. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8637. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8638. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8639. //$xmldoc->createElement('adlcp:timelimitaction','');
  8640. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8641. //$xmldoc->createElement('adlcp:datafromlms','');
  8642. // Give a child element <adlcp:masteryscore> to the <item> element.
  8643. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8644. $my_item->appendChild($my_masteryscore);
  8645. // Attach this item to the organization element or hits parent if there is one.
  8646. if (!empty($item->parent) && $item->parent != 0) {
  8647. $children = $organization->childNodes;
  8648. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8649. if (is_object($possible_parent)) {
  8650. $possible_parent->appendChild($my_item);
  8651. } else {
  8652. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8653. }
  8654. } else {
  8655. if ($this->debug > 0) { error_log('No parent'); }
  8656. $organization->appendChild($my_item);
  8657. }
  8658. // Get the path of the file(s) from the course directory root.
  8659. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8660. if (!empty($path_to_remove)) {
  8661. //From docs
  8662. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8663. //From quiz
  8664. if ($this->ref === 'chamilo_scorm_export') {
  8665. $path_to_remove = 'scorm/'.$this->path.'/';
  8666. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8667. }
  8668. } else {
  8669. $my_xml_file_path = $my_file_path;
  8670. }
  8671. $my_sub_dir = dirname($my_file_path);
  8672. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8673. $my_xml_sub_dir = $my_sub_dir;
  8674. // Give a <resource> child to the <resources> element
  8675. $my_resource = $xmldoc->createElement('resource');
  8676. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8677. $my_resource->setAttribute('type', 'webcontent');
  8678. $my_resource->setAttribute('href', $my_xml_file_path);
  8679. // adlcp:scormtype can be either 'sco' or 'asset'.
  8680. if ($item->type === 'sco') {
  8681. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8682. } else {
  8683. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8684. }
  8685. // xml:base is the base directory to find the files declared in this resource.
  8686. $my_resource->setAttribute('xml:base', '');
  8687. // Give a <file> child to the <resource> element.
  8688. $my_file = $xmldoc->createElement('file');
  8689. $my_file->setAttribute('href', $my_xml_file_path);
  8690. $my_resource->appendChild($my_file);
  8691. // Dependency to other files - not yet supported.
  8692. $i = 1;
  8693. if ($inc_docs)
  8694. foreach ($inc_docs as $doc_info) {
  8695. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8696. continue;
  8697. }
  8698. $my_dep = $xmldoc->createElement('resource');
  8699. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8700. $my_dep->setAttribute('identifier', $res_id);
  8701. $my_dep->setAttribute('type', 'webcontent');
  8702. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8703. $my_dep_file = $xmldoc->createElement('file');
  8704. // Check type of URL.
  8705. if ($doc_info[1] == 'remote') {
  8706. // Remote file. Save url as is.
  8707. $my_dep_file->setAttribute('href', $doc_info[0]);
  8708. $my_dep->setAttribute('xml:base', '');
  8709. } elseif ($doc_info[1] === 'local') {
  8710. switch ($doc_info[2]) {
  8711. case 'url': // Local URL - save path as url for now, don't zip file.
  8712. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8713. $current_dir = dirname($abs_path);
  8714. $current_dir = str_replace('\\', '/', $current_dir);
  8715. $file_path = realpath($abs_path);
  8716. $file_path = str_replace('\\', '/', $file_path);
  8717. $my_dep_file->setAttribute('href', $file_path);
  8718. $my_dep->setAttribute('xml:base', '');
  8719. if (strstr($file_path, $main_path) !== false) {
  8720. // The calculated real path is really inside Chamilo's root path.
  8721. // Reduce file path to what's under the DocumentRoot.
  8722. $file_path = substr($file_path, strlen($root_path) - 1);
  8723. //echo $file_path;echo '<br /><br />';
  8724. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8725. $zip_files_abs[] = $file_path;
  8726. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8727. $my_dep_file->setAttribute('href', $file_path);
  8728. $my_dep->setAttribute('xml:base', '');
  8729. } elseif (empty($file_path)) {
  8730. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8731. if (strpos($document_root, -1) == '/') {
  8732. $document_root = substr(0, -1, $document_root);
  8733. }*/
  8734. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8735. $file_path = str_replace('//', '/', $file_path);
  8736. if (file_exists($file_path)) {
  8737. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8738. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8739. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8740. $my_dep_file->setAttribute('href', $file_path);
  8741. $my_dep->setAttribute('xml:base', '');
  8742. }
  8743. }
  8744. break;
  8745. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8746. $my_dep_file->setAttribute('href', $doc_info[0]);
  8747. $my_dep->setAttribute('xml:base', '');
  8748. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8749. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8750. $abs_img_path_without_subdir = $doc_info[0];
  8751. $relp = api_get_path(REL_PATH); // The url-append config param.
  8752. $pos = strpos($abs_img_path_without_subdir, $relp);
  8753. if ($pos === 0) {
  8754. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8755. }
  8756. //$file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8757. $file_path = realpath(api_get_path(SYS_APP_PATH).$abs_img_path_without_subdir);
  8758. $file_path = str_replace('\\', '/', $file_path);
  8759. $file_path = str_replace('//', '/', $file_path);
  8760. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8761. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8762. // Check if the current document is in that path.
  8763. if (strstr($file_path, $cur_path) !== false) {
  8764. // The document is in that path, now get the relative path
  8765. // to the containing document.
  8766. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8767. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8768. $relative_path = '';
  8769. if (strstr($file_path, $cur_path) !== false) {
  8770. //$relative_path = substr($file_path, strlen($orig_file_path));
  8771. $relative_path = str_replace($cur_path, '', $file_path);
  8772. $file_path = substr($file_path, strlen($cur_path));
  8773. } else {
  8774. // This case is still a problem as it's difficult to calculate a relative path easily
  8775. // might still generate wrong links.
  8776. //$file_path = substr($file_path,strlen($cur_path));
  8777. // Calculate the directory path to the current file (without trailing slash).
  8778. $my_relative_path = dirname($file_path);
  8779. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8780. $my_relative_file = basename($file_path);
  8781. // Calculate the directory path to the containing file (without trailing slash).
  8782. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8783. $dotdot = '';
  8784. $subdir = '';
  8785. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8786. $my_relative_path2 = dirname($my_relative_path);
  8787. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8788. $my_orig_file_path = dirname($my_orig_file_path);
  8789. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8790. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8791. $dotdot += '../';
  8792. $my_relative_path = $my_relative_path2;
  8793. }
  8794. $relative_path = $dotdot.$subdir.$my_relative_file;
  8795. }
  8796. // Put the current document in the zip (this array is the array
  8797. // that will manage documents already in the course folder - relative).
  8798. $zip_files[] = $file_path;
  8799. // Update the links to the current document in the containing document (make them relative).
  8800. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8801. $my_dep_file->setAttribute('href', $file_path);
  8802. $my_dep->setAttribute('xml:base', '');
  8803. } elseif (strstr($file_path, $main_path) !== false) {
  8804. // The calculated real path is really inside Chamilo's root path.
  8805. // Reduce file path to what's under the DocumentRoot.
  8806. $file_path = substr($file_path, strlen($root_path));
  8807. //echo $file_path;echo '<br /><br />';
  8808. //error_log('Reduced path: '.$file_path, 0);
  8809. $zip_files_abs[] = $file_path;
  8810. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8811. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8812. $my_dep->setAttribute('xml:base', '');
  8813. } elseif (empty($file_path)) {
  8814. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8815. if(strpos($document_root,-1) == '/') {
  8816. $document_root = substr(0, -1, $document_root);
  8817. }*/
  8818. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8819. $file_path = str_replace('//', '/', $file_path);
  8820. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8821. $current_dir = dirname($abs_path);
  8822. $current_dir = str_replace('\\', '/', $current_dir);
  8823. if (file_exists($file_path)) {
  8824. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8825. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8826. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8827. $my_dep_file->setAttribute('href','document/'.$file_path);
  8828. $my_dep->setAttribute('xml:base', '');
  8829. }
  8830. }
  8831. break;
  8832. case 'rel':
  8833. // Path relative to the current document.
  8834. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8835. if (substr($doc_info[0], 0, 2) == '..') {
  8836. // Relative path going up.
  8837. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8838. $current_dir = str_replace('\\', '/', $current_dir);
  8839. $file_path = realpath($current_dir.$doc_info[0]);
  8840. $file_path = str_replace('\\', '/', $file_path);
  8841. //error_log($file_path.' <-> '.$main_path,0);
  8842. if (strstr($file_path, $main_path) !== false) {
  8843. // The calculated real path is really inside Chamilo's root path.
  8844. // Reduce file path to what's under the DocumentRoot.
  8845. $file_path = substr($file_path, strlen($root_path));
  8846. //error_log('Reduced path: '.$file_path, 0);
  8847. $zip_files_abs[] = $file_path;
  8848. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8849. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8850. $my_dep->setAttribute('xml:base', '');
  8851. }
  8852. } else {
  8853. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8854. $my_dep_file->setAttribute('href', $doc_info[0]);
  8855. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8856. }
  8857. break;
  8858. default:
  8859. $my_dep_file->setAttribute('href', $doc_info[0]);
  8860. $my_dep->setAttribute('xml:base', '');
  8861. break;
  8862. }
  8863. }
  8864. $my_dep->appendChild($my_dep_file);
  8865. $resources->appendChild($my_dep);
  8866. $dependency = $xmldoc->createElement('dependency');
  8867. $dependency->setAttribute('identifierref', $res_id);
  8868. $my_resource->appendChild($dependency);
  8869. $i++;
  8870. }
  8871. $resources->appendChild($my_resource);
  8872. $zip_files[] = $my_file_path;
  8873. } else {
  8874. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8875. switch ($item->type) {
  8876. case TOOL_LINK:
  8877. $my_item = $xmldoc->createElement('item');
  8878. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8879. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8880. $my_item->setAttribute('isvisible', 'true');
  8881. // Give a child element <title> to the <item> element.
  8882. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8883. $my_item->appendChild($my_title);
  8884. // Give a child element <adlcp:prerequisites> to the <item> element.
  8885. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8886. $my_prereqs->setAttribute('type', 'aicc_script');
  8887. $my_item->appendChild($my_prereqs);
  8888. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8889. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8890. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8891. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8892. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8893. //$xmldoc->createElement('adlcp:datafromlms', '');
  8894. // Give a child element <adlcp:masteryscore> to the <item> element.
  8895. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8896. $my_item->appendChild($my_masteryscore);
  8897. // Attach this item to the organization element or its parent if there is one.
  8898. if (!empty($item->parent) && $item->parent != 0) {
  8899. $children = $organization->childNodes;
  8900. for ($i = 0; $i < $children->length; $i++) {
  8901. $item_temp = $children->item($i);
  8902. if ($item_temp -> nodeName == 'item') {
  8903. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8904. $item_temp -> appendChild($my_item);
  8905. }
  8906. }
  8907. }
  8908. } else {
  8909. $organization->appendChild($my_item);
  8910. }
  8911. $my_file_path = 'link_'.$item->get_id().'.html';
  8912. $sql = 'SELECT url, title FROM '.Database::get_course_table(TABLE_LINK).'
  8913. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8914. $rs = Database::query($sql);
  8915. if ($link = Database::fetch_array($rs)) {
  8916. $url = $link['url'];
  8917. $title = stripslashes($link['title']);
  8918. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8919. $my_xml_file_path = $my_file_path;
  8920. $my_sub_dir = dirname($my_file_path);
  8921. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8922. $my_xml_sub_dir = $my_sub_dir;
  8923. // Give a <resource> child to the <resources> element.
  8924. $my_resource = $xmldoc->createElement('resource');
  8925. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8926. $my_resource->setAttribute('type', 'webcontent');
  8927. $my_resource->setAttribute('href', $my_xml_file_path);
  8928. // adlcp:scormtype can be either 'sco' or 'asset'.
  8929. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8930. // xml:base is the base directory to find the files declared in this resource.
  8931. $my_resource->setAttribute('xml:base', '');
  8932. // give a <file> child to the <resource> element.
  8933. $my_file = $xmldoc->createElement('file');
  8934. $my_file->setAttribute('href', $my_xml_file_path);
  8935. $my_resource->appendChild($my_file);
  8936. $resources->appendChild($my_resource);
  8937. }
  8938. break;
  8939. case TOOL_QUIZ:
  8940. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8941. $exe = new Exercise();
  8942. $exe->read($exe_id);
  8943. $my_item = $xmldoc->createElement('item');
  8944. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8945. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8946. $my_item->setAttribute('isvisible', 'true');
  8947. // Give a child element <title> to the <item> element.
  8948. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8949. $my_item->appendChild($my_title);
  8950. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8951. //$my_item->appendChild($my_max_score);
  8952. // Give a child element <adlcp:prerequisites> to the <item> element.
  8953. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8954. $my_prereqs->setAttribute('type','aicc_script');
  8955. $my_item->appendChild($my_prereqs);
  8956. // Give a child element <adlcp:masteryscore> to the <item> element.
  8957. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8958. $my_item->appendChild($my_masteryscore);
  8959. // Attach this item to the organization element or hits parent if there is one.
  8960. if (!empty($item->parent) && $item->parent != 0) {
  8961. $children = $organization->childNodes;
  8962. /*for ($i = 0; $i < $children->length; $i++) {
  8963. $item_temp = $children->item($i);
  8964. if ($exe_id == 81) {
  8965. error_log($item_temp->nodeName );
  8966. error_log($item_temp->getAttribute('identifier'));
  8967. }
  8968. if ($item_temp->nodeName == 'item') {
  8969. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8970. $item_temp->appendChild($my_item);
  8971. }
  8972. }
  8973. }*/
  8974. $possible_parent = $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8975. if ($possible_parent) {
  8976. if ($possible_parent->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8977. $possible_parent->appendChild($my_item);
  8978. }
  8979. }
  8980. } else {
  8981. $organization->appendChild($my_item);
  8982. }
  8983. // Get the path of the file(s) from the course directory root
  8984. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8985. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8986. // Write the contents of the exported exercise into a (big) html file
  8987. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8988. $contents = ScormSection::export_exercise_to_scorm($exe, true);
  8989. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8990. $res = file_put_contents($tmp_file_path, $contents);
  8991. if ($res === false) {
  8992. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8993. }
  8994. $files_cleanup[] = $tmp_file_path;
  8995. $my_xml_file_path = $my_file_path;
  8996. $my_sub_dir = dirname($my_file_path);
  8997. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8998. $my_xml_sub_dir = $my_sub_dir;
  8999. // Give a <resource> child to the <resources> element.
  9000. $my_resource = $xmldoc->createElement('resource');
  9001. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9002. $my_resource->setAttribute('type', 'webcontent');
  9003. $my_resource->setAttribute('href', $my_xml_file_path);
  9004. // adlcp:scormtype can be either 'sco' or 'asset'.
  9005. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  9006. // xml:base is the base directory to find the files declared in this resource.
  9007. $my_resource->setAttribute('xml:base', '');
  9008. // Give a <file> child to the <resource> element.
  9009. $my_file = $xmldoc->createElement('file');
  9010. $my_file->setAttribute('href', $my_xml_file_path);
  9011. $my_resource->appendChild($my_file);
  9012. // Get included docs.
  9013. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  9014. // Dependency to other files - not yet supported.
  9015. $i = 1;
  9016. foreach ($inc_docs as $doc_info) {
  9017. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  9018. $my_dep = $xmldoc->createElement('resource');
  9019. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  9020. $my_dep->setAttribute('identifier', $res_id);
  9021. $my_dep->setAttribute('type', 'webcontent');
  9022. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  9023. $my_dep_file = $xmldoc->createElement('file');
  9024. // Check type of URL.
  9025. if ($doc_info[1] == 'remote') {
  9026. // Remote file. Save url as is.
  9027. $my_dep_file->setAttribute('href', $doc_info[0]);
  9028. $my_dep->setAttribute('xml:base', '');
  9029. } elseif ($doc_info[1] == 'local') {
  9030. switch ($doc_info[2]) {
  9031. case 'url': // Local URL - save path as url for now, don't zip file.
  9032. // Save file but as local file (retrieve from URL).
  9033. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  9034. $current_dir = dirname($abs_path);
  9035. $current_dir = str_replace('\\', '/', $current_dir);
  9036. $file_path = realpath($abs_path);
  9037. $file_path = str_replace('\\', '/', $file_path);
  9038. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9039. $my_dep->setAttribute('xml:base', '');
  9040. if (strstr($file_path, $main_path) !== false) {
  9041. // The calculated real path is really inside the chamilo root path.
  9042. // Reduce file path to what's under the DocumentRoot.
  9043. $file_path = substr($file_path, strlen($root_path));
  9044. $zip_files_abs[] = $file_path;
  9045. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9046. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9047. $my_dep->setAttribute('xml:base', '');
  9048. } elseif (empty($file_path)) {
  9049. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  9050. if (strpos($document_root,-1) == '/') {
  9051. $document_root = substr(0, -1, $document_root);
  9052. }*/
  9053. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  9054. $file_path = str_replace('//', '/', $file_path);
  9055. if (file_exists($file_path)) {
  9056. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  9057. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9058. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  9059. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9060. $my_dep->setAttribute('xml:base', '');
  9061. }
  9062. }
  9063. break;
  9064. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  9065. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9066. $current_dir = str_replace('\\', '/', $current_dir);
  9067. $file_path = realpath($doc_info[0]);
  9068. $file_path = str_replace('\\', '/', $file_path);
  9069. $my_dep_file->setAttribute('href', $file_path);
  9070. $my_dep->setAttribute('xml:base', '');
  9071. if (strstr($file_path,$main_path) !== false) {
  9072. // The calculated real path is really inside the chamilo root path.
  9073. // Reduce file path to what's under the DocumentRoot.
  9074. $file_path = substr($file_path, strlen($root_path));
  9075. //echo $file_path;echo '<br /><br />';
  9076. //error_log('Reduced path: '.$file_path, 0);
  9077. $zip_files_abs[] = $file_path;
  9078. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9079. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9080. $my_dep->setAttribute('xml:base', '');
  9081. } elseif (empty($file_path)) {
  9082. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  9083. if (strpos($document_root,-1) == '/') {
  9084. $document_root = substr(0, -1, $document_root);
  9085. }*/
  9086. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  9087. $file_path = str_replace('//', '/', $file_path);
  9088. if (file_exists($file_path)) {
  9089. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  9090. $zip_files[] = $my_sub_dir.'/'.$file_path;
  9091. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  9092. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9093. $my_dep->setAttribute('xml:base', '');
  9094. }
  9095. }
  9096. break;
  9097. 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
  9098. if (substr($doc_info[0], 0, 2) == '..') {
  9099. // Relative path going up.
  9100. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  9101. $current_dir = str_replace('\\', '/', $current_dir);
  9102. $file_path = realpath($current_dir.$doc_info[0]);
  9103. $file_path = str_replace('\\', '/', $file_path);
  9104. //error_log($file_path.' <-> '.$main_path, 0);
  9105. if (strstr($file_path, $main_path) !== false) {
  9106. // The calculated real path is really inside Chamilo's root path.
  9107. // Reduce file path to what's under the DocumentRoot.
  9108. $file_path = substr($file_path, strlen($root_path));
  9109. $file_path_dest = $file_path;
  9110. // File path is courses/CHAMILO/document/....
  9111. $info_file_path = explode('/', $file_path);
  9112. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9113. $file_path_dest = 'document/'.$file_path;
  9114. }
  9115. //error_log('Reduced path: '.$file_path, 0);
  9116. $zip_files_abs[] = $file_path;
  9117. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  9118. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9119. $my_dep->setAttribute('xml:base', '');
  9120. }
  9121. } else {
  9122. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9123. $my_dep_file->setAttribute('href', $doc_info[0]);
  9124. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9125. }
  9126. break;
  9127. default:
  9128. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9129. $my_dep->setAttribute('xml:base', '');
  9130. break;
  9131. }
  9132. }
  9133. $my_dep->appendChild($my_dep_file);
  9134. $resources->appendChild($my_dep);
  9135. $dependency = $xmldoc->createElement('dependency');
  9136. $dependency->setAttribute('identifierref', $res_id);
  9137. $my_resource->appendChild($dependency);
  9138. $i++;
  9139. }
  9140. $resources->appendChild($my_resource);
  9141. $zip_files[] = $my_file_path;
  9142. break;
  9143. default:
  9144. // Get the path of the file(s) from the course directory root
  9145. $my_file_path = 'non_exportable.html';
  9146. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9147. $my_xml_file_path = $my_file_path;
  9148. $my_sub_dir = dirname($my_file_path);
  9149. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9150. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9151. $my_xml_sub_dir = $my_sub_dir;
  9152. // Give a <resource> child to the <resources> element.
  9153. $my_resource = $xmldoc->createElement('resource');
  9154. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9155. $my_resource->setAttribute('type', 'webcontent');
  9156. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9157. // adlcp:scormtype can be either 'sco' or 'asset'.
  9158. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9159. // xml:base is the base directory to find the files declared in this resource.
  9160. $my_resource->setAttribute('xml:base', '');
  9161. // Give a <file> child to the <resource> element.
  9162. $my_file = $xmldoc->createElement('file');
  9163. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9164. $my_resource->appendChild($my_file);
  9165. $resources->appendChild($my_resource);
  9166. break;
  9167. }
  9168. }
  9169. }
  9170. $organizations->appendChild($organization);
  9171. $root->appendChild($organizations);
  9172. $root->appendChild($resources);
  9173. $xmldoc->appendChild($root);
  9174. $copyAll = api_get_configuration_value('add_all_files_in_lp_export');
  9175. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9176. // then add the file to the zip, then destroy the file (this is done automatically).
  9177. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9178. foreach ($zip_files as $file_path) {
  9179. if (empty($file_path)) {
  9180. continue;
  9181. }
  9182. $filePath = $sys_course_path.$_course['path'].'/'.$file_path;
  9183. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9184. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9185. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9186. }
  9187. $this->create_path($dest_file);
  9188. @copy($filePath, $dest_file);
  9189. // Check if the file needs a link update.
  9190. if (in_array($file_path, array_keys($link_updates))) {
  9191. $string = file_get_contents($dest_file);
  9192. unlink($dest_file);
  9193. foreach ($link_updates[$file_path] as $old_new) {
  9194. // This is an ugly hack that allows .flv files to be found by the flv player that
  9195. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9196. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9197. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9198. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9199. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9200. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9201. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9202. }
  9203. //Fix to avoid problems with default_course_document
  9204. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9205. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9206. $newDestination = $old_new['dest'];
  9207. } else {
  9208. $newDestination = str_replace('document/', '', $old_new['dest']);
  9209. }
  9210. $string = str_replace($old_new['orig'], $newDestination, $string);
  9211. // Add files inside the HTMLs
  9212. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9213. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9214. if (file_exists($sys_course_path.$new_path)) {
  9215. copy($sys_course_path.$new_path, $destinationFile);
  9216. }
  9217. }
  9218. file_put_contents($dest_file, $string);
  9219. }
  9220. if (file_exists($filePath) && $copyAll) {
  9221. $extension = $this->get_extension($filePath);
  9222. if (in_array($extension, ['html', 'html'])) {
  9223. $containerOrigin = dirname($filePath);
  9224. $containerDestination = dirname($dest_file);
  9225. $finder = new Finder();
  9226. $finder->files()->in($containerOrigin)
  9227. ->notName('*_DELETED_*')
  9228. ->exclude('share_folder')
  9229. ->exclude('chat_files')
  9230. ->exclude('certificates')
  9231. ;
  9232. if (is_dir($containerOrigin) && is_dir($containerDestination)) {
  9233. $fs = new Filesystem();
  9234. $fs->mirror(
  9235. $containerOrigin,
  9236. $containerDestination,
  9237. $finder
  9238. );
  9239. }
  9240. }
  9241. }
  9242. }
  9243. foreach ($zip_files_abs as $file_path) {
  9244. if (empty($file_path)) {
  9245. continue;
  9246. }
  9247. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9248. continue;
  9249. }
  9250. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9251. $this->create_path($dest_file);
  9252. copy($main_path.$file_path, $dest_file);
  9253. // Check if the file needs a link update.
  9254. if (in_array($file_path, array_keys($link_updates))) {
  9255. $string = file_get_contents($dest_file);
  9256. unlink($dest_file);
  9257. foreach ($link_updates[$file_path] as $old_new) {
  9258. // This is an ugly hack that allows .flv files to be found by the flv player that
  9259. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9260. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9261. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9262. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9263. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9264. }
  9265. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9266. }
  9267. file_put_contents($dest_file, $string);
  9268. }
  9269. }
  9270. if (is_array($links_to_create)) {
  9271. foreach ($links_to_create as $file => $link) {
  9272. $file_content = '<!DOCTYPE html><head>
  9273. <meta charset="'.api_get_language_isocode().'" />
  9274. <title>'.$link['title'].'</title>
  9275. </head>
  9276. <body dir="'.api_get_text_direction().'">
  9277. <div style="text-align:center">
  9278. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9279. </body>
  9280. </html>';
  9281. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9282. }
  9283. }
  9284. // Add non exportable message explanation.
  9285. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9286. $file_content = '<!DOCTYPE html><head>
  9287. <meta charset="'.api_get_language_isocode().'" />
  9288. <title>'.$lang_not_exportable.'</title>
  9289. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9290. </head>
  9291. <body dir="'.api_get_text_direction().'">';
  9292. $file_content .=
  9293. <<<EOD
  9294. <style>
  9295. .error-message {
  9296. font-family: arial, verdana, helvetica, sans-serif;
  9297. border-width: 1px;
  9298. border-style: solid;
  9299. left: 50%;
  9300. margin: 10px auto;
  9301. min-height: 30px;
  9302. padding: 5px;
  9303. right: 50%;
  9304. width: 500px;
  9305. background-color: #FFD1D1;
  9306. border-color: #FF0000;
  9307. color: #000;
  9308. }
  9309. </style>
  9310. <body>
  9311. <div class="error-message">
  9312. $lang_not_exportable
  9313. </div>
  9314. </body>
  9315. </html>
  9316. EOD;
  9317. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9318. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9319. }
  9320. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9321. // Add the extra files that go along with a SCORM package.
  9322. $main_code_path = api_get_path(SYS_CODE_PATH) . 'lp/packaging/';
  9323. $fs = new Filesystem();
  9324. $fs->mirror($main_code_path, $archive_path.$temp_dir_short);
  9325. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9326. $manifest = @$xmldoc->saveXML();
  9327. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9328. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9329. $zip_folder->add(
  9330. $archive_path.'/'.$temp_dir_short,
  9331. PCLZIP_OPT_REMOVE_PATH,
  9332. $archive_path.'/'.$temp_dir_short.'/'
  9333. );
  9334. // Clean possible temporary files.
  9335. foreach ($files_cleanup as $file) {
  9336. $res = unlink($file);
  9337. if ($res === false) {
  9338. error_log(
  9339. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9340. 0
  9341. );
  9342. }
  9343. }
  9344. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9345. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9346. }
  9347. /**
  9348. * @param int $lp_id
  9349. * @return bool
  9350. */
  9351. public function scorm_export_to_pdf($lp_id)
  9352. {
  9353. $lp_id = intval($lp_id);
  9354. $files_to_export = array();
  9355. $course_data = api_get_course_info($this->cc);
  9356. if (!empty($course_data)) {
  9357. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9358. $list = self::get_flat_ordered_items_list($lp_id);
  9359. if (!empty($list)) {
  9360. foreach ($list as $item_id) {
  9361. $item = $this->items[$item_id];
  9362. switch ($item->type) {
  9363. case 'document':
  9364. //Getting documents from a LP with chamilo documents
  9365. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9366. // Try loading document from the base course.
  9367. if (empty($file_data) && !empty($sessionId)) {
  9368. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9369. }
  9370. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9371. if (file_exists($file_path)) {
  9372. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9373. }
  9374. break;
  9375. case 'asset': //commes from a scorm package generated by chamilo
  9376. case 'sco':
  9377. $file_path = $scorm_path.'/'.$item->path;
  9378. if (file_exists($file_path)) {
  9379. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9380. }
  9381. break;
  9382. case 'dir':
  9383. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9384. break;
  9385. }
  9386. }
  9387. }
  9388. $pdf = new PDF();
  9389. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9390. return $result;
  9391. }
  9392. return false;
  9393. }
  9394. /**
  9395. * Temp function to be moved in main_api or the best place around for this.
  9396. * Creates a file path if it doesn't exist
  9397. * @param string $path
  9398. */
  9399. public function create_path($path)
  9400. {
  9401. $path_bits = explode('/', dirname($path));
  9402. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9403. $path_built = IS_WINDOWS_OS ? '' : '/';
  9404. foreach ($path_bits as $bit) {
  9405. if (!empty ($bit)) {
  9406. $new_path = $path_built . $bit;
  9407. if (is_dir($new_path)) {
  9408. $path_built = $new_path . '/';
  9409. } else {
  9410. mkdir($new_path, api_get_permissions_for_new_directories());
  9411. $path_built = $new_path . '/';
  9412. }
  9413. }
  9414. }
  9415. }
  9416. /**
  9417. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9418. * @return boolean The results of the unlink function, or false if there was no image to start with
  9419. */
  9420. public function delete_lp_image()
  9421. {
  9422. $img = $this->get_preview_image();
  9423. if ($img != '') {
  9424. $del_file = $this->get_preview_image_path(null, 'sys');
  9425. if (isset($del_file) && file_exists($del_file)) {
  9426. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9427. if (file_exists($del_file_2)) {
  9428. unlink($del_file_2);
  9429. }
  9430. $this->set_preview_image('');
  9431. return @unlink($del_file);
  9432. }
  9433. }
  9434. return false;
  9435. }
  9436. /**
  9437. * Uploads an author image to the upload/learning_path/images directory
  9438. * @param array The image array, coming from the $_FILES superglobal
  9439. * @return boolean True on success, false on error
  9440. */
  9441. public function upload_image($image_array)
  9442. {
  9443. if (!empty ($image_array['name'])) {
  9444. $upload_ok = process_uploaded_file($image_array);
  9445. $has_attachment = true;
  9446. }
  9447. if ($upload_ok) {
  9448. if ($has_attachment) {
  9449. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9450. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9451. $updir = $sys_course_path . $courseDir;
  9452. // Try to add an extension to the file if it hasn't one.
  9453. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9454. if (!filter_extension($new_file_name)) {
  9455. } else {
  9456. $file_extension = explode('.', $image_array['name']);
  9457. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9458. $filename = uniqid('');
  9459. $new_file_name = $filename.'.'.$file_extension;
  9460. $new_path = $updir.'/'.$new_file_name;
  9461. // Resize the image.
  9462. $temp = new Image($image_array['tmp_name']);
  9463. $temp->resize(104);
  9464. $result = $temp->send_image($new_path);
  9465. // Storing the image filename.
  9466. if ($result) {
  9467. $this->set_preview_image($new_file_name);
  9468. //Resize to 64px to use on course homepage
  9469. $temp->resize(64);
  9470. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9471. return true;
  9472. }
  9473. }
  9474. }
  9475. }
  9476. return false;
  9477. }
  9478. /**
  9479. * @param int $lp_id
  9480. * @param string $status
  9481. */
  9482. public function set_autolaunch($lp_id, $status)
  9483. {
  9484. $course_id = api_get_course_int_id();
  9485. $lp_id = intval($lp_id);
  9486. $status = intval($status);
  9487. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9488. // Setting everything to autolaunch = 0
  9489. $attributes['autolaunch'] = 0;
  9490. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9491. Database::update($lp_table, $attributes, $where);
  9492. if ($status == 1) {
  9493. //Setting my lp_id to autolaunch = 1
  9494. $attributes['autolaunch'] = 1;
  9495. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9496. Database::update($lp_table, $attributes, $where );
  9497. }
  9498. }
  9499. /**
  9500. * Gets previous_item_id for the next element of the lp_item table
  9501. * @author Isaac flores paz
  9502. * @return integer Previous item ID
  9503. */
  9504. public function select_previous_item_id()
  9505. {
  9506. $course_id = api_get_course_int_id();
  9507. if ($this->debug > 0) {
  9508. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9509. }
  9510. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9511. // Get the max order of the items
  9512. $sql = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9513. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9514. $rs_max_order = Database::query($sql);
  9515. $row_max_order = Database::fetch_object($rs_max_order);
  9516. $max_order = $row_max_order->display_order;
  9517. // Get the previous item ID
  9518. $sql = "SELECT id as previous FROM $table_lp_item
  9519. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9520. $rs_max = Database::query($sql);
  9521. $row_max = Database::fetch_object($rs_max);
  9522. // Return the previous item ID
  9523. return $row_max->previous;
  9524. }
  9525. /**
  9526. * Copies an LP
  9527. */
  9528. public function copy()
  9529. {
  9530. // Course builder
  9531. $cb = new CourseBuilder();
  9532. //Setting tools that will be copied
  9533. $cb->set_tools_to_build(array('learnpaths'));
  9534. //Setting elements that will be copied
  9535. $cb->set_tools_specific_id_list(
  9536. array('learnpaths' => array($this->lp_id))
  9537. );
  9538. $course = $cb->build();
  9539. //Course restorer
  9540. $course_restorer = new CourseRestorer($course);
  9541. $course_restorer->set_add_text_in_items(true);
  9542. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9543. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9544. }
  9545. public function verify_document_size($s)
  9546. {
  9547. $post_max = ini_get('post_max_size');
  9548. if (substr($post_max, -1, 1) == 'M') {
  9549. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9550. } elseif (substr($post_max, -1, 1) == 'G') {
  9551. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9552. }
  9553. $upl_max = ini_get('upload_max_filesize');
  9554. if (substr($upl_max, -1, 1) == 'M') {
  9555. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9556. } elseif (substr($upl_max, -1, 1) == 'G') {
  9557. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9558. }
  9559. $documents_total_space = DocumentManager::documents_total_space();
  9560. $course_max_space = DocumentManager::get_course_quota();
  9561. $total_size = filesize($s) + $documents_total_space;
  9562. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9563. return true;
  9564. } else{
  9565. return false;
  9566. }
  9567. }
  9568. /**
  9569. * Clear LP prerequisites
  9570. */
  9571. public function clear_prerequisites()
  9572. {
  9573. $course_id = $this->get_course_int_id();
  9574. if ($this->debug > 0) {
  9575. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9576. }
  9577. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9578. $lp_id = $this->get_id();
  9579. //Cleaning prerequisites
  9580. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9581. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9582. Database::query($sql);
  9583. //Cleaning mastery score for exercises
  9584. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9585. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9586. Database::query($sql);
  9587. }
  9588. public function set_previous_step_as_prerequisite_for_all_items()
  9589. {
  9590. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9591. $course_id = $this->get_course_int_id();
  9592. $lp_id = $this->get_id();
  9593. if (!empty($this->items)) {
  9594. $previous_item_id = null;
  9595. $previous_item_max = 0;
  9596. $previous_item_type = null;
  9597. $last_item_not_dir = null;
  9598. $last_item_not_dir_type = null;
  9599. $last_item_not_dir_max = null;
  9600. foreach ($this->items as $item) {
  9601. // if there was a previous item... (otherwise jump to set it)
  9602. if (!empty($previous_item_id)) {
  9603. $current_item_id = $item->get_id(); //save current id
  9604. if ($item->get_type() != 'dir') {
  9605. // Current item is not a folder, so it qualifies to get a prerequisites
  9606. if ($last_item_not_dir_type == 'quiz') {
  9607. // if previous is quiz, mark its max score as default score to be achieved
  9608. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_dir_max'
  9609. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_dir'";
  9610. Database::query($sql);
  9611. }
  9612. // now simply update the prerequisite to set it to the last non-chapter item
  9613. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_dir'
  9614. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9615. Database::query($sql);
  9616. // record item as 'non-chapter' reference
  9617. $last_item_not_dir = $item->get_id();
  9618. $last_item_not_dir_type = $item->get_type();
  9619. $last_item_not_dir_max = $item->get_max();
  9620. }
  9621. } else {
  9622. if ($item->get_type() != 'dir') {
  9623. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9624. $last_item_not_dir = $item->get_id();
  9625. $last_item_not_dir_type = $item->get_type();
  9626. $last_item_not_dir_max = $item->get_max();
  9627. }
  9628. }
  9629. // Saving the item as "previous item" for the next loop
  9630. $previous_item_id = $item->get_id();
  9631. $previous_item_max = $item->get_max();
  9632. $previous_item_type = $item->get_type();
  9633. }
  9634. }
  9635. }
  9636. /**
  9637. * @param array $params
  9638. */
  9639. public static function createCategory($params)
  9640. {
  9641. $em = Database::getManager();
  9642. $item = new CLpCategory();
  9643. $item->setName($params['name']);
  9644. $item->setCId($params['c_id']);
  9645. $em->persist($item);
  9646. $em->flush();
  9647. }
  9648. /**
  9649. * @param array $params
  9650. */
  9651. public static function updateCategory($params)
  9652. {
  9653. $em = Database::getManager();
  9654. /** @var CLpCategory $item */
  9655. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9656. if ($item) {
  9657. $item->setName($params['name']);
  9658. $em->merge($item);
  9659. $em->flush();
  9660. }
  9661. }
  9662. /**
  9663. * @param int $id
  9664. */
  9665. public static function moveUpCategory($id)
  9666. {
  9667. $em = Database::getManager();
  9668. /** @var CLpCategory $item */
  9669. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9670. if ($item) {
  9671. $position = $item->getPosition() - 1;
  9672. $item->setPosition($position);
  9673. $em->persist($item);
  9674. $em->flush();
  9675. }
  9676. }
  9677. /**
  9678. * @param int $id
  9679. */
  9680. public static function moveDownCategory($id)
  9681. {
  9682. $em = Database::getManager();
  9683. /** @var CLpCategory $item */
  9684. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9685. if ($item) {
  9686. $position = $item->getPosition() + 1;
  9687. $item->setPosition($position);
  9688. $em->persist($item);
  9689. $em->flush();
  9690. }
  9691. }
  9692. /**
  9693. * @param int $courseId
  9694. * @return int|mixed
  9695. */
  9696. public static function getCountCategories($courseId)
  9697. {
  9698. if (empty($course_id)) {
  9699. return 0;
  9700. }
  9701. $em = Database::getManager();
  9702. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9703. $query->setParameter('id', $courseId);
  9704. return $query->getSingleScalarResult();
  9705. }
  9706. /**
  9707. * @param int $courseId
  9708. *
  9709. * @return mixed
  9710. */
  9711. public static function getCategories($courseId)
  9712. {
  9713. $em = Database::getManager();
  9714. //Default behaviour
  9715. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9716. array('cId' => $course_id),
  9717. array('name' => 'ASC')
  9718. );*/
  9719. // Using doctrine extensions
  9720. /** @var SortableRepository $repo */
  9721. $repo = $em->getRepository('ChamiloCourseBundle:CLpCategory');
  9722. $items = $repo
  9723. ->getBySortableGroupsQuery(['cId' => $courseId])
  9724. ->getResult();
  9725. return $items;
  9726. }
  9727. /**
  9728. * @param int $id
  9729. *
  9730. * @return CLpCategory
  9731. */
  9732. public static function getCategory($id)
  9733. {
  9734. $em = Database::getManager();
  9735. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9736. return $item;
  9737. }
  9738. /**
  9739. * @param int $courseId
  9740. * @return array
  9741. */
  9742. public static function getCategoryByCourse($courseId)
  9743. {
  9744. $em = Database::getManager();
  9745. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9746. array('cId' => $courseId)
  9747. );
  9748. return $items;
  9749. }
  9750. /**
  9751. * @param int $id
  9752. *
  9753. * @return mixed
  9754. */
  9755. public static function deleteCategory($id)
  9756. {
  9757. $em = Database::getManager();
  9758. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9759. if ($item) {
  9760. $courseId = $item->getCId();
  9761. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9762. $query->setParameter('id', $courseId);
  9763. $query->setParameter('catId', $item->getId());
  9764. $lps = $query->getResult();
  9765. // Setting category = 0.
  9766. if ($lps) {
  9767. foreach ($lps as $lpItem) {
  9768. $lpItem->setCategoryId(0);
  9769. }
  9770. }
  9771. // Removing category.
  9772. $em->remove($item);
  9773. $em->flush();
  9774. }
  9775. }
  9776. /**
  9777. * @param int $courseId
  9778. * @param bool $addSelectOption
  9779. *
  9780. * @return mixed
  9781. */
  9782. public static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9783. {
  9784. $items = self::getCategoryByCourse($courseId);
  9785. $cats = array();
  9786. if ($addSelectOption) {
  9787. $cats = array(get_lang('SelectACategory'));
  9788. }
  9789. if (!empty($items)) {
  9790. foreach ($items as $cat) {
  9791. $cats[$cat->getId()] = $cat->getName();
  9792. }
  9793. }
  9794. return $cats;
  9795. }
  9796. /**
  9797. * Return the scorm item type object with spaces replaced with _
  9798. * The return result is use to build a css classname like scorm_type_$return
  9799. * @param $in_type
  9800. * @return mixed
  9801. */
  9802. private static function format_scorm_type_item($in_type)
  9803. {
  9804. return str_replace(' ', '_', $in_type);
  9805. }
  9806. /**
  9807. * @param string $courseCode
  9808. * @param int $lp_id
  9809. * @param int $user_id
  9810. *
  9811. * @return learnpath
  9812. */
  9813. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9814. {
  9815. $learnPath = null;
  9816. $lpObject = Session::read('lpobject');
  9817. if ($lpObject !== null) {
  9818. $learnPath = unserialize($lpObject);
  9819. }
  9820. if (!is_object($learnPath)) {
  9821. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9822. }
  9823. return $learnPath;
  9824. }
  9825. /**
  9826. * @param int $itemId
  9827. * @return learnpathItem|false
  9828. */
  9829. public function getItem($itemId)
  9830. {
  9831. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9832. return $this->items[$itemId];
  9833. }
  9834. return false;
  9835. }
  9836. /**
  9837. * @return int
  9838. */
  9839. public function getCategoryId()
  9840. {
  9841. return $this->categoryId;
  9842. }
  9843. /**
  9844. * @param int $categoryId
  9845. * @return bool
  9846. */
  9847. public function setCategoryId($categoryId)
  9848. {
  9849. $this->categoryId = intval($categoryId);
  9850. $courseId = api_get_course_int_id();
  9851. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9852. $lp_id = $this->get_id();
  9853. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9854. WHERE c_id = $courseId AND id = $lp_id";
  9855. Database::query($sql);
  9856. return true;
  9857. }
  9858. /**
  9859. * Get whether this is a learning path with the possibility to subscribe
  9860. * users or not
  9861. * @return int
  9862. */
  9863. public function getSubscribeUsers()
  9864. {
  9865. return $this->subscribeUsers;
  9866. }
  9867. /**
  9868. * Set whether this is a learning path with the possibility to subscribe
  9869. * users or not
  9870. * @param int $subscribeUsers (0 = false, 1 = true)
  9871. */
  9872. public function setSubscribeUsers($value)
  9873. {
  9874. if ($this->debug > 0) {
  9875. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9876. }
  9877. $this->subscribeUsers = intval($value);;
  9878. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9879. $lp_id = $this->get_id();
  9880. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9881. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9882. Database::query($sql);
  9883. return true;
  9884. }
  9885. /**
  9886. * Calculate the count of stars for a user in this LP
  9887. * This calculation is based on the following rules:
  9888. * - the student gets one star when he gets to 50% of the learning path
  9889. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9890. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9891. * - the student gets the final star when the score for the *last* test is >= 80%
  9892. * @param int $sessionId Optional. The session ID
  9893. * @return int The count of stars
  9894. */
  9895. public function getCalculateStars($sessionId = 0)
  9896. {
  9897. $stars = 0;
  9898. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9899. if ($progress >= 50) {
  9900. $stars++;
  9901. }
  9902. // Calculate stars chapters evaluation
  9903. $exercisesItems = $this->getExercisesItems();
  9904. if (!empty($exercisesItems)) {
  9905. $totalResult = 0;
  9906. foreach ($exercisesItems as $exerciseItem) {
  9907. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9908. $this->user_id,
  9909. $exerciseItem->path,
  9910. $this->course_int_id,
  9911. $sessionId,
  9912. $this->lp_id,
  9913. $exerciseItem->db_id
  9914. );
  9915. $exerciseResultInfo = end($exerciseResultInfo);
  9916. if (!$exerciseResultInfo) {
  9917. continue;
  9918. }
  9919. if (!empty($exerciseResultInfo['exe_weighting'])) {
  9920. $exerciseResult = $exerciseResultInfo['exe_result'] * 100 / $exerciseResultInfo['exe_weighting'];
  9921. } else {
  9922. $exerciseResult = 0;
  9923. }
  9924. $totalResult += $exerciseResult;
  9925. }
  9926. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9927. if ($totalExerciseAverage >= 50) {
  9928. $stars++;
  9929. }
  9930. if ($totalExerciseAverage >= 80) {
  9931. $stars++;
  9932. }
  9933. }
  9934. // Calculate star for final evaluation
  9935. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9936. if (!empty($finalEvaluationItem)) {
  9937. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9938. $this->user_id,
  9939. $finalEvaluationItem->path,
  9940. $this->course_int_id,
  9941. $sessionId,
  9942. $this->lp_id,
  9943. $finalEvaluationItem->db_id
  9944. );
  9945. $evaluationResultInfo = end($evaluationResultInfo);
  9946. if ($evaluationResultInfo) {
  9947. $evaluationResult = $evaluationResultInfo['exe_result'] * 100 / $evaluationResultInfo['exe_weighting'];
  9948. if ($evaluationResult >= 80) {
  9949. $stars++;
  9950. }
  9951. }
  9952. }
  9953. return $stars;
  9954. }
  9955. /**
  9956. * Get the items of exercise type
  9957. * @return array The items. Otherwise return false
  9958. */
  9959. public function getExercisesItems()
  9960. {
  9961. $exercises = [];
  9962. foreach ($this->items as $item) {
  9963. if ($item->type != 'quiz') {
  9964. continue;
  9965. }
  9966. $exercises[] = $item;
  9967. }
  9968. array_pop($exercises);
  9969. return $exercises;
  9970. }
  9971. /**
  9972. * Get the item of exercise type (evaluation type)
  9973. * @return array The final evaluation. Otherwise return false
  9974. */
  9975. public function getFinalEvaluationItem()
  9976. {
  9977. $exercises = [];
  9978. foreach ($this->items as $item) {
  9979. if ($item->type != 'quiz') {
  9980. continue;
  9981. }
  9982. $exercises[] = $item;
  9983. }
  9984. return array_pop($exercises);
  9985. }
  9986. /**
  9987. * Calculate the total points achieved for the current user in this learning path
  9988. * @param int $sessionId Optional. The session Id
  9989. * @return int
  9990. */
  9991. public function getCalculateScore($sessionId = 0)
  9992. {
  9993. // Calculate stars chapters evaluation
  9994. $exercisesItems = $this->getExercisesItems();
  9995. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9996. $totalExercisesResult = 0;
  9997. $totalEvaluationResult = 0;
  9998. if ($exercisesItems !== false) {
  9999. foreach ($exercisesItems as $exerciseItem) {
  10000. $exerciseResultInfo = Event::getExerciseResultsByUser(
  10001. $this->user_id,
  10002. $exerciseItem->path,
  10003. $this->course_int_id,
  10004. $sessionId,
  10005. $this->lp_id,
  10006. $exerciseItem->db_id
  10007. );
  10008. $exerciseResultInfo = end($exerciseResultInfo);
  10009. if (!$exerciseResultInfo) {
  10010. continue;
  10011. }
  10012. $totalExercisesResult += $exerciseResultInfo['exe_result'];
  10013. }
  10014. }
  10015. if (!empty($finalEvaluationItem)) {
  10016. $evaluationResultInfo = Event::getExerciseResultsByUser(
  10017. $this->user_id,
  10018. $finalEvaluationItem->path,
  10019. $this->course_int_id,
  10020. $sessionId,
  10021. $this->lp_id,
  10022. $finalEvaluationItem->db_id
  10023. );
  10024. $evaluationResultInfo = end($evaluationResultInfo);
  10025. if ($evaluationResultInfo) {
  10026. $totalEvaluationResult += $evaluationResultInfo['exe_result'];
  10027. }
  10028. }
  10029. return $totalExercisesResult + $totalEvaluationResult;
  10030. }
  10031. /**
  10032. * Check if URL is not allowed to be show in a iframe
  10033. * @param string $src
  10034. *
  10035. * @return string
  10036. */
  10037. public function fixBlockedLinks($src)
  10038. {
  10039. $urlInfo = parse_url($src);
  10040. $platformProtocol = 'https';
  10041. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  10042. $platformProtocol = 'http';
  10043. }
  10044. $protocolFixApplied = false;
  10045. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  10046. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  10047. if ($platformProtocol != $scheme) {
  10048. Session::write('x_frame_source', $src);
  10049. $src = 'blank.php?error=x_frames_options';
  10050. $protocolFixApplied = true;
  10051. }
  10052. if ($protocolFixApplied == false) {
  10053. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  10054. // Check X-Frame-Options
  10055. $ch = curl_init();
  10056. $options = array(
  10057. CURLOPT_URL => $src,
  10058. CURLOPT_RETURNTRANSFER => true,
  10059. CURLOPT_HEADER => true,
  10060. CURLOPT_FOLLOWLOCATION => true,
  10061. CURLOPT_ENCODING => "",
  10062. CURLOPT_AUTOREFERER => true,
  10063. CURLOPT_CONNECTTIMEOUT => 120,
  10064. CURLOPT_TIMEOUT => 120,
  10065. CURLOPT_MAXREDIRS => 10,
  10066. );
  10067. curl_setopt_array($ch, $options);
  10068. $response = curl_exec($ch);
  10069. $httpCode = curl_getinfo($ch);
  10070. $headers = substr($response, 0, $httpCode['header_size']);
  10071. $error = false;
  10072. if (stripos($headers, 'X-Frame-Options: DENY') > -1
  10073. //|| stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  10074. ) {
  10075. $error = true;
  10076. }
  10077. if ($error) {
  10078. Session::write('x_frame_source', $src);
  10079. $src = 'blank.php?error=x_frames_options';
  10080. }
  10081. }
  10082. }
  10083. return $src;
  10084. }
  10085. /**
  10086. * Check if this LP has a created forum in the basis course
  10087. * @return boolean
  10088. */
  10089. public function lpHasForum()
  10090. {
  10091. $forumTable = Database::get_course_table(TABLE_FORUM);
  10092. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10093. $fakeFrom = "
  10094. $forumTable f
  10095. INNER JOIN $itemProperty ip
  10096. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  10097. ";
  10098. $resultData = Database::select(
  10099. 'COUNT(f.iid) AS qty',
  10100. $fakeFrom,
  10101. [
  10102. 'where' => [
  10103. 'ip.visibility != ? AND ' => 2,
  10104. 'ip.tool = ? AND ' => TOOL_FORUM,
  10105. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10106. 'f.lp_id = ?' => intval($this->lp_id)
  10107. ]
  10108. ],
  10109. 'first'
  10110. );
  10111. return $resultData['qty'] > 0;
  10112. }
  10113. /**
  10114. * Get the forum for this learning path
  10115. * @return boolean
  10116. */
  10117. public function getForum($sessionId = 0)
  10118. {
  10119. $forumTable = Database::get_course_table(TABLE_FORUM);
  10120. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  10121. $fakeFrom = "$forumTable f
  10122. INNER JOIN $itemProperty ip ";
  10123. if ($this->lp_session_id == 0) {
  10124. $fakeFrom .= "
  10125. ON (
  10126. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  10127. f.session_id = ip.session_id OR ip.session_id IS NULL
  10128. )
  10129. )
  10130. ";
  10131. } else {
  10132. $fakeFrom .= "
  10133. ON (
  10134. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  10135. )
  10136. ";
  10137. }
  10138. $resultData = Database::select(
  10139. 'f.*',
  10140. $fakeFrom,
  10141. [
  10142. 'where' => [
  10143. 'ip.visibility != ? AND ' => 2,
  10144. 'ip.tool = ? AND ' => TOOL_FORUM,
  10145. 'f.session_id = ? AND ' => $sessionId,
  10146. 'f.c_id = ? AND ' => intval($this->course_int_id),
  10147. 'f.lp_id = ?' => intval($this->lp_id)
  10148. ]
  10149. ],
  10150. 'first'
  10151. );
  10152. if (empty($resultData)) {
  10153. return false;
  10154. }
  10155. return $resultData;
  10156. }
  10157. /**
  10158. * Create a forum for this learning path
  10159. * @param type $forumCategoryId
  10160. * @return int The forum ID if was created. Otherwise return false
  10161. */
  10162. public function createForum($forumCategoryId)
  10163. {
  10164. require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
  10165. $forumId = store_forum(
  10166. [
  10167. 'lp_id' => $this->lp_id,
  10168. 'forum_title' => $this->name,
  10169. 'forum_comment' => null,
  10170. 'forum_category' => intval($forumCategoryId),
  10171. 'students_can_edit_group' => ['students_can_edit' => 0],
  10172. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  10173. 'default_view_type_group' => ['default_view_type' => 'flat'],
  10174. 'group_forum' => 0,
  10175. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  10176. ],
  10177. [],
  10178. true
  10179. );
  10180. return $forumId;
  10181. }
  10182. /**
  10183. * Check and obtain the lp final item if exist
  10184. *
  10185. * @return learnpathItem
  10186. */
  10187. private function getFinalItem()
  10188. {
  10189. if (empty($this->items)) {
  10190. return null;
  10191. }
  10192. foreach ($this->items as $item) {
  10193. if ($item->type !== 'final_item') {
  10194. continue;
  10195. }
  10196. return $item;
  10197. }
  10198. }
  10199. /**
  10200. * Get the LP Final Item Template
  10201. *
  10202. * @return string
  10203. */
  10204. private function getFinalItemTemplate()
  10205. {
  10206. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'lp/final_item_template/template.html');
  10207. }
  10208. /**
  10209. * Get the LP Final Item Url
  10210. *
  10211. * @return string
  10212. */
  10213. private function getSavedFinalItem()
  10214. {
  10215. $finalItem = $this->getFinalItem();
  10216. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10217. if ($doc && file_exists($doc['absolute_path'])) {
  10218. return file_get_contents($doc['absolute_path']);
  10219. }
  10220. return '';
  10221. }
  10222. /**
  10223. * Get the LP Final Item form
  10224. *
  10225. * @return string
  10226. */
  10227. public function getFinalItemForm()
  10228. {
  10229. $finalItem = $this->getFinalItem();
  10230. $title = '';
  10231. if ($finalItem) {
  10232. $title = $finalItem->get_title();
  10233. $buttonText = get_lang('Save');
  10234. $content = $this->getSavedFinalItem();
  10235. } else {
  10236. $buttonText = get_lang('LPCreateDocument');
  10237. $content = $this->getFinalItemTemplate();
  10238. }
  10239. $courseInfo = api_get_course_info();
  10240. $result = $this->generate_lp_folder($courseInfo);
  10241. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10242. $relative_prefix = '../../';
  10243. $editorConfig = [
  10244. 'ToolbarSet' => 'LearningPathDocuments',
  10245. 'Width' => '100%',
  10246. 'Height' => '500',
  10247. 'FullPage' => true,
  10248. 'CreateDocumentDir' => $relative_prefix,
  10249. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  10250. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  10251. ];
  10252. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  10253. 'type' => 'document',
  10254. 'lp_id' => $this->lp_id
  10255. ]);
  10256. $form = new FormValidator('final_item', 'POST', $url);
  10257. $form->addText('title', get_lang('Title'));
  10258. $form->addButtonSave($buttonText);
  10259. $form->addHtml(
  10260. Display::return_message(
  10261. 'Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b>',
  10262. 'normal',
  10263. false
  10264. )
  10265. );
  10266. $renderer = $form->defaultRenderer();
  10267. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp_certificate');
  10268. $form->addHtmlEditor('content_lp_certificate', null, true, false, $editorConfig, true);
  10269. $form->addHidden('action', 'add_final_item');
  10270. $form->addHidden('path', Session::read('pathItem'));
  10271. $form->addHidden('previous', $this->get_last());
  10272. $form->setDefaults(
  10273. ['title' => $title, 'content_lp_certificate' => $content]
  10274. );
  10275. if ($form->validate()) {
  10276. $values = $form->exportValues();
  10277. $lastItemId = $this->get_last();
  10278. if (!$finalItem) {
  10279. $documentId = $this->create_document(
  10280. $this->course_info,
  10281. $values['content_lp_certificate'],
  10282. $values['title']
  10283. );
  10284. $this->add_item(
  10285. 0,
  10286. $lastItemId,
  10287. 'final_item',
  10288. $documentId,
  10289. $values['title'],
  10290. ''
  10291. );
  10292. Display::addFlash(
  10293. Display::return_message(get_lang('Added'))
  10294. );
  10295. } else {
  10296. $this->edit_document($this->course_info);
  10297. }
  10298. }
  10299. return $form->returnForm();
  10300. }
  10301. /**
  10302. * Check if the current lp item is first, both, last or none from lp list
  10303. *
  10304. * @param int $currentItemId
  10305. * @return string
  10306. */
  10307. public function isFirstOrLastItem($currentItemId)
  10308. {
  10309. if ($this->debug > 0) {
  10310. error_log('New LP - In learnpath::isFirstOrLastItem('.$currentItemId.')', 0);
  10311. }
  10312. $lpItemId = [];
  10313. $typeListNotToVerify = self::getChapterTypes();
  10314. // Using get_toc() function instead $this->items because returns the correct order of the items
  10315. foreach ($this->get_toc() as $item) {
  10316. if (!in_array($item['type'], $typeListNotToVerify)) {
  10317. $lpItemId[] = $item['id'];
  10318. }
  10319. }
  10320. $lastLpItemIndex = count($lpItemId) - 1;
  10321. $position = array_search($currentItemId, $lpItemId);
  10322. switch ($position) {
  10323. case 0:
  10324. if (!$lastLpItemIndex) {
  10325. $answer = 'both';
  10326. break;
  10327. }
  10328. $answer = 'first';
  10329. break;
  10330. case $lastLpItemIndex:
  10331. $answer = 'last';
  10332. break;
  10333. default:
  10334. $answer = 'none';
  10335. }
  10336. return $answer;
  10337. }
  10338. /**
  10339. * Get whether this is a learning path with the accumulated SCORM time or not
  10340. * @return int
  10341. */
  10342. public function getAccumulateScormTime()
  10343. {
  10344. return $this->accumulateScormTime;
  10345. }
  10346. /**
  10347. * Set whether this is a learning path with the accumulated SCORM time or not
  10348. * @param int $value (0 = false, 1 = true)
  10349. * @return bool Always returns true
  10350. */
  10351. public function setAccumulateScormTime($value)
  10352. {
  10353. if ($this->debug > 0) {
  10354. error_log('New LP - In learnpath::setAccumulateScormTime()', 0);
  10355. }
  10356. $this->accumulateScormTime = intval($value);
  10357. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  10358. $lp_id = $this->get_id();
  10359. $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime."
  10360. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  10361. Database::query($sql);
  10362. return true;
  10363. }
  10364. /**
  10365. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10366. * the new learning path tool.
  10367. *
  10368. * The function is a big switch on tool type.
  10369. * In each case, we query the corresponding table for information and build the link
  10370. * with that information.
  10371. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on previous work (display_addedresource_link_in_learnpath())
  10372. * @param int $course_id Course code
  10373. * @param int $learningPathId The learning path ID (in lp table)
  10374. * @param int $id_in_path the unique index in the items table
  10375. * @param int $lpViewId
  10376. * @return string
  10377. */
  10378. public static function rl_get_resource_link_for_learnpath(
  10379. $course_id,
  10380. $learningPathId,
  10381. $id_in_path,
  10382. $lpViewId
  10383. ) {
  10384. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10385. $course_info = api_get_course_info_by_id($course_id);
  10386. $course_id = $course_info['real_id'];
  10387. $course_code = $course_info['code'];
  10388. $session_id = api_get_session_id();
  10389. $learningPathId = intval($learningPathId);
  10390. $id_in_path = intval($id_in_path);
  10391. $lpViewId = intval($lpViewId);
  10392. $em = Database::getManager();
  10393. $sql = "SELECT * FROM $tbl_lp_item
  10394. WHERE
  10395. c_id = $course_id AND
  10396. lp_id = $learningPathId AND
  10397. id = $id_in_path
  10398. ";
  10399. $res_item = Database::query($sql);
  10400. if (Database::num_rows($res_item) < 1) {
  10401. return -1;
  10402. }
  10403. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10404. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  10405. // Get the lp_item_view with the highest view_count.
  10406. $sql = "SELECT * FROM $item_view_table
  10407. WHERE
  10408. c_id = $course_id AND
  10409. lp_item_id = " . $row_item['id'] . " AND
  10410. lp_view_id = $lpViewId
  10411. ORDER BY view_count DESC";
  10412. $learnpathItemViewResult = Database::query($sql);
  10413. $learnpathItemViewData = Database::fetch_array($learnpathItemViewResult, 'ASSOC');
  10414. $learnpathItemViewId = 0;
  10415. if (isset($learnpathItemViewData)) {
  10416. $learnpathItemViewId = $learnpathItemViewData['id'];
  10417. }
  10418. $type = strtolower($row_item['item_type']);
  10419. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10420. $origin = 'learnpath';
  10421. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10422. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10423. $link = '';
  10424. switch ($type) {
  10425. case 'dir':
  10426. $link .= $main_dir_path . 'lp/blank.php';
  10427. case TOOL_CALENDAR_EVENT:
  10428. $link .= $main_dir_path.'calendar/agenda.php?origin='.$origin.'&agenda_id='.$id;
  10429. break;
  10430. case TOOL_ANNOUNCEMENT:
  10431. $link .= $main_dir_path.'announcements/announcements.php?origin='.$origin.'&ann_id='.$id;
  10432. break;
  10433. case TOOL_LINK:
  10434. $link .= $main_dir_path.'link/link_goto.php?'.api_get_cidreq().'&link_id='.$id;
  10435. break;
  10436. case TOOL_QUIZ:
  10437. if (!empty($id)) {
  10438. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10439. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10440. $result= Database::query($sql);
  10441. $myrow = Database::fetch_array($result);
  10442. if ($row_item['title'] != '') {
  10443. $myrow['title'] = $row_item['title'];
  10444. }
  10445. $link .= $main_dir_path . 'exercise/overview.php?cidReq='.$course_code.'&session_id='.$session_id.'&lp_init=1&origin='.$origin.'&learnpath_item_view_id='.$learnpathItemViewId.'&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id;
  10446. }
  10447. break;
  10448. case TOOL_HOTPOTATOES: //lowercase because of strtolower above
  10449. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10450. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10451. $myrow = Database::fetch_array($result);
  10452. $path = $myrow['path'];
  10453. $link .= $main_dir_path . 'exercise/showinframes.php?file='.$path.'&origin='.$origin.'&cid='.$course_code.'&uid='.api_get_user_id().'' .
  10454. '&learnpath_id='.$learningPathId.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId;
  10455. break;
  10456. case TOOL_FORUM:
  10457. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&origin=learnpath';
  10458. break;
  10459. case TOOL_THREAD: //forum post
  10460. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10461. if (!empty($id)) {
  10462. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10463. $result = Database::query($sql);
  10464. $myrow = Database::fetch_array($result);
  10465. $link .= $main_dir_path.'forum/viewthread.php?origin=learnpath&thread='.$id.'&forum='.$myrow['forum_id'].'&lp=true';
  10466. }
  10467. break;
  10468. case TOOL_POST:
  10469. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10470. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10471. $myrow = Database::fetch_array($result);
  10472. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'&lp=true';
  10473. break;
  10474. case TOOL_DOCUMENT:
  10475. $document = $em
  10476. ->getRepository('ChamiloCourseBundle:CDocument')
  10477. ->findOneBy(['cId' => $course_id, 'id' => $id]);
  10478. if (!$document) {
  10479. break;
  10480. }
  10481. $documentPathInfo = pathinfo($document->getPath());
  10482. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10483. $extension = isset($documentPathInfo['extension']) ? $documentPathInfo['extension'] : '';
  10484. $showDirectUrl = !in_array($extension, $jplayer_supported_files);
  10485. if ($showDirectUrl) {
  10486. $link = $main_course_path . 'document' . $document->getPath() . '?' . api_get_cidreq();
  10487. } else {
  10488. $link = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?' . http_build_query([
  10489. 'cidReq' => $course_code,
  10490. 'id' => $id,
  10491. 'origin' => 'learnpathitem'
  10492. ]);
  10493. }
  10494. $openmethod = 2;
  10495. $officedoc = false;
  10496. Session::write('openmethod',$openmethod);
  10497. Session::write('officedoc',$officedoc);
  10498. break;
  10499. case TOOL_LP_FINAL_ITEM:
  10500. $link .= api_get_path(WEB_CODE_PATH) . 'lp/lp_final_item.php?'.api_get_cidreq().'&id='.$id.'&lp_id='.$learningPathId;
  10501. break;
  10502. case 'assignments':
  10503. $link .= $main_dir_path.'work/work.php?origin='.$origin;
  10504. break;
  10505. case TOOL_DROPBOX:
  10506. $link .= $main_dir_path.'dropbox/index.php?origin=learnpath';
  10507. break;
  10508. case 'introduction_text': //DEPRECATED
  10509. $link .= '';
  10510. break;
  10511. case TOOL_COURSE_DESCRIPTION:
  10512. $link .= $main_dir_path.'course_description?origin=learnpath';
  10513. break;
  10514. case TOOL_GROUP:
  10515. $link .= $main_dir_path.'group/group.php?origin='.$origin;
  10516. break;
  10517. case TOOL_USER:
  10518. $link .= $main_dir_path.'user/user.php?origin='.$origin;
  10519. break;
  10520. case TOOL_STUDENTPUBLICATION:
  10521. if (!empty($row_item['path'])) {
  10522. $link .= $main_dir_path.'work/work_list.php?'.api_get_cidreq().'&id='.$row_item['path'].'&origin=learnpath';
  10523. break;
  10524. }
  10525. $link .= $main_dir_path . 'work/work.php?' . api_get_cidreq() . '&' . http_build_query([
  10526. 'id' => $row_item['path'],
  10527. 'origin' => 'learnpath'
  10528. ]);
  10529. break;
  10530. } //end switch
  10531. return $link;
  10532. }
  10533. /**
  10534. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10535. *
  10536. * @author Yannick Warnier <ywarnier@beeznest.org>
  10537. * @param string Course code
  10538. * @param string The tool type (using constants declared in main_api.lib.php)
  10539. * @param integer The resource ID
  10540. */
  10541. public static function rl_get_resource_name($course_code, $learningPathId, $id_in_path)
  10542. {
  10543. $_course = api_get_course_info($course_code);
  10544. $course_id = $_course['real_id'];
  10545. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10546. $learningPathId = intval($learningPathId);
  10547. $id_in_path = intval($id_in_path);
  10548. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10549. WHERE c_id = $course_id AND lp_id = $learningPathId AND id = $id_in_path";
  10550. $res_item = Database::query($sql_item);
  10551. if (Database::num_rows($res_item) < 1) {
  10552. return '';
  10553. }
  10554. $row_item = Database::fetch_array($res_item);
  10555. $type = strtolower($row_item['item_type']);
  10556. $id = $row_item['ref'];
  10557. $output = '';
  10558. switch ($type) {
  10559. case TOOL_CALENDAR_EVENT:
  10560. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10561. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10562. $myrow = Database::fetch_array($result);
  10563. $output = $myrow['title'];
  10564. break;
  10565. case TOOL_ANNOUNCEMENT:
  10566. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10567. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10568. $myrow = Database::fetch_array($result);
  10569. $output = $myrow['title'];
  10570. break;
  10571. case TOOL_LINK:
  10572. // Doesn't take $target into account.
  10573. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10574. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10575. $myrow = Database::fetch_array($result);
  10576. $output = $myrow['title'];
  10577. break;
  10578. case TOOL_QUIZ:
  10579. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10580. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10581. $myrow = Database::fetch_array($result);
  10582. $output = $myrow['title'];
  10583. break;
  10584. case TOOL_FORUM:
  10585. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10586. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10587. $myrow = Database::fetch_array($result);
  10588. $output = $myrow['forum_name'];
  10589. break;
  10590. case TOOL_THREAD: //=topics
  10591. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10592. // Grabbing the title of the post.
  10593. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10594. $result_title = Database::query($sql_title);
  10595. $myrow_title = Database::fetch_array($result_title);
  10596. $output = $myrow_title['post_title'];
  10597. break;
  10598. case TOOL_POST:
  10599. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10600. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10601. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10602. $result = Database::query($sql);
  10603. $post = Database::fetch_array($result);
  10604. $output = $post['post_title'];
  10605. break;
  10606. case 'dir':
  10607. $title = $row_item['title'];
  10608. if (!empty($title)) {
  10609. $output = $title;
  10610. } else {
  10611. $output = '-';
  10612. }
  10613. break;
  10614. case TOOL_DOCUMENT:
  10615. $title = $row_item['title'];
  10616. if (!empty($title)) {
  10617. $output = $title;
  10618. } else {
  10619. $output = '-';
  10620. }
  10621. break;
  10622. case 'hotpotatoes':
  10623. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10624. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id = $id");
  10625. $myrow = Database::fetch_array($result);
  10626. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10627. $last = count($pathname) - 1; // Making a correct name for the link.
  10628. $filename = $pathname[$last]; // Making a correct name for the link.
  10629. $ext = explode('.', $filename);
  10630. $ext = strtolower($ext[sizeof($ext) - 1]);
  10631. $myrow['path'] = rawurlencode($myrow['path']);
  10632. $output = $filename;
  10633. break;
  10634. }
  10635. return stripslashes($output);
  10636. }
  10637. }
  10638. if (!function_exists('trim_value')) {
  10639. function trim_value(& $value) {
  10640. $value = trim($value);
  10641. }
  10642. }