learnpath.class.php 472 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLpCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class learnpath
  7. * This class defines the parent attributes and methods for Chamilo learnpaths
  8. * and SCORM learnpaths. It is used by the scorm class.
  9. *
  10. * @package chamilo.learnpath
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  13. */
  14. class learnpath
  15. {
  16. public $attempt = 0; // The number for the current ID view.
  17. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  18. public $current; // Id of the current item the user is viewing.
  19. public $current_score; // The score of the current item.
  20. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  21. public $current_time_stop; // The time the user closed this resource.
  22. public $default_status = 'not attempted';
  23. public $encoding = 'UTF-8';
  24. public $error = '';
  25. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  26. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  27. public $index; // The index of the active learnpath_item in $ordered_items array.
  28. public $items = array();
  29. public $last; // item_id of last item viewed in the learning path.
  30. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  31. public $license; // Which license this course has been given - not used yet on 20060522.
  32. public $lp_id; // DB ID for this learnpath.
  33. public $lp_view_id; // DB ID for lp_view
  34. public $log_file; // File where to log learnpath API msg.
  35. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  36. public $message = '';
  37. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  38. public $name; // Learnpath name (they generally have one).
  39. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  40. public $path = ''; // Path inside the scorm directory (if scorm).
  41. public $theme; // The current theme of the learning path.
  42. public $preview_image; // The current image of the learning path.
  43. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  44. public $prevent_reinit = 1;
  45. // Describes the mode of progress bar display.
  46. public $seriousgame_mode = 0;
  47. public $progress_bar_mode = '%';
  48. // Percentage progress as saved in the db.
  49. public $progress_db = '0';
  50. public $proximity; // Wether the content is distant or local or unknown.
  51. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  52. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  53. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  54. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  55. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  56. public $user_id; //ID of the user that is viewing/using the course
  57. public $update_queue = array();
  58. public $scorm_debug = 0;
  59. public $arrMenu = array(); // Array for the menu items.
  60. public $debug = 0; // Logging level.
  61. public $lp_session_id = 0;
  62. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  63. public $prerequisite = 0;
  64. public $use_max_score = 1; // 1 or 0
  65. public $subscribeUsers = 0; // Subscribe users or not
  66. public $created_on = '';
  67. public $modified_on = '';
  68. public $publicated_on = '';
  69. public $expired_on = '';
  70. public $ref = null;
  71. public $course_int_id;
  72. public $course_info = array();
  73. public $categoryId;
  74. /**
  75. * Constructor.
  76. * Needs a database handler, a course code and a learnpath id from the database.
  77. * Also builds the list of items into $this->items.
  78. * @param string $course Course code
  79. * @param integer $lp_id
  80. * @param integer $user_id
  81. * @return mixed True on success, false on error
  82. */
  83. public function __construct($course, $lp_id, $user_id)
  84. {
  85. $this->encoding = api_get_system_encoding();
  86. if ($this->debug > 0) {
  87. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  88. }
  89. if (empty($course)) {
  90. $this->error = 'Course code is empty';
  91. return false;
  92. } else {
  93. $course_info = api_get_course_info($course);
  94. if (!empty($course_info)) {
  95. $this->cc = $course_info['code'];
  96. $this->course_info = $course_info;
  97. $course_id = $course_info['real_id'];
  98. } else {
  99. $this->error = 'Course code does not exist in database.';
  100. return false;
  101. }
  102. }
  103. $this->set_course_int_id($course_id);
  104. // Check learnpath ID.
  105. if (empty($lp_id)) {
  106. $this->error = 'Learnpath ID is empty';
  107. return false;
  108. } else {
  109. // TODO: Make it flexible to use any course_code (still using env course code here).
  110. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  111. $lp_id = intval($lp_id);
  112. $sql = "SELECT * FROM $lp_table
  113. WHERE id = '$lp_id' AND c_id = $course_id";
  114. if ($this->debug > 2) {
  115. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  116. }
  117. $res = Database::query($sql);
  118. if (Database::num_rows($res) > 0) {
  119. $this->lp_id = $lp_id;
  120. $row = Database::fetch_array($res);
  121. $this->type = $row['lp_type'];
  122. $this->name = stripslashes($row['name']);
  123. $this->proximity = $row['content_local'];
  124. $this->theme = $row['theme'];
  125. $this->maker = $row['content_maker'];
  126. $this->prevent_reinit = $row['prevent_reinit'];
  127. $this->seriousgame_mode = $row['seriousgame_mode'];
  128. $this->license = $row['content_license'];
  129. $this->scorm_debug = $row['debug'];
  130. $this->js_lib = $row['js_lib'];
  131. $this->path = $row['path'];
  132. $this->preview_image = $row['preview_image'];
  133. $this->author = $row['author'];
  134. $this->hide_toc_frame = $row['hide_toc_frame'];
  135. $this->lp_session_id = $row['session_id'];
  136. $this->use_max_score = $row['use_max_score'];
  137. $this->subscribeUsers = $row['subscribe_users'];
  138. $this->created_on = $row['created_on'];
  139. $this->modified_on = $row['modified_on'];
  140. $this->ref = $row['ref'];
  141. $this->categoryId = $row['category_id'];
  142. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  143. $this->publicated_on = $row['publicated_on'];
  144. }
  145. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  146. $this->expired_on = $row['expired_on'];
  147. }
  148. if ($this->type == 2) {
  149. if ($row['force_commit'] == 1) {
  150. $this->force_commit = true;
  151. }
  152. }
  153. $this->mode = $row['default_view_mod'];
  154. } else {
  155. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  156. return false;
  157. }
  158. }
  159. // Check user ID.
  160. if (empty($user_id)) {
  161. $this->error = 'User ID is empty';
  162. return false;
  163. } else {
  164. $user_info = api_get_user_info($user_id);
  165. if (!empty($user_info)) {
  166. $this->user_id = $user_info['user_id'];
  167. } else {
  168. $this->error = 'User ID does not exist in database ('.$sql.')';
  169. return false;
  170. }
  171. }
  172. // End of variables checking.
  173. $session_id = api_get_session_id();
  174. // Get the session condition for learning paths of the base + session.
  175. $session = api_get_session_condition($session_id);
  176. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  177. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  178. // Selecting by view_count descending allows to get the highest view_count first.
  179. $sql = "SELECT * FROM $lp_table
  180. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  181. ORDER BY view_count DESC";
  182. $res = Database::query($sql);
  183. if ($this->debug > 2) {
  184. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  185. }
  186. if (Database :: num_rows($res) > 0) {
  187. if ($this->debug > 2) {
  188. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  189. }
  190. $row = Database :: fetch_array($res);
  191. $this->attempt = $row['view_count'];
  192. $this->lp_view_id = $row['id'];
  193. $this->last_item_seen = $row['last_item'];
  194. $this->progress_db = $row['progress'];
  195. $this->lp_view_session_id = $row['session_id'];
  196. } else if (!api_is_invitee()) {
  197. if ($this->debug > 2) {
  198. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  199. }
  200. $this->attempt = 1;
  201. $params = [
  202. 'c_id' => $course_id,
  203. 'lp_id' => $lp_id,
  204. 'user_id' => $user_id,
  205. 'view_count' => 1,
  206. 'session_id' => $session_id,
  207. 'last_item' => 0
  208. ];
  209. Database::insert($lp_table, $params);
  210. $this->lp_view_id = Database::insert_id();
  211. if ($this->debug > 2) {
  212. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql, 0);
  213. }
  214. $sql = "UPDATE $lp_table SET id = iid WHERE iid = ".$this->lp_view_id;
  215. Database::query($sql);
  216. }
  217. // Initialise items.
  218. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  219. $sql = "SELECT * FROM $lp_item_table
  220. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  221. ORDER BY parent_item_id, display_order";
  222. $res = Database::query($sql);
  223. if ($this->debug > 2) {
  224. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  225. error_log('-- Start while--', 0);
  226. }
  227. $lp_item_id_list = array();
  228. while ($row = Database::fetch_array($res)) {
  229. $lp_item_id_list[] = $row['id'];
  230. switch ($this->type) {
  231. case 3: //aicc
  232. $oItem = new aiccItem('db', $row['id'], $course_id);
  233. if (is_object($oItem)) {
  234. $my_item_id = $oItem->get_id();
  235. $oItem->set_lp_view($this->lp_view_id, $course_id);
  236. $oItem->set_prevent_reinit($this->prevent_reinit);
  237. // Don't use reference here as the next loop will make the pointed object change.
  238. $this->items[$my_item_id] = $oItem;
  239. $this->refs_list[$oItem->ref] = $my_item_id;
  240. if ($this->debug > 2) {
  241. error_log(
  242. 'New LP - learnpath::__construct() - ' .
  243. 'aicc object with id ' . $my_item_id .
  244. ' set in items[]',
  245. 0
  246. );
  247. }
  248. }
  249. break;
  250. case 2:
  251. $oItem = new scormItem('db', $row['id'], $course_id);
  252. if (is_object($oItem)) {
  253. $my_item_id = $oItem->get_id();
  254. $oItem->set_lp_view($this->lp_view_id, $course_id);
  255. $oItem->set_prevent_reinit($this->prevent_reinit);
  256. // Don't use reference here as the next loop will make the pointed object change.
  257. $this->items[$my_item_id] = $oItem;
  258. $this->refs_list[$oItem->ref] = $my_item_id;
  259. if ($this->debug > 2) {
  260. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  261. }
  262. }
  263. break;
  264. case 1:
  265. default:
  266. if ($this->debug > 2) {
  267. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  268. }
  269. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  270. if ($this->debug > 2) {
  271. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  272. }
  273. if (is_object($oItem)) {
  274. $my_item_id = $oItem->get_id();
  275. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  276. $oItem->set_prevent_reinit($this->prevent_reinit);
  277. // Don't use reference here as the next loop will make the pointed object change.
  278. $this->items[$my_item_id] = $oItem;
  279. $this->refs_list[$my_item_id] = $my_item_id;
  280. if ($this->debug > 2) {
  281. error_log(
  282. 'New LP - learnpath::__construct() ' . __LINE__ .
  283. ' - object with id ' . $my_item_id . ' set in items[]',
  284. 0);
  285. }
  286. }
  287. break;
  288. }
  289. // Setting the object level with variable $this->items[$i][parent]
  290. foreach ($this->items as $itemLPObject) {
  291. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  292. $itemLPObject->level = $level;
  293. }
  294. // Setting the view in the item object.
  295. if (is_object($this->items[$row['id']])) {
  296. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  297. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  298. $this->items[$row['id']]->current_start_time = 0;
  299. $this->items[$row['id']]->current_stop_time = 0;
  300. }
  301. }
  302. }
  303. if ($this->debug > 2) {
  304. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  305. }
  306. if (!empty($lp_item_id_list)) {
  307. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  308. if (!empty($lp_item_id_list_to_string)) {
  309. // Get last viewing vars.
  310. $lp_item_view_table = Database:: get_course_table(
  311. TABLE_LP_ITEM_VIEW
  312. );
  313. // This query should only return one or zero result.
  314. $sql = "SELECT lp_item_id, status
  315. FROM $lp_item_view_table
  316. WHERE
  317. c_id = $course_id AND
  318. lp_view_id = ".$this->lp_view_id." AND
  319. lp_item_id IN ('".$lp_item_id_list_to_string."')
  320. ORDER BY view_count DESC ";
  321. if ($this->debug > 2) {
  322. error_log(
  323. 'New LP - learnpath::__construct() - Selecting item_views: '.$sql,
  324. 0
  325. );
  326. }
  327. $status_list = array();
  328. $res = Database::query($sql);
  329. while ($row = Database:: fetch_array($res)) {
  330. $status_list[$row['lp_item_id']] = $row['status'];
  331. }
  332. foreach ($lp_item_id_list as $item_id) {
  333. if (isset($status_list[$item_id])) {
  334. $status = $status_list[$item_id];
  335. if (is_object($this->items[$item_id])) {
  336. $this->items[$item_id]->set_status($status);
  337. if (empty($status)) {
  338. $this->items[$item_id]->set_status(
  339. $this->default_status
  340. );
  341. }
  342. }
  343. } else {
  344. if (!api_is_invitee()) {
  345. if (is_object($this->items[$item_id])) {
  346. $this->items[$item_id]->set_status(
  347. $this->default_status
  348. );
  349. }
  350. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  351. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status, start_time, total_time, score)
  352. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted', '".time()."', 0, 0)";
  353. if ($this->debug > 2) {
  354. error_log(
  355. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql,
  356. 0
  357. );
  358. }
  359. $this->items[$item_id]->set_lp_view(
  360. $this->lp_view_id,
  361. $course_id
  362. );
  363. Database::query($sql);
  364. $insertId = Database::insert_id();
  365. $sql = "UPDATE $lp_item_view_table SET id = iid
  366. WHERE iid = $insertId";
  367. Database::query($sql);
  368. }
  369. }
  370. }
  371. }
  372. }
  373. $this->ordered_items = self::get_flat_ordered_items_list(
  374. $this->get_id(),
  375. 0,
  376. $course_id
  377. );
  378. $this->max_ordered_items = 0;
  379. foreach ($this->ordered_items as $index => $dummy) {
  380. if ($index > $this->max_ordered_items && !empty($dummy)) {
  381. $this->max_ordered_items = $index;
  382. }
  383. }
  384. // TODO: Define the current item better.
  385. $this->first();
  386. if ($this->debug > 2) {
  387. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  388. }
  389. return true;
  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 = intval($course_id);
  412. }
  413. /**
  414. * Get the depth level of LP item
  415. * @param $in_tab_items
  416. * @param $in_current_item_id
  417. * @return int
  418. */
  419. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  420. {
  421. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  422. if ($parent_item_id == 0) {
  423. return 0;
  424. } else {
  425. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 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 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 = 'dokeos_chapter',
  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 ($previous == 0) {
  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 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. // Update the item that should come after the new item.
  572. $sql = " UPDATE $tbl_lp_item SET
  573. previous_item_id = $new_item_id,
  574. next_item_id = $new_item_id,
  575. id = $new_item_id
  576. WHERE iid = $new_item_id";
  577. Database::query($sql);
  578. // Update all the items after the new item.
  579. $sql = "UPDATE " . $tbl_lp_item . "
  580. SET display_order = display_order + 1
  581. WHERE
  582. c_id = $course_id AND
  583. lp_id = " . $this->get_id() . " AND
  584. id <> " . $new_item_id . " AND
  585. parent_item_id = " . $parent . " AND
  586. display_order > " . $display_order;
  587. Database::query($sql);
  588. // Update the item that should come after the new item.
  589. $sql = "UPDATE " . $tbl_lp_item . "
  590. SET ref = " . $new_item_id . "
  591. WHERE c_id = $course_id AND id = " . $new_item_id;
  592. Database::query($sql);
  593. // Upload audio.
  594. if (!empty($_FILES['mp3']['name'])) {
  595. // Create the audio folder if it does not exist yet.
  596. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  597. if (!is_dir($filepath . 'audio')) {
  598. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  599. $audio_id = add_document(
  600. $_course,
  601. '/audio',
  602. 'folder',
  603. 0,
  604. 'audio',
  605. '',
  606. 0,
  607. true,
  608. null,
  609. $sessionId,
  610. $userId
  611. );
  612. api_item_property_update(
  613. $_course,
  614. TOOL_DOCUMENT,
  615. $audio_id,
  616. 'FolderCreated',
  617. $userId,
  618. null,
  619. null,
  620. null,
  621. null,
  622. $sessionId
  623. );
  624. api_item_property_update(
  625. $_course,
  626. TOOL_DOCUMENT,
  627. $audio_id,
  628. 'invisible',
  629. $userId,
  630. null,
  631. null,
  632. null,
  633. null,
  634. $sessionId
  635. );
  636. }
  637. $file_path = handle_uploaded_document(
  638. $_course,
  639. $_FILES['mp3'],
  640. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  641. '/audio',
  642. $userId,
  643. '',
  644. '',
  645. '',
  646. '',
  647. false
  648. );
  649. // Getting the filename only.
  650. $file_components = explode('/', $file_path);
  651. $file = $file_components[count($file_components) - 1];
  652. // Store the mp3 file in the lp_item table.
  653. $sql = "UPDATE $tbl_lp_item SET
  654. audio = '" . Database::escape_string($file) . "'
  655. WHERE id = '" . intval($new_item_id) . "'";
  656. Database::query($sql);
  657. }
  658. }
  659. return $new_item_id;
  660. }
  661. /**
  662. * Static admin function allowing addition of a learnpath to a course.
  663. * @param string Course code
  664. * @param string Learnpath name
  665. * @param string Learnpath description string, if provided
  666. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  667. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  668. * @param string Zip file containing the learnpath or directory containing the learnpath
  669. * @return integer The new learnpath ID on success, 0 on failure
  670. */
  671. public static function add_lp(
  672. $courseCode,
  673. $name,
  674. $description = '',
  675. $learnpath = 'guess',
  676. $origin = 'zip',
  677. $zipname = '',
  678. $publicated_on = '',
  679. $expired_on = '',
  680. $categoryId = 0,
  681. $userId = 0
  682. ) {
  683. global $charset;
  684. if (!empty($courseCode)) {
  685. $courseInfo = api_get_course_info($courseCode);
  686. $course_id = $courseInfo['real_id'];
  687. } else {
  688. $course_id = api_get_course_int_id();
  689. $courseInfo = api_get_course_info();
  690. }
  691. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  692. // Check course code exists.
  693. // Check lp_name doesn't exist, otherwise append something.
  694. $i = 0;
  695. $name = Database::escape_string($name);
  696. $categoryId = intval($categoryId);
  697. // Session id.
  698. $session_id = api_get_session_id();
  699. $userId = empty($userId) ? api_get_user_id() : $userId;
  700. $check_name = "SELECT * FROM $tbl_lp
  701. WHERE c_id = $course_id AND name = '$name'";
  702. $res_name = Database::query($check_name);
  703. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  704. //by default the publication date is the same that the creation date
  705. //The behaviour above was changed due BT#2800
  706. global $_custom;
  707. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  708. $publicated_on = '';
  709. } else {
  710. $publicated_on = api_get_utc_datetime();
  711. }
  712. } else {
  713. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  714. }
  715. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  716. $expired_on = null;
  717. } else {
  718. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  719. }
  720. while (Database :: num_rows($res_name)) {
  721. // There is already one such name, update the current one a bit.
  722. $i++;
  723. $name = $name . ' - ' . $i;
  724. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  725. $res_name = Database::query($check_name);
  726. }
  727. // New name does not exist yet; keep it.
  728. // Escape description.
  729. // Kevin: added htmlentities().
  730. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  731. $type = 1;
  732. switch ($learnpath) {
  733. case 'guess':
  734. break;
  735. case 'dokeos':
  736. case 'chamilo':
  737. $type = 1;
  738. break;
  739. case 'aicc':
  740. break;
  741. }
  742. switch ($origin) {
  743. case 'zip':
  744. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  745. break;
  746. case 'manual':
  747. default:
  748. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  749. $res_max = Database::query($get_max);
  750. if (Database :: num_rows($res_max) < 1) {
  751. $dsp = 1;
  752. } else {
  753. $row = Database :: fetch_array($res_max);
  754. $dsp = $row[0] + 1;
  755. }
  756. $params = [
  757. 'c_id' => $course_id,
  758. 'lp_type' => $type,
  759. 'name' => $name,
  760. 'description' => $description,
  761. 'path' => '',
  762. 'default_view_mod' => 'embedded',
  763. 'default_encoding' => 'UTF-8',
  764. 'display_order' => $dsp,
  765. 'content_maker' => 'Chamilo',
  766. 'content_local' => 'local',
  767. 'js_lib' => '',
  768. 'session_id' => $session_id,
  769. 'created_on' => api_get_utc_datetime(),
  770. 'modified_on' => api_get_utc_datetime(),
  771. 'publicated_on' => $publicated_on,
  772. 'expired_on' => $expired_on,
  773. 'category_id' => $categoryId,
  774. 'force_commit' => 0,
  775. 'content_license' => '',
  776. 'debug' => 0,
  777. 'theme' => '',
  778. 'preview_image' => '',
  779. 'author' => '',
  780. 'prerequisite' => 0,
  781. 'hide_toc_frame' => 0,
  782. 'seriousgame_mode' => 0,
  783. 'autolaunch' => 0,
  784. 'max_attempts' => 0,
  785. 'subscribe_users' => 0
  786. ];
  787. $id = Database::insert($tbl_lp, $params);
  788. if ($id > 0) {
  789. $sql = "UPDATE $tbl_lp SET id = iid WHERE iid = $id";
  790. Database::query($sql);
  791. // Insert into item_property.
  792. api_item_property_update(
  793. $courseInfo,
  794. TOOL_LEARNPATH,
  795. $id,
  796. 'LearnpathAdded',
  797. $userId
  798. );
  799. api_set_default_visibility($id, TOOL_LEARNPATH, 0, $courseInfo, $session_id, $userId);
  800. return $id;
  801. }
  802. break;
  803. }
  804. }
  805. /**
  806. * Auto completes the parents of an item in case it's been completed or passed
  807. * @param integer $item Optional ID of the item from which to look for parents
  808. */
  809. public function autocomplete_parents($item)
  810. {
  811. $debug = $this->debug;
  812. if ($debug) {
  813. error_log('Learnpath::autocomplete_parents()', 0);
  814. }
  815. if (empty($item)) {
  816. $item = $this->current;
  817. }
  818. $currentItem = $this->getItem($item);
  819. if ($currentItem) {
  820. $parent_id = $currentItem->get_parent();
  821. $parent = $this->getItem($parent_id);
  822. if ($parent) {
  823. // if $item points to an object and there is a parent.
  824. if ($debug) {
  825. error_log(
  826. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  827. 0
  828. );
  829. }
  830. // New experiment including failed and browsed in completed status.
  831. //$current_status = $currentItem->get_status();
  832. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  833. // Fixes chapter auto complete
  834. if (true) {
  835. // If the current item is completed or passes or succeeded.
  836. $updateParentStatus = true;
  837. if ($debug) {
  838. error_log('Status of current item is alright', 0);
  839. }
  840. foreach ($parent->get_children() as $childItemId) {
  841. $childItem = $this->getItem($childItemId);
  842. // If children was not set try to get the info
  843. if (empty($childItem->db_item_view_id)) {
  844. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  845. }
  846. // Check all his brothers (parent's children) for completion status.
  847. if ($childItemId != $item) {
  848. if ($debug) {
  849. error_log(
  850. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  851. 0
  852. );
  853. }
  854. // Trying completing parents of failed and browsed items as well.
  855. if ($childItem->status_is(
  856. array(
  857. 'completed',
  858. 'passed',
  859. 'succeeded',
  860. 'browsed',
  861. 'failed'
  862. )
  863. )
  864. ) {
  865. // Keep completion status to true.
  866. continue;
  867. } else {
  868. if ($debug > 2) {
  869. error_log(
  870. '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,
  871. 0
  872. );
  873. }
  874. $updateParentStatus = false;
  875. break;
  876. }
  877. }
  878. }
  879. if ($updateParentStatus) {
  880. // If all the children were completed:
  881. $parent->set_status('completed');
  882. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  883. // Force the status to "completed"
  884. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  885. $this->update_queue[$parent->get_id()] = 'completed';
  886. if ($debug) {
  887. error_log(
  888. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  889. print_r($this->update_queue, 1),
  890. 0
  891. );
  892. }
  893. // Recursive call.
  894. $this->autocomplete_parents($parent->get_id());
  895. }
  896. }
  897. } else {
  898. if ($debug) {
  899. error_log("Parent #$parent_id does not exists");
  900. }
  901. }
  902. } else {
  903. if ($debug) {
  904. error_log("#$item is an item that doesn't have parents");
  905. }
  906. }
  907. }
  908. /**
  909. * Auto saves the current results into the database for the whole learnpath
  910. */
  911. public function autosave()
  912. {
  913. if ($this->debug > 0) {
  914. error_log('New LP - In learnpath::autosave()', 0);
  915. }
  916. // TODO: Add save operations for the learnpath itself.
  917. }
  918. /**
  919. * Closes the current resource
  920. *
  921. * Stops the timer
  922. * Saves into the database if required
  923. * Clears the current resource data from this object
  924. * @return boolean True on success, false on failure
  925. */
  926. public function close()
  927. {
  928. if ($this->debug > 0) {
  929. error_log('New LP - In learnpath::close()', 0);
  930. }
  931. if (empty ($this->lp_id)) {
  932. $this->error = 'Trying to close this learnpath but no ID is set';
  933. return false;
  934. }
  935. $this->current_time_stop = time();
  936. $this->ordered_items = array();
  937. $this->index = 0;
  938. unset ($this->lp_id);
  939. //unset other stuff
  940. return true;
  941. }
  942. /**
  943. * Static admin function allowing removal of a learnpath
  944. * @param array $courseInfo
  945. * @param integer Learnpath ID
  946. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  947. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  948. */
  949. public function delete($courseInfo = null, $id = null, $delete = 'keep')
  950. {
  951. $course_id = api_get_course_int_id();
  952. if (!empty($courseInfo)) {
  953. $course_id = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : $course_id;
  954. }
  955. // TODO: Implement a way of getting this to work when the current object is not set.
  956. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  957. // If an ID is specifically given and the current LP is not the same, prevent delete.
  958. if (!empty ($id) && ($id != $this->lp_id)) {
  959. return false;
  960. }
  961. $lp = Database:: get_course_table(TABLE_LP_MAIN);
  962. $lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  963. $lp_view = Database:: get_course_table(TABLE_LP_VIEW);
  964. $lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  965. // Delete lp item id.
  966. foreach ($this->items as $id => $dummy) {
  967. $sql = "DELETE FROM $lp_item_view
  968. WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  969. Database::query($sql);
  970. }
  971. // Proposed by Christophe (nickname: clefevre)
  972. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  973. Database::query($sql);
  974. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  975. Database::query($sql);
  976. self::toggle_publish($this->lp_id, 'i');
  977. if ($this->type == 2 || $this->type == 3) {
  978. // This is a scorm learning path, delete the files as well.
  979. $sql = "SELECT path FROM $lp
  980. WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  981. $res = Database::query($sql);
  982. if (Database :: num_rows($res) > 0) {
  983. $row = Database :: fetch_array($res);
  984. $path = $row['path'];
  985. $sql = "SELECT id FROM $lp
  986. WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  987. $res = Database::query($sql);
  988. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  989. if ($this->debug > 2) {
  990. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  991. }
  992. } else {
  993. // No other LP uses that directory, delete it.
  994. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  995. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  996. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  997. if ($this->debug > 2) {
  998. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  999. }
  1000. // Proposed by Christophe (clefevre).
  1001. if (strcmp(substr($path, -2), "/.") == 0) {
  1002. $path = substr($path, 0, -1); // Remove "." at the end.
  1003. }
  1004. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  1005. rmdirr($course_scorm_dir . $path);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  1011. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  1012. // Delete tools
  1013. $sql = "DELETE FROM $tbl_tool
  1014. WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  1015. Database::query($sql);
  1016. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  1017. Database::query($sql);
  1018. // Updates the display order of all lps.
  1019. $this->update_display_order();
  1020. api_item_property_update(
  1021. api_get_course_info(),
  1022. TOOL_LEARNPATH,
  1023. $this->lp_id,
  1024. 'delete',
  1025. api_get_user_id()
  1026. );
  1027. $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  1028. if ($link_info !== false) {
  1029. GradebookUtils::remove_resource_from_course_gradebook($link_info->getId());
  1030. }
  1031. if (api_get_setting('search.search_enabled') == 'true') {
  1032. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1033. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  1034. }
  1035. }
  1036. /**
  1037. * Removes all the children of one item - dangerous!
  1038. * @param integer $id Element ID of which children have to be removed
  1039. * @return integer Total number of children removed
  1040. */
  1041. public function delete_children_items($id)
  1042. {
  1043. $course_id = $this->course_info['real_id'];
  1044. if ($this->debug > 0) {
  1045. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  1046. }
  1047. $num = 0;
  1048. if (empty ($id) || $id != strval(intval($id))) {
  1049. return false;
  1050. }
  1051. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1052. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1053. $res = Database::query($sql);
  1054. while ($row = Database :: fetch_array($res)) {
  1055. $num += $this->delete_children_items($row['id']);
  1056. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1057. Database::query($sql_del);
  1058. $num++;
  1059. }
  1060. return $num;
  1061. }
  1062. /**
  1063. * Removes an item from the current learnpath
  1064. * @param integer $id Elem ID (0 if first)
  1065. * @param integer $remove Whether to remove the resource/data from the
  1066. * system or leave it (default: 'keep', others 'remove')
  1067. * @return integer Number of elements moved
  1068. * @todo implement resource removal
  1069. */
  1070. public function delete_item($id, $remove = 'keep')
  1071. {
  1072. $em = Database::getManager();
  1073. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  1074. if ($this->debug > 0) {
  1075. error_log('New LP - In learnpath::delete_item()', 0);
  1076. }
  1077. // TODO: Implement the resource removal.
  1078. if (empty ($id) || $id != strval(intval($id))) {
  1079. return false;
  1080. }
  1081. // First select item to get previous, next, and display order.
  1082. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1083. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = " . $course->getId() . " AND id = $id";
  1084. $res_sel = Database::query($sql_sel);
  1085. if (Database :: num_rows($res_sel) < 1) {
  1086. return false;
  1087. }
  1088. $row = Database :: fetch_array($res_sel);
  1089. $previous = $row['previous_item_id'];
  1090. $next = $row['next_item_id'];
  1091. $display = $row['display_order'];
  1092. $parent = $row['parent_item_id'];
  1093. $lp = $row['lp_id'];
  1094. // Delete children items.
  1095. $num = $this->delete_children_items($id);
  1096. if ($this->debug > 2) {
  1097. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1098. }
  1099. // Now delete the item.
  1100. $sql_del = "DELETE FROM $lp_item WHERE c_id = {$course->getId()} AND id = $id";
  1101. if ($this->debug > 2) {
  1102. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1103. }
  1104. Database::query($sql_del);
  1105. // Now update surrounding items.
  1106. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1107. WHERE c_id = " . $course->getId() . " AND id = $previous";
  1108. Database::query($sql_upd);
  1109. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1110. WHERE c_id = " . $course->getId() . " AND id = $next";
  1111. Database::query($sql_upd);
  1112. // Now update all following items with new display order.
  1113. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1114. WHERE c_id = " . $course->getId() . " AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1115. Database::query($sql_all);
  1116. //Removing prerequisites since the item will not longer exist
  1117. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = " . $course->getId() . " AND prerequisite = $id";
  1118. Database::query($sql_all);
  1119. // Remove from search engine if enabled.
  1120. if (api_get_setting('search.search_enabled') == 'true') {
  1121. $lpCourse = $em->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $this->cc]);
  1122. $row2 = $em
  1123. ->getRepository('ChamiloCoreBundle:SearchEngineRef')
  1124. ->findOneBy([
  1125. 'course' => $lpCourse,
  1126. 'toolId' => TOOL_LEARNPATH,
  1127. 'refIdHighLevel' => $lp,
  1128. 'refIdSecondLevel' => $id
  1129. ]);
  1130. if ($row2) {
  1131. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1132. $di = new ChamiloIndexer();
  1133. $di->remove_document($row2->getSearchDid());
  1134. }
  1135. $em->createQuery('
  1136. DELETE FROM ChamiloCoreBundle:SearchEngineRef ser
  1137. WHERE ser.course = ?1 AND ser.toolId = ?2 AND ser.refIdHighLevel = ?3 AND ser.refIdSecondLevel = ?4
  1138. ')
  1139. ->setMaxResults(1)
  1140. ->execute([1 => $lpCourse, 2 => TOOL_LEARNPATH, 3 => $lp, 4=> $id]);
  1141. }
  1142. }
  1143. /**
  1144. * Updates an item's content in place
  1145. * @param integer $id Element ID
  1146. * @param integer $parent Parent item ID
  1147. * @param integer $previous Previous item ID
  1148. * @param string $title Item title
  1149. * @param string $description Item description
  1150. * @param string $prerequisites Prerequisites (optional)
  1151. * @param array $audio The array resulting of the $_FILES[mp3] element
  1152. * @param int $max_time_allowed
  1153. * @param string $url
  1154. * @return boolean True on success, false on error
  1155. */
  1156. public function edit_item(
  1157. $id,
  1158. $parent,
  1159. $previous,
  1160. $title,
  1161. $description,
  1162. $prerequisites = '0',
  1163. $audio = array(),
  1164. $max_time_allowed = 0,
  1165. $url = ''
  1166. ) {
  1167. $course_id = api_get_course_int_id();
  1168. $_course = api_get_course_info();
  1169. if ($this->debug > 0) {
  1170. error_log('New LP - In learnpath::edit_item()', 0);
  1171. }
  1172. if (empty ($max_time_allowed)) {
  1173. $max_time_allowed = 0;
  1174. }
  1175. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1176. return false;
  1177. }
  1178. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1179. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1180. $res_select = Database::query($sql_select);
  1181. $row_select = Database :: fetch_array($res_select);
  1182. $audio_update_sql = '';
  1183. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1184. // Create the audio folder if it does not exist yet.
  1185. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1186. if (!is_dir($filepath . 'audio')) {
  1187. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1188. $audio_id = add_document(
  1189. $_course,
  1190. '/audio',
  1191. 'folder',
  1192. 0,
  1193. 'audio'
  1194. );
  1195. api_item_property_update(
  1196. $_course,
  1197. TOOL_DOCUMENT,
  1198. $audio_id,
  1199. 'FolderCreated',
  1200. api_get_user_id(),
  1201. null,
  1202. null,
  1203. null,
  1204. null,
  1205. api_get_session_id()
  1206. );
  1207. api_item_property_update(
  1208. $_course,
  1209. TOOL_DOCUMENT,
  1210. $audio_id,
  1211. 'invisible',
  1212. api_get_user_id(),
  1213. null,
  1214. null,
  1215. null,
  1216. null,
  1217. api_get_session_id()
  1218. );
  1219. }
  1220. // Upload file in documents.
  1221. $pi = pathinfo($audio['name']);
  1222. if ($pi['extension'] == 'mp3') {
  1223. $c_det = api_get_course_info($this->cc);
  1224. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1225. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1226. $path = substr($path, 7);
  1227. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1228. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1229. }
  1230. }
  1231. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1232. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1233. // TODO: htmlspecialchars to be checked for encoding related problems.
  1234. if ($same_parent && $same_previous) {
  1235. // Only update title and description.
  1236. $sql = "UPDATE " . $tbl_lp_item . "
  1237. SET title = '" . Database::escape_string($title) . "',
  1238. prerequisite = '" . $prerequisites . "',
  1239. description = '" . Database::escape_string($description) . "'
  1240. " . $audio_update_sql . ",
  1241. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1242. WHERE c_id = ".$course_id." AND id = " . $id;
  1243. Database::query($sql);
  1244. } else {
  1245. $old_parent = $row_select['parent_item_id'];
  1246. $old_previous = $row_select['previous_item_id'];
  1247. $old_next = $row_select['next_item_id'];
  1248. $old_order = $row_select['display_order'];
  1249. $old_prerequisite = $row_select['prerequisite'];
  1250. $old_max_time_allowed = $row_select['max_time_allowed'];
  1251. /* BEGIN -- virtually remove the current item id */
  1252. /* for the next and previous item it is like the current item doesn't exist anymore */
  1253. if ($old_previous != 0) {
  1254. // Next
  1255. $sql = "UPDATE " . $tbl_lp_item . "
  1256. SET next_item_id = " . $old_next . "
  1257. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1258. Database::query($sql);
  1259. }
  1260. if ($old_next != 0) {
  1261. // Previous
  1262. $sql = "UPDATE " . $tbl_lp_item . "
  1263. SET previous_item_id = " . $old_previous . "
  1264. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1265. Database::query($sql);
  1266. }
  1267. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1268. $sql = "UPDATE " . $tbl_lp_item . "
  1269. SET display_order = display_order - 1
  1270. WHERE
  1271. c_id = ".$course_id." AND
  1272. display_order > " . $old_order . " AND
  1273. lp_id = " . $this->lp_id . " AND
  1274. parent_item_id = " . $old_parent;
  1275. Database::query($sql);
  1276. /* END -- virtually remove the current item id */
  1277. /* BEGIN -- update the current item id to his new location */
  1278. if ($previous == 0) {
  1279. // Select the data of the item that should come after the current item.
  1280. $sql = "SELECT id, display_order
  1281. FROM " . $tbl_lp_item . "
  1282. WHERE
  1283. c_id = ".$course_id." AND
  1284. lp_id = " . $this->lp_id . " AND
  1285. parent_item_id = " . $parent . " AND
  1286. previous_item_id = " . $previous;
  1287. $res_select_old = Database::query($sql);
  1288. $row_select_old = Database::fetch_array($res_select_old);
  1289. // If the new parent didn't have children before.
  1290. if (Database :: num_rows($res_select_old) == 0) {
  1291. $new_next = 0;
  1292. $new_order = 1;
  1293. } else {
  1294. $new_next = $row_select_old['id'];
  1295. $new_order = $row_select_old['display_order'];
  1296. }
  1297. } else {
  1298. // Select the data of the item that should come before the current item.
  1299. $sql = "SELECT next_item_id, display_order
  1300. FROM " . $tbl_lp_item . "
  1301. WHERE c_id = ".$course_id." AND id = " . $previous;
  1302. $res_select_old = Database::query($sql);
  1303. $row_select_old = Database :: fetch_array($res_select_old);
  1304. $new_next = $row_select_old['next_item_id'];
  1305. $new_order = $row_select_old['display_order'] + 1;
  1306. }
  1307. // TODO: htmlspecialchars to be checked for encoding related problems.
  1308. // Update the current item with the new data.
  1309. $sql = "UPDATE " . $tbl_lp_item . "
  1310. SET
  1311. title = '" . Database::escape_string($title) . "',
  1312. description = '" . Database::escape_string($description) . "',
  1313. parent_item_id = " . $parent . ",
  1314. previous_item_id = " . $previous . ",
  1315. next_item_id = " . $new_next . ",
  1316. display_order = " . $new_order . "
  1317. " . $audio_update_sql . "
  1318. WHERE c_id = ".$course_id." AND id = " . $id;
  1319. Database::query($sql);
  1320. if ($previous != 0) {
  1321. // Update the previous item's next_item_id.
  1322. $sql = "UPDATE " . $tbl_lp_item . "
  1323. SET next_item_id = " . $id . "
  1324. WHERE c_id = ".$course_id." AND id = " . $previous;
  1325. Database::query($sql);
  1326. }
  1327. if ($new_next != 0) {
  1328. // Update the next item's previous_item_id.
  1329. $sql = "UPDATE " . $tbl_lp_item . "
  1330. SET previous_item_id = " . $id . "
  1331. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1332. Database::query($sql);
  1333. }
  1334. if ($old_prerequisite != $prerequisites) {
  1335. $sql = "UPDATE " . $tbl_lp_item . "
  1336. SET prerequisite = '" . $prerequisites . "'
  1337. WHERE c_id = ".$course_id." AND id = " . $id;
  1338. Database::query($sql);
  1339. }
  1340. if ($old_max_time_allowed != $max_time_allowed) {
  1341. // update max time allowed
  1342. $sql = "UPDATE " . $tbl_lp_item . "
  1343. SET max_time_allowed = " . $max_time_allowed . "
  1344. WHERE c_id = ".$course_id." AND id = " . $id;
  1345. Database::query($sql);
  1346. }
  1347. // Update all the items with the same or a bigger display_order than the current item.
  1348. $sql = "UPDATE " . $tbl_lp_item . "
  1349. SET display_order = display_order + 1
  1350. WHERE
  1351. c_id = ".$course_id." AND
  1352. lp_id = " . $this->get_id() . " AND
  1353. id <> " . $id . " AND
  1354. parent_item_id = " . $parent . " AND
  1355. display_order >= " . $new_order;
  1356. Database::query($sql);
  1357. }
  1358. if ($row_select['item_type'] == 'link') {
  1359. $link = new Link();
  1360. $linkId = $row_select['path'];
  1361. $link->updateLink($linkId, $url);
  1362. }
  1363. }
  1364. /**
  1365. * Updates an item's prereq in place
  1366. * @param integer $id Element ID
  1367. * @param string $prerequisite_id Prerequisite Element ID
  1368. * @param int $mastery_score Prerequisite min score
  1369. * @param int $max_score Prerequisite max score
  1370. *
  1371. * @return boolean True on success, false on error
  1372. */
  1373. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1374. {
  1375. $course_id = api_get_course_int_id();
  1376. if ($this->debug > 0) {
  1377. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1378. }
  1379. if (empty($id) || ($id != strval(intval($id))) || empty ($prerequisite_id)) {
  1380. return false;
  1381. }
  1382. $prerequisite_id = intval($prerequisite_id);
  1383. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1384. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1385. $mastery_score = 0;
  1386. }
  1387. if (!is_numeric($max_score) || $max_score < 0) {
  1388. $max_score = 100;
  1389. }
  1390. /*if ($mastery_score > $max_score) {
  1391. $max_score = $mastery_score;
  1392. }*/
  1393. if (!is_numeric($prerequisite_id)) {
  1394. $prerequisite_id = 'NULL';
  1395. }
  1396. $mastery_score = floatval($mastery_score);
  1397. $max_score = floatval($max_score);
  1398. $sql = " UPDATE $tbl_lp_item
  1399. SET
  1400. prerequisite = $prerequisite_id ,
  1401. prerequisite_min_score = $mastery_score ,
  1402. prerequisite_max_score = $max_score
  1403. WHERE c_id = $course_id AND id = $id";
  1404. Database::query($sql);
  1405. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1406. // Will this be enough to ensure unicity?
  1407. /*$sql = " UPDATE $tbl_lp_item
  1408. SET mastery_score = $mastery_score
  1409. WHERE c_id = $course_id AND ref = '$prerequisite_id'";
  1410. Database::query($sql);*/
  1411. }
  1412. // TODO: Update the item object (can be ignored for now because refreshed).
  1413. return true;
  1414. }
  1415. /**
  1416. * Escapes a string with the available database escape function
  1417. * @param string String to escape
  1418. * @return string String escaped
  1419. * @deprecated use Database::escape_string
  1420. */
  1421. public function escape_string($string)
  1422. {
  1423. return Database::escape_string($string);
  1424. }
  1425. /**
  1426. * Static admin function exporting a learnpath into a zip file
  1427. * @param string Export type (scorm, zip, cd)
  1428. * @param string Course code
  1429. * @param integer Learnpath ID
  1430. * @param string Zip file name
  1431. * @return string Zip file path (or false on error)
  1432. */
  1433. public function export_lp($type, $course, $id, $zipname)
  1434. {
  1435. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1436. return false;
  1437. }
  1438. $url = '';
  1439. switch ($type) {
  1440. case 'scorm':
  1441. break;
  1442. case 'zip':
  1443. break;
  1444. case 'cdrom':
  1445. break;
  1446. }
  1447. return $url;
  1448. }
  1449. /**
  1450. * Gets all the chapters belonging to the same parent as the item/chapter given
  1451. * Can also be called as abstract method
  1452. * @param integer Item ID
  1453. * @return array A list of all the "brother items" (or an empty array on failure)
  1454. */
  1455. public function get_brother_chapters($id)
  1456. {
  1457. $course_id = api_get_course_int_id();
  1458. if ($this->debug > 0) {
  1459. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1460. }
  1461. if (empty($id)|| $id != strval(intval($id))) {
  1462. return array ();
  1463. }
  1464. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1465. $sql_parent = "SELECT * FROM $lp_item
  1466. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1467. $res_parent = Database::query($sql_parent);
  1468. if (Database :: num_rows($res_parent) > 0) {
  1469. $row_parent = Database :: fetch_array($res_parent);
  1470. $parent = $row_parent['parent_item_id'];
  1471. $sql_bros = "SELECT * FROM $lp_item
  1472. WHERE
  1473. c_id = ".$course_id." AND
  1474. parent_item_id = $parent AND
  1475. id = $id AND
  1476. item_type='dokeos_chapter'
  1477. ORDER BY display_order";
  1478. $res_bros = Database::query($sql_bros);
  1479. $list = array ();
  1480. while ($row_bro = Database :: fetch_array($res_bros)) {
  1481. $list[] = $row_bro;
  1482. }
  1483. return $list;
  1484. }
  1485. return array ();
  1486. }
  1487. /**
  1488. * Gets all the items belonging to the same parent as the item given
  1489. * Can also be called as abstract method
  1490. * @param integer Item ID
  1491. * @return array A list of all the "brother items" (or an empty array on failure)
  1492. */
  1493. public function get_brother_items($id)
  1494. {
  1495. $course_id = api_get_course_int_id();
  1496. if ($this->debug > 0) {
  1497. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1498. }
  1499. if (empty ($id) || $id != strval(intval($id))) {
  1500. return array ();
  1501. }
  1502. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1503. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1504. $res_parent = Database::query($sql_parent);
  1505. if (Database :: num_rows($res_parent) > 0) {
  1506. $row_parent = Database :: fetch_array($res_parent);
  1507. $parent = $row_parent['parent_item_id'];
  1508. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1509. ORDER BY display_order";
  1510. $res_bros = Database::query($sql_bros);
  1511. $list = array ();
  1512. while ($row_bro = Database :: fetch_array($res_bros)) {
  1513. $list[] = $row_bro;
  1514. }
  1515. return $list;
  1516. }
  1517. return array ();
  1518. }
  1519. /**
  1520. * Get the specific prefix index terms of this learning path
  1521. * @param string $prefix
  1522. * @return array Array of terms
  1523. */
  1524. public function get_common_index_terms_by_prefix($prefix)
  1525. {
  1526. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1527. $terms = get_specific_field_values_list_by_prefix(
  1528. $prefix,
  1529. $this->cc,
  1530. TOOL_LEARNPATH,
  1531. $this->lp_id
  1532. );
  1533. $prefix_terms = array();
  1534. if (!empty($terms)) {
  1535. foreach ($terms as $term) {
  1536. $prefix_terms[] = $term['value'];
  1537. }
  1538. }
  1539. return $prefix_terms;
  1540. }
  1541. /**
  1542. * Gets the number of items currently completed
  1543. * @return integer The number of items currently completed
  1544. */
  1545. public function get_complete_items_count()
  1546. {
  1547. if ($this->debug > 0) {
  1548. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1549. }
  1550. $i = 0;
  1551. $completedStatusList = array(
  1552. 'completed',
  1553. 'passed',
  1554. 'succeeded',
  1555. 'browsed',
  1556. 'failed'
  1557. );
  1558. foreach ($this->items as $id => $dummy) {
  1559. // Trying failed and browsed considered "progressed" as well.
  1560. if ($this->items[$id]->status_is($completedStatusList) &&
  1561. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1562. $this->items[$id]->get_type() != 'dir'
  1563. ) {
  1564. $i++;
  1565. }
  1566. }
  1567. return $i;
  1568. }
  1569. /**
  1570. * Gets the current item ID
  1571. * @return integer The current learnpath item id
  1572. */
  1573. public function get_current_item_id()
  1574. {
  1575. $current = 0;
  1576. if ($this->debug > 0) {
  1577. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1578. }
  1579. if (!empty($this->current)) {
  1580. $current = $this->current;
  1581. }
  1582. if ($this->debug > 2) {
  1583. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1584. }
  1585. return $current;
  1586. }
  1587. /**
  1588. * Force to get the first learnpath item id
  1589. * @return integer The current learnpath item id
  1590. */
  1591. public function get_first_item_id()
  1592. {
  1593. $current = 0;
  1594. if (is_array($this->ordered_items)) {
  1595. $current = $this->ordered_items[0];
  1596. }
  1597. return $current;
  1598. }
  1599. /**
  1600. * Gets the total number of items available for viewing in this SCORM
  1601. * @return integer The total number of items
  1602. */
  1603. public function get_total_items_count()
  1604. {
  1605. if ($this->debug > 0) {
  1606. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1607. }
  1608. return count($this->items);
  1609. }
  1610. /**
  1611. * Gets the total number of items available for viewing in this SCORM but without chapters
  1612. * @return integer The total no-chapters number of items
  1613. */
  1614. public function get_total_items_count_without_chapters()
  1615. {
  1616. if ($this->debug > 0) {
  1617. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1618. }
  1619. $total = 0;
  1620. $typeListNotToCount = self::getChapterTypes();
  1621. foreach ($this->items as $temp2) {
  1622. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1623. $total++;
  1624. }
  1625. }
  1626. return $total;
  1627. }
  1628. /**
  1629. * Gets the first element URL.
  1630. * @return string URL to load into the viewer
  1631. */
  1632. public function first()
  1633. {
  1634. if ($this->debug > 0) {
  1635. error_log('New LP - In learnpath::first()', 0);
  1636. error_log('$this->last_item_seen '.$this->last_item_seen);
  1637. }
  1638. // Test if the last_item_seen exists and is not a dir.
  1639. if (count($this->ordered_items) == 0) {
  1640. $this->index = 0;
  1641. }
  1642. if ($this->debug > 0) {
  1643. if (isset($this->items[$this->last_item_seen])) {
  1644. $status = $this->items[$this->last_item_seen]->get_status();
  1645. }
  1646. error_log('status '.$status);
  1647. }
  1648. if (!empty($this->last_item_seen) &&
  1649. !empty($this->items[$this->last_item_seen]) &&
  1650. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1651. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1652. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1653. //&& !$this->items[$this->last_item_seen]->is_done()
  1654. ) {
  1655. if ($this->debug > 2) {
  1656. 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);
  1657. }
  1658. $index = -1;
  1659. foreach ($this->ordered_items as $myindex => $item_id) {
  1660. if ($item_id == $this->last_item_seen) {
  1661. $index = $myindex;
  1662. break;
  1663. }
  1664. }
  1665. if ($index == -1) {
  1666. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1667. if ($this->debug > 2) {
  1668. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1669. }
  1670. return false;
  1671. } else {
  1672. $this->last = $this->last_item_seen;
  1673. $this->current = $this->last_item_seen;
  1674. $this->index = $index;
  1675. }
  1676. } else {
  1677. if ($this->debug > 2) {
  1678. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1679. }
  1680. $index = 0;
  1681. // Loop through all ordered items and stop at the first item that is
  1682. // not a directory *and* that has not been completed yet.
  1683. while ( !empty($this->ordered_items[$index]) AND
  1684. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1685. (
  1686. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1687. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1688. $this->items[$this->ordered_items[$index]]->is_done() === true
  1689. ) AND $index < $this->max_ordered_items) {
  1690. $index++;
  1691. }
  1692. $this->last = $this->current;
  1693. // current is
  1694. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1695. $this->index = $index;
  1696. if ($this->debug > 2) {
  1697. error_log('$index ' . $index);
  1698. }
  1699. if ($this->debug > 2) {
  1700. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1701. }
  1702. }
  1703. if ($this->debug > 2) {
  1704. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1705. }
  1706. }
  1707. /**
  1708. * Gets the information about an item in a format usable as JavaScript to update
  1709. * the JS API by just printing this content into the <head> section of the message frame
  1710. * @param int $item_id
  1711. * @return string
  1712. */
  1713. public function get_js_info($item_id = '')
  1714. {
  1715. if ($this->debug > 0) {
  1716. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1717. }
  1718. $info = '';
  1719. $item_id = intval($item_id);
  1720. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1721. //if item is defined, return values from DB
  1722. $oItem = $this->items[$item_id];
  1723. $info .= '<script language="javascript">';
  1724. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1725. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1726. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1727. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1728. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1729. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1730. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1731. $info .= "top.set_flag_synchronized();";
  1732. $info .= '</script>';
  1733. if ($this->debug > 2) {
  1734. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1735. }
  1736. return $info;
  1737. } else {
  1738. // If item_id is empty, just update to default SCORM data.
  1739. $info .= '<script language="javascript">';
  1740. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1741. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1742. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1743. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1744. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1745. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1746. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1747. $info .= "top.set_flag_synchronized();";
  1748. $info .= '</script>';
  1749. if ($this->debug > 2) {
  1750. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1751. }
  1752. return $info;
  1753. }
  1754. }
  1755. /**
  1756. * Gets the js library from the database
  1757. * @return string The name of the javascript library to be used
  1758. */
  1759. public function get_js_lib()
  1760. {
  1761. $lib = '';
  1762. if (!empty ($this->js_lib)) {
  1763. $lib = $this->js_lib;
  1764. }
  1765. return $lib;
  1766. }
  1767. /**
  1768. * Gets the learnpath database ID
  1769. * @return integer Learnpath ID in the lp table
  1770. */
  1771. public function get_id()
  1772. {
  1773. if (!empty ($this->lp_id)) {
  1774. return $this->lp_id;
  1775. } else {
  1776. return 0;
  1777. }
  1778. }
  1779. /**
  1780. * Gets the last element URL.
  1781. * @return string URL to load into the viewer
  1782. */
  1783. public function get_last()
  1784. {
  1785. if ($this->debug > 0) {
  1786. error_log('New LP - In learnpath::get_last()', 0);
  1787. }
  1788. //This is just in case the lesson doesn't cointain a valid scheme, just to avoid "Notices"
  1789. if (count($this->ordered_items) > 0) {
  1790. $this->index = count($this->ordered_items) - 1;
  1791. return $this->ordered_items[$this->index];
  1792. }
  1793. return false;
  1794. }
  1795. /**
  1796. * Gets the navigation bar for the learnpath display screen
  1797. * @return string The HTML string to use as a navigation bar
  1798. */
  1799. public function get_navigation_bar($idBar = null, $display = null)
  1800. {
  1801. if ($this->debug > 0) {
  1802. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1803. }
  1804. if(empty($idBar)){
  1805. $idBar='control-top';
  1806. }
  1807. /* if(empty($display)){
  1808. $display='display:block';
  1809. } */
  1810. $navbar = null;
  1811. $lp_id = $this->lp_id;
  1812. $mycurrentitemid = $this->get_current_item_id();
  1813. if ($this->mode == 'fullscreen') {
  1814. $navbar = '
  1815. <span id="'.$idBar.'" class="buttons">
  1816. <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="stats" id="stats_link">
  1817. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1818. </a>
  1819. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1820. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1821. </a>
  1822. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1823. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1824. </a>
  1825. <a class="icon-toolbar" id="view-embedded" href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode">
  1826. <span class="fa fa-columns"></span><span class="sr-only">' . get_lang('ScormExitFullScreen') . '</span>
  1827. </a>
  1828. </span>';
  1829. } else {
  1830. $navbar = '
  1831. <span id="'.$idBar.'" class="buttons text-right">
  1832. <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="stats" id="stats_link">
  1833. <span class="fa fa-info"></span><span class="sr-only">' . get_lang('Reporting') . '</span>
  1834. </a>
  1835. <a class="icon-toolbar" id="scorm-previous" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous">
  1836. <span class="fa fa-chevron-left"></span><span class="sr-only">' . get_lang('ScormPrevious') . '</span>
  1837. </a>
  1838. <a class="icon-toolbar" id="scorm-next" href="#" onclick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next">
  1839. <span class="fa fa-chevron-right"></span><span class="sr-only">' . get_lang('ScormNext') . '</span>
  1840. </a>
  1841. </span>';
  1842. }
  1843. return $navbar;
  1844. }
  1845. /**
  1846. * Gets the next resource in queue (url).
  1847. * @return string URL to load into the viewer
  1848. */
  1849. public function get_next_index()
  1850. {
  1851. if ($this->debug > 0) {
  1852. error_log('New LP - In learnpath::get_next_index()', 0);
  1853. }
  1854. // TODO
  1855. $index = $this->index;
  1856. $index++;
  1857. if ($this->debug > 2) {
  1858. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1859. }
  1860. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1861. $index++;
  1862. if ($index == $this->max_ordered_items){
  1863. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1864. return $this->index;
  1865. } else {
  1866. return $index;
  1867. }
  1868. }
  1869. }
  1870. if (empty ($this->ordered_items[$index])) {
  1871. return $this->index;
  1872. }
  1873. if ($this->debug > 2) {
  1874. error_log('New LP - index is now ' . $index, 0);
  1875. }
  1876. return $index;
  1877. }
  1878. /**
  1879. * Gets item_id for the next element
  1880. * @return integer Next item (DB) ID
  1881. */
  1882. public function get_next_item_id()
  1883. {
  1884. if ($this->debug > 0) {
  1885. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1886. }
  1887. $new_index = $this->get_next_index();
  1888. if (!empty ($new_index)) {
  1889. if (isset ($this->ordered_items[$new_index])) {
  1890. if ($this->debug > 2) {
  1891. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1892. }
  1893. return $this->ordered_items[$new_index];
  1894. }
  1895. }
  1896. if ($this->debug > 2) {
  1897. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1898. }
  1899. return 0;
  1900. }
  1901. /**
  1902. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1903. *
  1904. * Generally, the package provided is in the form of a zip file, so the function
  1905. * has been written to test a zip file. If not a zip, the function will return the
  1906. * default return value: ''
  1907. * @param string the path to the file
  1908. * @param string the original name of the file
  1909. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1910. */
  1911. public static function get_package_type($file_path, $file_name)
  1912. {
  1913. // Get name of the zip file without the extension.
  1914. $file_info = pathinfo($file_name);
  1915. $filename = $file_info['basename']; // Name including extension.
  1916. $extension = $file_info['extension']; // Extension only.
  1917. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1918. 'dll',
  1919. 'exe'
  1920. ))) {
  1921. return 'oogie';
  1922. }
  1923. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1924. 'dll',
  1925. 'exe'
  1926. ))) {
  1927. return 'woogie';
  1928. }
  1929. // Filename without its extension.
  1930. $file_base_name = str_replace('.' . $extension, '', $filename);
  1931. $zipFile = new PclZip($file_path);
  1932. // Check the zip content (real size and file extension).
  1933. $zipContentArray = $zipFile->listContent();
  1934. $package_type = '';
  1935. $at_root = false;
  1936. $manifest = '';
  1937. $aicc_match_crs = 0;
  1938. $aicc_match_au = 0;
  1939. $aicc_match_des = 0;
  1940. $aicc_match_cst = 0;
  1941. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1942. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1943. foreach ($zipContentArray as $thisContent) {
  1944. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1945. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1946. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1947. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1948. $package_type = 'scorm';
  1949. break; // Exit the foreach loop.
  1950. } elseif (
  1951. preg_match('/aicc\//i', $thisContent['filename']) ||
  1952. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1953. ) {
  1954. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1955. switch ($ext) {
  1956. case 'crs':
  1957. $aicc_match_crs = 1;
  1958. break;
  1959. case 'au':
  1960. $aicc_match_au = 1;
  1961. break;
  1962. case 'des':
  1963. $aicc_match_des = 1;
  1964. break;
  1965. case 'cst':
  1966. $aicc_match_cst = 1;
  1967. break;
  1968. default:
  1969. break;
  1970. }
  1971. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1972. } else {
  1973. $package_type = '';
  1974. }
  1975. }
  1976. }
  1977. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1978. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1979. $package_type = 'aicc';
  1980. }
  1981. return $package_type;
  1982. }
  1983. /**
  1984. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1985. * @return string URL to load into the viewer
  1986. */
  1987. public function get_previous_index()
  1988. {
  1989. if ($this->debug > 0) {
  1990. error_log('New LP - In learnpath::get_previous_index()', 0);
  1991. }
  1992. $index = $this->index;
  1993. if (isset ($this->ordered_items[$index -1])) {
  1994. $index--;
  1995. while (isset($this->ordered_items[$index]) && ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1996. $index--;
  1997. if ($index < 0) {
  1998. return $this->index;
  1999. }
  2000. }
  2001. } else {
  2002. if ($this->debug > 2) {
  2003. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  2004. }
  2005. // There is no previous item.
  2006. }
  2007. return $index;
  2008. }
  2009. /**
  2010. * Gets item_id for the next element
  2011. * @return integer Previous item (DB) ID
  2012. */
  2013. public function get_previous_item_id()
  2014. {
  2015. if ($this->debug > 0) {
  2016. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  2017. }
  2018. $new_index = $this->get_previous_index();
  2019. return $this->ordered_items[$new_index];
  2020. }
  2021. /**
  2022. * Gets the progress value from the progress_db attribute
  2023. * @return integer Current progress value
  2024. */
  2025. public function get_progress()
  2026. {
  2027. if ($this->debug > 0) {
  2028. error_log('New LP - In learnpath::get_progress()', 0);
  2029. }
  2030. if (!empty ($this->progress_db)) {
  2031. return $this->progress_db;
  2032. }
  2033. return 0;
  2034. }
  2035. /**
  2036. * @return learnpath
  2037. */
  2038. public static function getCurrentLpFromSession()
  2039. {
  2040. return Session::read('oLP');
  2041. }
  2042. /**
  2043. * Update oLP session value
  2044. */
  2045. public function updateCurrentLpFromSession()
  2046. {
  2047. Session::write('oLP', $this);
  2048. }
  2049. /**
  2050. * Returns the HTML necessary to print a mediaplayer block inside a page
  2051. * @return string The mediaplayer HTML
  2052. */
  2053. public function get_mediaplayer($autostart = 'true')
  2054. {
  2055. $learnPath = self::getCurrentLpFromSession();
  2056. $course_id = api_get_course_int_id();
  2057. $_course = api_get_course_info();
  2058. $tbl_lp_item = Database:: get_course_table(TABLE_LP_ITEM);
  2059. $tbl_lp_item_view = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  2060. // Getting all the information about the item.
  2061. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2062. INNER JOIN ".$tbl_lp_item_view." as lp_view
  2063. ON lp.id = lp_view.lp_item_id
  2064. WHERE
  2065. lp.id = '".$learnPath->current."' AND
  2066. lp.c_id = $course_id AND
  2067. lp_view.c_id = $course_id";
  2068. $result = Database::query($sql);
  2069. $row = Database::fetch_assoc($result);
  2070. $output = '';
  2071. if (!empty ($row['audio'])) {
  2072. $list = $learnPath->get_toc();
  2073. $type_quiz = false;
  2074. foreach($list as $toc) {
  2075. if ($toc['id'] == $learnPath->current && ($toc['type'] == 'quiz')) {
  2076. $type_quiz = true;
  2077. }
  2078. }
  2079. if ($type_quiz) {
  2080. if ($learnPath->prevent_reinit == 1) {
  2081. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2082. } else {
  2083. $autostart_audio = $autostart;
  2084. }
  2085. } else {
  2086. $autostart_audio = 'true';
  2087. }
  2088. $courseInfo = api_get_course_info();
  2089. $audio = $row['audio'];
  2090. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2091. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2092. if (!file_exists($file)) {
  2093. $lpPathInfo = $learnPath->generate_lp_folder(
  2094. api_get_course_info()
  2095. );
  2096. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2097. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2098. }
  2099. $player = Display::getMediaPlayer(
  2100. $file,
  2101. array(
  2102. 'id' => 'lp_audio_media_player',
  2103. 'url' => $url,
  2104. 'autoplay' => $autostart_audio,
  2105. 'width' => '100%'
  2106. )
  2107. );
  2108. // The mp3 player.
  2109. $output = '<div id="container">';
  2110. $output .= $player;
  2111. $output .= '</div>';
  2112. }
  2113. return $output;
  2114. }
  2115. /**
  2116. * Checks if the learning path is visible for student after the progress
  2117. * of its prerequisite is completed, considering the time availability and
  2118. * the LP visibility.
  2119. * @param int $lp_id
  2120. * @param int $student_id
  2121. * @param string Course code (optional)
  2122. * @param int $sessionId
  2123. * @return bool
  2124. */
  2125. public static function is_lp_visible_for_student(
  2126. $lp_id,
  2127. $student_id,
  2128. $courseCode = null,
  2129. $sessionId = null
  2130. ) {
  2131. $lp_id = (int)$lp_id;
  2132. $courseInfo = api_get_course_info($courseCode);
  2133. $sessionId = intval($sessionId);
  2134. if (empty($sessionId)) {
  2135. $sessionId = api_get_session_id();
  2136. }
  2137. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2138. // Get current prerequisite
  2139. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2140. FROM $tbl_learnpath
  2141. WHERE c_id = ".$courseInfo['real_id']." AND id = $lp_id";
  2142. $itemInfo = api_get_item_property_info(
  2143. $courseInfo['real_id'],
  2144. TOOL_LEARNPATH,
  2145. $lp_id,
  2146. $sessionId
  2147. );
  2148. // If the item was deleted.
  2149. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2150. return false;
  2151. }
  2152. $rs = Database::query($sql);
  2153. $now = time();
  2154. if (Database::num_rows($rs) > 0) {
  2155. $row = Database::fetch_array($rs, 'ASSOC');
  2156. $prerequisite = $row['prerequisite'];
  2157. $is_visible = true;
  2158. if (!empty($prerequisite)) {
  2159. $progress = self::getProgress(
  2160. $prerequisite,
  2161. $student_id,
  2162. $courseInfo['real_id'],
  2163. $sessionId
  2164. );
  2165. $progress = intval($progress);
  2166. if ($progress < 100) {
  2167. $is_visible = false;
  2168. }
  2169. }
  2170. // Also check the time availability of the LP
  2171. if ($is_visible) {
  2172. // Adding visibility restrictions
  2173. if (!empty($row['publicated_on']) &&
  2174. $row['publicated_on'] != '0000-00-00 00:00:00'
  2175. ) {
  2176. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2177. //api_not_allowed();
  2178. $is_visible = false;
  2179. }
  2180. }
  2181. // Blocking empty start times see BT#2800
  2182. global $_custom;
  2183. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2184. $_custom['lps_hidden_when_no_start_date']
  2185. ) {
  2186. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2187. //api_not_allowed();
  2188. $is_visible = false;
  2189. }
  2190. }
  2191. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2192. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2193. //api_not_allowed();
  2194. $is_visible = false;
  2195. }
  2196. }
  2197. }
  2198. // Check if the subscription users/group to a LP is ON
  2199. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1) {
  2200. // Try group
  2201. $is_visible = false;
  2202. // Checking only the user visibility
  2203. $userVisibility = api_get_item_visibility(
  2204. $courseInfo,
  2205. 'learnpath',
  2206. $row['id'],
  2207. $sessionId,
  2208. $student_id,
  2209. 'LearnpathSubscription'
  2210. );
  2211. if ($userVisibility == 1) {
  2212. $is_visible = true;
  2213. } else {
  2214. $userGroups = GroupManager::getAllGroupPerUserSubscription($student_id);
  2215. if (!empty($userGroups)) {
  2216. foreach ($userGroups as $groupInfo) {
  2217. $groupId = $groupInfo['iid'];
  2218. $userVisibility = api_get_item_visibility(
  2219. $courseInfo,
  2220. 'learnpath',
  2221. $row['id'],
  2222. $sessionId,
  2223. null,
  2224. 'LearnpathSubscription',
  2225. $groupId
  2226. );
  2227. if ($userVisibility == 1) {
  2228. $is_visible = true;
  2229. break;
  2230. }
  2231. }
  2232. }
  2233. }
  2234. }
  2235. return $is_visible;
  2236. }
  2237. return false;
  2238. }
  2239. /**
  2240. * @param int $lpId
  2241. * @param int $userId
  2242. * @param int $courseId
  2243. * @param int $sessionId
  2244. * @return int
  2245. */
  2246. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2247. {
  2248. $lpId = intval($lpId);
  2249. $userId = intval($userId);
  2250. $courseId = intval($courseId);
  2251. $sessionId = intval($sessionId);
  2252. $progress = 0;
  2253. $sessionCondition = api_get_session_condition($sessionId);
  2254. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2255. $sql = "SELECT * FROM $table
  2256. WHERE
  2257. c_id = ".$courseId." AND
  2258. lp_id = $lpId AND
  2259. user_id = $userId $sessionCondition";
  2260. $res = Database::query($sql);
  2261. if (Database :: num_rows($res) > 0) {
  2262. $row = Database:: fetch_array($res);
  2263. $progress = $row['progress'];
  2264. }
  2265. return $progress;
  2266. }
  2267. /**
  2268. * Displays a progress bar
  2269. * completed so far.
  2270. * @param integer $percentage Progress value to display
  2271. * @param string $text_add Text to display near the progress value
  2272. * @return string HTML string containing the progress bar
  2273. */
  2274. public static function get_progress_bar($percentage = -1, $text_add = '')
  2275. {
  2276. $text = $percentage . $text_add;
  2277. $output = '<div class="progress">
  2278. <div id="progress_bar_value" class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' .$percentage. '" aria-valuemin="0" aria-valuemax="100" style="width: '.$text.';">
  2279. '. $text .'
  2280. </div>
  2281. </div>';
  2282. return $output;
  2283. }
  2284. /**
  2285. * @param string $mode can be '%' or 'abs'
  2286. * otherwise this value will be used $this->progress_bar_mode
  2287. * @return string
  2288. */
  2289. public function getProgressBar($mode = null)
  2290. {
  2291. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2292. return self::get_progress_bar($percentage, $text_add);
  2293. }
  2294. /**
  2295. * Gets the progress bar info to display inside the progress bar.
  2296. * Also used by scorm_api.php
  2297. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2298. * we display a number of completed elements per total elements
  2299. * @param integer $add Additional steps to fake as completed
  2300. * @return list array Percentage or number and symbol (% or /xx)
  2301. */
  2302. public function get_progress_bar_text($mode = '', $add = 0)
  2303. {
  2304. if ($this->debug > 0) {
  2305. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2306. }
  2307. if (empty($mode)) {
  2308. $mode = $this->progress_bar_mode;
  2309. }
  2310. $total_items = $this->get_total_items_count_without_chapters();
  2311. if ($this->debug > 2) {
  2312. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2313. }
  2314. $completeItems = $this->get_complete_items_count();
  2315. if ($this->debug > 2) {
  2316. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2317. }
  2318. if ($add != 0) {
  2319. $completeItems += $add;
  2320. if ($this->debug > 2) {
  2321. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2322. }
  2323. }
  2324. $text = '';
  2325. if ($completeItems > $total_items) {
  2326. $completeItems = $total_items;
  2327. }
  2328. $percentage = 0;
  2329. if ($mode == '%') {
  2330. if ($total_items > 0) {
  2331. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2332. } else {
  2333. $percentage = 0;
  2334. }
  2335. $percentage = number_format($percentage, 0);
  2336. $text = '%';
  2337. } elseif ($mode == 'abs') {
  2338. $percentage = $completeItems;
  2339. $text = '/' . $total_items;
  2340. }
  2341. return array(
  2342. $percentage,
  2343. $text
  2344. );
  2345. }
  2346. /**
  2347. * Gets the progress bar mode
  2348. * @return string The progress bar mode attribute
  2349. */
  2350. public function get_progress_bar_mode()
  2351. {
  2352. if ($this->debug > 0) {
  2353. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2354. }
  2355. if (!empty ($this->progress_bar_mode)) {
  2356. return $this->progress_bar_mode;
  2357. } else {
  2358. return '%';
  2359. }
  2360. }
  2361. /**
  2362. * Gets the learnpath proximity (remote or local)
  2363. * @return string Learnpath proximity
  2364. */
  2365. public function get_proximity()
  2366. {
  2367. if ($this->debug > 0) {
  2368. error_log('New LP - In learnpath::get_proximity()', 0);
  2369. }
  2370. if (!empty ($this->proximity)) {
  2371. return $this->proximity;
  2372. } else {
  2373. return '';
  2374. }
  2375. }
  2376. /**
  2377. * Gets the learnpath theme (remote or local)
  2378. * @return string Learnpath theme
  2379. */
  2380. public function get_theme()
  2381. {
  2382. if ($this->debug > 0) {
  2383. error_log('New LP - In learnpath::get_theme()', 0);
  2384. }
  2385. if (!empty ($this->theme)) {
  2386. return $this->theme;
  2387. } else {
  2388. return '';
  2389. }
  2390. }
  2391. /**
  2392. * Gets the learnpath session id
  2393. * @return string Learnpath theme
  2394. */
  2395. public function get_lp_session_id()
  2396. {
  2397. if ($this->debug > 0) {
  2398. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2399. }
  2400. if (!empty ($this->lp_session_id)) {
  2401. return $this->lp_session_id;
  2402. } else {
  2403. return 0;
  2404. }
  2405. }
  2406. /**
  2407. * Gets the learnpath image
  2408. * @return string Web URL of the LP image
  2409. */
  2410. public function get_preview_image()
  2411. {
  2412. if ($this->debug > 0) {
  2413. error_log('New LP - In learnpath::get_preview_image()', 0);
  2414. }
  2415. if (!empty($this->preview_image)) {
  2416. return $this->preview_image;
  2417. } else {
  2418. return '';
  2419. }
  2420. }
  2421. /**
  2422. * @param string $size
  2423. * @param string $path_type
  2424. * @return bool|string
  2425. */
  2426. public function get_preview_image_path($size = null, $path_type = 'web')
  2427. {
  2428. $preview_image = $this->get_preview_image();
  2429. if (isset($preview_image) && !empty($preview_image)) {
  2430. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2431. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2432. if (isset($size)) {
  2433. $info = pathinfo($preview_image);
  2434. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2435. if (file_exists($image_sys_path.$image_custom_size)) {
  2436. if ($path_type == 'web') {
  2437. return $image_path.$image_custom_size;
  2438. } else {
  2439. return $image_sys_path.$image_custom_size;
  2440. }
  2441. }
  2442. } else {
  2443. if ($path_type == 'web') {
  2444. return $image_path.$preview_image;
  2445. } else {
  2446. return $image_sys_path.$preview_image;
  2447. }
  2448. }
  2449. }
  2450. return false;
  2451. }
  2452. /**
  2453. * Gets the learnpath author
  2454. * @return string LP's author
  2455. */
  2456. public function get_author()
  2457. {
  2458. if ($this->debug > 0) {
  2459. error_log('New LP - In learnpath::get_author()', 0);
  2460. }
  2461. if (!empty ($this->author)) {
  2462. return $this->author;
  2463. } else {
  2464. return '';
  2465. }
  2466. }
  2467. /**
  2468. * Gets the learnpath author
  2469. * @return string LP's author
  2470. */
  2471. public function get_hide_toc_frame()
  2472. {
  2473. if ($this->debug > 0) {
  2474. error_log('New LP - In learnpath::get_author()', 0);
  2475. }
  2476. if (!empty ($this->hide_toc_frame)) {
  2477. return $this->hide_toc_frame;
  2478. } else {
  2479. return '';
  2480. }
  2481. }
  2482. /**
  2483. * Generate a new prerequisites string for a given item. If this item was a sco and
  2484. * its prerequisites were strings (instead of IDs), then transform those strings into
  2485. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2486. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2487. * same rule as the scorm_export() method
  2488. * @param integer Item ID
  2489. * @return string Prerequisites string ready for the export as SCORM
  2490. */
  2491. public function get_scorm_prereq_string($item_id)
  2492. {
  2493. if ($this->debug > 0) {
  2494. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2495. }
  2496. if (!is_object($this->items[$item_id])) {
  2497. return false;
  2498. }
  2499. /** @var learnpathItem $oItem */
  2500. $oItem = $this->items[$item_id];
  2501. $prereq = $oItem->get_prereq_string();
  2502. if (empty($prereq)) {
  2503. return '';
  2504. }
  2505. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2506. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2507. // then simply return it (with the ITEM_ prefix).
  2508. //return 'ITEM_' . $prereq;
  2509. return $this->items[$prereq]->ref;
  2510. } else {
  2511. if (isset($this->refs_list[$prereq])) {
  2512. // It's a simple string item from which the ID can be found in the refs list,
  2513. // so we can transform it directly to an ID for export.
  2514. return $this->items[$this->refs_list[$prereq]]->ref;
  2515. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2516. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2517. } else {
  2518. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2519. // and replace them, one by one, by the internal IDs (chamilo db)
  2520. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2521. // by a space as well.
  2522. $find = array (
  2523. '&',
  2524. '|',
  2525. '~',
  2526. '=',
  2527. '<>',
  2528. '{',
  2529. '}',
  2530. '*',
  2531. '(',
  2532. ')'
  2533. );
  2534. $replace = array (
  2535. ' ',
  2536. ' ',
  2537. ' ',
  2538. ' ',
  2539. ' ',
  2540. ' ',
  2541. ' ',
  2542. ' ',
  2543. ' ',
  2544. ' '
  2545. );
  2546. $prereq_mod = str_replace($find, $replace, $prereq);
  2547. $ids = explode(' ', $prereq_mod);
  2548. foreach ($ids as $id) {
  2549. $id = trim($id);
  2550. if (isset ($this->refs_list[$id])) {
  2551. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2552. }
  2553. }
  2554. return $prereq;
  2555. }
  2556. }
  2557. }
  2558. /**
  2559. * Returns the XML DOM document's node
  2560. * @param resource Reference to a list of objects to search for the given ITEM_*
  2561. * @param string The identifier to look for
  2562. * @return mixed The reference to the element found with that identifier. False if not found
  2563. */
  2564. public function get_scorm_xml_node(& $children, $id)
  2565. {
  2566. for ($i = 0; $i < $children->length; $i++) {
  2567. $item_temp = $children->item($i);
  2568. if ($item_temp->nodeName == 'item') {
  2569. if ($item_temp->getAttribute('identifier') == $id) {
  2570. return $item_temp;
  2571. }
  2572. }
  2573. $subchildren = $item_temp->childNodes;
  2574. if ($subchildren->length > 0) {
  2575. $val = $this->get_scorm_xml_node($subchildren, $id);
  2576. if (is_object($val)) {
  2577. return $val;
  2578. }
  2579. }
  2580. }
  2581. return false;
  2582. }
  2583. /**
  2584. * Returns a usable array of stats related to the current learnpath and user
  2585. * @return array Well-formatted array containing status for the current learnpath
  2586. */
  2587. public function get_stats()
  2588. {
  2589. if ($this->debug > 0) {
  2590. error_log('New LP - In learnpath::get_stats()', 0);
  2591. }
  2592. }
  2593. /**
  2594. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2595. * the given course (for all learnpaths and all users)
  2596. * @param string Course code
  2597. * @return array Well-formatted array containing status for the course's learnpaths
  2598. */
  2599. public function get_stats_course($course)
  2600. {
  2601. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2602. // TODO
  2603. }
  2604. /**
  2605. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2606. * the given course and learnpath (for all users)
  2607. * @param string Course code
  2608. * @param integer Learnpath ID
  2609. * @return array Well-formatted array containing status for the specified learnpath
  2610. */
  2611. public function get_stats_lp($course, $lp)
  2612. {
  2613. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2614. // TODO
  2615. }
  2616. /**
  2617. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2618. * the given course, learnpath and user.
  2619. * @param string Course code
  2620. * @param integer Learnpath ID
  2621. * @param integer User ID
  2622. * @return array Well-formatted array containing status for the specified learnpath and user
  2623. */
  2624. public function get_stats_lp_user($course, $lp, $user)
  2625. {
  2626. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2627. // TODO
  2628. }
  2629. /**
  2630. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2631. * the given course and learnpath (for all users)
  2632. * @param string Course code
  2633. * @param integer User ID
  2634. * @return array Well-formatted array containing status for the user's learnpaths
  2635. */
  2636. public function get_stats_user($course, $user) {
  2637. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2638. // TODO
  2639. }
  2640. /**
  2641. * Gets the status list for all LP's items
  2642. * @return array Array of [index] => [item ID => current status]
  2643. */
  2644. public function get_items_status_list()
  2645. {
  2646. if ($this->debug > 0) {
  2647. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2648. }
  2649. $list = array ();
  2650. foreach ($this->ordered_items as $item_id) {
  2651. $list[] = array (
  2652. $item_id => $this->items[$item_id]->get_status()
  2653. );
  2654. }
  2655. return $list;
  2656. }
  2657. /**
  2658. * Return the number of interactions for the given learnpath Item View ID.
  2659. * This method can be used as static.
  2660. * @param integer Item View ID
  2661. * @param integer course id
  2662. * @return integer Number of interactions
  2663. */
  2664. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2665. {
  2666. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2667. $lp_iv_id = intval($lp_iv_id);
  2668. $course_id = intval($course_id);
  2669. $sql = "SELECT count(*) FROM $table
  2670. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2671. $res = Database::query($sql);
  2672. $num = 0;
  2673. if (Database::num_rows($res)) {
  2674. $row = Database::fetch_array($res);
  2675. $num = $row[0];
  2676. }
  2677. return $num;
  2678. }
  2679. /**
  2680. * Return the interactions as an array for the given lp_iv_id.
  2681. * This method can be used as static.
  2682. * @param integer Learnpath Item View ID
  2683. * @return array
  2684. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2685. */
  2686. public static function get_iv_interactions_array($lp_iv_id)
  2687. {
  2688. $course_id = api_get_course_int_id();
  2689. $list = array();
  2690. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2691. if (empty($lp_iv_id)) {
  2692. return array();
  2693. }
  2694. $sql = "SELECT * FROM $table
  2695. WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id
  2696. ORDER BY order_id ASC";
  2697. $res = Database::query($sql);
  2698. $num = Database :: num_rows($res);
  2699. if ($num > 0) {
  2700. $list[] = array (
  2701. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2702. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2703. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2704. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2705. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2706. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2707. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2708. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2709. );
  2710. while ($row = Database :: fetch_array($res)) {
  2711. $list[] = array (
  2712. 'order_id' => ($row['order_id'] + 1),
  2713. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2714. 'type' => $row['interaction_type'],
  2715. 'time' => $row['completion_time'],
  2716. //'correct_responses' => $row['correct_responses'],
  2717. 'correct_responses' => '', // Hide correct responses from students.
  2718. 'student_response' => $row['student_response'],
  2719. 'result' => $row['result'],
  2720. 'latency' => $row['latency']
  2721. );
  2722. }
  2723. }
  2724. return $list;
  2725. }
  2726. /**
  2727. * Return the number of objectives for the given learnpath Item View ID.
  2728. * This method can be used as static.
  2729. * @param integer Item View ID
  2730. * @return integer Number of objectives
  2731. */
  2732. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2733. {
  2734. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2735. $course_id = intval($course_id);
  2736. $lp_iv_id = intval($lp_iv_id);
  2737. $sql = "SELECT count(*) FROM $table
  2738. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2739. //@todo seems that this always returns 0
  2740. $res = Database::query($sql);
  2741. $num = 0;
  2742. if (Database::num_rows($res)) {
  2743. $row = Database :: fetch_array($res);
  2744. $num = $row[0];
  2745. }
  2746. return $num;
  2747. }
  2748. /**
  2749. * Return the objectives as an array for the given lp_iv_id.
  2750. * This method can be used as static.
  2751. * @param integer Learnpath Item View ID
  2752. * @return array
  2753. * @todo Translate labels
  2754. */
  2755. public static function get_iv_objectives_array($lp_iv_id = 0)
  2756. {
  2757. $course_id = api_get_course_int_id();
  2758. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2759. $sql = "SELECT * FROM $table
  2760. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id
  2761. ORDER BY order_id ASC";
  2762. $res = Database::query($sql);
  2763. $num = Database :: num_rows($res);
  2764. $list = array();
  2765. if ($num > 0) {
  2766. $list[] = array(
  2767. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2768. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2769. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2770. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2771. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2772. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2773. );
  2774. while ($row = Database :: fetch_array($res)) {
  2775. $list[] = array (
  2776. 'order_id' => ($row['order_id'] + 1),
  2777. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2778. 'score_raw' => $row['score_raw'],
  2779. 'score_max' => $row['score_max'],
  2780. 'score_min' => $row['score_min'],
  2781. 'status' => $row['status']
  2782. );
  2783. }
  2784. }
  2785. return $list;
  2786. }
  2787. /**
  2788. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2789. * used by get_html_toc() to be ready to display
  2790. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2791. */
  2792. public function get_toc()
  2793. {
  2794. if ($this->debug > 0) {
  2795. error_log('learnpath::get_toc()', 0);
  2796. }
  2797. $toc = array();
  2798. foreach ($this->ordered_items as $item_id) {
  2799. if ($this->debug > 2) {
  2800. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2801. }
  2802. // TODO: Change this link generation and use new function instead.
  2803. $toc[] = array (
  2804. 'id' => $item_id,
  2805. 'title' => $this->items[$item_id]->get_title(),
  2806. 'status' => $this->items[$item_id]->get_status(),
  2807. 'level' => $this->items[$item_id]->get_level(),
  2808. 'type' => $this->items[$item_id]->get_type(),
  2809. 'description' => $this->items[$item_id]->get_description(),
  2810. 'path' => $this->items[$item_id]->get_path(),
  2811. );
  2812. }
  2813. if ($this->debug > 2) {
  2814. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2815. }
  2816. return $toc;
  2817. }
  2818. /**
  2819. * Generate and return the table of contents for this learnpath. The JS
  2820. * table returned is used inside of scorm_api.php
  2821. * @return string A JS array vairiable construction
  2822. */
  2823. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2824. {
  2825. if ($this->debug > 0) {
  2826. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2827. }
  2828. $toc = $varname.' = new Array();';
  2829. foreach ($this->ordered_items as $item_id) {
  2830. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2831. }
  2832. if ($this->debug > 2) {
  2833. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2834. }
  2835. return $toc;
  2836. }
  2837. /**
  2838. * Gets the learning path type
  2839. * @param boolean Return the name? If false, return the ID. Default is false.
  2840. * @return mixed Type ID or name, depending on the parameter
  2841. */
  2842. public function get_type($get_name = false)
  2843. {
  2844. $res = false;
  2845. if ($this->debug > 0) {
  2846. error_log('New LP - In learnpath::get_type()', 0);
  2847. }
  2848. if (!empty ($this->type)) {
  2849. if ($get_name) {
  2850. // Get it from the lp_type table in main db.
  2851. } else {
  2852. $res = $this->type;
  2853. }
  2854. }
  2855. if ($this->debug > 2) {
  2856. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2857. }
  2858. return $res;
  2859. }
  2860. /**
  2861. * Gets the learning path type as static method
  2862. * @param boolean Return the name? If false, return the ID. Default is false.
  2863. * @return mixed Type ID or name, depending on the parameter
  2864. */
  2865. public static function get_type_static($lp_id = 0)
  2866. {
  2867. $course_id = api_get_course_int_id();
  2868. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2869. $lp_id = intval($lp_id);
  2870. $sql = "SELECT lp_type FROM $tbl_lp
  2871. WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2872. $res = Database::query($sql);
  2873. if ($res === false) {
  2874. return null;
  2875. }
  2876. if (Database :: num_rows($res) <= 0) {
  2877. return null;
  2878. }
  2879. $row = Database :: fetch_array($res);
  2880. return $row['lp_type'];
  2881. }
  2882. /**
  2883. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2884. * This method can be used as abstract and is recursive
  2885. * @param integer Learnpath ID
  2886. * @param integer Parent ID of the items to look for
  2887. * @return mixed Ordered list of item IDs or false on error
  2888. */
  2889. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2890. {
  2891. if (empty($course_id)) {
  2892. $course_id = api_get_course_int_id();
  2893. } else {
  2894. $course_id = intval($course_id);
  2895. }
  2896. $list = array();
  2897. if (empty($lp)) {
  2898. return false;
  2899. }
  2900. $lp = intval($lp);
  2901. $parent = intval($parent);
  2902. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2903. $sql = "SELECT id FROM $tbl_lp_item
  2904. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2905. ORDER BY display_order";
  2906. $res = Database::query($sql);
  2907. while ($row = Database :: fetch_array($res)) {
  2908. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2909. $list[] = $row['id'];
  2910. foreach ($sublist as $item) {
  2911. $list[] = $item;
  2912. }
  2913. }
  2914. return $list;
  2915. }
  2916. /**
  2917. * @return array
  2918. */
  2919. public static function getChapterTypes()
  2920. {
  2921. return array(
  2922. 'dokeos_chapter',
  2923. 'dokeos_module',
  2924. 'chapter',
  2925. 'dir'
  2926. );
  2927. }
  2928. /**
  2929. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2930. * @return string HTML TOC ready to display
  2931. */
  2932. public function get_html_toc($toc_list = null)
  2933. {
  2934. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2935. if ($this->debug > 0) {
  2936. error_log('In learnpath::get_html_toc()', 0);
  2937. }
  2938. if (empty($toc_list)) {
  2939. $toc_list = $this->get_toc();
  2940. }
  2941. //$html = '<div id="scorm_title" class="scorm-heading">'.Security::remove_XSS($this->get_name()) . '</div>';
  2942. $html = '<div class="scorm-body">';
  2943. $html .= '<div id="inner_lp_toc" class="inner_lp_toc scrollbar-light">';
  2944. // Temporary variables.
  2945. $mycurrentitemid = $this->get_current_item_id();
  2946. $color_counter = 0;
  2947. $i = 0;
  2948. foreach ($toc_list as $item) {
  2949. // Style Status
  2950. $class_name = [
  2951. 'not attempted' => 'scorm_not_attempted',
  2952. 'incomplete' => 'scorm_not_attempted',
  2953. 'failed' => 'scorm_failed',
  2954. 'completed' => 'scorm_completed',
  2955. 'passed' => 'scorm_completed',
  2956. 'succeeded' => 'scorm_completed',
  2957. 'browsed' => 'scorm_completed',
  2958. ];
  2959. $scorm_color_background = 'row_odd';
  2960. $style_item = '';
  2961. if ($color_counter % 2 == 0) {
  2962. $scorm_color_background = 'row_even';
  2963. }
  2964. $dirTypes = self::getChapterTypes();
  2965. if (in_array($item['type'], $dirTypes)) {
  2966. $scorm_color_background ='scorm_item_section ';
  2967. $style_item = '';
  2968. }
  2969. if ($item['id'] == $this->current) {
  2970. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' scorm_highlight';
  2971. } elseif (!in_array($item['type'], $dirTypes)) {
  2972. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2973. }
  2974. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2975. // Learning path title
  2976. $title = $item['title'];
  2977. if (empty ($title)) {
  2978. $title = self::rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2979. }
  2980. $title = Security::remove_XSS($title);
  2981. // Learning path personalization
  2982. // build the LP tree
  2983. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2984. $description = $item['description'];
  2985. if (empty($description)) {
  2986. $description = $title;
  2987. }
  2988. if (in_array($item['type'], $dirTypes)) {
  2989. // Chapters
  2990. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2991. } else {
  2992. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2993. $html .= '<a name="atoc_'.$item['id'].'"></a>';
  2994. }
  2995. if (in_array($item['type'], $dirTypes)) {
  2996. // Chapter
  2997. // if you want to put an image before, you should use css
  2998. $html .= stripslashes($title);
  2999. } else {
  3000. $this->get_link('http', $item['id'], $toc_list);
  3001. $html .= '<a class="items-list" href="#" onclick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  3002. }
  3003. $html .= "</div>";
  3004. if ($scorm_color_background != '') {
  3005. $html .= '</div>';
  3006. }
  3007. $color_counter++;
  3008. }
  3009. $html .= "</div>";
  3010. $html .= "</div>";
  3011. return $html;
  3012. }
  3013. /**
  3014. * Returns an HTML-formatted string ready to display with teacher buttons
  3015. * in LP view menu
  3016. * @return string HTML TOC ready to display
  3017. */
  3018. public function get_teacher_toc_buttons()
  3019. {
  3020. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  3021. $hide_teacher_icons_lp = api_get_configuration_value('hide_teacher_icons_lp');
  3022. $html = '';
  3023. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  3024. if ($this->get_lp_session_id() == api_get_session_id()) {
  3025. $html .= '<div id="actions_lp" class="actions_lp"><hr>';
  3026. $html .= '<div class="btn-group">';
  3027. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=build&lp_id=" . $this->lp_id . "' target='_parent'>" .
  3028. Display::returnFontAwesomeIcon('street-view') . get_lang('Overview') . "</a>";
  3029. $html .= "<a class='btn btn-sm btn-default' href='lp_controller.php?" . api_get_cidreq()."&action=add_item&type=step&lp_id=" . $this->lp_id . "' target='_parent'>" .
  3030. Display::returnFontAwesomeIcon('pencil') . get_lang('Edit') . "</a>";
  3031. $html .= '<a class="btn btn-sm btn-default" href="lp_controller.php?'.api_get_cidreq()."&action=edit&lp_id=" . $this->lp_id.'">' .
  3032. Display::returnFontAwesomeIcon('cog') . get_lang('Settings').'</a>';
  3033. $html .= '</div>';
  3034. $html .= '</div>';
  3035. }
  3036. }
  3037. return $html;
  3038. }
  3039. /**
  3040. * Gets the learnpath maker name - generally the editor's name
  3041. * @return string Learnpath maker name
  3042. */
  3043. public function get_maker()
  3044. {
  3045. if ($this->debug > 0) {
  3046. error_log('New LP - In learnpath::get_maker()', 0);
  3047. }
  3048. if (!empty ($this->maker)) {
  3049. return $this->maker;
  3050. } else {
  3051. return '';
  3052. }
  3053. }
  3054. /**
  3055. * Gets the learnpath name/title
  3056. * @return string Learnpath name/title
  3057. */
  3058. public function get_name()
  3059. {
  3060. if ($this->debug > 0) {
  3061. error_log('New LP - In learnpath::get_name()', 0);
  3062. }
  3063. if (!empty ($this->name)) {
  3064. return $this->name;
  3065. } else {
  3066. return 'N/A';
  3067. }
  3068. }
  3069. /**
  3070. * Gets a link to the resource from the present location, depending on item ID.
  3071. * @param string $type Type of link expected
  3072. * @param integer $item_id Learnpath item ID
  3073. * @return string $provided_toc Link to the lp_item resource
  3074. */
  3075. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3076. {
  3077. $course_id = $this->get_course_int_id();
  3078. if ($this->debug > 0) {
  3079. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3080. }
  3081. if (empty($item_id)) {
  3082. if ($this->debug > 2) {
  3083. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3084. }
  3085. $item_id = $this->get_current_item_id();
  3086. }
  3087. if (empty($item_id)) {
  3088. if ($this->debug > 2) {
  3089. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3090. }
  3091. //still empty, this means there was no item_id given and we are not in an object context or
  3092. //the object property is empty, return empty link
  3093. $item_id = $this->first();
  3094. return '';
  3095. }
  3096. $file = '';
  3097. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3098. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3099. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3100. $item_id = intval($item_id);
  3101. $sql = "SELECT
  3102. l.lp_type as ltype,
  3103. l.path as lpath,
  3104. li.item_type as litype,
  3105. li.path as lipath,
  3106. li.parameters as liparams
  3107. FROM $lp_table l
  3108. INNER JOIN $lp_item_table li
  3109. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3110. WHERE li.id = $item_id ";
  3111. if ($this->debug > 2) {
  3112. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3113. }
  3114. $res = Database::query($sql);
  3115. if (Database :: num_rows($res) > 0) {
  3116. $row = Database :: fetch_array($res);
  3117. $lp_type = $row['ltype'];
  3118. $lp_path = $row['lpath'];
  3119. $lp_item_type = $row['litype'];
  3120. $lp_item_path = $row['lipath'];
  3121. $lp_item_params = $row['liparams'];
  3122. if (empty($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3123. list($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3124. }
  3125. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3126. if ($type == 'http') {
  3127. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3128. } else {
  3129. $course_path = $sys_course_path; //system path
  3130. }
  3131. // 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.
  3132. if (in_array($lp_item_type, array('quiz', 'document', 'final_item', 'link', 'forum', 'thread', 'student_publication'))) {
  3133. $lp_type = 1;
  3134. }
  3135. if ($this->debug > 2) {
  3136. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3137. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3138. }
  3139. // Now go through the specific cases to get the end of the path
  3140. // @todo Use constants instead of int values.
  3141. switch ($lp_type) {
  3142. case 1 :
  3143. $file = self::rl_get_resource_link_for_learnpath(
  3144. $course_id,
  3145. $this->get_id(),
  3146. $item_id,
  3147. $this->get_view_id()
  3148. );
  3149. if ($this->debug > 0) {
  3150. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3151. }
  3152. switch ($lp_item_type) {
  3153. case 'dokeos_chapter':
  3154. $file = 'lp_content.php?type=dir';
  3155. break;
  3156. case 'link':
  3157. if (Link::is_youtube_link($file)) {
  3158. $src = Link::get_youtube_video_id($file);
  3159. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=youtube&source='.$src;
  3160. } elseif (Link::isVimeoLink($file)) {
  3161. $src = Link::getVimeoLinkId($file);
  3162. $file = api_get_path(WEB_CODE_PATH).'newscorm/embed.php?type=vimeo&source='.$src;
  3163. } else {
  3164. // If the current site is HTTPS and the link is
  3165. // HTTP, browsers will refuse opening the link
  3166. $urlId = api_get_current_access_url_id();
  3167. $url = api_get_access_url($urlId, false);
  3168. $protocol = substr($url['url'], 0, 5);
  3169. if ($protocol === 'https') {
  3170. $linkProtocol = substr($file, 0, 5);
  3171. if ($linkProtocol === 'http:') {
  3172. //this is the special intervention case
  3173. $file = api_get_path(
  3174. WEB_CODE_PATH
  3175. ).'newscorm/embed.php?type=nonhttps&source='.urlencode($file);
  3176. }
  3177. }
  3178. }
  3179. break;
  3180. case 'quiz':
  3181. // Check how much attempts of a exercise exits in lp
  3182. $lp_item_id = $this->get_current_item_id();
  3183. $lp_view_id = $this->get_view_id();
  3184. $prevent_reinit = null;
  3185. if (isset($this->items[$this->current])) {
  3186. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3187. }
  3188. if (empty($provided_toc)) {
  3189. if ($this->debug > 0) {
  3190. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3191. }
  3192. $list = $this->get_toc();
  3193. } else {
  3194. if ($this->debug > 0) {
  3195. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3196. }
  3197. $list = $provided_toc;
  3198. }
  3199. $type_quiz = false;
  3200. foreach ($list as $toc) {
  3201. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3202. $type_quiz = true;
  3203. }
  3204. }
  3205. if ($type_quiz) {
  3206. $lp_item_id = intval($lp_item_id);
  3207. $lp_view_id = intval($lp_view_id);
  3208. $sql = "SELECT count(*) FROM $lp_item_view_table
  3209. WHERE
  3210. c_id = $course_id AND
  3211. lp_item_id='".$lp_item_id."' AND
  3212. lp_view_id ='".$lp_view_id."' AND
  3213. status='completed'";
  3214. $result = Database::query($sql);
  3215. $row_count = Database:: fetch_row($result);
  3216. $count_item_view = (int)$row_count[0];
  3217. $not_multiple_attempt = 0;
  3218. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3219. $not_multiple_attempt = 1;
  3220. }
  3221. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3222. }
  3223. break;
  3224. }
  3225. $tmp_array = explode('/', $file);
  3226. $document_name = $tmp_array[count($tmp_array) - 1];
  3227. if (strpos($document_name, '_DELETED_')) {
  3228. $file = 'blank.php?error=document_deleted';
  3229. }
  3230. break;
  3231. case 2 :
  3232. if ($this->debug > 2) {
  3233. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3234. }
  3235. if ($lp_item_type != 'dir') {
  3236. // Quite complex here:
  3237. // We want to make sure 'http://' (and similar) links can
  3238. // be loaded as is (withouth the Chamilo path in front) but
  3239. // some contents use this form: resource.htm?resource=http://blablabla
  3240. // which means we have to find a protocol at the path's start, otherwise
  3241. // it should not be considered as an external URL.
  3242. //if ($this->prerequisites_match($item_id)) {
  3243. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3244. if ($this->debug > 2) {
  3245. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3246. }
  3247. // Distant url, return as is.
  3248. $file = $lp_item_path;
  3249. } else {
  3250. if ($this->debug > 2) {
  3251. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3252. }
  3253. // Prevent getting untranslatable urls.
  3254. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3255. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3256. // Prepare the path.
  3257. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3258. // TODO: Fix this for urls with protocol header.
  3259. $file = str_replace('//', '/', $file);
  3260. $file = str_replace(':/', '://', $file);
  3261. if (substr($lp_path, -1) == '/') {
  3262. $lp_path = substr($lp_path, 0, -1);
  3263. }
  3264. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3265. // if file not found.
  3266. $decoded = html_entity_decode($lp_item_path);
  3267. list ($decoded) = explode('?', $decoded);
  3268. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3269. $file = self::rl_get_resource_link_for_learnpath(
  3270. $course_id,
  3271. $this->get_id(),
  3272. $item_id,
  3273. $this->get_view_id()
  3274. );
  3275. if (empty($file)) {
  3276. $file = 'blank.php?error=document_not_found';
  3277. } else {
  3278. $tmp_array = explode('/', $file);
  3279. $document_name = $tmp_array[count($tmp_array) - 1];
  3280. if (strpos($document_name, '_DELETED_')) {
  3281. $file = 'blank.php?error=document_deleted';
  3282. } else {
  3283. $file = 'blank.php?error=document_not_found';
  3284. }
  3285. }
  3286. } else {
  3287. $file = $course_path . '/scorm/' . $lp_path . '/' . $decoded;
  3288. }
  3289. }
  3290. }
  3291. // We want to use parameters if they were defined in the imsmanifest
  3292. if (strpos($file, 'blank.php') === false) {
  3293. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3294. }
  3295. } else {
  3296. $file = 'lp_content.php?type=dir';
  3297. }
  3298. break;
  3299. case 3 :
  3300. if ($this->debug > 2) {
  3301. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3302. }
  3303. // Formatting AICC HACP append URL.
  3304. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3305. if (!empty($lp_item_params)) {
  3306. $aicc_append .= $lp_item_params . '&';
  3307. }
  3308. if ($lp_item_type != 'dir') {
  3309. // Quite complex here:
  3310. // We want to make sure 'http://' (and similar) links can
  3311. // be loaded as is (withouth the Chamilo path in front) but
  3312. // some contents use this form: resource.htm?resource=http://blablabla
  3313. // which means we have to find a protocol at the path's start, otherwise
  3314. // it should not be considered as an external URL.
  3315. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3316. if ($this->debug > 2) {
  3317. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3318. }
  3319. // Distant url, return as is.
  3320. $file = $lp_item_path;
  3321. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3322. /*
  3323. if (stristr($file,'<servername>') !== false) {
  3324. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3325. }
  3326. */
  3327. if (stripos($file, '<servername>') !== false) {
  3328. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3329. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3330. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3331. }
  3332. //
  3333. $file .= $aicc_append;
  3334. } else {
  3335. if ($this->debug > 2) {
  3336. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3337. }
  3338. // Prevent getting untranslatable urls.
  3339. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3340. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3341. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3342. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3343. // TODO: Fix this for urls with protocol header.
  3344. $file = str_replace('//', '/', $file);
  3345. $file = str_replace(':/', '://', $file);
  3346. $file .= $aicc_append;
  3347. }
  3348. } else {
  3349. $file = 'lp_content.php?type=dir';
  3350. }
  3351. break;
  3352. case 4 :
  3353. break;
  3354. default :
  3355. break;
  3356. }
  3357. // Replace &amp; by & because &amp; will break URL with params
  3358. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3359. }
  3360. if ($this->debug > 2) {
  3361. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3362. }
  3363. return $file;
  3364. }
  3365. /**
  3366. * Gets the latest usable view or generate a new one
  3367. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3368. * @return integer DB lp_view id
  3369. */
  3370. public function get_view($attempt_num = 0)
  3371. {
  3372. if ($this->debug > 0) {
  3373. error_log('New LP - In learnpath::get_view()', 0);
  3374. }
  3375. $search = '';
  3376. // Use $attempt_num to enable multi-views management (disabled so far).
  3377. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3378. $search = 'AND view_count = ' . $attempt_num;
  3379. }
  3380. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3381. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3382. $course_id = api_get_course_int_id();
  3383. $sessionId = api_get_session_id();
  3384. $sql = "SELECT id, view_count FROM $lp_view_table
  3385. WHERE
  3386. c_id = " . $course_id . " AND
  3387. lp_id = " . $this->get_id() . " AND
  3388. user_id = " . $this->get_user_id() . " AND
  3389. session_id = $sessionId
  3390. $search
  3391. ORDER BY view_count DESC";
  3392. $res = Database::query($sql);
  3393. if (Database :: num_rows($res) > 0) {
  3394. $row = Database :: fetch_array($res);
  3395. $this->lp_view_id = $row['id'];
  3396. } else if (!api_is_invitee()) {
  3397. // There is no database record, create one.
  3398. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3399. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3400. Database::query($sql);
  3401. $id = Database :: insert_id();
  3402. $this->lp_view_id = $id;
  3403. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $id";
  3404. Database::query($sql);
  3405. }
  3406. return $this->lp_view_id;
  3407. }
  3408. /**
  3409. * Gets the current view id
  3410. * @return integer View ID (from lp_view)
  3411. */
  3412. public function get_view_id()
  3413. {
  3414. if ($this->debug > 0) {
  3415. error_log('New LP - In learnpath::get_view_id()', 0);
  3416. }
  3417. if (!empty ($this->lp_view_id)) {
  3418. return $this->lp_view_id;
  3419. } else {
  3420. return 0;
  3421. }
  3422. }
  3423. /**
  3424. * Gets the update queue
  3425. * @return array Array containing IDs of items to be updated by JavaScript
  3426. */
  3427. public function get_update_queue()
  3428. {
  3429. if ($this->debug > 0) {
  3430. error_log('New LP - In learnpath::get_update_queue()', 0);
  3431. }
  3432. return $this->update_queue;
  3433. }
  3434. /**
  3435. * Gets the user ID
  3436. * @return integer User ID
  3437. */
  3438. public function get_user_id()
  3439. {
  3440. if ($this->debug > 0) {
  3441. error_log('New LP - In learnpath::get_user_id()', 0);
  3442. }
  3443. if (!empty ($this->user_id)) {
  3444. return $this->user_id;
  3445. } else {
  3446. return false;
  3447. }
  3448. }
  3449. /**
  3450. * Checks if any of the items has an audio element attached
  3451. * @return bool True or false
  3452. */
  3453. public function has_audio()
  3454. {
  3455. if ($this->debug > 1) {
  3456. error_log('New LP - In learnpath::has_audio()', 0);
  3457. }
  3458. $has = false;
  3459. foreach ($this->items as $i => $item) {
  3460. if (!empty ($this->items[$i]->audio)) {
  3461. $has = true;
  3462. break;
  3463. }
  3464. }
  3465. return $has;
  3466. }
  3467. /**
  3468. * Logs a message into a file
  3469. * @param string Message to log
  3470. * @return boolean True on success, false on error or if msg empty
  3471. */
  3472. public function log($msg)
  3473. {
  3474. if ($this->debug > 0) {
  3475. error_log('New LP - In learnpath::log()', 0);
  3476. }
  3477. // TODO
  3478. $this->error .= $msg;
  3479. return true;
  3480. }
  3481. /**
  3482. * Moves an item up and down at its level
  3483. * @param integer Item to move up and down
  3484. * @param string Direction 'up' or 'down'
  3485. * @return integer New display order, or false on error
  3486. */
  3487. public function move_item($id, $direction)
  3488. {
  3489. $course_id = api_get_course_int_id();
  3490. if ($this->debug > 0) {
  3491. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3492. }
  3493. if (empty($id) || empty($direction)) {
  3494. return false;
  3495. }
  3496. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3497. $sql_sel = "SELECT *
  3498. FROM " . $tbl_lp_item . "
  3499. WHERE c_id = ".$course_id." AND id = " . $id;
  3500. $res_sel = Database::query($sql_sel);
  3501. // Check if elem exists.
  3502. if (Database :: num_rows($res_sel) < 1) {
  3503. return false;
  3504. }
  3505. // Gather data.
  3506. $row = Database :: fetch_array($res_sel);
  3507. $previous = $row['previous_item_id'];
  3508. $next = $row['next_item_id'];
  3509. $display = $row['display_order'];
  3510. $parent = $row['parent_item_id'];
  3511. $lp = $row['lp_id'];
  3512. // Update the item (switch with previous/next one).
  3513. switch ($direction) {
  3514. case 'up':
  3515. if ($this->debug > 2) {
  3516. error_log('Movement up detected', 0);
  3517. }
  3518. if ($display <= 1) { /*do nothing*/
  3519. } else {
  3520. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3521. WHERE c_id = ".$course_id." AND id = $previous";
  3522. if ($this->debug > 2) {
  3523. error_log('Selecting previous: ' . $sql_sel2, 0);
  3524. }
  3525. $res_sel2 = Database::query($sql_sel2);
  3526. if (Database :: num_rows($res_sel2) < 1) {
  3527. $previous_previous = 0;
  3528. }
  3529. // Gather data.
  3530. $row2 = Database :: fetch_array($res_sel2);
  3531. $previous_previous = $row2['previous_item_id'];
  3532. // Update previous_previous item (switch "next" with current).
  3533. if ($previous_previous != 0) {
  3534. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3535. next_item_id = $id
  3536. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3537. if ($this->debug > 2) {
  3538. error_log($sql_upd2, 0);
  3539. }
  3540. Database::query($sql_upd2);
  3541. }
  3542. // Update previous item (switch with current).
  3543. if ($previous != 0) {
  3544. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3545. next_item_id = $next,
  3546. previous_item_id = $id,
  3547. display_order = display_order +1
  3548. WHERE c_id = ".$course_id." AND id = $previous";
  3549. if ($this->debug > 2) {
  3550. error_log($sql_upd2, 0);
  3551. }
  3552. Database::query($sql_upd2);
  3553. }
  3554. // Update current item (switch with previous).
  3555. if ($id != 0) {
  3556. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3557. next_item_id = $previous,
  3558. previous_item_id = $previous_previous,
  3559. display_order = display_order-1
  3560. WHERE c_id = ".$course_id." AND id = $id";
  3561. if ($this->debug > 2) {
  3562. error_log($sql_upd2, 0);
  3563. }
  3564. Database::query($sql_upd2);
  3565. }
  3566. // Update next item (new previous item).
  3567. if ($next != 0) {
  3568. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3569. WHERE c_id = ".$course_id." AND id = $next";
  3570. if ($this->debug > 2) {
  3571. error_log($sql_upd2, 0);
  3572. }
  3573. Database::query($sql_upd2);
  3574. }
  3575. $display = $display -1;
  3576. }
  3577. break;
  3578. case 'down':
  3579. if ($this->debug > 2) {
  3580. error_log('Movement down detected', 0);
  3581. }
  3582. if ($next == 0) { /* Do nothing. */
  3583. } else {
  3584. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3585. if ($this->debug > 2) {
  3586. error_log('Selecting next: ' . $sql_sel2, 0);
  3587. }
  3588. $res_sel2 = Database::query($sql_sel2);
  3589. if (Database :: num_rows($res_sel2) < 1) {
  3590. $next_next = 0;
  3591. }
  3592. // Gather data.
  3593. $row2 = Database :: fetch_array($res_sel2);
  3594. $next_next = $row2['next_item_id'];
  3595. // Update previous item (switch with current).
  3596. if ($previous != 0) {
  3597. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3598. WHERE c_id = ".$course_id." AND id = $previous";
  3599. Database::query($sql_upd2);
  3600. }
  3601. // Update current item (switch with previous).
  3602. if ($id != 0) {
  3603. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3604. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3605. WHERE c_id = ".$course_id." AND id = $id";
  3606. Database::query($sql_upd2);
  3607. }
  3608. // Update next item (new previous item).
  3609. if ($next != 0) {
  3610. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3611. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3612. WHERE c_id = ".$course_id." AND id = $next";
  3613. Database::query($sql_upd2);
  3614. }
  3615. // Update next_next item (switch "previous" with current).
  3616. if ($next_next != 0) {
  3617. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3618. previous_item_id = $id
  3619. WHERE c_id = ".$course_id." AND id = $next_next";
  3620. Database::query($sql_upd2);
  3621. }
  3622. $display = $display +1;
  3623. }
  3624. break;
  3625. default :
  3626. return false;
  3627. }
  3628. return $display;
  3629. }
  3630. /**
  3631. * Move a learnpath up (display_order)
  3632. * @param integer $lp_id Learnpath ID
  3633. */
  3634. public static function move_up($lp_id)
  3635. {
  3636. $course_id = api_get_course_int_id();
  3637. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3638. $sql = "SELECT * FROM $lp_table
  3639. WHERE c_id = ".$course_id."
  3640. ORDER BY display_order";
  3641. $res = Database::query($sql);
  3642. if ($res === false)
  3643. return false;
  3644. $lps = array ();
  3645. $lp_order = array ();
  3646. $num = Database :: num_rows($res);
  3647. // First check the order is correct, globally (might be wrong because
  3648. // of versions < 1.8.4)
  3649. if ($num > 0) {
  3650. $i = 1;
  3651. while ($row = Database :: fetch_array($res)) {
  3652. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3653. $need_fix = true;
  3654. $sql_u = "UPDATE $lp_table SET display_order = $i
  3655. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3656. Database::query($sql_u);
  3657. }
  3658. $row['display_order'] = $i;
  3659. $lps[$row['id']] = $row;
  3660. $lp_order[$i] = $row['id'];
  3661. $i++;
  3662. }
  3663. }
  3664. if ($num > 1) { // If there's only one element, no need to sort.
  3665. $order = $lps[$lp_id]['display_order'];
  3666. if ($order > 1) { // If it's the first element, no need to move up.
  3667. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3668. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3669. Database::query($sql_u1);
  3670. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3671. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3672. Database::query($sql_u2);
  3673. }
  3674. }
  3675. }
  3676. /**
  3677. * Move a learnpath down (display_order)
  3678. * @param integer $lp_id Learnpath ID
  3679. */
  3680. public static function move_down($lp_id)
  3681. {
  3682. $course_id = api_get_course_int_id();
  3683. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3684. $sql = "SELECT * FROM $lp_table
  3685. WHERE c_id = ".$course_id."
  3686. ORDER BY display_order";
  3687. $res = Database::query($sql);
  3688. if ($res === false) {
  3689. return false;
  3690. }
  3691. $lps = array ();
  3692. $lp_order = array ();
  3693. $num = Database :: num_rows($res);
  3694. $max = 0;
  3695. // First check the order is correct, globally (might be wrong because
  3696. // of versions < 1.8.4).
  3697. if ($num > 0) {
  3698. $i = 1;
  3699. while ($row = Database :: fetch_array($res)) {
  3700. $max = $i;
  3701. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3702. $need_fix = true;
  3703. $sql_u = "UPDATE $lp_table SET display_order = $i
  3704. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3705. Database::query($sql_u);
  3706. }
  3707. $row['display_order'] = $i;
  3708. $lps[$row['id']] = $row;
  3709. $lp_order[$i] = $row['id'];
  3710. $i++;
  3711. }
  3712. }
  3713. if ($num > 1) { // If there's only one element, no need to sort.
  3714. $order = $lps[$lp_id]['display_order'];
  3715. if ($order < $max) { // If it's the first element, no need to move up.
  3716. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3717. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3718. Database::query($sql_u1);
  3719. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3720. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3721. Database::query($sql_u2);
  3722. }
  3723. }
  3724. }
  3725. /**
  3726. * Updates learnpath attributes to point to the next element
  3727. * The last part is similar to set_current_item but processing the other way around
  3728. */
  3729. public function next()
  3730. {
  3731. if ($this->debug > 0) {
  3732. error_log('New LP - In learnpath::next()', 0);
  3733. }
  3734. $this->last = $this->get_current_item_id();
  3735. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3736. $this->autocomplete_parents($this->last);
  3737. $new_index = $this->get_next_index();
  3738. if ($this->debug > 2) {
  3739. error_log('New LP - New index: ' . $new_index, 0);
  3740. }
  3741. $this->index = $new_index;
  3742. if ($this->debug > 2) {
  3743. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3744. }
  3745. $this->current = $this->ordered_items[$new_index];
  3746. if ($this->debug > 2) {
  3747. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3748. }
  3749. }
  3750. /**
  3751. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3752. * class, this might be redefined to allow several behaviours depending on the document type.
  3753. * @param integer Resource ID
  3754. * @return boolean True on success, false otherwise
  3755. */
  3756. public function open($id)
  3757. {
  3758. if ($this->debug > 0) {
  3759. error_log('New LP - In learnpath::open()', 0);
  3760. }
  3761. // TODO:
  3762. // set the current resource attribute to this resource
  3763. // switch on element type (redefine in child class?)
  3764. // set status for this item to "opened"
  3765. // start timer
  3766. // initialise score
  3767. $this->index = 0; //or = the last item seen (see $this->last)
  3768. }
  3769. /**
  3770. * Check that all prerequisites are fulfilled. Returns true and an
  3771. * empty string on succes, returns false
  3772. * and the prerequisite string on error.
  3773. * This function is based on the rules for aicc_script language as
  3774. * described in the SCORM 1.2 CAM documentation page 108.
  3775. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3776. * @return boolean True if prerequisites are matched, false otherwise -
  3777. * Empty string if true returned, prerequisites string otherwise.
  3778. */
  3779. public function prerequisites_match($itemId = null)
  3780. {
  3781. $debug = $this->debug;
  3782. if ($debug > 0) {
  3783. error_log('In learnpath::prerequisites_match()', 0);
  3784. }
  3785. if (empty($itemId)) {
  3786. $itemId = $this->current;
  3787. }
  3788. $currentItem = $this->getItem($itemId);
  3789. if ($currentItem) {
  3790. if ($this->type == 2) {
  3791. // Getting prereq from scorm
  3792. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3793. } else {
  3794. $prereq_string = $currentItem->get_prereq_string();
  3795. }
  3796. if (empty($prereq_string)) {
  3797. if ($debug > 0) {
  3798. error_log('Found prereq_string is empty return true');
  3799. }
  3800. return true;
  3801. }
  3802. // Clean spaces.
  3803. $prereq_string = str_replace(' ', '', $prereq_string);
  3804. if ($debug > 0) {
  3805. error_log('Found prereq_string: ' . $prereq_string, 0);
  3806. }
  3807. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3808. $result = $currentItem->parse_prereq(
  3809. $prereq_string,
  3810. $this->items,
  3811. $this->refs_list,
  3812. $this->get_user_id()
  3813. );
  3814. if ($result === false) {
  3815. $this->set_error_msg($currentItem->prereq_alert);
  3816. }
  3817. } else {
  3818. $result = true;
  3819. if ($debug > 1) {
  3820. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3821. }
  3822. }
  3823. if ($debug > 1) {
  3824. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3825. }
  3826. return $result;
  3827. }
  3828. /**
  3829. * Updates learnpath attributes to point to the previous element
  3830. * The last part is similar to set_current_item but processing the other way around
  3831. */
  3832. public function previous()
  3833. {
  3834. if ($this->debug > 0) {
  3835. error_log('New LP - In learnpath::previous()', 0);
  3836. }
  3837. $this->last = $this->get_current_item_id();
  3838. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3839. $this->autocomplete_parents($this->last);
  3840. $new_index = $this->get_previous_index();
  3841. $this->index = $new_index;
  3842. $this->current = $this->ordered_items[$new_index];
  3843. }
  3844. /**
  3845. * Publishes a learnpath. This basically means show or hide the learnpath
  3846. * to normal users.
  3847. * Can be used as abstract
  3848. * @param integer Learnpath ID
  3849. * @param string New visibility
  3850. */
  3851. public static function toggle_visibility($lp_id, $set_visibility = 1)
  3852. {
  3853. $action = 'visible';
  3854. if ($set_visibility != 1) {
  3855. $action = 'invisible';
  3856. self::toggle_publish($lp_id, 'i');
  3857. }
  3858. return api_item_property_update(
  3859. api_get_course_info(),
  3860. TOOL_LEARNPATH,
  3861. $lp_id,
  3862. $action,
  3863. api_get_user_id()
  3864. );
  3865. }
  3866. /**
  3867. * Publishes a learnpath. This basically means show or hide the learnpath
  3868. * on the course homepage
  3869. * Can be used as abstract
  3870. * @param integer $lp_id Learnpath id
  3871. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3872. * @return bool
  3873. */
  3874. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3875. {
  3876. $course_id = api_get_course_int_id();
  3877. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3878. $lp_id = intval($lp_id);
  3879. $sql = "SELECT * FROM $tbl_lp
  3880. WHERE c_id = ".$course_id." AND id = $lp_id";
  3881. $result = Database::query($sql);
  3882. if (Database::num_rows($result)) {
  3883. $row = Database :: fetch_array($result);
  3884. $name = domesticate($row['name']);
  3885. if ($set_visibility == 'i') {
  3886. $s = $name . " " . get_lang('LearnpathNotPublished');
  3887. $dialogBox = $s;
  3888. $v = 0;
  3889. }
  3890. if ($set_visibility == 'v') {
  3891. $s = $name . " " . get_lang('LearnpathPublished');
  3892. $dialogBox = $s;
  3893. $v = 1;
  3894. }
  3895. } else {
  3896. return false;
  3897. }
  3898. $session_id = api_get_session_id();
  3899. $session_condition = api_get_session_condition($session_id);
  3900. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3901. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3902. $sql = "SELECT * FROM $tbl_tool
  3903. WHERE
  3904. c_id = ".$course_id." AND
  3905. link='$link' and
  3906. image='scormbuilder.gif' and
  3907. link LIKE '$link%'
  3908. $session_condition
  3909. ";
  3910. $result = Database::query($sql);
  3911. $num = Database :: num_rows($result);
  3912. if ($set_visibility == 'i' && $num > 0) {
  3913. $sql = "DELETE FROM $tbl_tool
  3914. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3915. Database::query($sql);
  3916. } elseif ($set_visibility == 'v' && $num == 0) {
  3917. $sql = "INSERT INTO $tbl_tool (category, c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3918. ('authoring', $course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3919. Database::query($sql);
  3920. $insertId = Database::insert_id();
  3921. if ($insertId) {
  3922. $sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $insertId";
  3923. Database::query($sql);
  3924. }
  3925. } elseif ($set_visibility == 'v' && $num > 0) {
  3926. $sql = "UPDATE $tbl_tool SET
  3927. c_id = $course_id,
  3928. name = '$name',
  3929. link = '$link',
  3930. image = 'scormbuilder.gif',
  3931. visibility = '$v',
  3932. admin = '0',
  3933. address = 'pastillegris.gif',
  3934. added_tool = 0,
  3935. session_id = $session_id
  3936. WHERE
  3937. c_id = ".$course_id." AND
  3938. (link='$link' and image='scormbuilder.gif' $session_condition)
  3939. ";
  3940. Database::query($sql);
  3941. } else {
  3942. // Parameter and database incompatible, do nothing, exit.
  3943. return false;
  3944. }
  3945. }
  3946. /**
  3947. * Restart the whole learnpath. Return the URL of the first element.
  3948. * Make sure the results are saved with anoter method. This method should probably be
  3949. * redefined in children classes.
  3950. * To use a similar method statically, use the create_new_attempt() method
  3951. * @return string URL to load in the viewer
  3952. */
  3953. public function restart()
  3954. {
  3955. if ($this->debug > 0) {
  3956. error_log('New LP - In learnpath::restart()', 0);
  3957. }
  3958. // TODO
  3959. // Call autosave method to save the current progress.
  3960. //$this->index = 0;
  3961. if (api_is_invitee()) {
  3962. return false;
  3963. }
  3964. $session_id = api_get_session_id();
  3965. $course_id = api_get_course_int_id();
  3966. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3967. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id)
  3968. VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3969. if ($this->debug > 2) {
  3970. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3971. }
  3972. $res = Database::query($sql);
  3973. $view_id = Database::insert_id();
  3974. if ($view_id) {
  3975. $sql = "UPDATE $lp_view_table SET id = iid WHERE iid = $view_id";
  3976. Database::query($sql);
  3977. $this->lp_view_id = $view_id;
  3978. $this->attempt = $this->attempt + 1;
  3979. } else {
  3980. $this->error = 'Could not insert into item_view table...';
  3981. return false;
  3982. }
  3983. $this->autocomplete_parents($this->current);
  3984. foreach ($this->items as $index => $dummy) {
  3985. $this->items[$index]->restart();
  3986. $this->items[$index]->set_lp_view($this->lp_view_id);
  3987. }
  3988. $this->first();
  3989. return true;
  3990. }
  3991. /**
  3992. * Saves the current item
  3993. * @return boolean
  3994. */
  3995. public function save_current()
  3996. {
  3997. if ($this->debug > 0) {
  3998. error_log('learnpath::save_current()', 0);
  3999. }
  4000. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4001. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4002. if ($this->debug > 2) {
  4003. error_log('New LP - save_current() saving item ' . $this->current, 0);
  4004. }
  4005. if ($this->debug > 2) {
  4006. error_log('' . print_r($this->items, true), 0);
  4007. }
  4008. if (isset($this->items[$this->current]) &&
  4009. is_object($this->items[$this->current])
  4010. ) {
  4011. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  4012. $this->autocomplete_parents($this->current);
  4013. $status = $this->items[$this->current]->get_status();
  4014. $this->update_queue[$this->current] = $status;
  4015. return $res;
  4016. }
  4017. return false;
  4018. }
  4019. /**
  4020. * Saves the given item
  4021. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  4022. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  4023. * @return boolean
  4024. */
  4025. public function save_item($item_id = null, $from_outside = true)
  4026. {
  4027. $debug = $this->debug;
  4028. if ($debug) {
  4029. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  4030. }
  4031. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  4032. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  4033. if (empty($item_id)) {
  4034. $item_id = intval($_REQUEST['id']);
  4035. }
  4036. if (empty($item_id)) {
  4037. $item_id = $this->get_current_item_id();
  4038. }
  4039. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4040. if ($debug) {
  4041. error_log('Object exists');
  4042. }
  4043. // Saving the item.
  4044. $res = $this->items[$item_id]->save(
  4045. $from_outside,
  4046. $this->prerequisites_match($item_id)
  4047. );
  4048. if ($debug) {
  4049. error_log('update_queue before:');
  4050. error_log(print_r($this->update_queue,1));
  4051. }
  4052. $this->autocomplete_parents($item_id);
  4053. $status = $this->items[$item_id]->get_status();
  4054. $this->update_queue[$item_id] = $status;
  4055. if ($debug) {
  4056. error_log('get_status(): ' . $status);
  4057. error_log('update_queue after:');
  4058. error_log(print_r($this->update_queue,1));
  4059. }
  4060. return $res;
  4061. }
  4062. return false;
  4063. }
  4064. /**
  4065. * Saves the last item seen's ID only in case
  4066. */
  4067. public function save_last()
  4068. {
  4069. $course_id = api_get_course_int_id();
  4070. if ($this->debug > 0) {
  4071. error_log('New LP - In learnpath::save_last()', 0);
  4072. }
  4073. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  4074. $table = Database :: get_course_table(TABLE_LP_VIEW);
  4075. if (isset($this->current) && !api_is_invitee()) {
  4076. if ($this->debug > 2) {
  4077. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  4078. }
  4079. $sql = "UPDATE $table SET
  4080. last_item = " . intval($this->get_current_item_id()). "
  4081. WHERE
  4082. c_id = $course_id AND
  4083. lp_id = " . $this->get_id() . " AND
  4084. user_id = " . $this->get_user_id()." ".$session_condition;
  4085. if ($this->debug > 2) {
  4086. error_log('New LP - Saving last item seen : ' . $sql, 0);
  4087. }
  4088. Database::query($sql);
  4089. }
  4090. if (!api_is_invitee()) {
  4091. // Save progress.
  4092. list($progress, $text) = $this->get_progress_bar_text('%');
  4093. if ($progress >= 0 && $progress <= 100) {
  4094. $progress = (int) $progress;
  4095. $sql = "UPDATE $table SET
  4096. progress = $progress
  4097. WHERE
  4098. c_id = ".$course_id." AND
  4099. lp_id = " . $this->get_id() . " AND
  4100. user_id = " . $this->get_user_id()." ".$session_condition;
  4101. // Ignore errors as some tables might not have the progress field just yet.
  4102. Database::query($sql);
  4103. $this->progress_db = $progress;
  4104. }
  4105. }
  4106. }
  4107. /**
  4108. * Sets the current item ID (checks if valid and authorized first)
  4109. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4110. */
  4111. public function set_current_item($item_id = null)
  4112. {
  4113. if ($this->debug > 0) {
  4114. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4115. }
  4116. if (empty ($item_id)) {
  4117. if ($this->debug > 2) {
  4118. error_log('New LP - No new current item given, ignore...', 0);
  4119. }
  4120. // Do nothing.
  4121. } else {
  4122. if ($this->debug > 2) {
  4123. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4124. }
  4125. if (is_numeric($item_id)) {
  4126. $item_id = intval($item_id);
  4127. // TODO: Check in database here.
  4128. $this->last = $this->current;
  4129. $this->current = $item_id;
  4130. // TODO: Update $this->index as well.
  4131. foreach ($this->ordered_items as $index => $item) {
  4132. if ($item == $this->current) {
  4133. $this->index = $index;
  4134. break;
  4135. }
  4136. }
  4137. if ($this->debug > 2) {
  4138. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4139. }
  4140. } else {
  4141. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4142. }
  4143. }
  4144. }
  4145. /**
  4146. * Sets the encoding
  4147. * @param string New encoding
  4148. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4149. */
  4150. public function set_encoding($enc = 'UTF-8')
  4151. {
  4152. if ($this->debug > 0) {
  4153. error_log('New LP - In learnpath::set_encoding()', 0);
  4154. }
  4155. $course_id = api_get_course_int_id();
  4156. if (empty($enc)) {
  4157. $enc = api_get_system_encoding();
  4158. }
  4159. $lp = $this->get_id();
  4160. if ($lp != 0) {
  4161. $tbl_lp = Database:: get_course_table(TABLE_LP_MAIN);
  4162. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4163. $res = Database::query($sql);
  4164. return $res;
  4165. }
  4166. return false;
  4167. }
  4168. /**
  4169. * Sets the JS lib setting in the database directly.
  4170. * This is the JavaScript library file this lp needs to load on startup
  4171. * @param string Proximity setting
  4172. * @return boolean True on update success. False otherwise.
  4173. */
  4174. public function set_jslib($lib = '')
  4175. {
  4176. if ($this->debug > 0) {
  4177. error_log('New LP - In learnpath::set_jslib()', 0);
  4178. }
  4179. $lp = $this->get_id();
  4180. $course_id = api_get_course_int_id();
  4181. if ($lp != 0) {
  4182. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4183. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4184. $res = Database::query($sql);
  4185. return $res;
  4186. } else {
  4187. return false;
  4188. }
  4189. }
  4190. /**
  4191. * Sets the name of the LP maker (publisher) (and save)
  4192. * @param string Optional string giving the new content_maker of this learnpath
  4193. * @return boolean True
  4194. */
  4195. public function set_maker($name = '')
  4196. {
  4197. if ($this->debug > 0) {
  4198. error_log('New LP - In learnpath::set_maker()', 0);
  4199. }
  4200. if (empty ($name))
  4201. return false;
  4202. $this->maker = $name;
  4203. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4204. $course_id = api_get_course_int_id();
  4205. $lp_id = $this->get_id();
  4206. $sql = "UPDATE $lp_table SET
  4207. content_maker = '" . Database::escape_string($this->maker) . "'
  4208. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4209. if ($this->debug > 2) {
  4210. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4211. }
  4212. Database::query($sql);
  4213. return true;
  4214. }
  4215. /**
  4216. * Sets the name of the current learnpath (and save)
  4217. * @param string $name Optional string giving the new name of this learnpath
  4218. * @return boolean True/False
  4219. */
  4220. public function set_name($name = null)
  4221. {
  4222. if ($this->debug > 0) {
  4223. error_log('New LP - In learnpath::set_name()', 0);
  4224. }
  4225. if (empty($name)) {
  4226. return false;
  4227. }
  4228. $this->name = Database::escape_string($name);
  4229. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4230. $lp_id = $this->get_id();
  4231. $course_id = $this->course_info['real_id'];
  4232. $sql = "UPDATE $lp_table SET
  4233. name = '" . Database::escape_string($this->name). "'
  4234. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4235. if ($this->debug > 2) {
  4236. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4237. }
  4238. $result = Database::query($sql);
  4239. // If the lp is visible on the homepage, change his name there.
  4240. if (Database::affected_rows($result)) {
  4241. $session_id = api_get_session_id();
  4242. $session_condition = api_get_session_condition($session_id);
  4243. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4244. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4245. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4246. WHERE
  4247. c_id = $course_id AND
  4248. (link='$link' AND image='scormbuilder.gif' $session_condition)";
  4249. Database::query($sql);
  4250. return true;
  4251. } else {
  4252. return false;
  4253. }
  4254. }
  4255. /**
  4256. * Set index specified prefix terms for all items in this path
  4257. * @param string Comma-separated list of terms
  4258. * @param char Xapian term prefix
  4259. * @return boolean False on error, true otherwise
  4260. */
  4261. public function set_terms_by_prefix($terms_string, $prefix)
  4262. {
  4263. $em = Database::getManager();
  4264. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  4265. if (api_get_setting('search.search_enabled') !== 'true')
  4266. return false;
  4267. if (!extension_loaded('xapian')) {
  4268. return false;
  4269. }
  4270. $terms_string = trim($terms_string);
  4271. $terms = explode(',', $terms_string);
  4272. array_walk($terms, 'trim_value');
  4273. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4274. // Don't do anything if no change, verify only at DB, not the search engine.
  4275. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4276. return false;
  4277. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4278. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4279. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4280. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4281. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4282. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4283. $lp_id = intval($_POST['lp_id']);
  4284. $sql = "SELECT * FROM $items_table WHERE c_id = {$course->getId()} AND lp_id = $lp_id";
  4285. $result = Database::query($sql);
  4286. $di = new ChamiloIndexer();
  4287. while ($lp_item = Database :: fetch_array($result)) {
  4288. $lpCourse = $em->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $this->cc]);
  4289. // Get search_did.
  4290. $se_ref = $em->getRepository('ChamiloCoreBundle:SearchEngineRef')
  4291. ->findOneBy([
  4292. 'course' => $lpCourse,
  4293. 'toolId' => TOOL_LEARNPATH,
  4294. 'refIdHighLevel' => $lp_id,
  4295. 'refIdSecondLevel' => $lp_item['id']
  4296. ]);
  4297. if ($se_ref) {
  4298. // Compare terms.
  4299. $doc = $di->get_document($se_ref->getSearchDid());
  4300. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4301. $xterms = array();
  4302. foreach ($xapian_terms as $xapian_term) {
  4303. $xterms[] = substr($xapian_term['name'], 1);
  4304. }
  4305. $dterms = $terms;
  4306. $missing_terms = array_diff($dterms, $xterms);
  4307. $deprecated_terms = array_diff($xterms, $dterms);
  4308. // Save it to search engine.
  4309. foreach ($missing_terms as $term) {
  4310. $doc->add_term($prefix . $term, 1);
  4311. }
  4312. foreach ($deprecated_terms as $term) {
  4313. $doc->remove_term($prefix . $term);
  4314. }
  4315. $di->getDb()->replace_document($se_ref->getSearchDid(), $doc);
  4316. $di->getDb()->flush();
  4317. } else {
  4318. //@todo What we should do here?
  4319. }
  4320. }
  4321. return true;
  4322. }
  4323. /**
  4324. * Sets the theme of the LP (local/remote) (and save)
  4325. * @param string Optional string giving the new theme of this learnpath
  4326. * @return bool Returns true if theme name is not empty
  4327. */
  4328. public function set_theme($name = '')
  4329. {
  4330. $course_id = api_get_course_int_id();
  4331. if ($this->debug > 0) {
  4332. error_log('New LP - In learnpath::set_theme()', 0);
  4333. }
  4334. $this->theme = $name;
  4335. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4336. $lp_id = $this->get_id();
  4337. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4338. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4339. if ($this->debug > 2) {
  4340. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4341. }
  4342. Database::query($sql);
  4343. return true;
  4344. }
  4345. /**
  4346. * Sets the image of an LP (and save)
  4347. * @param string Optional string giving the new image of this learnpath
  4348. * @return bool Returns true if theme name is not empty
  4349. */
  4350. public function set_preview_image($name = '')
  4351. {
  4352. $course_id = api_get_course_int_id();
  4353. if ($this->debug > 0) {
  4354. error_log('New LP - In learnpath::set_preview_image()', 0);
  4355. }
  4356. $this->preview_image = $name;
  4357. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4358. $lp_id = $this->get_id();
  4359. $sql = "UPDATE $lp_table SET
  4360. preview_image = '" . Database::escape_string($this->preview_image). "'
  4361. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4362. if ($this->debug > 2) {
  4363. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4364. }
  4365. Database::query($sql);
  4366. return true;
  4367. }
  4368. /**
  4369. * Sets the author of a LP (and save)
  4370. * @param string Optional string giving the new author of this learnpath
  4371. * @return bool Returns true if author's name is not empty
  4372. */
  4373. public function set_author($name = '')
  4374. {
  4375. $course_id = api_get_course_int_id();
  4376. if ($this->debug > 0) {
  4377. error_log('New LP - In learnpath::set_author()', 0);
  4378. }
  4379. $this->author = $name;
  4380. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4381. $lp_id = $this->get_id();
  4382. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4383. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4384. if ($this->debug > 2) {
  4385. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4386. }
  4387. Database::query($sql);
  4388. return true;
  4389. }
  4390. /**
  4391. * Sets the hide_toc_frame parameter of a LP (and save)
  4392. * @param int 1 if frame is hidden 0 then else
  4393. * @return bool Returns true if author's name is not empty
  4394. */
  4395. public function set_hide_toc_frame($hide)
  4396. {
  4397. $course_id = api_get_course_int_id();
  4398. if ($this->debug > 0) {
  4399. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4400. }
  4401. if (intval($hide) == $hide){
  4402. $this->hide_toc_frame = $hide;
  4403. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4404. $lp_id = $this->get_id();
  4405. $sql = "UPDATE $lp_table SET
  4406. hide_toc_frame = '" . $this->hide_toc_frame . "'
  4407. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4408. if ($this->debug > 2) {
  4409. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4410. }
  4411. Database::query($sql);
  4412. return true;
  4413. } else {
  4414. return false;
  4415. }
  4416. }
  4417. /**
  4418. * Sets the prerequisite of a LP (and save)
  4419. * @param int integer giving the new prerequisite of this learnpath
  4420. * @return bool returns true if prerequisite is not empty
  4421. */
  4422. public function set_prerequisite($prerequisite)
  4423. {
  4424. $course_id = api_get_course_int_id();
  4425. if ($this->debug > 0) {
  4426. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4427. }
  4428. $this->prerequisite = intval($prerequisite);
  4429. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4430. $lp_id = $this->get_id();
  4431. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4432. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4433. if ($this->debug > 2) {
  4434. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4435. }
  4436. Database::query($sql);
  4437. return true;
  4438. }
  4439. /**
  4440. * Sets the location/proximity of the LP (local/remote) (and save)
  4441. * @param string Optional string giving the new location of this learnpath
  4442. * @return boolean True on success / False on error
  4443. */
  4444. public function set_proximity($name = '')
  4445. {
  4446. $course_id = api_get_course_int_id();
  4447. if ($this->debug > 0) {
  4448. error_log('New LP - In learnpath::set_proximity()', 0);
  4449. }
  4450. if (empty ($name))
  4451. return false;
  4452. $this->proximity = $name;
  4453. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4454. $lp_id = $this->get_id();
  4455. $sql = "UPDATE $lp_table SET
  4456. content_local = '" . Database::escape_string($name) . "'
  4457. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4458. if ($this->debug > 2) {
  4459. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4460. }
  4461. Database::query($sql);
  4462. return true;
  4463. }
  4464. /**
  4465. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4466. * @param integer DB ID of the item
  4467. */
  4468. public function set_previous_item($id)
  4469. {
  4470. if ($this->debug > 0) {
  4471. error_log('New LP - In learnpath::set_previous_item()', 0);
  4472. }
  4473. $this->last = $id;
  4474. }
  4475. /**
  4476. * Sets use_max_score
  4477. * @param string $use_max_score Optional string giving the new location of this learnpath
  4478. * @return boolean True on success / False on error
  4479. */
  4480. public function set_use_max_score($use_max_score = 1)
  4481. {
  4482. $course_id = api_get_course_int_id();
  4483. if ($this->debug > 0) {
  4484. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4485. }
  4486. $use_max_score = intval($use_max_score);
  4487. $this->use_max_score = $use_max_score;
  4488. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4489. $lp_id = $this->get_id();
  4490. $sql = "UPDATE $lp_table SET
  4491. use_max_score = '" . $this->use_max_score . "'
  4492. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4493. if ($this->debug > 2) {
  4494. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4495. }
  4496. Database::query($sql);
  4497. return true;
  4498. }
  4499. /**
  4500. * Sets and saves the expired_on date
  4501. * @param string $expired_on Optional string giving the new author of this learnpath
  4502. * @return bool Returns true if author's name is not empty
  4503. */
  4504. public function set_expired_on($expired_on)
  4505. {
  4506. $course_id = api_get_course_int_id();
  4507. if ($this->debug > 0) {
  4508. error_log('New LP - In learnpath::set_expired_on()', 0);
  4509. }
  4510. if (!empty($expired_on)) {
  4511. $this->expired_on = api_get_utc_datetime($expired_on);
  4512. } else {
  4513. $this->expired_on = null;
  4514. }
  4515. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4516. $lp_id = $this->get_id();
  4517. if ($this->debug > 2) {
  4518. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4519. }
  4520. $params = [
  4521. 'expired_on' => $this->expired_on,
  4522. ];
  4523. Database::update($lp_table, $params, ['c_id = ? AND id = ?' => [$course_id, $lp_id]], true);
  4524. return true;
  4525. }
  4526. /**
  4527. * Sets and saves the publicated_on date
  4528. * @param string $publicated_on Optional string giving the new author of this learnpath
  4529. * @return bool Returns true if author's name is not empty
  4530. */
  4531. public function set_publicated_on($publicated_on)
  4532. {
  4533. $course_id = api_get_course_int_id();
  4534. if ($this->debug > 0) {
  4535. error_log('New LP - In learnpath::set_expired_on()', 0);
  4536. }
  4537. if (!empty($publicated_on)) {
  4538. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4539. } else {
  4540. $this->publicated_on = '';
  4541. }
  4542. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4543. $lp_id = $this->get_id();
  4544. $sql = "UPDATE $lp_table SET
  4545. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4546. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4547. if ($this->debug > 2) {
  4548. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4549. }
  4550. Database::query($sql);
  4551. return true;
  4552. }
  4553. /**
  4554. * Sets and saves the expired_on date
  4555. * @return bool Returns true if author's name is not empty
  4556. */
  4557. public function set_modified_on()
  4558. {
  4559. $course_id = api_get_course_int_id();
  4560. if ($this->debug > 0) {
  4561. error_log('New LP - In learnpath::set_expired_on()', 0);
  4562. }
  4563. $this->modified_on = api_get_utc_datetime();
  4564. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4565. $lp_id = $this->get_id();
  4566. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4567. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4568. if ($this->debug > 2) {
  4569. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4570. }
  4571. Database::query($sql);
  4572. return true;
  4573. }
  4574. /**
  4575. * Sets the object's error message
  4576. * @param string Error message. If empty, reinits the error string
  4577. * @return void
  4578. */
  4579. public function set_error_msg($error = '')
  4580. {
  4581. if ($this->debug > 0) {
  4582. error_log('New LP - In learnpath::set_error_msg()', 0);
  4583. }
  4584. if (empty ($error)) {
  4585. $this->error = '';
  4586. } else {
  4587. $this->error .= $error;
  4588. }
  4589. }
  4590. /**
  4591. * Launches the current item if not 'sco'
  4592. * (starts timer and make sure there is a record ready in the DB)
  4593. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4594. * @return boolean
  4595. */
  4596. public function start_current_item($allow_new_attempt = false)
  4597. {
  4598. if ($this->debug > 0) {
  4599. error_log('New LP - In learnpath::start_current_item()', 0);
  4600. }
  4601. if ($this->current != 0 && is_object($this->items[$this->current])) {
  4602. $type = $this->get_type();
  4603. $item_type = $this->items[$this->current]->get_type();
  4604. if (($type == 2 && $item_type != 'sco') ||
  4605. ($type == 3 && $item_type != 'au') ||
  4606. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4607. ) {
  4608. $this->items[$this->current]->open($allow_new_attempt);
  4609. $this->autocomplete_parents($this->current);
  4610. $prereq_check = $this->prerequisites_match($this->current);
  4611. $this->items[$this->current]->save(false, $prereq_check);
  4612. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4613. } else {
  4614. // If sco, then it is supposed to have been updated by some other call.
  4615. }
  4616. if ($item_type == 'sco') {
  4617. $this->items[$this->current]->restart();
  4618. }
  4619. }
  4620. if ($this->debug > 0) {
  4621. error_log('New LP - End of learnpath::start_current_item()', 0);
  4622. }
  4623. return true;
  4624. }
  4625. /**
  4626. * Stops the processing and counters for the old item (as held in $this->last)
  4627. * @return boolean True/False
  4628. */
  4629. public function stop_previous_item()
  4630. {
  4631. if ($this->debug > 0) {
  4632. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4633. }
  4634. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4635. if ($this->debug > 2) {
  4636. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4637. }
  4638. switch ($this->get_type()) {
  4639. case '3' :
  4640. if ($this->items[$this->last]->get_type() != 'au') {
  4641. if ($this->debug > 2) {
  4642. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4643. }
  4644. $this->items[$this->last]->close();
  4645. //$this->autocomplete_parents($this->last);
  4646. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4647. } else {
  4648. if ($this->debug > 2) {
  4649. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4650. }
  4651. }
  4652. case '2' :
  4653. if ($this->items[$this->last]->get_type() != 'sco') {
  4654. if ($this->debug > 2) {
  4655. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4656. }
  4657. $this->items[$this->last]->close();
  4658. //$this->autocomplete_parents($this->last);
  4659. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4660. } else {
  4661. if ($this->debug > 2) {
  4662. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4663. }
  4664. }
  4665. break;
  4666. case '1' :
  4667. default :
  4668. if ($this->debug > 2) {
  4669. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4670. }
  4671. $this->items[$this->last]->close();
  4672. break;
  4673. }
  4674. } else {
  4675. if ($this->debug > 2) {
  4676. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4677. }
  4678. return false;
  4679. }
  4680. return true;
  4681. }
  4682. /**
  4683. * Updates the default view mode from fullscreen to embedded and inversely
  4684. * @return string The current default view mode ('fullscreen' or 'embedded')
  4685. */
  4686. public function update_default_view_mode()
  4687. {
  4688. $course_id = api_get_course_int_id();
  4689. if ($this->debug > 0) {
  4690. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4691. }
  4692. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4693. $sql = "SELECT * FROM $lp_table
  4694. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4695. $res = Database::query($sql);
  4696. if (Database :: num_rows($res) > 0) {
  4697. $row = Database :: fetch_array($res);
  4698. $default_view_mode = $row['default_view_mod'];
  4699. $view_mode = $default_view_mode;
  4700. switch ($default_view_mode) {
  4701. case 'fullscreen': // default with popup
  4702. $view_mode = 'embedded';
  4703. break;
  4704. case 'embedded': // default view with left menu
  4705. $view_mode = 'embedframe';
  4706. break;
  4707. case 'embedframe': //folded menu
  4708. $view_mode = 'impress';
  4709. break;
  4710. case 'impress':
  4711. $view_mode = 'fullscreen';
  4712. break;
  4713. }
  4714. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4715. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4716. Database::query($sql);
  4717. $this->mode = $view_mode;
  4718. return $view_mode;
  4719. } else {
  4720. if ($this->debug > 2) {
  4721. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4722. }
  4723. }
  4724. return -1;
  4725. }
  4726. /**
  4727. * Updates the default behaviour about auto-commiting SCORM updates
  4728. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4729. */
  4730. public function update_default_scorm_commit()
  4731. {
  4732. $course_id = api_get_course_int_id();
  4733. if ($this->debug > 0) {
  4734. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4735. }
  4736. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4737. $sql = "SELECT * FROM $lp_table
  4738. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4739. $res = Database::query($sql);
  4740. if (Database :: num_rows($res) > 0) {
  4741. $row = Database :: fetch_array($res);
  4742. $force = $row['force_commit'];
  4743. if ($force == 1) {
  4744. $force = 0;
  4745. $force_return = false;
  4746. } elseif ($force == 0) {
  4747. $force = 1;
  4748. $force_return = true;
  4749. }
  4750. $sql = "UPDATE $lp_table SET force_commit = $force
  4751. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4752. Database::query($sql);
  4753. $this->force_commit = $force_return;
  4754. return $force_return;
  4755. } else {
  4756. if ($this->debug > 2) {
  4757. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4758. }
  4759. }
  4760. return -1;
  4761. }
  4762. /**
  4763. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4764. * @return bool True on success, false on failure
  4765. */
  4766. public function update_display_order()
  4767. {
  4768. $course_id = api_get_course_int_id();
  4769. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4770. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4771. $res = Database::query($sql);
  4772. if ($res === false)
  4773. return false;
  4774. $num = Database :: num_rows($res);
  4775. // First check the order is correct, globally (might be wrong because
  4776. // of versions < 1.8.4).
  4777. if ($num > 0) {
  4778. $i = 1;
  4779. while ($row = Database :: fetch_array($res)) {
  4780. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4781. $need_fix = true;
  4782. $sql = "UPDATE $lp_table SET display_order = $i
  4783. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4784. Database::query($sql);
  4785. }
  4786. $i++;
  4787. }
  4788. }
  4789. return true;
  4790. }
  4791. /**
  4792. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4793. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4794. */
  4795. public function update_reinit()
  4796. {
  4797. $course_id = api_get_course_int_id();
  4798. if ($this->debug > 0) {
  4799. error_log('New LP - In learnpath::update_reinit()', 0);
  4800. }
  4801. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4802. $sql = "SELECT * FROM $lp_table
  4803. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4804. $res = Database::query($sql);
  4805. if (Database :: num_rows($res) > 0) {
  4806. $row = Database :: fetch_array($res);
  4807. $force = $row['prevent_reinit'];
  4808. if ($force == 1) {
  4809. $force = 0;
  4810. } elseif ($force == 0) {
  4811. $force = 1;
  4812. }
  4813. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4814. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4815. Database::query($sql);
  4816. $this->prevent_reinit = $force;
  4817. return $force;
  4818. } else {
  4819. if ($this->debug > 2) {
  4820. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4821. }
  4822. }
  4823. return -1;
  4824. }
  4825. /**
  4826. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4827. *
  4828. * @return string 'single', 'multi' or 'seriousgame'
  4829. * @author ndiechburg <noel@cblue.be>
  4830. **/
  4831. public function get_attempt_mode()
  4832. {
  4833. //Set default value for seriousgame_mode
  4834. if (!isset($this->seriousgame_mode)) {
  4835. $this->seriousgame_mode=0;
  4836. }
  4837. // Set default value for prevent_reinit
  4838. if (!isset($this->prevent_reinit)) {
  4839. $this->prevent_reinit =1;
  4840. }
  4841. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4842. return 'seriousgame';
  4843. }
  4844. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4845. return 'single';
  4846. }
  4847. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4848. return 'multiple';
  4849. }
  4850. return 'single';
  4851. }
  4852. /**
  4853. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4854. *
  4855. * @param string 'seriousgame', 'single' or 'multiple'
  4856. * @return boolean
  4857. * @author ndiechburg <noel@cblue.be>
  4858. **/
  4859. public function set_attempt_mode($mode)
  4860. {
  4861. $course_id = api_get_course_int_id();
  4862. switch ($mode) {
  4863. case 'seriousgame' :
  4864. $sg_mode = 1;
  4865. $prevent_reinit = 1;
  4866. break;
  4867. case 'single' :
  4868. $sg_mode = 0;
  4869. $prevent_reinit = 1;
  4870. break;
  4871. case 'multiple' :
  4872. $sg_mode = 0;
  4873. $prevent_reinit = 0;
  4874. break;
  4875. default :
  4876. $sg_mode = 0;
  4877. $prevent_reinit = 0;
  4878. break;
  4879. }
  4880. $this->prevent_reinit = $prevent_reinit;
  4881. $this->seriousgame_mode = $sg_mode;
  4882. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4883. $sql = "UPDATE $lp_table SET
  4884. prevent_reinit = $prevent_reinit ,
  4885. seriousgame_mode = $sg_mode
  4886. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4887. $res = Database::query($sql);
  4888. if ($res) {
  4889. return true;
  4890. } else {
  4891. return false;
  4892. }
  4893. }
  4894. /**
  4895. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4896. *
  4897. * @return boolean
  4898. * @author ndiechburg <noel@cblue.be>
  4899. **/
  4900. public function switch_attempt_mode()
  4901. {
  4902. if ($this->debug > 0) {
  4903. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4904. }
  4905. $mode = $this->get_attempt_mode();
  4906. switch ($mode) {
  4907. case 'single' :
  4908. $next_mode = 'multiple';
  4909. break;
  4910. case 'multiple' :
  4911. $next_mode = 'seriousgame';
  4912. break;
  4913. case 'seriousgame' :
  4914. $next_mode = 'single';
  4915. break;
  4916. default :
  4917. $next_mode = 'single';
  4918. break;
  4919. }
  4920. $this->set_attempt_mode($next_mode);
  4921. }
  4922. /**
  4923. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4924. * but possibility to do again a completed item.
  4925. *
  4926. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4927. * @author ndiechburg <noel@cblue.be>
  4928. **/
  4929. public function set_seriousgame_mode()
  4930. {
  4931. $course_id = api_get_course_int_id();
  4932. if ($this->debug > 0) {
  4933. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4934. }
  4935. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4936. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4937. $res = Database::query($sql);
  4938. if (Database :: num_rows($res) > 0) {
  4939. $row = Database :: fetch_array($res);
  4940. $force = $row['seriousgame_mode'];
  4941. if ($force == 1) {
  4942. $force = 0;
  4943. } elseif ($force == 0) {
  4944. $force = 1;
  4945. }
  4946. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4947. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4948. Database::query($sql);
  4949. $this->seriousgame_mode = $force;
  4950. return $force;
  4951. } else {
  4952. if ($this->debug > 2) {
  4953. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4954. }
  4955. }
  4956. return -1;
  4957. }
  4958. /**
  4959. * Updates the "scorm_debug" value that shows or hide the debug window
  4960. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4961. */
  4962. public function update_scorm_debug()
  4963. {
  4964. $course_id = api_get_course_int_id();
  4965. if ($this->debug > 0) {
  4966. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4967. }
  4968. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4969. $sql = "SELECT * FROM $lp_table
  4970. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4971. $res = Database::query($sql);
  4972. if (Database :: num_rows($res) > 0) {
  4973. $row = Database :: fetch_array($res);
  4974. $force = $row['debug'];
  4975. if ($force == 1) {
  4976. $force = 0;
  4977. } elseif ($force == 0) {
  4978. $force = 1;
  4979. }
  4980. $sql = "UPDATE $lp_table SET debug = $force
  4981. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4982. $res = Database::query($sql);
  4983. $this->scorm_debug = $force;
  4984. return $force;
  4985. } else {
  4986. if ($this->debug > 2) {
  4987. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4988. }
  4989. }
  4990. return -1;
  4991. }
  4992. /**
  4993. * Function that makes a call to the function sort_tree_array and create_tree_array
  4994. * @author Kevin Van Den Haute
  4995. * @param array
  4996. */
  4997. public function tree_array($array)
  4998. {
  4999. if ($this->debug > 1) {
  5000. error_log('New LP - In learnpath::tree_array()', 0);
  5001. }
  5002. $array = $this->sort_tree_array($array);
  5003. $this->create_tree_array($array);
  5004. }
  5005. /**
  5006. * Creates an array with the elements of the learning path tree in it
  5007. *
  5008. * @author Kevin Van Den Haute
  5009. * @param array $array
  5010. * @param int $parent
  5011. * @param int $depth
  5012. * @param array $tmp
  5013. */
  5014. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ())
  5015. {
  5016. if ($this->debug > 1) {
  5017. error_log('New LP - In learnpath::create_tree_array())', 0);
  5018. }
  5019. if (is_array($array)) {
  5020. for ($i = 0; $i < count($array); $i++) {
  5021. if ($array[$i]['parent_item_id'] == $parent) {
  5022. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  5023. $tmp[] = $array[$i]['parent_item_id'];
  5024. $depth++;
  5025. }
  5026. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  5027. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  5028. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  5029. $prerequisiteMinScore = isset($array[$i]['prerequisite_min_score']) ? $array[$i]['prerequisite_min_score'] : null;
  5030. $prerequisiteMaxScore = isset($array[$i]['prerequisite_max_score']) ? $array[$i]['prerequisite_max_score'] : null;
  5031. $ref = isset($array[$i]['ref']) ? $array[$i]['ref'] : '';
  5032. $this->arrMenu[] = array(
  5033. 'id' => $array[$i]['id'],
  5034. 'ref' => $ref,
  5035. 'item_type' => $array[$i]['item_type'],
  5036. 'title' => $array[$i]['title'],
  5037. 'path' => $path,
  5038. 'description' => $array[$i]['description'],
  5039. 'parent_item_id' => $array[$i]['parent_item_id'],
  5040. 'previous_item_id' => $array[$i]['previous_item_id'],
  5041. 'next_item_id' => $array[$i]['next_item_id'],
  5042. 'min_score' => $array[$i]['min_score'],
  5043. 'max_score' => $array[$i]['max_score'],
  5044. 'mastery_score' => $array[$i]['mastery_score'],
  5045. 'display_order' => $array[$i]['display_order'],
  5046. 'prerequisite' => $preq,
  5047. 'depth' => $depth,
  5048. 'audio' => $audio,
  5049. 'prerequisite_min_score' => $prerequisiteMinScore,
  5050. 'prerequisite_max_score' => $prerequisiteMaxScore
  5051. );
  5052. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5053. }
  5054. }
  5055. }
  5056. }
  5057. /**
  5058. * Sorts a multi dimensional array by parent id and display order
  5059. * @author Kevin Van Den Haute
  5060. *
  5061. * @param array $array (array with al the learning path items in it)
  5062. *
  5063. * @return array
  5064. */
  5065. public function sort_tree_array($array) {
  5066. foreach ($array as $key => $row) {
  5067. $parent[$key] = $row['parent_item_id'];
  5068. $position[$key] = $row['display_order'];
  5069. }
  5070. if (count($array) > 0)
  5071. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5072. return $array;
  5073. }
  5074. /**
  5075. * Function that creates a html list of learning path items so that we can add audio files to them
  5076. * @author Kevin Van Den Haute
  5077. * @param int $lp_id
  5078. * @return string
  5079. */
  5080. public function overview()
  5081. {
  5082. $learnPath = self::getCurrentLpFromSession();
  5083. if ($this->debug > 0) {
  5084. error_log('New LP - In learnpath::overview()', 0);
  5085. }
  5086. $return = '';
  5087. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5088. // we need to start a form when we want to update all the mp3 files
  5089. if ($update_audio == 'true') {
  5090. $return .= '<form action="'.api_get_self().
  5091. '?cidReq='.Security:: remove_XSS($_GET['cidReq']).
  5092. '&updateaudio='.Security:: remove_XSS($_GET['updateaudio']).
  5093. '&action='.Security:: remove_XSS($_GET['action']).
  5094. '&lp_id='.$learnPath->lp_id.'"
  5095. method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3"
  5096. >';
  5097. }
  5098. $return .= '<div id="message"></div>';
  5099. if (count($this->items) == 0) {
  5100. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  5101. } else {
  5102. $return_audio = '<table class="data_table">';
  5103. $return_audio .= '<tr>';
  5104. $return_audio .= '<th width="40%">' . get_lang('Title') . '</th>';
  5105. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  5106. $return_audio .= '</tr>';
  5107. if ($update_audio != 'true') {
  5108. $return .= '<div class="col-md-12">';
  5109. $return .= self::return_new_tree($update_audio);
  5110. $return .='</div>';
  5111. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn btn-primary')), array('style'=>'float:left; margin-top:15px;width:100%'));
  5112. } else {
  5113. $return_audio .= self::return_new_tree($update_audio);
  5114. $return .= $return_audio.'</table>';
  5115. }
  5116. // We need to close the form when we are updating the mp3 files.
  5117. if ($update_audio == 'true') {
  5118. $return .= '<div class="footer-audio">';
  5119. $return .= Display::button('save_audio','<em class="fa fa-file-audio-o"></em> '. get_lang('SaveAudioAndOrganization'),array('class'=>'btn btn-primary','type'=>'submit'));
  5120. $return .= '</div>';
  5121. //$return .= '<div><button class="btn btn-primary" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  5122. }
  5123. }
  5124. // We need to close the form when we are updating the mp3 files.
  5125. if ($update_audio == 'true' && isset($this->arrMenu) && count(
  5126. $this->arrMenu
  5127. ) != 0
  5128. ) {
  5129. $return .= '</form>';
  5130. }
  5131. return $return;
  5132. }
  5133. /**
  5134. * @param string string $update_audio
  5135. * @param bool $drop_element_here
  5136. * @return string
  5137. */
  5138. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5139. {
  5140. $return = '';
  5141. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5142. $course_id = api_get_course_int_id();
  5143. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5144. $sql = "SELECT * FROM $tbl_lp_item
  5145. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5146. $result = Database::query($sql);
  5147. $arrLP = array();
  5148. while ($row = Database :: fetch_array($result)) {
  5149. $arrLP[] = array(
  5150. 'id' => $row['id'],
  5151. 'item_type' => $row['item_type'],
  5152. 'title' => Security :: remove_XSS($row['title']),
  5153. 'path' => $row['path'],
  5154. 'description' => Security::remove_XSS($row['description']),
  5155. 'parent_item_id' => $row['parent_item_id'],
  5156. 'previous_item_id' => $row['previous_item_id'],
  5157. 'next_item_id' => $row['next_item_id'],
  5158. 'max_score' => $row['max_score'],
  5159. 'min_score' => $row['min_score'],
  5160. 'mastery_score' => $row['mastery_score'],
  5161. 'prerequisite' => $row['prerequisite'],
  5162. 'display_order' => $row['display_order'],
  5163. 'audio' => $row['audio'],
  5164. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  5165. 'prerequisite_min_score' => $row['prerequisite_min_score']
  5166. );
  5167. }
  5168. $this->tree_array($arrLP);
  5169. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  5170. unset ($this->arrMenu);
  5171. $default_data = null;
  5172. $default_content = null;
  5173. $elements = array();
  5174. $return_audio = null;
  5175. for ($i = 0; $i < count($arrLP); $i++) {
  5176. $title = $arrLP[$i]['title'];
  5177. $title_cut = cut($arrLP[$i]['title'], 25);
  5178. $url = api_get_self() . '?'.api_get_cidreq().'&action=view_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id;
  5179. $title_cut = Display::url(
  5180. $title_cut,
  5181. $url,
  5182. array(
  5183. 'class' => 'ajax moved',
  5184. 'data-title' => $title_cut
  5185. )
  5186. );
  5187. // Detect if type is FINAL_ITEM to set path_id to SESSION
  5188. if ($arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  5189. $_SESSION['pathItem'] = $arrLP[$i]['path'];
  5190. }
  5191. if (($i % 2) == 0) {
  5192. $oddClass = 'row_odd';
  5193. } else {
  5194. $oddClass = 'row_even';
  5195. }
  5196. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5197. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5198. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5199. $icon = Display::return_icon('lp_' . $icon_name . '.png');
  5200. } else {
  5201. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5202. $icon = Display::return_icon('lp_' . $icon_name . '.gif');
  5203. } else {
  5204. if ($arrLP[$i]['item_type'] === TOOL_LP_FINAL_ITEM) {
  5205. $icon = Display::return_icon('certificate.png');
  5206. } else {
  5207. $icon = Display::return_icon('folder_document.gif');
  5208. }
  5209. }
  5210. }
  5211. // The audio column.
  5212. $return_audio .= '<td align="left" style="padding-left:10px;">';
  5213. $audio = '';
  5214. if (!$update_audio || $update_audio <> 'true') {
  5215. if (!empty($arrLP[$i]['audio'])) {
  5216. } else {
  5217. $audio .= '';
  5218. }
  5219. } else {
  5220. $types = self::getChapterTypes();
  5221. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5222. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5223. if (!empty ($arrLP[$i]['audio'])) {
  5224. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5225. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5226. }
  5227. }
  5228. }
  5229. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5230. $return_audio .= '</td>';
  5231. $move_icon = '';
  5232. $move_item_icon = '';
  5233. $edit_icon = '';
  5234. $delete_icon = '';
  5235. $audio_icon = '';
  5236. $prerequisities_icon = '';
  5237. $forumIcon = '';
  5238. $previewIcon = '';
  5239. if ($is_allowed_to_edit) {
  5240. if (!$update_audio || $update_audio <> 'true') {
  5241. if ($arrLP[$i]['item_type'] !== TOOL_LP_FINAL_ITEM) {
  5242. $move_icon .= '<a class="moved" href="#">';
  5243. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5244. $move_icon .= '</a>';
  5245. }
  5246. }
  5247. // No edit for this item types
  5248. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset', 'final_item'))) {
  5249. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5250. $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">';
  5251. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5252. $edit_icon .= '</a>';
  5253. if (!in_array($arrLP[$i]['item_type'], ['forum', 'thread'])) {
  5254. if (
  5255. $this->items[$arrLP[$i]['id']]->getForumThread(
  5256. $this->course_int_id,
  5257. $this->lp_session_id
  5258. )
  5259. ) {
  5260. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5261. 'action' => 'dissociate_forum',
  5262. 'id' => $arrLP[$i]['id'],
  5263. 'lp_id' => $this->lp_id
  5264. ]);
  5265. $forumIcon = Display::url(
  5266. Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY),
  5267. $forumIconUrl,
  5268. ['class' => 'btn btn-default lp-btn-dissociate-forum']
  5269. );
  5270. } else {
  5271. $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  5272. 'action' => 'create_forum',
  5273. 'id' => $arrLP[$i]['id'],
  5274. 'lp_id' => $this->lp_id
  5275. ]);
  5276. $forumIcon = Display::url(
  5277. Display::return_icon('forum.png', get_lang('AssociateForumToLPItem'), [], ICON_SIZE_TINY),
  5278. $forumIconUrl,
  5279. ['class' => "btn btn-default lp-btn-associate-forum"]
  5280. );
  5281. }
  5282. }
  5283. } else {
  5284. $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">';
  5285. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5286. $edit_icon .= '</a>';
  5287. }
  5288. }
  5289. $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">';
  5290. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5291. $delete_icon .= '</a>';
  5292. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5293. if ($arrLP[$i]['item_type'] == 'document') {
  5294. $urlPreviewLink = api_get_self().'?'.api_get_cidreq().'&action=view_item&mode=preview_document&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5295. $previewIcon = Display::url(
  5296. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5297. $urlPreviewLink,
  5298. array(
  5299. 'class' => 'btn btn-default ajax',
  5300. 'data-title' => $arrLP[$i]['title']
  5301. )
  5302. );
  5303. } else {
  5304. $previewIcon = Display::url(
  5305. Display::return_icon('preview_view.png', get_lang('Preview'), array(), ICON_SIZE_TINY),
  5306. $url.'&action=view_item', ['class' => 'btn btn-default']
  5307. );
  5308. }
  5309. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5310. $prerequisities_icon = Display::url(
  5311. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5312. $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']
  5313. );
  5314. $move_item_icon = Display::url(
  5315. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5316. $url.'&action=move_item',
  5317. ['class' => 'btn btn-default']
  5318. );
  5319. $audio_icon = Display::url(
  5320. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5321. $url.'&action=add_audio',
  5322. ['class' => 'btn btn-default']
  5323. );
  5324. }
  5325. }
  5326. if ($update_audio != 'true') {
  5327. $row = $move_icon . ' ' . $icon .
  5328. Display::span($title_cut) .
  5329. Display::tag(
  5330. 'div',
  5331. "<div class=\"btn-group btn-group-xs\">$previewIcon $audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
  5332. array('class'=>'btn-toolbar button_actions')
  5333. );
  5334. } else {
  5335. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5336. }
  5337. $parent_id = $arrLP[$i]['parent_item_id'];
  5338. $default_data[$arrLP[$i]['id']] = $row;
  5339. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5340. if (empty($parent_id)) {
  5341. $elements[$arrLP[$i]['id']]['data'] = $row;
  5342. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5343. } else {
  5344. $parent_arrays = array();
  5345. if ($arrLP[$i]['depth'] > 1) {
  5346. //Getting list of parents
  5347. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5348. foreach($arrLP as $item) {
  5349. if ($item['id'] == $parent_id) {
  5350. if ($item['parent_item_id'] == 0) {
  5351. $parent_id = $item['id'];
  5352. break;
  5353. } else {
  5354. $parent_id = $item['parent_item_id'];
  5355. if (empty($parent_arrays)) {
  5356. $parent_arrays[] = intval($item['id']);
  5357. }
  5358. $parent_arrays[] = $parent_id;
  5359. break;
  5360. }
  5361. }
  5362. }
  5363. }
  5364. }
  5365. if (!empty($parent_arrays)) {
  5366. $parent_arrays = array_reverse($parent_arrays);
  5367. $val = '$elements';
  5368. $x = 0;
  5369. foreach($parent_arrays as $item) {
  5370. if ($x != count($parent_arrays) -1) {
  5371. $val .= '["'.$item.'"]["children"]';
  5372. } else {
  5373. $val .= '["'.$item.'"]["children"]';
  5374. }
  5375. $x++;
  5376. }
  5377. $val .= "";
  5378. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5379. eval($code_str);
  5380. } else {
  5381. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5382. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5383. }
  5384. }
  5385. }
  5386. $list = '<ul id="lp_item_list">';
  5387. $tree = self::print_recursive($elements, $default_data, $default_content);
  5388. if (!empty($tree)) {
  5389. $list .= $tree;
  5390. } else {
  5391. if ($drop_element_here) {
  5392. $list .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5393. }
  5394. }
  5395. $list .= '</ul>';
  5396. $return .= Display::panelCollapse(
  5397. $this->name,
  5398. $list,
  5399. 'scorm-list',
  5400. null,
  5401. 'scorm-list-accordion',
  5402. 'scorm-list-collapse'
  5403. );
  5404. if ($update_audio == 'true') {
  5405. $return = $return_audio;
  5406. }
  5407. return $return;
  5408. }
  5409. /**
  5410. * @param array $elements
  5411. * @param array $default_data
  5412. * @param array $default_content
  5413. * @return string
  5414. */
  5415. public function print_recursive($elements, $default_data, $default_content)
  5416. {
  5417. $return = '';
  5418. foreach ($elements as $key => $item) {
  5419. if (isset($item['load_data']) || empty($item['data'])) {
  5420. $item['data'] = $default_data[$item['load_data']];
  5421. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5422. }
  5423. $sub_list = '';
  5424. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5425. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5426. }
  5427. if (empty($item['children'])) {
  5428. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5429. $active = null;
  5430. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5431. $active = 'active';
  5432. }
  5433. $return .= Display::tag(
  5434. 'li',
  5435. Display::div($item['data'], array('class'=>"item_data $active")).$sub_list,
  5436. array('id'=>$key, 'class'=>'record li_container')
  5437. );
  5438. } else {
  5439. // Sections
  5440. if (isset($item['children'])) {
  5441. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5442. }
  5443. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5444. $return .= Display::tag(
  5445. 'li',
  5446. Display::div($item['data'], array('class'=>'item_data')).$sub_list,
  5447. array('id'=>$key, 'class'=>'record li_container')
  5448. );
  5449. }
  5450. }
  5451. return $return;
  5452. }
  5453. /**
  5454. * This function builds the action menu
  5455. * @param bool $returnContent
  5456. * @return void
  5457. */
  5458. public function build_action_menu($returnContent = false)
  5459. {
  5460. $learnPath = self::getCurrentLpFromSession();
  5461. $return = '<div class="actions">';
  5462. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5463. ).'&action=view&lp_id='.$learnPath->lp_id.'&isStudentView=true">'.Display:: return_icon(
  5464. 'preview_view.png',
  5465. get_lang('Display'),
  5466. '',
  5467. ICON_SIZE_MEDIUM
  5468. ).'</a> ';
  5469. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5470. ).'&action=admin_view&lp_id='.$learnPath->lp_id.'&updateaudio=true">'.Display:: return_icon(
  5471. 'upload_audio.png',
  5472. get_lang('UpdateAllAudioFragments'),
  5473. '',
  5474. ICON_SIZE_MEDIUM
  5475. ).'</a>';
  5476. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5477. ).'&action=edit&lp_id='.$learnPath->lp_id.'">'.Display:: return_icon(
  5478. 'settings.png',
  5479. get_lang('CourseSettings'),
  5480. '',
  5481. ICON_SIZE_MEDIUM
  5482. ).'</a>';
  5483. $buttons = array(
  5484. array(
  5485. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5486. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5487. ).'&action=set_previous_step_as_prerequisite&lp_id='.$learnPath->lp_id,
  5488. ),
  5489. array(
  5490. 'title' => get_lang('ClearAllPrerequisites'),
  5491. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5492. ).'&action=clear_prerequisites&lp_id='.$learnPath->lp_id,
  5493. ),
  5494. );
  5495. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5496. $return .= '</div>';
  5497. if ($returnContent) {
  5498. return $return;
  5499. }
  5500. echo $return;
  5501. }
  5502. /**
  5503. * Creates the default learning path folder
  5504. * @param array $course
  5505. * @param int $creatorId
  5506. *
  5507. * @return bool
  5508. */
  5509. public static function generate_learning_path_folder($course, $creatorId = 0)
  5510. {
  5511. // Creating learning_path folder
  5512. $dir = '/learning_path';
  5513. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5514. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5515. $folder = false;
  5516. if (!is_dir($filepath.'/'.$dir)) {
  5517. $folderData = create_unexisting_directory(
  5518. $course,
  5519. $creatorId,
  5520. api_get_session_id(),
  5521. 0,
  5522. 0,
  5523. $filepath,
  5524. $dir,
  5525. get_lang('LearningPaths'),
  5526. 0
  5527. );
  5528. if (!empty($folderData)) {
  5529. $folder = true;
  5530. }
  5531. } else {
  5532. $folder = true;
  5533. }
  5534. return $folder;
  5535. }
  5536. /**
  5537. * @param array $course
  5538. * @param string $lp_name
  5539. * @param int $creatorId
  5540. *
  5541. * @return array
  5542. */
  5543. public function generate_lp_folder($course, $lp_name = '', $creatorId = 0)
  5544. {
  5545. $filepath = '';
  5546. $dir = '/learning_path/';
  5547. if (empty($lp_name)) {
  5548. $lp_name = $this->name;
  5549. }
  5550. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5551. $folder = self::generate_learning_path_folder($course, $creatorId);
  5552. // Limits title size
  5553. $title = api_substr(api_replace_dangerous_char($lp_name), 0 , 80);
  5554. $dir = $dir.$title;
  5555. // Creating LP folder
  5556. $documentId = null;
  5557. if ($folder) {
  5558. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5559. if (!is_dir($filepath.'/'.$dir)) {
  5560. $folderData = create_unexisting_directory(
  5561. $course,
  5562. $creatorId,
  5563. 0,
  5564. 0,
  5565. 0,
  5566. $filepath,
  5567. $dir,
  5568. $lp_name
  5569. );
  5570. if (!empty($folderData)) {
  5571. $folder = true;
  5572. }
  5573. $documentId = $folderData['id'];
  5574. } else {
  5575. $folder = true;
  5576. }
  5577. $dir = $dir.'/';
  5578. if ($folder) {
  5579. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5580. }
  5581. }
  5582. if (empty($documentId)) {
  5583. $dir = api_remove_trailing_slash($dir);
  5584. $documentId = DocumentManager::get_document_id($course, $dir, 0);
  5585. }
  5586. $array = array(
  5587. 'dir' => $dir,
  5588. 'filepath' => $filepath,
  5589. 'folder' => $folder,
  5590. 'id' => $documentId
  5591. );
  5592. return $array;
  5593. }
  5594. /**
  5595. * Create a new document //still needs some finetuning
  5596. * @param array $courseInfo
  5597. * @param string $content
  5598. * @param string $title
  5599. * @param string $extension
  5600. * @param int $parentId
  5601. * @param int $creatorId creator id
  5602. *
  5603. * @return string
  5604. */
  5605. public function create_document($courseInfo, $content = '', $title = '', $extension = 'html', $parentId = 0, $creatorId = 0)
  5606. {
  5607. if (!empty($courseInfo)) {
  5608. $course_id = $courseInfo['real_id'];
  5609. } else {
  5610. $course_id = api_get_course_int_id();
  5611. }
  5612. $creatorId = empty($creatorId) ? api_get_user_id() : $creatorId;
  5613. $sessionId = api_get_session_id();
  5614. // Generates folder
  5615. $result = $this->generate_lp_folder($courseInfo);
  5616. $dir = $result['dir'];
  5617. if (empty($parentId)) {
  5618. $postDir = isset($_POST['dir']) ? $_POST['dir'] : '';
  5619. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $postDir; // Please, do not modify this dirname formatting.
  5620. // Please, do not modify this dirname formatting.
  5621. if (strstr($dir, '..')) {
  5622. $dir = '/';
  5623. }
  5624. if (!empty($dir[0]) && $dir[0] == '.') {
  5625. $dir = substr($dir, 1);
  5626. }
  5627. if (!empty($dir[0]) && $dir[0] != '/') {
  5628. $dir = '/'.$dir;
  5629. }
  5630. if (isset($dir[strlen($dir) - 1]) && $dir[strlen($dir) - 1] != '/') {
  5631. $dir .= '/';
  5632. }
  5633. } else {
  5634. $parentInfo = DocumentManager::get_document_data_by_id($parentId, $courseInfo['code']);
  5635. if (!empty($parentInfo)) {
  5636. $dir = $parentInfo['path'].'/';
  5637. }
  5638. }
  5639. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document' . $dir;
  5640. if (!is_dir($filepath)) {
  5641. $dir = '/';
  5642. $filepath = api_get_path(SYS_COURSE_PATH) . $courseInfo['path'] . '/document'.$dir;
  5643. }
  5644. // stripslashes() before calling api_replace_dangerous_char() because $_POST['title']
  5645. // is already escaped twice when it gets here.
  5646. $originalTitle = !empty($title) ? $title : $_POST['title'];
  5647. if (!empty($title)) {
  5648. $title = api_replace_dangerous_char(stripslashes($title));
  5649. } else {
  5650. $title = api_replace_dangerous_char(stripslashes($_POST['title']));
  5651. }
  5652. $title = disable_dangerous_file($title);
  5653. $filename = $title;
  5654. $content = !empty($content) ? $content : $_POST['content_lp'];
  5655. $tmp_filename = $filename;
  5656. $i = 0;
  5657. while (file_exists($filepath . $tmp_filename . '.'.$extension)) {
  5658. $tmp_filename = $filename . '_' . ++ $i;
  5659. }
  5660. $filename = $tmp_filename . '.'.$extension;
  5661. if ($extension == 'html') {
  5662. $content = stripslashes($content);
  5663. $content = str_replace(
  5664. api_get_path(WEB_COURSE_PATH),
  5665. api_get_path(REL_PATH).'courses/',
  5666. $content
  5667. );
  5668. // Change the path of mp3 to absolute.
  5669. // The first regexp deals with :// urls.
  5670. $content = preg_replace(
  5671. "|(flashvars=\"file=)([^:/]+)/|",
  5672. "$1".api_get_path(
  5673. REL_COURSE_PATH
  5674. ).$courseInfo['path'].'/document/',
  5675. $content
  5676. );
  5677. // The second regexp deals with audio/ urls.
  5678. $content = preg_replace(
  5679. "|(flashvars=\"file=)([^/]+)/|",
  5680. "$1".api_get_path(
  5681. REL_COURSE_PATH
  5682. ).$courseInfo['path'].'/document/$2/',
  5683. $content
  5684. );
  5685. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5686. $content = str_replace(
  5687. '</body>',
  5688. '<style type="text/css">body{}</style></body>',
  5689. $content
  5690. );
  5691. }
  5692. if (!file_exists($filepath . $filename)) {
  5693. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5694. fputs($fp, $content);
  5695. fclose($fp);
  5696. $file_size = filesize($filepath . $filename);
  5697. $save_file_path = $dir.$filename;
  5698. $document_id = add_document(
  5699. $courseInfo,
  5700. $save_file_path,
  5701. 'file',
  5702. $file_size,
  5703. $tmp_filename,
  5704. '',
  5705. 0, //readonly
  5706. true,
  5707. null,
  5708. $sessionId,
  5709. $creatorId
  5710. );
  5711. if ($document_id) {
  5712. api_item_property_update(
  5713. $courseInfo,
  5714. TOOL_DOCUMENT,
  5715. $document_id,
  5716. 'DocumentAdded',
  5717. $creatorId,
  5718. null,
  5719. null,
  5720. null,
  5721. null,
  5722. $sessionId
  5723. );
  5724. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  5725. $new_title = $originalTitle;
  5726. if ($new_comment || $new_title) {
  5727. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5728. $ct = '';
  5729. if ($new_comment)
  5730. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5731. if ($new_title)
  5732. $ct .= ", title='" . Database::escape_string($new_title)."' ";
  5733. $sql = "UPDATE ".$tbl_doc." SET ".substr($ct, 1)."
  5734. WHERE c_id = ".$course_id." AND id = ".$document_id;
  5735. Database::query($sql);
  5736. }
  5737. }
  5738. return $document_id;
  5739. }
  5740. }
  5741. }
  5742. /**
  5743. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5744. * @param array $_course array
  5745. * @return void
  5746. */
  5747. public function edit_document($_course)
  5748. {
  5749. $course_id = api_get_course_int_id();
  5750. $urlAppend = api_get_configuration_value('url_append');
  5751. // Please, do not modify this dirname formatting.
  5752. $dir = isset($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5753. if (strstr($dir, '..'))
  5754. $dir = '/';
  5755. if ($dir[0] == '.')
  5756. $dir = substr($dir, 1);
  5757. if ($dir[0] != '/')
  5758. $dir = '/' . $dir;
  5759. if ($dir[strlen($dir) - 1] != '/')
  5760. $dir .= '/';
  5761. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5762. if (!is_dir($filepath)) {
  5763. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5764. $dir = '/';
  5765. }
  5766. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5767. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5768. $document_id = intval($_POST['path']);
  5769. $sql = "SELECT path FROM " . $table_doc . "
  5770. WHERE c_id = $course_id AND id = " . $document_id;
  5771. $res = Database::query($sql);
  5772. $row = Database :: fetch_array($res);
  5773. $content = stripslashes($_POST['content_lp']);
  5774. $file = $filepath . $row['path'];
  5775. if ($fp = @ fopen($file, 'w')) {
  5776. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].api_get_path(REL_COURSE_PATH), $content);
  5777. // Change the path of mp3 to absolute.
  5778. // The first regexp deals with :// urls.
  5779. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5780. // The second regexp deals with audio/ urls.
  5781. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5782. fputs($fp, $content);
  5783. fclose($fp);
  5784. $sql = "UPDATE " . $table_doc ." SET
  5785. title='".Database::escape_string($_POST['title'])."'
  5786. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5787. Database::query($sql);
  5788. }
  5789. }
  5790. }
  5791. /**
  5792. * Displays the selected item, with a panel for manipulating the item
  5793. * @param int $item_id
  5794. * @param string $msg
  5795. * @return string
  5796. */
  5797. public function display_item($item_id, $msg = null, $show_actions = true)
  5798. {
  5799. $course_id = api_get_course_int_id();
  5800. $return = '';
  5801. if (is_numeric($item_id)) {
  5802. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5803. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5804. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5805. $result = Database::query($sql);
  5806. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5807. $valueId = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5808. Session::write('parent_item_id', $valueId);
  5809. // Prevents wrong parent selection for document, see Bug#1251.
  5810. if ($row['item_type'] != 'dokeos_chapter' && $row['item_type'] != 'dokeos_module') {
  5811. Session::write('parent_item_id', $row['parent_item_id']);
  5812. }
  5813. if ($show_actions) {
  5814. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5815. }
  5816. $return .= '<div style="padding:10px;">';
  5817. if ($msg != '')
  5818. $return .= $msg;
  5819. $return .= '<h3>'.$row['title'].'</h3>';
  5820. switch ($row['item_type']) {
  5821. case TOOL_QUIZ:
  5822. if (!empty($row['path'])) {
  5823. $exercise = new Exercise();
  5824. $exercise->read($row['path']);
  5825. $return .= $exercise->description.'<br />';
  5826. $return .= Display::url(
  5827. get_lang('GoToExercise'),
  5828. api_get_path(WEB_CODE_PATH).'exercice/overview.php?'.api_get_cidreq().'&exerciseId='.$exercise->id,
  5829. ['class' => 'btn btn-primary']
  5830. );
  5831. }
  5832. break;
  5833. case TOOL_DOCUMENT:
  5834. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5835. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5836. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5837. $result = Database::query($sql_doc);
  5838. $path_file = Database::result($result, 0, 0);
  5839. $path_parts = pathinfo($path_file);
  5840. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5841. if (in_array($path_parts['extension'], array(
  5842. 'html',
  5843. 'txt',
  5844. 'png',
  5845. 'jpg',
  5846. 'JPG',
  5847. 'jpeg',
  5848. 'JPEG',
  5849. 'gif',
  5850. 'swf'
  5851. ))) {
  5852. $return .= $this->display_document($row['path'], true, true);
  5853. }
  5854. break;
  5855. }
  5856. $return .= '</div>';
  5857. }
  5858. }
  5859. return $return;
  5860. }
  5861. /**
  5862. * Shows the needed forms for editing a specific item
  5863. * @param int $item_id
  5864. * @return string
  5865. */
  5866. public function display_edit_item($item_id)
  5867. {
  5868. $course_id = api_get_course_int_id();
  5869. $return = '';
  5870. if (is_numeric($item_id)) {
  5871. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5872. $sql = "SELECT * FROM $tbl_lp_item
  5873. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5874. $res = Database::query($sql);
  5875. $row = Database::fetch_array($res);
  5876. switch ($row['item_type']) {
  5877. case 'dokeos_chapter' :
  5878. case 'dir' :
  5879. case 'asset' :
  5880. case 'sco' :
  5881. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5882. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5883. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5884. } else {
  5885. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5886. }
  5887. break;
  5888. case TOOL_DOCUMENT :
  5889. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5890. $sql = "SELECT lp.*, doc.path as dir
  5891. FROM " . $tbl_lp_item . " as lp
  5892. LEFT JOIN " . $tbl_doc . " as doc
  5893. ON doc.id = lp.path
  5894. WHERE
  5895. lp.c_id = $course_id AND
  5896. doc.c_id = $course_id AND
  5897. lp.id = " . intval($item_id);
  5898. $res_step = Database::query($sql);
  5899. $row_step = Database :: fetch_array($res_step);
  5900. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5901. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5902. break;
  5903. case TOOL_LINK :
  5904. $link_id = (string) $row['path'];
  5905. if (ctype_digit($link_id)) {
  5906. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5907. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5908. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5909. $res_link = Database::query($sql_select);
  5910. $row_link = Database :: fetch_array($res_link);
  5911. if (is_array($row_link)) {
  5912. $row['url'] = $row_link['url'];
  5913. }
  5914. }
  5915. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5916. $return .= $this->display_link_form('edit', $item_id, $row);
  5917. break;
  5918. case TOOL_LP_FINAL_ITEM :
  5919. $_SESSION['finalItem'] = true;
  5920. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5921. $sql = "SELECT lp.*, doc.path as dir
  5922. FROM " . $tbl_lp_item . " as lp
  5923. LEFT JOIN " . $tbl_doc . " as doc
  5924. ON doc.id = lp.path
  5925. WHERE
  5926. lp.c_id = $course_id AND
  5927. doc.c_id = $course_id AND
  5928. lp.id = " . intval($item_id);
  5929. $res_step = Database::query($sql);
  5930. $row_step = Database :: fetch_array($res_step, 'ASSOC');
  5931. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5932. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5933. break;
  5934. case 'dokeos_module' :
  5935. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5936. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5937. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5938. } else {
  5939. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5940. }
  5941. break;
  5942. case TOOL_QUIZ :
  5943. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5944. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5945. break;
  5946. case TOOL_HOTPOTATOES :
  5947. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5948. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5949. break;
  5950. case TOOL_STUDENTPUBLICATION :
  5951. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5952. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5953. break;
  5954. case TOOL_FORUM :
  5955. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5956. $return .= $this->display_forum_form('edit', $item_id, $row);
  5957. break;
  5958. case TOOL_THREAD :
  5959. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5960. $return .= $this->display_thread_form('edit', $item_id, $row);
  5961. break;
  5962. }
  5963. }
  5964. return $return;
  5965. }
  5966. /**
  5967. * Function that displays a list with al the resources that
  5968. * could be added to the learning path
  5969. * @return string
  5970. */
  5971. public function display_resources()
  5972. {
  5973. $learnPath = self::getCurrentLpFromSession();
  5974. $course_code = api_get_course_id();
  5975. // Get all the docs.
  5976. $documents = $this->get_documents(true);
  5977. // Get all the exercises.
  5978. $exercises = $this->get_exercises();
  5979. // Get all the links.
  5980. $links = $this->get_links();
  5981. // Get all the student publications.
  5982. $works = $this->get_student_publications();
  5983. // Get all the forums.
  5984. $forums = $this->get_forums(null, $course_code);
  5985. // Get the final item form (see BT#11048).
  5986. $finish = $this->getFinalItemForm();
  5987. $headers = array(
  5988. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5989. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5990. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5991. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5992. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5993. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG),
  5994. Display::return_icon('certificate.png', get_lang('Certificate'), [], ICON_SIZE_BIG),
  5995. );
  5996. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5997. $chapter = $learnPath->display_item_form(
  5998. 'chapter',
  5999. get_lang('EnterDataNewChapter'),
  6000. 'add_item'
  6001. );
  6002. echo Display::tabs(
  6003. $headers,
  6004. array($documents, $exercises, $links, $works, $forums, $chapter, $finish), 'resource_tab'
  6005. );
  6006. return true;
  6007. }
  6008. /**
  6009. * Returns the extension of a document
  6010. * @param string $filename
  6011. * @return string Extension (part after the last dot)
  6012. */
  6013. public function get_extension($filename)
  6014. {
  6015. $explode = explode('.', $filename);
  6016. return $explode[count($explode) - 1];
  6017. }
  6018. /**
  6019. * Displays a document by id
  6020. *
  6021. * @param int $id
  6022. * @return string
  6023. */
  6024. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6025. {
  6026. $_course = api_get_course_info();
  6027. $course_id = api_get_course_int_id();
  6028. $return = '';
  6029. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6030. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  6031. WHERE c_id = ".$course_id." AND id = " . $id;
  6032. $res_doc = Database::query($sql_doc);
  6033. $row_doc = Database :: fetch_array($res_doc);
  6034. // TODO: Add a path filter.
  6035. if ($iframe) {
  6036. $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>';
  6037. } else {
  6038. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  6039. }
  6040. return $return;
  6041. }
  6042. /**
  6043. * Return HTML form to add/edit a quiz
  6044. * @param string Action (add/edit)
  6045. * @param integer Item ID if already exists
  6046. * @param mixed Extra information (quiz ID if integer)
  6047. * @return string HTML form
  6048. */
  6049. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6050. {
  6051. $course_id = api_get_course_int_id();
  6052. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6053. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6054. if ($id != 0 && is_array($extra_info)) {
  6055. $item_title = $extra_info['title'];
  6056. $item_description = $extra_info['description'];
  6057. } elseif (is_numeric($extra_info)) {
  6058. $sql = "SELECT title, description
  6059. FROM " . $tbl_quiz . "
  6060. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6061. $result = Database::query($sql);
  6062. $row = Database::fetch_array($result);
  6063. $item_title = $row['title'];
  6064. $item_description = $row['description'];
  6065. } else {
  6066. $item_title = '';
  6067. $item_description = '';
  6068. }
  6069. $item_title = Security::remove_XSS($item_title);
  6070. $item_description = Security::remove_XSS($item_description);
  6071. if ($id != 0 && is_array($extra_info))
  6072. $parent = $extra_info['parent_item_id'];
  6073. else
  6074. $parent = 0;
  6075. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6076. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6077. $result = Database::query($sql);
  6078. $arrLP = array ();
  6079. while ($row = Database :: fetch_array($result)) {
  6080. $arrLP[] = array (
  6081. 'id' => $row['id'],
  6082. 'item_type' => $row['item_type'],
  6083. 'title' => $row['title'],
  6084. 'path' => $row['path'],
  6085. 'description' => $row['description'],
  6086. 'parent_item_id' => $row['parent_item_id'],
  6087. 'previous_item_id' => $row['previous_item_id'],
  6088. 'next_item_id' => $row['next_item_id'],
  6089. 'display_order' => $row['display_order'],
  6090. 'max_score' => $row['max_score'],
  6091. 'min_score' => $row['min_score'],
  6092. 'mastery_score' => $row['mastery_score'],
  6093. 'prerequisite' => $row['prerequisite'],
  6094. 'max_time_allowed' => $row['max_time_allowed']
  6095. );
  6096. }
  6097. $this->tree_array($arrLP);
  6098. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6099. unset ($this->arrMenu);
  6100. $form = new FormValidator('quiz_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6101. $defaults = [];
  6102. if ($action == 'add') {
  6103. $legend = get_lang('CreateTheExercise');
  6104. } elseif ($action == 'move') {
  6105. $legend = get_lang('MoveTheCurrentExercise');
  6106. } else {
  6107. $legend = get_lang('EditCurrentExecice');
  6108. }
  6109. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6110. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6111. }
  6112. $form->addHeader($legend);
  6113. if ($action != 'move') {
  6114. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6115. $defaults['title'] = $item_title;
  6116. }
  6117. // Select for Parent item, root or chapter
  6118. $selectParent = $form->addSelect(
  6119. 'parent',
  6120. get_lang('Parent'),
  6121. [],
  6122. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6123. );
  6124. $selectParent->addOption($this->name, 0);
  6125. $arrHide = array (
  6126. $id
  6127. );
  6128. for ($i = 0; $i < count($arrLP); $i++) {
  6129. if ($action != 'add') {
  6130. if (
  6131. (
  6132. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6133. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6134. $arrLP[$i]['item_type'] == 'dir'
  6135. ) &&
  6136. !in_array($arrLP[$i]['id'], $arrHide) &&
  6137. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6138. ) {
  6139. $selectParent->addOption(
  6140. $arrLP[$i]['title'],
  6141. $arrLP[$i]['id'],
  6142. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6143. );
  6144. if ($parent == $arrLP[$i]['id']) {
  6145. $selectParent->setSelected($arrLP[$i]['id']);
  6146. }
  6147. } else {
  6148. $arrHide[] = $arrLP[$i]['id'];
  6149. }
  6150. } else {
  6151. if (
  6152. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6153. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6154. $arrLP[$i]['item_type'] == 'dir'
  6155. ) {
  6156. $selectParent->addOption(
  6157. $arrLP[$i]['title'],
  6158. $arrLP[$i]['id'], ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6159. );
  6160. if ($parent == $arrLP[$i]['id']) {
  6161. $selectParent->setSelected($arrLP[$i]['id']);
  6162. }
  6163. }
  6164. }
  6165. }
  6166. if (is_array($arrLP)) {
  6167. reset($arrLP);
  6168. }
  6169. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6170. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6171. for ($i = 0; $i < count($arrLP); $i++) {
  6172. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6173. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6174. if (is_array($extra_info)) {
  6175. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6176. $selectPrevious->setSelected($arrLP[$i]['id']);
  6177. }
  6178. } elseif ($action == 'add') {
  6179. $selectPrevious->setSelected($arrLP[$i]['id']);
  6180. }
  6181. }
  6182. }
  6183. if ($action != 'move') {
  6184. $id_prerequisite = 0;
  6185. if (is_array($arrLP)) {
  6186. foreach ($arrLP as $key => $value) {
  6187. if ($value['id'] == $id) {
  6188. $id_prerequisite = $value['prerequisite'];
  6189. break;
  6190. }
  6191. }
  6192. }
  6193. $arrHide = array ();
  6194. for ($i = 0; $i < count($arrLP); $i++) {
  6195. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6196. if (is_array($extra_info)) {
  6197. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6198. $s_selected_position = $arrLP[$i]['id'];
  6199. }
  6200. } elseif ($action == 'add') {
  6201. $s_selected_position = 0;
  6202. }
  6203. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6204. }
  6205. }
  6206. /*// Commented the prerequisites, only visible in edit (exercise).
  6207. $return .= '<tr>';
  6208. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6209. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6210. foreach($arrHide as $key => $value){
  6211. if($key==$s_selected_position && $action == 'add'){
  6212. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6213. }
  6214. elseif($key==$id_prerequisite && $action == 'edit'){
  6215. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6216. }
  6217. else{
  6218. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6219. }
  6220. }
  6221. $return .= "</select></td>";
  6222. */
  6223. /*$return .= '<tr>';
  6224. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6225. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6226. // Remove temporarily the test description.
  6227. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6228. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6229. $return .= '</tr>'; */
  6230. }
  6231. if ($action == 'add') {
  6232. $form->addButtonSave(get_lang('AddExercise'), 'submit_button');
  6233. } else {
  6234. $form->addButtonSave(get_lang('EditCurrentExecice'), 'submit_button');
  6235. }
  6236. if ($action == 'move') {
  6237. $form->addHidden('title', $item_title);
  6238. $form->addHidden('description', $item_description);
  6239. }
  6240. if (is_numeric($extra_info)) {
  6241. $form->addHidden('path', $extra_info);
  6242. } elseif (is_array($extra_info)) {
  6243. $form->addHidden('path', $extra_info['path']);
  6244. }
  6245. $form->addHidden('type', TOOL_QUIZ);
  6246. $form->addHidden('post_time', time());
  6247. $form->setDefaults($defaults);
  6248. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6249. }
  6250. /**
  6251. * Addition of Hotpotatoes tests
  6252. * @param string Action
  6253. * @param integer Internal ID of the item
  6254. * @param mixed Extra information - can be an array with title and description indexes
  6255. * @return string HTML structure to display the hotpotatoes addition formular
  6256. */
  6257. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6258. {
  6259. $course_id = api_get_course_int_id();
  6260. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6261. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6262. if ($id != 0 && is_array($extra_info)) {
  6263. $item_title = stripslashes($extra_info['title']);
  6264. $item_description = stripslashes($extra_info['description']);
  6265. } elseif (is_numeric($extra_info)) {
  6266. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6267. $sql = "SELECT * FROM " . $TBL_DOCUMENT . "
  6268. WHERE
  6269. c_id = ".$course_id." AND
  6270. path LIKE '" . $uploadPath . "/%/%htm%' AND
  6271. id = " . (int) $extra_info . "
  6272. ORDER BY id ASC";
  6273. $res_hot = Database::query($sql);
  6274. $row = Database::fetch_array($res_hot);
  6275. $item_title = $row['title'];
  6276. $item_description = $row['description'];
  6277. if (!empty ($row['comment'])) {
  6278. $item_title = $row['comment'];
  6279. }
  6280. } else {
  6281. $item_title = '';
  6282. $item_description = '';
  6283. }
  6284. if ($id != 0 && is_array($extra_info)) {
  6285. $parent = $extra_info['parent_item_id'];
  6286. } else {
  6287. $parent = 0;
  6288. }
  6289. $sql = "SELECT * FROM $tbl_lp_item
  6290. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6291. $result = Database::query($sql);
  6292. $arrLP = array ();
  6293. while ($row = Database :: fetch_array($result)) {
  6294. $arrLP[] = array (
  6295. 'id' => $row['id'],
  6296. 'item_type' => $row['item_type'],
  6297. 'title' => $row['title'],
  6298. 'path' => $row['path'],
  6299. 'description' => $row['description'],
  6300. 'parent_item_id' => $row['parent_item_id'],
  6301. 'previous_item_id' => $row['previous_item_id'],
  6302. 'next_item_id' => $row['next_item_id'],
  6303. 'display_order' => $row['display_order'],
  6304. 'max_score' => $row['max_score'],
  6305. 'min_score' => $row['min_score'],
  6306. 'mastery_score' => $row['mastery_score'],
  6307. 'prerequisite' => $row['prerequisite'],
  6308. 'max_time_allowed' => $row['max_time_allowed']
  6309. );
  6310. }
  6311. $legend = '<legend>';
  6312. if ($action == 'add')
  6313. $legend .= get_lang('CreateTheExercise');
  6314. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6315. else
  6316. $legend .= get_lang('EditCurrentExecice');
  6317. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6318. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6319. }
  6320. $legend .= '</legend>';
  6321. $return = '<form method="POST">';
  6322. $return .= $legend;
  6323. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6324. $return .= '<tr>';
  6325. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6326. $return .= '<td class="input">';
  6327. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6328. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6329. $arrHide = array (
  6330. $id
  6331. );
  6332. if (count($arrLP) > 0) {
  6333. for ($i = 0; $i < count($arrLP); $i++) {
  6334. if ($action != 'add') {
  6335. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6336. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6337. } else {
  6338. $arrHide[] = $arrLP[$i]['id'];
  6339. }
  6340. } else {
  6341. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6342. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6343. }
  6344. }
  6345. reset($arrLP);
  6346. }
  6347. $return .= '</select>';
  6348. $return .= '</td>';
  6349. $return .= '</tr>';
  6350. $return .= '<tr>';
  6351. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6352. $return .= '<td class="input">';
  6353. $return .= '<select id="previous" name="previous" size="1">';
  6354. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6355. for ($i = 0; $i < count($arrLP); $i++) {
  6356. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6357. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6358. $selected = 'selected="selected" ';
  6359. elseif ($action == 'add') $selected = 'selected="selected" ';
  6360. else
  6361. $selected = '';
  6362. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6363. }
  6364. }
  6365. $return .= '</select>';
  6366. $return .= '</td>';
  6367. $return .= '</tr>';
  6368. if ($action != 'move') {
  6369. $return .= '<tr>';
  6370. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6371. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6372. $return .= '</tr>';
  6373. $id_prerequisite = 0;
  6374. if (is_array($arrLP) && count($arrLP) > 0) {
  6375. foreach ($arrLP as $key => $value) {
  6376. if ($value['id'] == $id) {
  6377. $id_prerequisite = $value['prerequisite'];
  6378. break;
  6379. }
  6380. }
  6381. $arrHide = array ();
  6382. for ($i = 0; $i < count($arrLP); $i++) {
  6383. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6384. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6385. $s_selected_position = $arrLP[$i]['id'];
  6386. elseif ($action == 'add') $s_selected_position = 0;
  6387. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6388. }
  6389. }
  6390. }
  6391. }
  6392. $return .= '<tr>';
  6393. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6394. $return .= '</tr>';
  6395. $return .= '</table>';
  6396. if ($action == 'move') {
  6397. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6398. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6399. }
  6400. if (is_numeric($extra_info)) {
  6401. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6402. } elseif (is_array($extra_info)) {
  6403. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6404. }
  6405. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6406. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6407. $return .= '</form>';
  6408. return $return;
  6409. }
  6410. /**
  6411. * Return the form to display the forum edit/add option
  6412. * @param string Action (add/edit)
  6413. * @param integer ID of the lp_item if already exists
  6414. * @param mixed Forum ID or title
  6415. * @return string HTML form
  6416. */
  6417. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6418. {
  6419. $course_id = api_get_course_int_id();
  6420. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6421. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6422. if ($id != 0 && is_array($extra_info)) {
  6423. $item_title = stripslashes($extra_info['title']);
  6424. } elseif (is_numeric($extra_info)) {
  6425. $sql = "SELECT forum_title as title, forum_comment as comment
  6426. FROM " . $tbl_forum . "
  6427. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6428. $result = Database::query($sql);
  6429. $row = Database :: fetch_array($result);
  6430. $item_title = $row['title'];
  6431. $item_description = $row['comment'];
  6432. } else {
  6433. $item_title = '';
  6434. $item_description = '';
  6435. }
  6436. if ($id != 0 && is_array($extra_info)) {
  6437. $parent = $extra_info['parent_item_id'];
  6438. } else {
  6439. $parent = 0;
  6440. }
  6441. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6442. WHERE
  6443. c_id = ".$course_id." AND
  6444. lp_id = " . $this->lp_id;
  6445. $result = Database::query($sql);
  6446. $arrLP = array();
  6447. while ($row = Database :: fetch_array($result)) {
  6448. $arrLP[] = array (
  6449. 'id' => $row['id'],
  6450. 'item_type' => $row['item_type'],
  6451. 'title' => $row['title'],
  6452. 'path' => $row['path'],
  6453. 'description' => $row['description'],
  6454. 'parent_item_id' => $row['parent_item_id'],
  6455. 'previous_item_id' => $row['previous_item_id'],
  6456. 'next_item_id' => $row['next_item_id'],
  6457. 'display_order' => $row['display_order'],
  6458. 'max_score' => $row['max_score'],
  6459. 'min_score' => $row['min_score'],
  6460. 'mastery_score' => $row['mastery_score'],
  6461. 'prerequisite' => $row['prerequisite']
  6462. );
  6463. }
  6464. $this->tree_array($arrLP);
  6465. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6466. unset($this->arrMenu);
  6467. if ($action == 'add') {
  6468. $legend = get_lang('CreateTheForum');
  6469. } elseif ($action == 'move') {
  6470. $legend = get_lang('MoveTheCurrentForum');
  6471. } else {
  6472. $legend = get_lang('EditCurrentForum');
  6473. }
  6474. $form = new FormValidator('forum_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6475. $defaults = [];
  6476. $form->addHeader($legend);
  6477. if ($action != 'move') {
  6478. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
  6479. $defaults['title'] = $item_title;
  6480. }
  6481. $selectParent = $form->addSelect(
  6482. 'parent',
  6483. get_lang('Parent'),
  6484. [],
  6485. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  6486. );
  6487. $selectParent->addOption($this->name, 0);
  6488. $arrHide = array(
  6489. $id
  6490. );
  6491. for ($i = 0; $i < count($arrLP); $i++) {
  6492. if ($action != 'add') {
  6493. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6494. $selectParent->addOption(
  6495. $arrLP[$i]['title'],
  6496. $arrLP[$i]['id'],
  6497. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6498. );
  6499. if ($parent == $arrLP[$i]['id']) {
  6500. $selectParent->setSelected($arrLP[$i]['id']);
  6501. }
  6502. } else {
  6503. $arrHide[] = $arrLP[$i]['id'];
  6504. }
  6505. } else {
  6506. if (
  6507. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6508. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6509. $arrLP[$i]['item_type'] == 'dir'
  6510. ) {
  6511. $selectParent->addOption(
  6512. $arrLP[$i]['title'],
  6513. $arrLP[$i]['id'],
  6514. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6515. );
  6516. if ($parent == $arrLP[$i]['id']) {
  6517. $selectParent->setSelected($arrLP[$i]['id']);
  6518. }
  6519. }
  6520. }
  6521. }
  6522. if (is_array($arrLP)) {
  6523. reset($arrLP);
  6524. }
  6525. $selectPrevious = $form->addSelect(
  6526. 'previous',
  6527. get_lang('Position'),
  6528. [],
  6529. ['id' => 'previous', 'class' => 'learnpath_item_form']
  6530. );
  6531. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6532. for ($i = 0; $i < count($arrLP); $i++) {
  6533. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6534. $selectPrevious->addOption(get_lang('After') . ' "' . $arrLP[$i]['title'] . '"', $arrLP[$i]['id']);
  6535. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6536. $selectPrevious->setSelected($arrLP[$i]['id']);
  6537. } elseif ($action == 'add') {
  6538. $selectPrevious->setSelected($arrLP[$i]['id']);
  6539. }
  6540. }
  6541. }
  6542. if ($action != 'move') {
  6543. $id_prerequisite = 0;
  6544. if (is_array($arrLP)) {
  6545. foreach ($arrLP as $key => $value) {
  6546. if ($value['id'] == $id) {
  6547. $id_prerequisite = $value['prerequisite'];
  6548. break;
  6549. }
  6550. }
  6551. }
  6552. $arrHide = array();
  6553. for ($i = 0; $i < count($arrLP); $i++) {
  6554. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6555. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6556. $s_selected_position = $arrLP[$i]['id'];
  6557. elseif ($action == 'add') $s_selected_position = 0;
  6558. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6559. }
  6560. }
  6561. }
  6562. if ($action == 'add') {
  6563. $form->addButtonSave(get_lang('AddForumToCourse'), 'submit_button');
  6564. } else {
  6565. $form->addButtonSave(get_lang('EditCurrentForum'), 'submit_button');
  6566. }
  6567. if ($action == 'move') {
  6568. $form->addHidden('title', $item_title);
  6569. $form->addHidden('description', $item_description);
  6570. }
  6571. if (is_numeric($extra_info)) {
  6572. $form->addHidden('path', $extra_info);
  6573. } elseif (is_array($extra_info)) {
  6574. $form->addHidden('path', $extra_info['path']);
  6575. }
  6576. $form->addHidden('type', TOOL_FORUM);
  6577. $form->addHidden('post_time', time());
  6578. $form->setDefaults($defaults);
  6579. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  6580. }
  6581. /**
  6582. * Return HTML form to add/edit forum threads
  6583. * @param string Action (add/edit)
  6584. * @param integer Item ID if already exists in learning path
  6585. * @param mixed Extra information (thread ID if integer)
  6586. * @return string HTML form
  6587. */
  6588. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6589. {
  6590. $course_id = api_get_course_int_id();
  6591. if (empty($course_id)) {
  6592. return null;
  6593. }
  6594. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6595. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6596. if ($id != 0 && is_array($extra_info)) {
  6597. $item_title = stripslashes($extra_info['title']);
  6598. } elseif (is_numeric($extra_info)) {
  6599. $sql = "SELECT thread_title as title FROM $tbl_forum
  6600. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6601. $result = Database::query($sql);
  6602. $row = Database :: fetch_array($result);
  6603. $item_title = $row['title'];
  6604. $item_description = '';
  6605. } else {
  6606. $item_title = '';
  6607. $item_description = '';
  6608. }
  6609. if ($id != 0 && is_array($extra_info)) {
  6610. $parent = $extra_info['parent_item_id'];
  6611. } else {
  6612. $parent = 0;
  6613. }
  6614. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6615. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6616. $result = Database::query($sql);
  6617. $arrLP = array ();
  6618. while ($row = Database :: fetch_array($result)) {
  6619. $arrLP[] = array (
  6620. 'id' => $row['id'],
  6621. 'item_type' => $row['item_type'],
  6622. 'title' => $row['title'],
  6623. 'path' => $row['path'],
  6624. 'description' => $row['description'],
  6625. 'parent_item_id' => $row['parent_item_id'],
  6626. 'previous_item_id' => $row['previous_item_id'],
  6627. 'next_item_id' => $row['next_item_id'],
  6628. 'display_order' => $row['display_order'],
  6629. 'max_score' => $row['max_score'],
  6630. 'min_score' => $row['min_score'],
  6631. 'mastery_score' => $row['mastery_score'],
  6632. 'prerequisite' => $row['prerequisite']
  6633. );
  6634. }
  6635. $this->tree_array($arrLP);
  6636. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6637. unset ($this->arrMenu);
  6638. $form = new FormValidator('thread_form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  6639. $defaults = [];
  6640. if ($action == 'add') {
  6641. $legend = get_lang('CreateTheForum');
  6642. } elseif ($action == 'move') {
  6643. $legend = get_lang('MoveTheCurrentForum');
  6644. } else {
  6645. $legend = get_lang('EditCurrentForum');
  6646. }
  6647. $form->addHeader($legend);
  6648. $selectParent = $form->addSelect(
  6649. 'parent',
  6650. get_lang('Parent'),
  6651. [],
  6652. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);']
  6653. );
  6654. $selectParent->addOption($this->name, 0);
  6655. $arrHide = array (
  6656. $id
  6657. );
  6658. for ($i = 0; $i < count($arrLP); $i++) {
  6659. if ($action != 'add') {
  6660. if (
  6661. (
  6662. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6663. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6664. $arrLP[$i]['item_type'] == 'dir'
  6665. ) &&
  6666. !in_array($arrLP[$i]['id'], $arrHide) &&
  6667. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6668. ) {
  6669. $selectParent->addOption(
  6670. $arrLP[$i]['title'],
  6671. $arrLP[$i]['id'],
  6672. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6673. );
  6674. if ($parent == $arrLP[$i]['id']) {
  6675. $selectParent->setSelected($arrLP[$i]['id']);
  6676. }
  6677. } else {
  6678. $arrHide[] = $arrLP[$i]['id'];
  6679. }
  6680. } else {
  6681. if (
  6682. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  6683. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  6684. $arrLP[$i]['item_type'] == 'dir'
  6685. ) {
  6686. $selectParent->addOption(
  6687. $arrLP[$i]['title'],
  6688. $arrLP[$i]['id'],
  6689. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  6690. );
  6691. if ($parent == $arrLP[$i]['id']) {
  6692. $selectParent->setSelected($arrLP[$i]['id']);
  6693. }
  6694. }
  6695. }
  6696. }
  6697. if ($arrLP != null) {
  6698. reset($arrLP);
  6699. }
  6700. $selectPrevious = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  6701. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  6702. for ($i = 0; $i < count($arrLP); $i++) {
  6703. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6704. $selectPrevious->addOption(
  6705. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  6706. $arrLP[$i]['id']
  6707. );
  6708. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6709. $selectPrevious->setSelected($arrLP[$i]['id']);
  6710. } elseif ($action == 'add') {
  6711. $selectPrevious->setSelected($arrLP[$i]['id']);
  6712. }
  6713. }
  6714. }
  6715. if ($action != 'move') {
  6716. $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle']);
  6717. $defaults['title'] = $item_title;
  6718. $id_prerequisite = 0;
  6719. if ($arrLP != null) {
  6720. foreach ($arrLP as $key => $value) {
  6721. if ($value['id'] == $id) {
  6722. $id_prerequisite = $value['prerequisite'];
  6723. break;
  6724. }
  6725. }
  6726. }
  6727. $arrHide = array();
  6728. $s_selected_position = 0;
  6729. for ($i = 0; $i < count($arrLP); $i++) {
  6730. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6731. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6732. $s_selected_position = $arrLP[$i]['id'];
  6733. elseif ($action == 'add') $s_selected_position = 0;
  6734. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6735. }
  6736. }
  6737. $selectPrerequisites = $form->addSelect(
  6738. 'prerequisites',
  6739. get_lang('LearnpathPrerequisites'),
  6740. [],
  6741. ['id' => 'prerequisites']
  6742. );
  6743. $selectPrerequisites->addOption(get_lang('NoPrerequisites'), 0);
  6744. foreach ($arrHide as $key => $value) {
  6745. $selectPrerequisites->addOption($value['value'], $key);
  6746. if ($key == $s_selected_position && $action == 'add') {
  6747. $selectPrerequisites->setSelected($key);
  6748. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6749. $selectPrerequisites->setSelected($key);
  6750. }
  6751. }
  6752. }
  6753. $form->addButtonSave(get_lang('Ok'), 'submit_button');
  6754. if ($action == 'move') {
  6755. $form->addHidden('title', $item_title);
  6756. $form->addHidden('description', $item_description);
  6757. }
  6758. if (is_numeric($extra_info)) {
  6759. $form->addHidden('path', $extra_info);
  6760. }
  6761. elseif (is_array($extra_info)) {
  6762. $form->addHidden('path', $extra_info['path']);
  6763. }
  6764. $form->addHidden('type', TOOL_THREAD);
  6765. $form->addHidden('post_time', time());
  6766. $form->setDefaults($defaults);
  6767. return $form->returnForm();
  6768. }
  6769. /**
  6770. * Return the HTML form to display an item (generally a section/module item)
  6771. * @param string Item type (module/dokeos_module)
  6772. * @param string Title (optional, only when creating)
  6773. * @param string Action ('add'/'edit')
  6774. * @param integer lp_item ID
  6775. * @param mixed Extra info
  6776. * @return string HTML form
  6777. */
  6778. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6779. {
  6780. $course_id = api_get_course_int_id();
  6781. $_course = api_get_course_info();
  6782. global $charset;
  6783. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6784. if ($id != 0 && is_array($extra_info)) {
  6785. $item_title = $extra_info['title'];
  6786. $item_description = $extra_info['description'];
  6787. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6788. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6789. } else {
  6790. $item_title = '';
  6791. $item_description = '';
  6792. $item_path_fck = '';
  6793. }
  6794. if ($id != 0 && is_array($extra_info)) {
  6795. $parent = $extra_info['parent_item_id'];
  6796. } else {
  6797. $parent = 0;
  6798. }
  6799. $id = intval($id);
  6800. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6801. WHERE
  6802. c_id = ".$course_id." AND
  6803. lp_id = " . $this->lp_id . " AND
  6804. id != $id";
  6805. if ($item_type == 'module')
  6806. $sql .= " AND parent_item_id = 0";
  6807. $result = Database::query($sql);
  6808. $arrLP = array ();
  6809. while ($row = Database :: fetch_array($result)) {
  6810. $arrLP[] = array(
  6811. 'id' => $row['id'],
  6812. 'item_type' => $row['item_type'],
  6813. 'title' => $row['title'],
  6814. 'path' => $row['path'],
  6815. 'description' => $row['description'],
  6816. 'parent_item_id' => $row['parent_item_id'],
  6817. 'previous_item_id' => $row['previous_item_id'],
  6818. 'next_item_id' => $row['next_item_id'],
  6819. 'max_score' => $row['max_score'],
  6820. 'min_score' => $row['min_score'],
  6821. 'mastery_score' => $row['mastery_score'],
  6822. 'prerequisite' => $row['prerequisite'],
  6823. 'display_order' => $row['display_order']
  6824. );
  6825. }
  6826. $this->tree_array($arrLP);
  6827. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6828. unset ($this->arrMenu);
  6829. $url = api_get_self() . '?' .api_get_cidreq().'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6830. $form = new FormValidator('form', 'POST', $url);
  6831. $defaults['title'] = !empty($item_title) ? api_html_entity_decode($item_title, ENT_QUOTES, $charset) : '';
  6832. $defaults['description'] = $item_description;
  6833. $form->addElement('header', $title);
  6834. //$arrHide = array($id);
  6835. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6836. $arrHide[0]['padding'] = 20;
  6837. $charset = api_get_system_encoding();
  6838. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6839. for ($i = 0; $i < count($arrLP); $i++) {
  6840. if ($action != 'add') {
  6841. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6842. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6843. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6844. if ($parent == $arrLP[$i]['id']) {
  6845. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6846. }
  6847. }
  6848. } else {
  6849. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6850. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6851. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  6852. if ($parent == $arrLP[$i]['id']) {
  6853. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6854. }
  6855. }
  6856. }
  6857. }
  6858. if ($action != 'move') {
  6859. $form->addElement('text', 'title', get_lang('Title'));
  6860. $form->applyFilter('title', 'html_filter');
  6861. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6862. } else {
  6863. $form->addElement('hidden', 'title');
  6864. }
  6865. $parent_select = $form->addElement(
  6866. 'select',
  6867. 'parent',
  6868. get_lang('Parent'),
  6869. '',
  6870. array(
  6871. 'id' => 'idParent',
  6872. 'onchange' => "javascript: load_cbo(this.value);",
  6873. )
  6874. );
  6875. foreach ($arrHide as $key => $value) {
  6876. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6877. }
  6878. if (!empty($s_selected_parent)) {
  6879. $parent_select->setSelected($s_selected_parent);
  6880. }
  6881. }
  6882. if (is_array($arrLP)) {
  6883. reset($arrLP);
  6884. }
  6885. $arrHide = array();
  6886. // POSITION
  6887. for ($i = 0; $i < count($arrLP); $i++) {
  6888. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6889. //this is the same!
  6890. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6891. $s_selected_position = $arrLP[$i]['id'];
  6892. } elseif ($action == 'add') {
  6893. $s_selected_position = $arrLP[$i]['id'];
  6894. }
  6895. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6896. }
  6897. }
  6898. $position = $form->addElement('select', 'previous', get_lang('Position'), '', array('id' => 'previous'));
  6899. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6900. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6901. foreach ($arrHide as $key => $value) {
  6902. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6903. }
  6904. if (!empty ($s_selected_position)) {
  6905. $position->setSelected($s_selected_position);
  6906. }
  6907. if (is_array($arrLP)) {
  6908. reset($arrLP);
  6909. }
  6910. $form->addButtonSave(get_lang('SaveSection'), 'submit_button');
  6911. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6912. $form->addElement('hidden', 'parent', '0');
  6913. }
  6914. //fix in order to use the tab
  6915. if ($item_type == 'chapter') {
  6916. $form->addElement('hidden', 'type', 'chapter');
  6917. }
  6918. $extension = null;
  6919. if (!empty($item_path)) {
  6920. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6921. }
  6922. //assets can't be modified
  6923. //$item_type == 'asset' ||
  6924. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6925. if ($item_type == 'sco') {
  6926. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6927. }
  6928. $renderer = $form->defaultRenderer();
  6929. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6930. $editor_config = array(
  6931. 'ToolbarSet' => 'LearningPathDocuments',
  6932. 'Width' => '100%',
  6933. 'Height' => '500',
  6934. 'FullPage' => true
  6935. );
  6936. $form->addHtmlEditor(
  6937. 'content_lp',
  6938. '',
  6939. false,
  6940. false,
  6941. $editor_config
  6942. );
  6943. $content_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck;
  6944. $defaults['content_lp'] = file_get_contents($content_path);
  6945. }
  6946. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6947. $form->addElement('hidden', 'post_time', time());
  6948. $form->setDefaults($defaults);
  6949. return $form->return_form();
  6950. }
  6951. /**
  6952. * Returns the form to update or create a document
  6953. * @param string Action (add/edit)
  6954. * @param integer ID of the lp_item (if already exists)
  6955. * @param mixed Integer if document ID, string if info ('new')
  6956. * @return string HTML form
  6957. */
  6958. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6959. {
  6960. $course_id = api_get_course_int_id();
  6961. $_course = api_get_course_info();
  6962. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6963. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6964. $no_display_edit_textarea = false;
  6965. $item_description = '';
  6966. //If action==edit document
  6967. //We don't display the document form if it's not an editable document (html or txt file)
  6968. if ($action == "edit") {
  6969. if (is_array($extra_info)) {
  6970. $path_parts = pathinfo($extra_info['dir']);
  6971. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6972. $no_display_edit_textarea = true;
  6973. }
  6974. }
  6975. }
  6976. $no_display_add = false;
  6977. // If action==add an existing document
  6978. // We don't display the document form if it's not an editable document (html or txt file).
  6979. if ($action == "add") {
  6980. if (is_numeric($extra_info)) {
  6981. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6982. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6983. $result = Database::query($sql_doc);
  6984. $path_file = Database :: result($result, 0, 0);
  6985. $path_parts = pathinfo($path_file);
  6986. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6987. $no_display_add = true;
  6988. }
  6989. }
  6990. }
  6991. if ($id != 0 && is_array($extra_info)) {
  6992. $item_title = stripslashes($extra_info['title']);
  6993. $item_description = stripslashes($extra_info['description']);
  6994. $item_terms = stripslashes($extra_info['terms']);
  6995. if (empty ($item_title)) {
  6996. $path_parts = pathinfo($extra_info['path']);
  6997. $item_title = stripslashes($path_parts['filename']);
  6998. }
  6999. } elseif (is_numeric($extra_info)) {
  7000. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  7001. WHERE
  7002. c_id = ".$course_id." AND
  7003. id = " . intval($extra_info);
  7004. $result = Database::query($sql_doc);
  7005. $row = Database::fetch_array($result);
  7006. $item_title = $row['title'];
  7007. $item_title = str_replace('_', ' ', $item_title);
  7008. if (empty ($item_title)) {
  7009. $path_parts = pathinfo($row['path']);
  7010. $item_title = stripslashes($path_parts['filename']);
  7011. }
  7012. } else {
  7013. $item_title = '';
  7014. $item_description = '';
  7015. }
  7016. $return = '<legend>';
  7017. if ($id != 0 && is_array($extra_info)) {
  7018. $parent = $extra_info['parent_item_id'];
  7019. } else {
  7020. $parent = 0;
  7021. }
  7022. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7023. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7024. $result = Database::query($sql);
  7025. $arrLP = array ();
  7026. while ($row = Database :: fetch_array($result)) {
  7027. $arrLP[] = array(
  7028. 'id' => $row['id'],
  7029. 'item_type' => $row['item_type'],
  7030. 'title' => $row['title'],
  7031. 'path' => $row['path'],
  7032. 'description' => $row['description'],
  7033. 'parent_item_id' => $row['parent_item_id'],
  7034. 'previous_item_id' => $row['previous_item_id'],
  7035. 'next_item_id' => $row['next_item_id'],
  7036. 'display_order' => $row['display_order'],
  7037. 'max_score' => $row['max_score'],
  7038. 'min_score' => $row['min_score'],
  7039. 'mastery_score' => $row['mastery_score'],
  7040. 'prerequisite' => $row['prerequisite']
  7041. );
  7042. }
  7043. $this->tree_array($arrLP);
  7044. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7045. unset ($this->arrMenu);
  7046. if ($action == 'add') {
  7047. $return .= get_lang('CreateTheDocument');
  7048. } elseif ($action == 'move') {
  7049. $return .= get_lang('MoveTheCurrentDocument');
  7050. } else {
  7051. $return .= get_lang('EditTheCurrentDocument');
  7052. }
  7053. $return .= '</legend>';
  7054. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7055. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  7056. }
  7057. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  7058. $defaults['title'] = Security :: remove_XSS($item_title);
  7059. if (empty($item_title)) {
  7060. $defaults['title'] = Security::remove_XSS($item_title);
  7061. }
  7062. $defaults['description'] = $item_description;
  7063. $form->addElement('html', $return);
  7064. if ($action != 'move') {
  7065. $data = $this->generate_lp_folder($_course);
  7066. $folders = DocumentManager::get_all_document_folders(
  7067. $_course,
  7068. 0,
  7069. true
  7070. );
  7071. DocumentManager::build_directory_selector(
  7072. $folders,
  7073. '',
  7074. array(),
  7075. true,
  7076. $form,
  7077. 'directory_parent_id'
  7078. );
  7079. if (isset($data['id'])) {
  7080. $defaults['directory_parent_id'] = $data['id'];
  7081. }
  7082. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'col-md-4'));
  7083. $form->applyFilter('title', 'html_filter');
  7084. }
  7085. $arrHide[0]['value'] = $this->name;
  7086. $arrHide[0]['padding'] = 20;
  7087. for ($i = 0; $i < count($arrLP); $i++) {
  7088. if ($action != 'add') {
  7089. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7090. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7091. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7092. if ($parent == $arrLP[$i]['id']) {
  7093. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7094. }
  7095. }
  7096. } else {
  7097. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7098. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7099. $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
  7100. if ($parent == $arrLP[$i]['id']) {
  7101. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7102. }
  7103. }
  7104. }
  7105. }
  7106. $parent_select = $form->addSelect('parent', get_lang('Parent'), [], ['id' => 'idParent', 'onchange' => 'javascript: load_cbo(this.value);']);
  7107. $my_count=0;
  7108. foreach ($arrHide as $key => $value) {
  7109. if ($my_count!=0) {
  7110. // The LP name is also the first section and is not in the same charset like the other sections.
  7111. $value['value'] = Security :: remove_XSS($value['value']);
  7112. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7113. } else {
  7114. $value['value'] = Security :: remove_XSS($value['value']);
  7115. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  7116. }
  7117. $my_count++;
  7118. }
  7119. if (!empty($id)) {
  7120. $parent_select->setSelected($parent);
  7121. } else {
  7122. $parent_item_id = Session::read('parent_item_id');
  7123. $parent_select->setSelected($parent_item_id);
  7124. }
  7125. if (is_array($arrLP)) {
  7126. reset($arrLP);
  7127. }
  7128. $arrHide = array();
  7129. $s_selected_position = null;
  7130. //POSITION
  7131. for ($i = 0; $i < count($arrLP); $i++) {
  7132. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id || $arrLP[$i]['item_type'] == TOOL_LP_FINAL_ITEM) {
  7133. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'] || $action == 'add') {
  7134. $s_selected_position = $arrLP[$i]['id'];
  7135. }
  7136. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  7137. }
  7138. }
  7139. $position = $form->addSelect('previous', get_lang('Position'), [], ['id' => 'previous']);
  7140. $position->addOption(get_lang('FirstPosition'), 0);
  7141. foreach ($arrHide as $key => $value) {
  7142. $padding = isset($value['padding']) ? $value['padding']: 20;
  7143. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  7144. }
  7145. $position->setSelected($s_selected_position);
  7146. if (is_array($arrLP)) {
  7147. reset($arrLP);
  7148. }
  7149. if ($action != 'move') {
  7150. $id_prerequisite = 0;
  7151. if (is_array($arrLP)) {
  7152. foreach ($arrLP as $key => $value) {
  7153. if ($value['id'] == $id) {
  7154. $id_prerequisite = $value['prerequisite'];
  7155. break;
  7156. }
  7157. }
  7158. }
  7159. $arrHide = array();
  7160. for ($i = 0; $i < count($arrLP); $i++) {
  7161. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7162. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7163. $s_selected_position = $arrLP[$i]['id'];
  7164. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  7165. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7166. }
  7167. }
  7168. if (!$no_display_add) {
  7169. $item_type = isset($extra_info['item_type']) ? $extra_info['item_type'] : null;
  7170. $edit = isset($_GET['edit']) ? $_GET['edit'] : null;
  7171. if (($extra_info == 'new' || $item_type == TOOL_DOCUMENT || $edit == 'true')) {
  7172. if (isset ($_POST['content']))
  7173. $content = stripslashes($_POST['content']);
  7174. elseif (is_array($extra_info)) {
  7175. //If it's an html document or a text file
  7176. if (!$no_display_edit_textarea) {
  7177. $content = $this->display_document($extra_info['path'], false, false);
  7178. }
  7179. } elseif (is_numeric($extra_info))
  7180. $content = $this->display_document($extra_info, false, false);
  7181. else
  7182. $content = '';
  7183. if (!$no_display_edit_textarea) {
  7184. // We need to calculate here some specific settings for the online editor.
  7185. // The calculated settings work for documents in the Documents tool
  7186. // (on the root or in subfolders).
  7187. // For documents in native scorm packages it is unclear whether the
  7188. // online editor should be activated or not.
  7189. // A new document, it is in the root of the repository.
  7190. $relative_path = '';
  7191. $relative_prefix = '';
  7192. if (is_array($extra_info) && $extra_info != 'new') {
  7193. // The document already exists. Whe have to determine its relative path towards the repository root.
  7194. $relative_path = explode('/', $extra_info['dir']);
  7195. $cnt = count($relative_path) - 2;
  7196. if ($cnt < 0) {
  7197. $cnt = 0;
  7198. }
  7199. $relative_prefix = str_repeat('../', $cnt);
  7200. $relative_path = array_slice($relative_path, 1, $cnt);
  7201. $relative_path = implode('/', $relative_path);
  7202. if (strlen($relative_path) > 0) {
  7203. $relative_path = $relative_path . '/';
  7204. }
  7205. } else {
  7206. $result = $this->generate_lp_folder($_course);
  7207. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7208. $relative_prefix = '../../';
  7209. }
  7210. $editor_config = array(
  7211. 'ToolbarSet'=> 'LearningPathDocuments',
  7212. 'Width' => '100%',
  7213. 'Height' => '500',
  7214. 'FullPage' => true,
  7215. );
  7216. if ($_GET['action'] == 'add_item') {
  7217. $class = 'add';
  7218. $text = get_lang('LPCreateDocument');
  7219. } else {
  7220. if ($_GET['action'] == 'edit_item') {
  7221. $class = 'save';
  7222. $text = get_lang('SaveDocument');
  7223. }
  7224. }
  7225. $form->addButtonSave($text, 'submit_button');
  7226. $renderer = $form->defaultRenderer();
  7227. $renderer->setElementTemplate('&nbsp;{label}{element}', 'content_lp');
  7228. $form->addElement('html', '<div class="editor-lp">');
  7229. $form->addHtmlEditor('content_lp', null, null, true, $editor_config);
  7230. $form->addElement('html', '</div>');
  7231. $defaults['content_lp'] = $content;
  7232. }
  7233. } elseif (is_numeric($extra_info)) {
  7234. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7235. $return = $this->display_document($extra_info, true, true, true);
  7236. $form->addElement('html', $return);
  7237. }
  7238. }
  7239. }
  7240. if ($action == 'move') {
  7241. $form->addElement('hidden', 'title', $item_title);
  7242. $form->addElement('hidden', 'description', $item_description);
  7243. }
  7244. if (is_numeric($extra_info)) {
  7245. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7246. $form->addElement('hidden', 'path', $extra_info);
  7247. } elseif (is_array($extra_info)) {
  7248. $form->addButtonSave(get_lang('SaveDocument'), 'submit_button');
  7249. $form->addElement('hidden', 'path', $extra_info['path']);
  7250. }
  7251. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7252. $form->addElement('hidden', 'post_time', time());
  7253. $form->setDefaults($defaults);
  7254. return $form->returnForm();
  7255. }
  7256. /**
  7257. * Return HTML form to add/edit a link item
  7258. * @param string $action (add/edit)
  7259. * @param integer $id Item ID if exists
  7260. * @param mixed $extra_info
  7261. * @return string HTML form
  7262. */
  7263. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7264. {
  7265. $course_id = api_get_course_int_id();
  7266. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7267. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7268. if ($id != 0 && is_array($extra_info)) {
  7269. $item_title = stripslashes($extra_info['title']);
  7270. $item_description = stripslashes($extra_info['description']);
  7271. $item_url = stripslashes($extra_info['url']);
  7272. } elseif (is_numeric($extra_info)) {
  7273. $extra_info = intval($extra_info);
  7274. $sql = "SELECT title, description, url FROM " . $tbl_link . "
  7275. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7276. $result = Database::query($sql);
  7277. $row = Database :: fetch_array($result);
  7278. $item_title = $row['title'];
  7279. $item_description = $row['description'];
  7280. $item_url = $row['url'];
  7281. } else {
  7282. $item_title = '';
  7283. $item_description = '';
  7284. $item_url = '';
  7285. }
  7286. $form = new FormValidator('edit_link', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING']);
  7287. $defaults = [];
  7288. if ($id != 0 && is_array($extra_info)) {
  7289. $parent = $extra_info['parent_item_id'];
  7290. } else {
  7291. $parent = 0;
  7292. }
  7293. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7294. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7295. $result = Database::query($sql);
  7296. $arrLP = array();
  7297. while ($row = Database :: fetch_array($result)) {
  7298. $arrLP[] = array(
  7299. 'id' => $row['id'],
  7300. 'item_type' => $row['item_type'],
  7301. 'title' => $row['title'],
  7302. 'path' => $row['path'],
  7303. 'description' => $row['description'],
  7304. 'parent_item_id' => $row['parent_item_id'],
  7305. 'previous_item_id' => $row['previous_item_id'],
  7306. 'next_item_id' => $row['next_item_id'],
  7307. 'display_order' => $row['display_order'],
  7308. 'max_score' => $row['max_score'],
  7309. 'min_score' => $row['min_score'],
  7310. 'mastery_score' => $row['mastery_score'],
  7311. 'prerequisite' => $row['prerequisite']
  7312. );
  7313. }
  7314. $this->tree_array($arrLP);
  7315. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7316. unset ($this->arrMenu);
  7317. if ($action == 'add') {
  7318. $legend = get_lang('CreateTheLink');
  7319. } elseif ($action == 'move') {
  7320. $legend = get_lang('MoveCurrentLink');
  7321. } else {
  7322. $legend = get_lang('EditCurrentLink');
  7323. }
  7324. $form->addHeader($legend);
  7325. if ($action != 'move') {
  7326. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form']);
  7327. $defaults['title'] = $item_title;
  7328. }
  7329. $selectParent = $form->addSelect(
  7330. 'parent',
  7331. get_lang('Parent'),
  7332. [],
  7333. ['id' => 'idParent', 'onchange' => 'load_cbo(this.value);', 'class' => 'learnpath_item_form']
  7334. );
  7335. $selectParent->addOption($this->name, 0);
  7336. $arrHide = array(
  7337. $id
  7338. );
  7339. $parent_item_id = Session::read('parent_item_id');
  7340. for ($i = 0; $i < count($arrLP); $i++) {
  7341. if ($action != 'add') {
  7342. if (
  7343. (
  7344. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7345. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7346. $arrLP[$i]['item_type'] == 'dir'
  7347. ) &&
  7348. !in_array($arrLP[$i]['id'], $arrHide) &&
  7349. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7350. ) {
  7351. $selectParent->addOption(
  7352. $arrLP[$i]['title'],
  7353. $arrLP[$i]['id'],
  7354. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px;']
  7355. );
  7356. if ($parent == $arrLP[$i]['id']) {
  7357. $selectParent->setSelected($arrLP[$i]['id']);
  7358. }
  7359. } else {
  7360. $arrHide[] = $arrLP[$i]['id'];
  7361. }
  7362. } else {
  7363. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7364. $selectParent->addOption(
  7365. $arrLP[$i]['title'],
  7366. $arrLP[$i]['id'],
  7367. ['style' => 'padding-left: ' . (20 + $arrLP[$i]['depth'] * 20) . 'px']
  7368. );
  7369. if ($parent_item_id == $arrLP[$i]['id']) {
  7370. $selectParent->setSelected($arrLP[$i]['id']);
  7371. }
  7372. }
  7373. }
  7374. }
  7375. if (is_array($arrLP)) {
  7376. reset($arrLP);
  7377. }
  7378. $selectPrevious = $form->addSelect(
  7379. 'previous',
  7380. get_lang('Position'),
  7381. [],
  7382. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7383. );
  7384. $selectPrevious->addOption(get_lang('FirstPosition'), 0);
  7385. for ($i = 0; $i < count($arrLP); $i++) {
  7386. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7387. $selectPrevious->addOption($arrLP[$i]['title'], $arrLP[$i]['id']);
  7388. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7389. $selectPrevious->setSelected($arrLP[$i]['id']);
  7390. } elseif ($action == 'add') {
  7391. $selectPrevious->setSelected($arrLP[$i]['id']);
  7392. }
  7393. }
  7394. }
  7395. if ($action != 'move') {
  7396. $urlAttributes = ['class' => 'learnpath_item_form'];
  7397. if (is_numeric($extra_info)) {
  7398. $urlAttributes['disabled'] = 'disabled';
  7399. }
  7400. $form->addElement('url', 'url', get_lang('Url'), $urlAttributes);
  7401. $defaults['url'] = $item_url;
  7402. $id_prerequisite = 0;
  7403. if (is_array($arrLP)) {
  7404. foreach ($arrLP as $key => $value) {
  7405. if ($value['id'] == $id) {
  7406. $id_prerequisite = $value['prerequisite'];
  7407. break;
  7408. }
  7409. }
  7410. }
  7411. $arrHide = array();
  7412. for ($i = 0; $i < count($arrLP); $i++) {
  7413. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7414. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7415. $s_selected_position = $arrLP[$i]['id'];
  7416. elseif ($action == 'add') $s_selected_position = 0;
  7417. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7418. }
  7419. }
  7420. }
  7421. if ($action == 'add') {
  7422. $form->addButtonSave(get_lang('AddLinkToCourse'), 'submit_button');
  7423. } else {
  7424. $form->addButtonSave(get_lang('EditCurrentLink'), 'submit_button');
  7425. }
  7426. if ($action == 'move') {
  7427. $form->addHidden('title', $item_title);
  7428. $form->addHidden('description', $item_description);
  7429. }
  7430. if (is_numeric($extra_info)) {
  7431. $form->addHidden('path', $extra_info);
  7432. } elseif (is_array($extra_info)) {
  7433. $form->addHidden('path', $extra_info['path']);
  7434. }
  7435. $form->addHidden('type', TOOL_LINK);
  7436. $form->addHidden('post_time', time());
  7437. $form->setDefaults($defaults);
  7438. return '<div class="sectioncomment">' . $form->returnForm() . '</div>';
  7439. }
  7440. /**
  7441. * Return HTML form to add/edit a student publication (work)
  7442. * @param string Action (add/edit)
  7443. * @param integer Item ID if already exists
  7444. * @param mixed Extra info (work ID if integer)
  7445. * @return string HTML form
  7446. */
  7447. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7448. {
  7449. $course_id = api_get_course_int_id();
  7450. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7451. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7452. if ($id != 0 && is_array($extra_info)) {
  7453. $item_title = stripslashes($extra_info['title']);
  7454. $item_description = stripslashes($extra_info['description']);
  7455. } elseif (is_numeric($extra_info)) {
  7456. $extra_info = intval($extra_info);
  7457. $sql = "SELECT title, description
  7458. FROM " . $tbl_publication . "
  7459. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7460. $result = Database::query($sql);
  7461. $row = Database :: fetch_array($result);
  7462. $item_title = $row['title'];
  7463. } else {
  7464. $item_title = get_lang('Student_publication');
  7465. }
  7466. if ($id != 0 && is_array($extra_info)) {
  7467. $parent = $extra_info['parent_item_id'];
  7468. } else {
  7469. $parent = 0;
  7470. }
  7471. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7472. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7473. $result = Database::query($sql);
  7474. $arrLP = array();
  7475. while ($row = Database :: fetch_array($result)) {
  7476. $arrLP[] = array (
  7477. 'id' => $row['id'],
  7478. 'item_type' => $row['item_type'],
  7479. 'title' => $row['title'],
  7480. 'path' => $row['path'],
  7481. 'description' => $row['description'],
  7482. 'parent_item_id' => $row['parent_item_id'],
  7483. 'previous_item_id' => $row['previous_item_id'],
  7484. 'next_item_id' => $row['next_item_id'],
  7485. 'display_order' => $row['display_order'],
  7486. 'max_score' => $row['max_score'],
  7487. 'min_score' => $row['min_score'],
  7488. 'mastery_score' => $row['mastery_score'],
  7489. 'prerequisite' => $row['prerequisite']
  7490. );
  7491. }
  7492. $this->tree_array($arrLP);
  7493. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7494. unset ($this->arrMenu);
  7495. $form = new FormValidator('frm_student_publication', 'post', '#');
  7496. if ($action == 'add') {
  7497. $form->addHeader(get_lang('Student_publication'));
  7498. } elseif ($action == 'move') {
  7499. $form->addHeader(get_lang('MoveCurrentStudentPublication'));
  7500. } else {
  7501. $form->addHeader(get_lang('EditCurrentStudentPublication'));
  7502. }
  7503. if ($action != 'move') {
  7504. $form->addText('title', get_lang('Title'), true, ['class' => 'learnpath_item_form', 'id' => 'idTitle']);
  7505. }
  7506. $parentSelect = $form->addSelect(
  7507. 'parent',
  7508. get_lang('Parent'),
  7509. ['0' => $this->name],
  7510. [
  7511. 'onchange' => 'javascript: load_cbo(this.value);',
  7512. 'class' => 'learnpath_item_form',
  7513. 'id' => 'idParent'
  7514. ]
  7515. );
  7516. $arrHide = array (
  7517. $id
  7518. );
  7519. for ($i = 0; $i < count($arrLP); $i++) {
  7520. if ($action != 'add') {
  7521. if (
  7522. (
  7523. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7524. $arrLP[$i]['item_type'] == 'dokeos_chapter' ||
  7525. $arrLP[$i]['item_type'] == 'dir'
  7526. ) &&
  7527. !in_array($arrLP[$i]['id'], $arrHide) &&
  7528. !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7529. ) {
  7530. $parentSelect->addOption(
  7531. $arrLP[$i]['title'],
  7532. $arrLP[$i]['id'],
  7533. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7534. );
  7535. if ($parent == $arrLP[$i]['id']) {
  7536. $parentSelect->setSelected($arrLP[$i]['id']);
  7537. }
  7538. } else {
  7539. $arrHide[] = $arrLP[$i]['id'];
  7540. }
  7541. } else {
  7542. if (
  7543. $arrLP[$i]['item_type'] == 'dokeos_module' ||
  7544. $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir'
  7545. ) {
  7546. $parentSelect->addOption(
  7547. $arrLP[$i]['title'],
  7548. $arrLP[$i]['id'],
  7549. ['style' => 'padding-left: ' . (($arrLP[$i]['depth'] * 10) + 20) . 'px;']
  7550. );
  7551. if ($parent == $arrLP[$i]['id']) {
  7552. $parentSelect->setSelected($arrLP[$i]['id']);
  7553. }
  7554. }
  7555. }
  7556. }
  7557. if (is_array($arrLP)) {
  7558. reset($arrLP);
  7559. }
  7560. $previousSelect = $form->addSelect(
  7561. 'previous',
  7562. get_lang('Position'),
  7563. ['0' => get_lang('FirstPosition')],
  7564. ['id' => 'previous', 'class' => 'learnpath_item_form']
  7565. );
  7566. for ($i = 0; $i < count($arrLP); $i++) {
  7567. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7568. $previousSelect->addOption(
  7569. get_lang('After') . ' "' . $arrLP[$i]['title'] . '"',
  7570. $arrLP[$i]['id']
  7571. );
  7572. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7573. $previousSelect->setSelected($arrLP[$i]['id']);
  7574. } elseif ($action == 'add') {
  7575. $previousSelect->setSelected($arrLP[$i]['id']);
  7576. }
  7577. }
  7578. }
  7579. if ($action != 'move') {
  7580. $id_prerequisite = 0;
  7581. if (is_array($arrLP)) {
  7582. foreach ($arrLP as $key => $value) {
  7583. if ($value['id'] == $id) {
  7584. $id_prerequisite = $value['prerequisite'];
  7585. break;
  7586. }
  7587. }
  7588. }
  7589. $arrHide = array ();
  7590. for ($i = 0; $i < count($arrLP); $i++) {
  7591. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7592. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7593. $s_selected_position = $arrLP[$i]['id'];
  7594. elseif ($action == 'add') $s_selected_position = 0;
  7595. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7596. }
  7597. }
  7598. }
  7599. if ($action == 'add') {
  7600. $form->addButtonCreate(get_lang('AddAssignmentToCourse'), 'submit_button');
  7601. } else {
  7602. $form->addButtonCreate(get_lang('EditCurrentStudentPublication'), 'submit_button');
  7603. }
  7604. if ($action == 'move') {
  7605. $form->addHidden('title', $item_title);
  7606. $form->addHidden('description', $item_description);
  7607. }
  7608. if (is_numeric($extra_info)) {
  7609. $form->addHidden('path', $extra_info);
  7610. } elseif (is_array($extra_info)) {
  7611. $form->addHidden('path', $extra_info['path']);
  7612. }
  7613. $form->addHidden('type', TOOL_STUDENTPUBLICATION);
  7614. $form->addHidden('post_time', time());
  7615. $form->setDefaults(['title' => $item_title]);
  7616. $return = '<div class="sectioncomment">';
  7617. $return .= $form->returnForm();
  7618. $return .= '</div>';
  7619. return $return;
  7620. }
  7621. /**
  7622. * Displays the menu for manipulating a step
  7623. *
  7624. * @param $item_id
  7625. * @param string $item_type
  7626. * @return string
  7627. */
  7628. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7629. {
  7630. $_course = api_get_course_info();
  7631. $course_id = api_get_course_int_id();
  7632. $course_code = api_get_course_id();
  7633. $return = '<div class="actions">';
  7634. switch ($item_type) {
  7635. case 'dokeos_chapter' :
  7636. case 'chapter' :
  7637. // Commented the message cause should not show it.
  7638. //$lang = get_lang('TitleManipulateChapter');
  7639. break;
  7640. case 'dokeos_module' :
  7641. case 'module' :
  7642. // Commented the message cause should not show it.
  7643. //$lang = get_lang('TitleManipulateModule');
  7644. break;
  7645. case TOOL_LP_FINAL_ITEM:
  7646. case TOOL_DOCUMENT :
  7647. // Commented the message cause should not show it.
  7648. //$lang = get_lang('TitleManipulateDocument');
  7649. break;
  7650. case TOOL_LINK :
  7651. case 'link' :
  7652. // Commented the message cause should not show it.
  7653. //$lang = get_lang('TitleManipulateLink');
  7654. break;
  7655. case TOOL_QUIZ :
  7656. // Commented the message cause should not show it.
  7657. //$lang = get_lang('TitleManipulateQuiz');
  7658. break;
  7659. case TOOL_STUDENTPUBLICATION :
  7660. // Commented the message cause should not show it.
  7661. //$lang = get_lang('TitleManipulateStudentPublication');
  7662. break;
  7663. }
  7664. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7665. $item_id = intval($item_id);
  7666. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7667. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7668. $result = Database::query($sql);
  7669. $row = Database::fetch_assoc($result);
  7670. $audio_player = null;
  7671. // We display an audio player if needed.
  7672. if (!empty($row['audio'])) {
  7673. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7674. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7675. </div>';
  7676. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7677. $audio_player .= '<script>
  7678. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7679. s1.addParam("allowscriptaccess","always");
  7680. s1.addParam("flashvars","file=../..'.api_get_path(REL_COURSE_PATH).$_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7681. s1.write("container");
  7682. </script>';
  7683. }
  7684. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7685. $return .= Display::url(
  7686. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7687. $url.'&action=edit_item&path_item=' . $row['path']
  7688. );
  7689. $return .= Display::url(
  7690. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7691. $url.'&action=move_item'
  7692. );
  7693. // Commented for now as prerequisites cannot be added to chapters.
  7694. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7695. $return .= Display::url(
  7696. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7697. $url.'&action=edit_item_prereq'
  7698. );
  7699. }
  7700. $return .= Display::url(
  7701. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7702. $url.'&action=delete_item'
  7703. );
  7704. if ($item_type == TOOL_HOTPOTATOES ) {
  7705. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7706. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7707. }
  7708. if ($item_type == TOOL_DOCUMENT || $item_type == TOOL_LP_FINAL_ITEM) {
  7709. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7710. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7711. }
  7712. $return .= '</div>';
  7713. if (!empty($audio_player)) {
  7714. $return .= '<br />'.$audio_player;
  7715. }
  7716. return $return;
  7717. }
  7718. /**
  7719. * Creates the javascript needed for filling up the checkboxes without page reload
  7720. * @return string
  7721. */
  7722. public function get_js_dropdown_array()
  7723. {
  7724. $course_id = api_get_course_int_id();
  7725. $return = 'var child_name = new Array();' . "\n";
  7726. $return .= 'var child_value = new Array();' . "\n\n";
  7727. $return .= 'child_name[0] = new Array();' . "\n";
  7728. $return .= 'child_value[0] = new Array();' . "\n\n";
  7729. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7730. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7731. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7732. ORDER BY display_order ASC";
  7733. $res_zero = Database::query($sql_zero);
  7734. $i = 0;
  7735. while ($row_zero = Database :: fetch_array($res_zero)) {
  7736. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7737. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7738. }
  7739. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7740. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7741. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7742. }
  7743. $return .= "\n";
  7744. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7745. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7746. $res = Database::query($sql);
  7747. while ($row = Database :: fetch_array($res)) {
  7748. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7749. WHERE
  7750. c_id = ".$course_id." AND
  7751. parent_item_id = " . $row['id'] . "
  7752. ORDER BY display_order ASC";
  7753. $res_parent = Database::query($sql_parent);
  7754. $i = 0;
  7755. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7756. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7757. while ($row_parent = Database :: fetch_array($res_parent)) {
  7758. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7759. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7760. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7761. }
  7762. $return .= "\n";
  7763. }
  7764. return $return;
  7765. }
  7766. /**
  7767. * Display the form to allow moving an item
  7768. * @param integer Item ID
  7769. * @return string HTML form
  7770. */
  7771. public function display_move_item($item_id)
  7772. {
  7773. $course_id = api_get_course_int_id();
  7774. $return = '';
  7775. if (is_numeric($item_id)) {
  7776. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7777. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7778. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7779. $res = Database::query($sql);
  7780. $row = Database :: fetch_array($res);
  7781. switch ($row['item_type']) {
  7782. case 'dokeos_chapter' :
  7783. case 'dir' :
  7784. case 'asset' :
  7785. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7786. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7787. break;
  7788. case 'dokeos_module' :
  7789. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7790. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7791. break;
  7792. case TOOL_DOCUMENT :
  7793. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7794. $return .= $this->display_document_form('move', $item_id, $row);
  7795. break;
  7796. case TOOL_LINK :
  7797. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7798. $return .= $this->display_link_form('move', $item_id, $row);
  7799. break;
  7800. case TOOL_HOTPOTATOES :
  7801. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7802. $return .= $this->display_link_form('move', $item_id, $row);
  7803. break;
  7804. case TOOL_QUIZ :
  7805. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7806. $return .= $this->display_quiz_form('move', $item_id, $row);
  7807. break;
  7808. case TOOL_STUDENTPUBLICATION :
  7809. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7810. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7811. break;
  7812. case TOOL_FORUM :
  7813. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7814. $return .= $this->display_forum_form('move', $item_id, $row);
  7815. break;
  7816. case TOOL_THREAD :
  7817. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7818. $return .= $this->display_forum_form('move', $item_id, $row);
  7819. break;
  7820. }
  7821. }
  7822. return $return;
  7823. }
  7824. /**
  7825. * Displays a basic form on the overview page for changing the item title and the item description.
  7826. * @param string $item_type
  7827. * @param string $title
  7828. * @param array $data
  7829. * @return string
  7830. */
  7831. public function display_item_small_form($item_type, $title = '', $data = array())
  7832. {
  7833. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7834. $form = new FormValidator('small_form', 'post', $url);
  7835. $form->addElement('header', $title);
  7836. $form->addElement('text', 'title', get_lang('Title'));
  7837. $form->addButtonSave(get_lang('Save'), 'submit_button');
  7838. $form->addElement('hidden', 'id', $data['id']);
  7839. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7840. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7841. $form->setDefaults(array('title' => $data['title']));
  7842. return $form->toHtml();
  7843. }
  7844. /**
  7845. * Return HTML form to allow prerequisites selection
  7846. * @todo use FormValidator
  7847. * @param integer Item ID
  7848. * @return string HTML form
  7849. */
  7850. public function display_item_prerequisites_form($item_id)
  7851. {
  7852. $course_id = api_get_course_int_id();
  7853. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7854. $item_id = intval($item_id);
  7855. /* Current prerequisite */
  7856. $sql = "SELECT * FROM $tbl_lp_item
  7857. WHERE c_id = $course_id AND id = " . $item_id;
  7858. $result = Database::query($sql);
  7859. $row = Database::fetch_array($result);
  7860. $prerequisiteId = $row['prerequisite'];
  7861. $return = '<legend>';
  7862. $return .= get_lang('AddEditPrerequisites');
  7863. $return .= '</legend>';
  7864. $return .= '<form method="POST">';
  7865. $return .= '<table class="data_table">';
  7866. $return .= '<tr>';
  7867. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7868. $return .= '<th width="70" >' . get_lang('Minimum') . '</th>';
  7869. $return .= '<th width="70">' . get_lang('Maximum') . '</th>';
  7870. $return .= '</tr>';
  7871. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7872. $return .= '<tr >';
  7873. $return .= '<td colspan="3" class="radio">';
  7874. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7875. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7876. $return .= '</tr>';
  7877. $sql = "SELECT * FROM $tbl_lp_item
  7878. WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7879. $result = Database::query($sql);
  7880. $arrLP = array();
  7881. $selectedMinScore = array();
  7882. $selectedMaxScore = array();
  7883. while ($row = Database :: fetch_array($result)) {
  7884. if ($row['id'] == $item_id) {
  7885. $selectedMinScore[$row['prerequisite']] = $row['prerequisite_min_score'];
  7886. $selectedMaxScore[$row['prerequisite']] = $row['prerequisite_max_score'];
  7887. }
  7888. $arrLP[] = array(
  7889. 'id' => $row['id'],
  7890. 'item_type' => $row['item_type'],
  7891. 'title' => $row['title'],
  7892. 'ref' => $row['ref'],
  7893. 'description' => $row['description'],
  7894. 'parent_item_id' => $row['parent_item_id'],
  7895. 'previous_item_id' => $row['previous_item_id'],
  7896. 'next_item_id' => $row['next_item_id'],
  7897. 'max_score' => $row['max_score'],
  7898. 'min_score' => $row['min_score'],
  7899. 'mastery_score' => $row['mastery_score'],
  7900. 'prerequisite' => $row['prerequisite'],
  7901. 'next_item_id' => $row['next_item_id'],
  7902. 'display_order' => $row['display_order'],
  7903. 'prerequisite_min_score' => $row['prerequisite_min_score'],
  7904. 'prerequisite_max_score' => $row['prerequisite_max_score'],
  7905. );
  7906. }
  7907. $this->tree_array($arrLP);
  7908. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  7909. unset($this->arrMenu);
  7910. for ($i = 0; $i < count($arrLP); $i++) {
  7911. $item = $arrLP[$i];
  7912. if ($item['id'] == $item_id) {
  7913. break;
  7914. }
  7915. $selectedMaxScoreValue = isset($selectedMaxScore[$item['id']]) ? $selectedMaxScore[$item['id']] : $item['max_score'];
  7916. $selectedMinScoreValue = isset($selectedMinScore[$item['id']]) ? $selectedMinScore[$item['id']]: 0;
  7917. $return .= '<tr>';
  7918. $return .= '<td class="radio"' . (($item['item_type'] != TOOL_QUIZ && $item['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7919. $return .= '<label for="id' . $item['id'] . '">';
  7920. $return .= '<input' . (in_array($prerequisiteId, array($item['id'], $item['ref'])) ? ' checked="checked" ' : '') . (($item['item_type'] == 'dokeos_module' || $item['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ') . 'id="id' . $item['id'] . '" name="prerequisites" style="margin-left:' . $item['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $item['id'] . '" />';
  7921. $icon_name = str_replace(' ', '', $item['item_type']);
  7922. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7923. $return .= Display::return_icon('lp_' . $icon_name . '.png');
  7924. } else {
  7925. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7926. $return .= Display::return_icon('lp_' . $icon_name . '.gif');
  7927. } else {
  7928. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7929. }
  7930. }
  7931. $return .= $item['title'] . '</label>';
  7932. $return .= '</td>';
  7933. if ($item['item_type'] == TOOL_QUIZ) {
  7934. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7935. $tmp_obj_lp_item = new LpItem($course_id, $item['id']);
  7936. $tmp_obj_exercice = new Exercise();
  7937. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7938. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7939. $tmp_obj_lp_item->update_in_bdd();
  7940. $item['max_score'] = $tmp_obj_lp_item->max_score;
  7941. $return .= '<td class="exercise">';
  7942. $return .= '<input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue. '" />';
  7943. $return .= '</td>';
  7944. $return .= '<td class="exercise">';
  7945. $return .= '<input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMaxScoreValue . '" />';
  7946. $return .= '</td>';
  7947. }
  7948. if ($item['item_type'] == TOOL_HOTPOTATOES) {
  7949. $return .= '<td class="exercise">';
  7950. $return .= '<center><input size="4" maxlength="3" name="min_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="' . $selectedMinScoreValue . '" /></center>';
  7951. $return .= '</td>';
  7952. $return .= '<td class="exercise"">';
  7953. $return .= '<center><input size="4" maxlength="3" name="max_' . $item['id'] . '" type="number" min="0" step="any" max="'.$item['max_score'].'" value="'.$selectedMaxScoreValue . '" /></center>';
  7954. $return .= '</td>';
  7955. }
  7956. $return .= '</tr>';
  7957. }
  7958. $return .= '<tr>';
  7959. $return .= '</tr>';
  7960. $return .= '</table>';
  7961. $return .= '<div style="padding-top:3px;">';
  7962. $return .= '<button class="btn btn-primary" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7963. $return .= '</form>';
  7964. return $return;
  7965. }
  7966. /**
  7967. * Return HTML list to allow prerequisites selection for lp
  7968. * @param integer Item ID
  7969. * @return string HTML form
  7970. */
  7971. public function display_lp_prerequisites_list()
  7972. {
  7973. $course_id = api_get_course_int_id();
  7974. $lp_id = $this->lp_id;
  7975. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7976. // get current prerequisite
  7977. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7978. $result = Database::query($sql);
  7979. $row = Database :: fetch_array($result);
  7980. $prerequisiteId = $row['prerequisite'];
  7981. $session_id = api_get_session_id();
  7982. $session_condition = api_get_session_condition($session_id);
  7983. $sql = "SELECT * FROM $tbl_lp
  7984. WHERE c_id = $course_id $session_condition
  7985. ORDER BY display_order ";
  7986. $rs = Database::query($sql);
  7987. $return = '';
  7988. $return .= '<select name="prerequisites" class="form-control">';
  7989. $return .= '<option value="0">'.get_lang('None').'</option>';
  7990. if (Database::num_rows($rs) > 0) {
  7991. while ($row = Database::fetch_array($rs)) {
  7992. if ($row['id'] == $lp_id) {
  7993. continue;
  7994. }
  7995. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$prerequisiteId)?' selected ' : '').'>'.$row['name'].'</option>';
  7996. }
  7997. }
  7998. $return .= '</select>';
  7999. return $return;
  8000. }
  8001. /**
  8002. * Creates a list with all the documents in it
  8003. * @param bool $showInvisibleFiles
  8004. * @return string
  8005. */
  8006. public function get_documents($showInvisibleFiles = false)
  8007. {
  8008. $course_info = api_get_course_info();
  8009. $sessionId = api_get_session_id();
  8010. $documentTree = DocumentManager::get_document_preview(
  8011. $course_info,
  8012. $this->lp_id,
  8013. null,
  8014. $sessionId,
  8015. true,
  8016. null,
  8017. null,
  8018. $showInvisibleFiles,
  8019. true
  8020. );
  8021. $headers = array(
  8022. get_lang('Files'),
  8023. get_lang('NewDocument'),
  8024. get_lang('Upload'),
  8025. );
  8026. $form = new FormValidator(
  8027. 'form_upload',
  8028. 'POST',
  8029. api_get_self().'?'.$_SERVER['QUERY_STRING'],
  8030. '',
  8031. array('enctype' => "multipart/form-data")
  8032. );
  8033. $folders = DocumentManager::get_all_document_folders(
  8034. api_get_course_info(),
  8035. 0,
  8036. true
  8037. );
  8038. DocumentManager::build_directory_selector(
  8039. $folders,
  8040. '',
  8041. array(),
  8042. true,
  8043. $form,
  8044. 'directory_parent_id'
  8045. );
  8046. $form->addElement('radio', 'if_exists', get_lang('UplWhatIfFileExists'), get_lang('UplDoNothing'), 'nothing');
  8047. $form->addElement('radio', 'if_exists', '', get_lang('UplOverwriteLong'), 'overwrite');
  8048. $form->addElement('radio', 'if_exists', '', get_lang('UplRenameLong'), 'rename');
  8049. $form->setDefaults(['if_exists' => 'rename']);
  8050. // Check box options
  8051. $form->addElement(
  8052. 'checkbox',
  8053. 'unzip',
  8054. get_lang('Options'),
  8055. get_lang('Uncompress')
  8056. );
  8057. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
  8058. $form->addMultipleUpload($url);
  8059. $new = $this->display_document_form('add', 0);
  8060. $tabs = Display::tabs($headers, array($documentTree, $new, $form->returnForm()), 'subtab');
  8061. return $tabs;
  8062. }
  8063. /**
  8064. * Creates a list with all the exercises (quiz) in it
  8065. * @return string
  8066. */
  8067. public function get_exercises()
  8068. {
  8069. $course_id = api_get_course_int_id();
  8070. // New for hotpotatoes.
  8071. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8072. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8073. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8074. $session_id = api_get_session_id();
  8075. $condition_session = api_get_session_condition($session_id);
  8076. $setting = api_get_configuration_value('show_invisible_exercise_in_lp_list');
  8077. $activeCondition = " active <> -1 ";
  8078. if ($setting) {
  8079. $activeCondition = " active = 1 ";
  8080. }
  8081. $sql_quiz = "SELECT * FROM $tbl_quiz
  8082. WHERE c_id = $course_id AND $activeCondition $condition_session
  8083. ORDER BY title ASC";
  8084. $sql_hot = "SELECT * FROM $tbl_doc
  8085. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  8086. ORDER BY id ASC";
  8087. $res_quiz = Database::query($sql_quiz);
  8088. $res_hot = Database::query($sql_hot);
  8089. $return = '<ul class="lp_resource">';
  8090. $return .= '<li class="lp_resource_element">';
  8091. $return .= Display::return_icon('new_test_small.gif');
  8092. $return .= '<a href="' . api_get_path(WEB_CODE_PATH) . 'exercice/exercise_admin.php?'.api_get_cidreq().'&lp_id=' . $this->lp_id . '">' .
  8093. get_lang('NewExercise') . '</a>';
  8094. $return .= '</li>';
  8095. // Display hotpotatoes
  8096. while ($row_hot = Database :: fetch_array($res_hot)) {
  8097. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8098. $return .= '<a class="moved" href="#">';
  8099. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8100. $return .= '</a> ';
  8101. $return .= Display::return_icon('hotpotatoes_s.png');
  8102. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&action=add_item&type=' . TOOL_HOTPOTATOES . '&file=' . $row_hot['id'] . '&lp_id=' . $this->lp_id . '">'.
  8103. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  8104. $return .= '</li>';
  8105. }
  8106. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8107. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8108. $return .= '<a class="moved" href="#">';
  8109. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8110. $return .= '</a> ';
  8111. $return .= Display::return_icon(
  8112. 'quizz_small.gif',
  8113. '',
  8114. array(),
  8115. ICON_SIZE_TINY
  8116. );
  8117. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_QUIZ . '&file=' . $row_quiz['id'] . '&lp_id=' . $this->lp_id . '">' .
  8118. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  8119. '</a>';
  8120. $return .= '</li>';
  8121. }
  8122. $return .= '</ul>';
  8123. return $return;
  8124. }
  8125. /**
  8126. * Creates a list with all the links in it
  8127. * @return string
  8128. */
  8129. public function get_links()
  8130. {
  8131. $selfUrl = api_get_self();
  8132. $courseIdReq = api_get_cidreq();
  8133. $course = api_get_course_info();
  8134. $course_id = $course['real_id'];
  8135. $tbl_link = Database::get_course_table(TABLE_LINK);
  8136. $linkCategoryTable = Database::get_course_table(TABLE_LINK_CATEGORY);
  8137. $moveEverywhereIcon = Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8138. $session_id = api_get_session_id();
  8139. $condition_session = api_get_session_condition($session_id, true, null, "link.session_id");
  8140. $sql = "SELECT link.id as link_id,
  8141. link.title as link_title,
  8142. link.category_id as category_id,
  8143. link_category.category_title as category_title
  8144. FROM $tbl_link as link
  8145. LEFT JOIN $linkCategoryTable as link_category
  8146. ON link.category_id = link_category.id
  8147. WHERE link.c_id = ".$course_id." $condition_session
  8148. ORDER BY link_category.category_title ASC, link.title ASC";
  8149. $links = Database::query($sql);
  8150. $categorizedLinks = array();
  8151. $categories = array();
  8152. while ($link = Database :: fetch_array($links)) {
  8153. if (!$link['category_id']) {
  8154. $link['category_title'] = get_lang('Uncategorized');
  8155. }
  8156. $categories[$link['category_id']] = $link['category_title'];
  8157. $categorizedLinks[$link['category_id']][$link['link_id']] = $link['link_title'];
  8158. }
  8159. $linksHtmlCode =
  8160. '<script>
  8161. function toggle_tool(tool, id){
  8162. if(document.getElementById(tool+"_"+id+"_content").style.display == "none"){
  8163. document.getElementById(tool+"_"+id+"_content").style.display = "block";
  8164. document.getElementById(tool+"_"+id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8165. } else {
  8166. document.getElementById(tool+"_"+id+"_content").style.display = "none";
  8167. document.getElementById(tool+"_"+id+"_opener").src = "'.Display::returnIconPath('add.gif').'";
  8168. }
  8169. }
  8170. </script>
  8171. <ul class="lp_resource">
  8172. <li class="lp_resource_element">
  8173. '.Display::return_icon(
  8174. 'linksnew.gif',
  8175. '',
  8176. array(),
  8177. ICON_SIZE_TINY
  8178. ).'
  8179. <a href="'.api_get_path(WEB_CODE_PATH).'link/link.php?'.$courseIdReq.
  8180. '&action=addlink&lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>
  8181. </li>';
  8182. foreach ($categorizedLinks as $categoryId => $links) {
  8183. $linkNodes = null;
  8184. foreach ($links as $key => $title) {
  8185. $link = Display::url(
  8186. Display::return_icon('preview_view.png', get_lang('Preview')),
  8187. api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$key,
  8188. ['target' => '_blank']
  8189. );
  8190. if (api_get_item_visibility($course, TOOL_LINK, $key, $session_id) != 2) {
  8191. $linkNodes .=
  8192. '<li class="lp_resource_element" data_id="'.$key.
  8193. '" data_type="'.TOOL_LINK.'" title="'.$title.'" >
  8194. <a class="moved" href="#">'.
  8195. $moveEverywhereIcon.
  8196. '</a>
  8197. '.Display::return_icon(
  8198. 'lp_link.png',
  8199. '',
  8200. array(),
  8201. ICON_SIZE_TINY
  8202. ).'
  8203. <a href="'.$selfUrl.'?'.$courseIdReq.'&action=add_item&type='.
  8204. TOOL_LINK.'&file='.$key.'&lp_id='.$this->lp_id.'">'.
  8205. Security::remove_XSS($title).
  8206. '</a>
  8207. </li>';
  8208. }
  8209. }
  8210. $linksHtmlCode .=
  8211. '<li>
  8212. <a style="cursor:hand" onclick="javascript: toggle_tool(\''.TOOL_LINK.'\','.$categoryId.')" style="vertical-align:middle">
  8213. <img src="'.Display::returnIconPath('add.gif').'" id="'.TOOL_LINK.'_'.$categoryId.'_opener"
  8214. align="absbottom" />
  8215. </a>
  8216. <span style="vertical-align:middle">'.Security::remove_XSS($categories[$categoryId]).'</span>
  8217. </li>
  8218. <div style="display:none" id="'.TOOL_LINK.'_'.$categoryId.'_content">'.$linkNodes.'</div>';
  8219. }
  8220. $linksHtmlCode .= '</ul>';
  8221. return $linksHtmlCode;
  8222. }
  8223. /**
  8224. * Creates a list with all the student publications in it
  8225. * @return string
  8226. */
  8227. public function get_student_publications()
  8228. {
  8229. $return = '<ul class="lp_resource" >';
  8230. $return .= '<li class="lp_resource_element">';
  8231. $return .= Display::return_icon('works_new.gif');
  8232. $return .= ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_STUDENTPUBLICATION . '&lp_id=' . $this->lp_id . '">' .
  8233. get_lang('AddAssignmentPage') . '</a>';
  8234. $return .= '</li>';
  8235. $sessionId = api_get_session_id();
  8236. if (empty($sessionId)) {
  8237. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8238. $works = getWorkListTeacher(0, 100, null, null, null);
  8239. if (!empty($works)) {
  8240. foreach ($works as $work) {
  8241. $link = Display::url(
  8242. Display::return_icon('preview_view.png', get_lang('Preview')),
  8243. api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work['iid'],
  8244. ['target' => '_blank']
  8245. );
  8246. $return .= '<li class="lp_resource_element" data_id="'.$work['iid'].'" data_type="'.TOOL_STUDENTPUBLICATION.'" title="'.Security :: remove_XSS(cut(strip_tags($work['title']), 80)).'">';
  8247. $return .= '<a class="moved" href="#">';
  8248. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8249. $return .= '</a> ';
  8250. $return .= Display::return_icon('works.gif');
  8251. $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 . '">' .
  8252. Security :: remove_XSS(cut(strip_tags($work['title']), 80)).' '.$link.'
  8253. </a>';
  8254. $return .= '</li>';
  8255. }
  8256. }
  8257. }
  8258. $return .= '</ul>';
  8259. return $return;
  8260. }
  8261. /**
  8262. * Creates a list with all the forums in it
  8263. * @return string
  8264. */
  8265. public function get_forums()
  8266. {
  8267. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  8268. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  8269. $a_forums = get_forums();
  8270. $return = '<ul class="lp_resource">';
  8271. //First add link
  8272. $return .= '<li class="lp_resource_element">';
  8273. $return .= Display::return_icon(
  8274. 'forum_new_small.gif',
  8275. '',
  8276. array(),
  8277. ICON_SIZE_TINY
  8278. );
  8279. $return .= Display::url(
  8280. get_lang('CreateANewForum'),
  8281. api_get_path(WEB_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&' . http_build_query([
  8282. 'action' => 'add',
  8283. 'content' => 'forum',
  8284. 'lp_id' => $this->lp_id
  8285. ]),
  8286. ['title' => get_lang('CreateANewForum')]
  8287. );
  8288. $return .= '</li>';
  8289. $return .= '<script>
  8290. function toggle_forum(forum_id){
  8291. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8292. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8293. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('remove.gif').'";
  8294. } else {
  8295. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8296. document.getElementById("forum_"+forum_id+"_opener").src = "' . Display::returnIconPath('add.gif').'";
  8297. }
  8298. }
  8299. </script>';
  8300. foreach ($a_forums as $forum) {
  8301. if (!empty($forum['forum_id'])) {
  8302. $link = Display::url(
  8303. Display::return_icon('preview_view.png', get_lang('Preview')),
  8304. api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forum['forum_id'],
  8305. ['target' => '_blank']
  8306. );
  8307. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8308. $return .= '<a class="moved" href="#">';
  8309. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8310. $return .= ' </a>';
  8311. $return .= Display::return_icon('lp_forum.png', '', array(), ICON_SIZE_TINY);
  8312. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  8313. <img src="' . Display::returnIconPath('add.gif').'" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  8314. </a>
  8315. <a href="' . api_get_self() . '?'.api_get_cidreq().'&action=add_item&type=' . TOOL_FORUM . '&forum_id=' . $forum['forum_id'] . '&lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  8316. Security :: remove_XSS($forum['forum_title']) . '</a>';
  8317. $return .= '</li>';
  8318. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  8319. $a_threads = get_threads($forum['forum_id']);
  8320. if (is_array($a_threads)) {
  8321. foreach ($a_threads as $thread) {
  8322. $link = Display::url(
  8323. Display::return_icon('preview_view.png', get_lang('Preview')),
  8324. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum['forum_id'].'&thread='.$thread['thread_id'],
  8325. ['target' => '_blank']
  8326. );
  8327. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8328. $return .= '&nbsp;<a class="moved" href="#">';
  8329. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8330. $return .= ' </a>';
  8331. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8332. $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 . '">' .
  8333. Security :: remove_XSS($thread['thread_title']) . ' '.$link.'</a>';
  8334. $return .= '</li>';
  8335. }
  8336. }
  8337. $return .= '</div>';
  8338. }
  8339. }
  8340. $return .= '</ul>';
  8341. return $return;
  8342. }
  8343. /**
  8344. * // TODO: The output encoding should be equal to the system encoding.
  8345. *
  8346. * Exports the learning path as a SCORM package. This is the main function that
  8347. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8348. * whole thing and returns the zip.
  8349. *
  8350. * This method needs to be called in PHP5, as it will fail with non-adequate
  8351. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8352. * you need to call it on a learnpath object.
  8353. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8354. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8355. * path has been modified, it should use the generic method here below.
  8356. * @TODO link this function with the export_lp() function in the same class
  8357. * @param string Optional name of zip file. If none, title of learnpath is
  8358. * domesticated and trailed with ".zip"
  8359. * @return string Returns the zip package string, or null if error
  8360. */
  8361. public function scorm_export()
  8362. {
  8363. $_course = api_get_course_info();
  8364. $course_id = $_course['real_id'];
  8365. // Remove memory and time limits as much as possible as this might be a long process...
  8366. if (function_exists('ini_set')) {
  8367. api_set_memory_limit('128M');
  8368. ini_set('max_execution_time', 600);
  8369. }
  8370. // Create the zip handler (this will remain available throughout the method).
  8371. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8372. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8373. $temp_dir_short = uniqid();
  8374. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8375. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8376. $zip_folder = new PclZip($temp_zip_file);
  8377. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8378. $root_path = $main_path = api_get_path(SYS_PATH);
  8379. $files_cleanup = array();
  8380. // Place to temporarily stash the zip file.
  8381. // create the temp dir if it doesn't exist
  8382. // or do a cleanup before creating the zip file.
  8383. if (!is_dir($temp_zip_dir)) {
  8384. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8385. } else {
  8386. // Cleanup: Check the temp dir for old files and delete them.
  8387. $handle = opendir($temp_zip_dir);
  8388. while (false !== ($file = readdir($handle))) {
  8389. if ($file != '.' && $file != '..') {
  8390. unlink("$temp_zip_dir/$file");
  8391. }
  8392. }
  8393. closedir($handle);
  8394. }
  8395. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8396. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  8397. // Remove the possible . at the end of the path.
  8398. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8399. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8400. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8401. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  8402. }
  8403. // Build a dummy imsmanifest structure.
  8404. // Do not add to the zip yet (we still need it).
  8405. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8406. // Aggregation Model official document, section "2.3 Content Packaging".
  8407. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8408. $xmldoc = new DOMDocument('1.0');
  8409. $root = $xmldoc->createElement('manifest');
  8410. $root->setAttribute('identifier', 'SingleCourseManifest');
  8411. $root->setAttribute('version', '1.1');
  8412. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8413. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8414. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8415. $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');
  8416. // Build mandatory sub-root container elements.
  8417. $metadata = $xmldoc->createElement('metadata');
  8418. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8419. $metadata->appendChild($md_schema);
  8420. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8421. $metadata->appendChild($md_schemaversion);
  8422. $root->appendChild($metadata);
  8423. $organizations = $xmldoc->createElement('organizations');
  8424. $resources = $xmldoc->createElement('resources');
  8425. // Build the only organization we will use in building our learnpaths.
  8426. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8427. $organization = $xmldoc->createElement('organization');
  8428. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8429. // To set the title of the SCORM entity (=organization), we take the name given
  8430. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8431. // learning path charset) as it is the encoding that defines how it is stored
  8432. // in the database. Then we convert it to HTML entities again as the "&" character
  8433. // alone is not authorized in XML (must be &amp;).
  8434. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8435. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8436. $organization->appendChild($org_title);
  8437. $folder_name = 'document';
  8438. // Removes the learning_path/scorm_folder path when exporting see #4841
  8439. $path_to_remove = null;
  8440. $result = $this->generate_lp_folder($_course);
  8441. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8442. $path_to_remove = 'document'.$result['dir'];
  8443. $path_to_replace = $folder_name.'/';
  8444. }
  8445. //Fixes chamilo scorm exports
  8446. if ($this->ref == 'chamilo_scorm_export') {
  8447. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8448. }
  8449. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8450. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8451. $link_updates = array();
  8452. $links_to_create = array();
  8453. foreach ($this->items as $index => $item) {
  8454. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8455. // Get included documents from this item.
  8456. if ($item->type == 'sco') {
  8457. $inc_docs = $item->get_resources_from_source(
  8458. null,
  8459. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  8460. );
  8461. } else {
  8462. $inc_docs = $item->get_resources_from_source();
  8463. }
  8464. // Give a child element <item> to the <organization> element.
  8465. $my_item_id = $item->get_id();
  8466. $my_item = $xmldoc->createElement('item');
  8467. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8468. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8469. $my_item->setAttribute('isvisible', 'true');
  8470. // Give a child element <title> to the <item> element.
  8471. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8472. $my_item->appendChild($my_title);
  8473. // Give a child element <adlcp:prerequisites> to the <item> element.
  8474. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  8475. $my_prereqs->setAttribute('type', 'aicc_script');
  8476. $my_item->appendChild($my_prereqs);
  8477. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8478. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8479. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8480. //$xmldoc->createElement('adlcp:timelimitaction','');
  8481. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8482. //$xmldoc->createElement('adlcp:datafromlms','');
  8483. // Give a child element <adlcp:masteryscore> to the <item> element.
  8484. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8485. $my_item->appendChild($my_masteryscore);
  8486. // Attach this item to the organization element or hits parent if there is one.
  8487. if (!empty($item->parent) && $item->parent != 0) {
  8488. $children = $organization->childNodes;
  8489. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8490. if (is_object($possible_parent)) {
  8491. $possible_parent->appendChild($my_item);
  8492. } else {
  8493. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  8494. }
  8495. } else {
  8496. if ($this->debug > 0) { error_log('No parent'); }
  8497. $organization->appendChild($my_item);
  8498. }
  8499. // Get the path of the file(s) from the course directory root.
  8500. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8501. if (!empty($path_to_remove)) {
  8502. //From docs
  8503. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8504. //From quiz
  8505. if ($this->ref == 'chamilo_scorm_export') {
  8506. $path_to_remove = 'scorm/'.$this->path.'/';
  8507. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8508. }
  8509. } else {
  8510. $my_xml_file_path = $my_file_path;
  8511. }
  8512. $my_sub_dir = dirname($my_file_path);
  8513. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8514. $my_xml_sub_dir = $my_sub_dir;
  8515. // Give a <resource> child to the <resources> element
  8516. $my_resource = $xmldoc->createElement('resource');
  8517. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8518. $my_resource->setAttribute('type', 'webcontent');
  8519. $my_resource->setAttribute('href', $my_xml_file_path);
  8520. // adlcp:scormtype can be either 'sco' or 'asset'.
  8521. if ($item->type == 'sco') {
  8522. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8523. } else {
  8524. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8525. }
  8526. // xml:base is the base directory to find the files declared in this resource.
  8527. $my_resource->setAttribute('xml:base', '');
  8528. // Give a <file> child to the <resource> element.
  8529. $my_file = $xmldoc->createElement('file');
  8530. $my_file->setAttribute('href', $my_xml_file_path);
  8531. $my_resource->appendChild($my_file);
  8532. // Dependency to other files - not yet supported.
  8533. $i = 1;
  8534. foreach ($inc_docs as $doc_info) {
  8535. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8536. $my_dep = $xmldoc->createElement('resource');
  8537. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8538. $my_dep->setAttribute('identifier', $res_id);
  8539. $my_dep->setAttribute('type', 'webcontent');
  8540. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8541. $my_dep_file = $xmldoc->createElement('file');
  8542. // Check type of URL.
  8543. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8544. if ($doc_info[1] == 'remote') {
  8545. // Remote file. Save url as is.
  8546. $my_dep_file->setAttribute('href', $doc_info[0]);
  8547. $my_dep->setAttribute('xml:base', '');
  8548. } elseif ($doc_info[1] == 'local') {
  8549. switch ($doc_info[2]) {
  8550. case 'url': // Local URL - save path as url for now, don't zip file.
  8551. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8552. $current_dir = dirname($abs_path);
  8553. $current_dir = str_replace('\\', '/', $current_dir);
  8554. $file_path = realpath($abs_path);
  8555. $file_path = str_replace('\\', '/', $file_path);
  8556. $my_dep_file->setAttribute('href', $file_path);
  8557. $my_dep->setAttribute('xml:base', '');
  8558. if (strstr($file_path, $main_path) !== false) {
  8559. // The calculated real path is really inside Chamilo's root path.
  8560. // Reduce file path to what's under the DocumentRoot.
  8561. $file_path = substr($file_path, strlen($root_path) - 1);
  8562. //echo $file_path;echo '<br /><br />';
  8563. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8564. $zip_files_abs[] = $file_path;
  8565. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8566. $my_dep_file->setAttribute('href', $file_path);
  8567. $my_dep->setAttribute('xml:base', '');
  8568. } elseif (empty($file_path)) {
  8569. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8570. if (strpos($document_root, -1) == '/') {
  8571. $document_root = substr(0, -1, $document_root);
  8572. }*/
  8573. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8574. $file_path = str_replace('//', '/', $file_path);
  8575. if (file_exists($file_path)) {
  8576. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8577. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8578. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8579. $my_dep_file->setAttribute('href', $file_path);
  8580. $my_dep->setAttribute('xml:base', '');
  8581. }
  8582. }
  8583. break;
  8584. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8585. $my_dep_file->setAttribute('href', $doc_info[0]);
  8586. $my_dep->setAttribute('xml:base', '');
  8587. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8588. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8589. $abs_img_path_without_subdir = $doc_info[0];
  8590. $relp = api_get_path(REL_PATH); // The url-append config param.
  8591. $pos = strpos($abs_img_path_without_subdir, $relp);
  8592. if ($pos === 0) {
  8593. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8594. }
  8595. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8596. $file_path = str_replace('\\', '/', $file_path);
  8597. $file_path = str_replace('//', '/', $file_path);
  8598. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8599. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8600. // Check if the current document is in that path.
  8601. if (strstr($file_path, $cur_path) !== false) {
  8602. // The document is in that path, now get the relative path
  8603. // to the containing document.
  8604. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8605. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8606. $relative_path = '';
  8607. if (strstr($file_path, $cur_path) !== false) {
  8608. //$relative_path = substr($file_path, strlen($orig_file_path));
  8609. $relative_path = str_replace($cur_path, '', $file_path);
  8610. $file_path = substr($file_path, strlen($cur_path));
  8611. } else {
  8612. // This case is still a problem as it's difficult to calculate a relative path easily
  8613. // might still generate wrong links.
  8614. //$file_path = substr($file_path,strlen($cur_path));
  8615. // Calculate the directory path to the current file (without trailing slash).
  8616. $my_relative_path = dirname($file_path);
  8617. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8618. $my_relative_file = basename($file_path);
  8619. // Calculate the directory path to the containing file (without trailing slash).
  8620. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8621. $dotdot = '';
  8622. $subdir = '';
  8623. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8624. $my_relative_path2 = dirname($my_relative_path);
  8625. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8626. $my_orig_file_path = dirname($my_orig_file_path);
  8627. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8628. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8629. $dotdot += '../';
  8630. $my_relative_path = $my_relative_path2;
  8631. }
  8632. $relative_path = $dotdot.$subdir.$my_relative_file;
  8633. }
  8634. // Put the current document in the zip (this array is the array
  8635. // that will manage documents already in the course folder - relative).
  8636. $zip_files[] = $file_path;
  8637. // Update the links to the current document in the containing document (make them relative).
  8638. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8639. $my_dep_file->setAttribute('href', $file_path);
  8640. $my_dep->setAttribute('xml:base', '');
  8641. } elseif (strstr($file_path, $main_path) !== false) {
  8642. // The calculated real path is really inside Chamilo's root path.
  8643. // Reduce file path to what's under the DocumentRoot.
  8644. $file_path = substr($file_path, strlen($root_path));
  8645. //echo $file_path;echo '<br /><br />';
  8646. //error_log('Reduced path: '.$file_path, 0);
  8647. $zip_files_abs[] = $file_path;
  8648. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8649. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8650. $my_dep->setAttribute('xml:base', '');
  8651. } elseif (empty($file_path)) {
  8652. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8653. if(strpos($document_root,-1) == '/') {
  8654. $document_root = substr(0, -1, $document_root);
  8655. }*/
  8656. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8657. $file_path = str_replace('//', '/', $file_path);
  8658. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8659. $current_dir = dirname($abs_path);
  8660. $current_dir = str_replace('\\', '/', $current_dir);
  8661. if (file_exists($file_path)) {
  8662. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8663. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8664. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8665. $my_dep_file->setAttribute('href','document/'.$file_path);
  8666. $my_dep->setAttribute('xml:base', '');
  8667. }
  8668. }
  8669. break;
  8670. case 'rel':
  8671. // Path relative to the current document.
  8672. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8673. if (substr($doc_info[0], 0, 2) == '..') {
  8674. // Relative path going up.
  8675. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8676. $current_dir = str_replace('\\', '/', $current_dir);
  8677. $file_path = realpath($current_dir.$doc_info[0]);
  8678. $file_path = str_replace('\\', '/', $file_path);
  8679. //error_log($file_path.' <-> '.$main_path,0);
  8680. if (strstr($file_path, $main_path) !== false) {
  8681. // The calculated real path is really inside Chamilo's root path.
  8682. // Reduce file path to what's under the DocumentRoot.
  8683. $file_path = substr($file_path, strlen($root_path));
  8684. //error_log('Reduced path: '.$file_path, 0);
  8685. $zip_files_abs[] = $file_path;
  8686. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8687. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8688. $my_dep->setAttribute('xml:base', '');
  8689. }
  8690. } else {
  8691. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8692. $my_dep_file->setAttribute('href', $doc_info[0]);
  8693. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8694. }
  8695. break;
  8696. default:
  8697. $my_dep_file->setAttribute('href', $doc_info[0]);
  8698. $my_dep->setAttribute('xml:base', '');
  8699. break;
  8700. }
  8701. }
  8702. $my_dep->appendChild($my_dep_file);
  8703. $resources->appendChild($my_dep);
  8704. $dependency = $xmldoc->createElement('dependency');
  8705. $dependency->setAttribute('identifierref', $res_id);
  8706. $my_resource->appendChild($dependency);
  8707. $i++;
  8708. }
  8709. $resources->appendChild($my_resource);
  8710. $zip_files[] = $my_file_path;
  8711. } else {
  8712. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8713. switch ($item->type) {
  8714. case TOOL_LINK:
  8715. $my_item = $xmldoc->createElement('item');
  8716. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8717. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8718. $my_item->setAttribute('isvisible', 'true');
  8719. // Give a child element <title> to the <item> element.
  8720. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8721. $my_item->appendChild($my_title);
  8722. // Give a child element <adlcp:prerequisites> to the <item> element.
  8723. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8724. $my_prereqs->setAttribute('type', 'aicc_script');
  8725. $my_item->appendChild($my_prereqs);
  8726. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8727. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8728. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8729. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8730. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8731. //$xmldoc->createElement('adlcp:datafromlms', '');
  8732. // Give a child element <adlcp:masteryscore> to the <item> element.
  8733. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8734. $my_item->appendChild($my_masteryscore);
  8735. // Attach this item to the organization element or its parent if there is one.
  8736. if (!empty($item->parent) && $item->parent != 0) {
  8737. $children = $organization->childNodes;
  8738. for ($i = 0; $i < $children->length; $i++) {
  8739. $item_temp = $children->item($i);
  8740. if ($item_temp -> nodeName == 'item') {
  8741. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8742. $item_temp -> appendChild($my_item);
  8743. }
  8744. }
  8745. }
  8746. } else {
  8747. $organization->appendChild($my_item);
  8748. }
  8749. $my_file_path = 'link_'.$item->get_id().'.html';
  8750. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8751. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8752. $rs = Database::query($sql);
  8753. if ($link = Database :: fetch_array($rs)) {
  8754. $url = $link['url'];
  8755. $title = stripslashes($link['title']);
  8756. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8757. $my_xml_file_path = $my_file_path;
  8758. $my_sub_dir = dirname($my_file_path);
  8759. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8760. $my_xml_sub_dir = $my_sub_dir;
  8761. // Give a <resource> child to the <resources> element.
  8762. $my_resource = $xmldoc->createElement('resource');
  8763. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8764. $my_resource->setAttribute('type', 'webcontent');
  8765. $my_resource->setAttribute('href', $my_xml_file_path);
  8766. // adlcp:scormtype can be either 'sco' or 'asset'.
  8767. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8768. // xml:base is the base directory to find the files declared in this resource.
  8769. $my_resource->setAttribute('xml:base', '');
  8770. // give a <file> child to the <resource> element.
  8771. $my_file = $xmldoc->createElement('file');
  8772. $my_file->setAttribute('href', $my_xml_file_path);
  8773. $my_resource->appendChild($my_file);
  8774. $resources->appendChild($my_resource);
  8775. }
  8776. break;
  8777. case TOOL_QUIZ:
  8778. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8779. $exe = new Exercise();
  8780. $exe->read($exe_id);
  8781. $my_item = $xmldoc->createElement('item');
  8782. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8783. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8784. $my_item->setAttribute('isvisible', 'true');
  8785. // Give a child element <title> to the <item> element.
  8786. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8787. $my_item->appendChild($my_title);
  8788. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8789. //$my_item->appendChild($my_max_score);
  8790. // Give a child element <adlcp:prerequisites> to the <item> element.
  8791. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8792. $my_prereqs->setAttribute('type','aicc_script');
  8793. $my_item->appendChild($my_prereqs);
  8794. // Give a child element <adlcp:masteryscore> to the <item> element.
  8795. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8796. $my_item->appendChild($my_masteryscore);
  8797. // Attach this item to the organization element or hits parent if there is one.
  8798. if (!empty($item->parent) && $item->parent != 0) {
  8799. $children = $organization->childNodes;
  8800. for ($i = 0; $i < $children->length; $i++) {
  8801. $item_temp = $children->item($i);
  8802. if ($item_temp -> nodeName == 'item') {
  8803. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8804. $item_temp -> appendChild($my_item);
  8805. }
  8806. }
  8807. }
  8808. } else {
  8809. $organization->appendChild($my_item);
  8810. }
  8811. // Get the path of the file(s) from the course directory root
  8812. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8813. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8814. // Write the contents of the exported exercise into a (big) html file
  8815. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8816. $contents = ScormSection::export_exercise_to_scorm($exe_id, true);
  8817. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8818. $res = file_put_contents($tmp_file_path, $contents);
  8819. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8820. $files_cleanup[] = $tmp_file_path;
  8821. //error_log($tmp_path); die();
  8822. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8823. $my_xml_file_path = $my_file_path;
  8824. $my_sub_dir = dirname($my_file_path);
  8825. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8826. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8827. $my_xml_sub_dir = $my_sub_dir;
  8828. // Give a <resource> child to the <resources> element.
  8829. $my_resource = $xmldoc->createElement('resource');
  8830. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8831. $my_resource->setAttribute('type', 'webcontent');
  8832. $my_resource->setAttribute('href', $my_xml_file_path);
  8833. // adlcp:scormtype can be either 'sco' or 'asset'.
  8834. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8835. // xml:base is the base directory to find the files declared in this resource.
  8836. $my_resource->setAttribute('xml:base', '');
  8837. // Give a <file> child to the <resource> element.
  8838. $my_file = $xmldoc->createElement('file');
  8839. $my_file->setAttribute('href', $my_xml_file_path);
  8840. $my_resource->appendChild($my_file);
  8841. // Get included docs.
  8842. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8843. // Dependency to other files - not yet supported.
  8844. $i = 1;
  8845. foreach ($inc_docs as $doc_info) {
  8846. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8847. $my_dep = $xmldoc->createElement('resource');
  8848. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8849. $my_dep->setAttribute('identifier', $res_id);
  8850. $my_dep->setAttribute('type', 'webcontent');
  8851. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8852. $my_dep_file = $xmldoc->createElement('file');
  8853. // Check type of URL.
  8854. if ($doc_info[1] == 'remote') {
  8855. // Remote file. Save url as is.
  8856. $my_dep_file->setAttribute('href', $doc_info[0]);
  8857. $my_dep->setAttribute('xml:base', '');
  8858. } elseif ($doc_info[1] == 'local') {
  8859. switch ($doc_info[2]) {
  8860. case 'url': // Local URL - save path as url for now, don't zip file.
  8861. // Save file but as local file (retrieve from URL).
  8862. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8863. $current_dir = dirname($abs_path);
  8864. $current_dir = str_replace('\\', '/', $current_dir);
  8865. $file_path = realpath($abs_path);
  8866. $file_path = str_replace('\\', '/', $file_path);
  8867. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8868. $my_dep->setAttribute('xml:base', '');
  8869. if (strstr($file_path, $main_path) !== false) {
  8870. // The calculated real path is really inside the chamilo root path.
  8871. // Reduce file path to what's under the DocumentRoot.
  8872. $file_path = substr($file_path, strlen($root_path));
  8873. //echo $file_path;echo '<br /><br />';
  8874. //error_log('Reduced path: '.$file_path, 0);
  8875. $zip_files_abs[] = $file_path;
  8876. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8877. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8878. $my_dep->setAttribute('xml:base', '');
  8879. } elseif (empty($file_path)) {
  8880. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8881. if (strpos($document_root,-1) == '/') {
  8882. $document_root = substr(0, -1, $document_root);
  8883. }*/
  8884. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8885. $file_path = str_replace('//', '/', $file_path);
  8886. if (file_exists($file_path)) {
  8887. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8888. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8889. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8890. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8891. $my_dep->setAttribute('xml:base', '');
  8892. }
  8893. }
  8894. break;
  8895. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8896. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8897. $current_dir = str_replace('\\', '/', $current_dir);
  8898. $file_path = realpath($doc_info[0]);
  8899. $file_path = str_replace('\\', '/', $file_path);
  8900. $my_dep_file->setAttribute('href', $file_path);
  8901. $my_dep->setAttribute('xml:base', '');
  8902. if (strstr($file_path,$main_path) !== false) {
  8903. // The calculated real path is really inside the chamilo root path.
  8904. // Reduce file path to what's under the DocumentRoot.
  8905. $file_path = substr($file_path, strlen($root_path));
  8906. //echo $file_path;echo '<br /><br />';
  8907. //error_log('Reduced path: '.$file_path, 0);
  8908. $zip_files_abs[] = $file_path;
  8909. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8910. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8911. $my_dep->setAttribute('xml:base', '');
  8912. } elseif (empty($file_path)) {
  8913. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8914. if (strpos($document_root,-1) == '/') {
  8915. $document_root = substr(0, -1, $document_root);
  8916. }*/
  8917. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8918. $file_path = str_replace('//', '/', $file_path);
  8919. if (file_exists($file_path)) {
  8920. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8921. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8922. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8923. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8924. $my_dep->setAttribute('xml:base', '');
  8925. }
  8926. }
  8927. break;
  8928. 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
  8929. if (substr($doc_info[0], 0, 2) == '..') {
  8930. // Relative path going up.
  8931. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8932. $current_dir = str_replace('\\', '/', $current_dir);
  8933. $file_path = realpath($current_dir.$doc_info[0]);
  8934. $file_path = str_replace('\\', '/', $file_path);
  8935. //error_log($file_path.' <-> '.$main_path, 0);
  8936. if (strstr($file_path, $main_path) !== false) {
  8937. // The calculated real path is really inside Chamilo's root path.
  8938. // Reduce file path to what's under the DocumentRoot.
  8939. $file_path = substr($file_path, strlen($root_path));
  8940. $file_path_dest = $file_path;
  8941. // File path is courses/CHAMILO/document/....
  8942. $info_file_path = explode('/', $file_path);
  8943. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8944. $file_path_dest = 'document/'.$file_path;
  8945. }
  8946. //error_log('Reduced path: '.$file_path, 0);
  8947. $zip_files_abs[] = $file_path;
  8948. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8949. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8950. $my_dep->setAttribute('xml:base', '');
  8951. }
  8952. } else {
  8953. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8954. $my_dep_file->setAttribute('href', $doc_info[0]);
  8955. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8956. }
  8957. break;
  8958. default:
  8959. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8960. $my_dep->setAttribute('xml:base', '');
  8961. break;
  8962. }
  8963. }
  8964. $my_dep->appendChild($my_dep_file);
  8965. $resources->appendChild($my_dep);
  8966. $dependency = $xmldoc->createElement('dependency');
  8967. $dependency->setAttribute('identifierref', $res_id);
  8968. $my_resource->appendChild($dependency);
  8969. $i++;
  8970. }
  8971. $resources->appendChild($my_resource);
  8972. $zip_files[] = $my_file_path;
  8973. break;
  8974. default:
  8975. // Get the path of the file(s) from the course directory root
  8976. $my_file_path = 'non_exportable.html';
  8977. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8978. $my_xml_file_path = $my_file_path;
  8979. $my_sub_dir = dirname($my_file_path);
  8980. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8981. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8982. $my_xml_sub_dir = $my_sub_dir;
  8983. // Give a <resource> child to the <resources> element.
  8984. $my_resource = $xmldoc->createElement('resource');
  8985. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8986. $my_resource->setAttribute('type', 'webcontent');
  8987. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8988. // adlcp:scormtype can be either 'sco' or 'asset'.
  8989. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8990. // xml:base is the base directory to find the files declared in this resource.
  8991. $my_resource->setAttribute('xml:base', '');
  8992. // Give a <file> child to the <resource> element.
  8993. $my_file = $xmldoc->createElement('file');
  8994. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8995. $my_resource->appendChild($my_file);
  8996. $resources->appendChild($my_resource);
  8997. break;
  8998. }
  8999. }
  9000. }
  9001. $organizations->appendChild($organization);
  9002. $root->appendChild($organizations);
  9003. $root->appendChild($resources);
  9004. $xmldoc->appendChild($root);
  9005. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9006. // then add the file to the zip, then destroy the file (this is done automatically).
  9007. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9008. foreach ($zip_files as $file_path) {
  9009. if (empty($file_path)) {
  9010. continue;
  9011. }
  9012. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9013. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  9014. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  9015. }
  9016. $this->create_path($dest_file);
  9017. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9018. // Check if the file needs a link update.
  9019. if (in_array($file_path, array_keys($link_updates))) {
  9020. $string = file_get_contents($dest_file);
  9021. unlink($dest_file);
  9022. foreach ($link_updates[$file_path] as $old_new) {
  9023. // This is an ugly hack that allows .flv files to be found by the flv player that
  9024. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9025. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9026. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9027. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9028. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9029. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9030. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9031. }
  9032. //Fix to avoid problems with default_course_document
  9033. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9034. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  9035. $newDestination = $old_new['dest'];
  9036. } else {
  9037. $newDestination = str_replace('document/', '', $old_new['dest']);
  9038. }
  9039. $string = str_replace($old_new['orig'], $newDestination, $string);
  9040. //Add files inside the HTMLs
  9041. $new_path = str_replace(api_get_path(REL_COURSE_PATH), '', $old_new['orig']);
  9042. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  9043. if (file_exists($sys_course_path.$new_path)) {
  9044. copy($sys_course_path.$new_path, $destinationFile);
  9045. }
  9046. }
  9047. file_put_contents($dest_file, $string);
  9048. }
  9049. }
  9050. foreach ($zip_files_abs as $file_path) {
  9051. if (empty($file_path)) {
  9052. continue;
  9053. }
  9054. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9055. continue;
  9056. }
  9057. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9058. $this->create_path($dest_file);
  9059. copy($main_path.$file_path, $dest_file);
  9060. // Check if the file needs a link update.
  9061. if (in_array($file_path, array_keys($link_updates))) {
  9062. $string = file_get_contents($dest_file);
  9063. unlink($dest_file);
  9064. foreach ($link_updates[$file_path] as $old_new) {
  9065. // This is an ugly hack that allows .flv files to be found by the flv player that
  9066. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9067. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9068. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9069. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9070. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9071. }
  9072. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9073. }
  9074. file_put_contents($dest_file, $string);
  9075. }
  9076. }
  9077. if (is_array($links_to_create)) {
  9078. foreach ($links_to_create as $file => $link) {
  9079. $file_content = '<!DOCTYPE html><head>
  9080. <meta charset="'.api_get_language_isocode().'" />
  9081. <title>'.$link['title'].'</title>
  9082. </head>
  9083. <body dir="'.api_get_text_direction().'">
  9084. <div style="text-align:center">
  9085. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9086. </body>
  9087. </html>';
  9088. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9089. }
  9090. }
  9091. // Add non exportable message explanation.
  9092. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9093. $file_content = '<!DOCTYPE html><head>
  9094. <meta charset="'.api_get_language_isocode().'" />
  9095. <title>'.$lang_not_exportable.'</title>
  9096. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9097. </head>
  9098. <body dir="'.api_get_text_direction().'">';
  9099. $file_content .=
  9100. <<<EOD
  9101. <style>
  9102. .error-message {
  9103. font-family: arial, verdana, helvetica, sans-serif;
  9104. border-width: 1px;
  9105. border-style: solid;
  9106. left: 50%;
  9107. margin: 10px auto;
  9108. min-height: 30px;
  9109. padding: 5px;
  9110. right: 50%;
  9111. width: 500px;
  9112. background-color: #FFD1D1;
  9113. border-color: #FF0000;
  9114. color: #000;
  9115. }
  9116. </style>
  9117. <body>
  9118. <div class="error-message">
  9119. $lang_not_exportable
  9120. </div>
  9121. </body>
  9122. </html>
  9123. EOD;
  9124. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9125. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9126. }
  9127. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9128. // Add the extra files that go along with a SCORM package.
  9129. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9130. $extra_files = scandir($main_code_path);
  9131. foreach ($extra_files as $extra_file) {
  9132. if (strpos($extra_file, '.') === 0)
  9133. continue;
  9134. else {
  9135. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  9136. $this->create_path($dest_file);
  9137. copy($main_code_path.$extra_file, $dest_file);
  9138. }
  9139. }
  9140. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9141. $manifest = @$xmldoc->saveXML();
  9142. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9143. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9144. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  9145. // Clean possible temporary files.
  9146. foreach ($files_cleanup as $file) {
  9147. $res = unlink($file);
  9148. if ($res === false) {
  9149. error_log(
  9150. 'Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__,
  9151. 0
  9152. );
  9153. }
  9154. }
  9155. $name = api_replace_dangerous_char($this->get_name()).'.zip';
  9156. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9157. }
  9158. /**
  9159. * @param int $lp_id
  9160. * @return bool
  9161. */
  9162. public function scorm_export_to_pdf($lp_id)
  9163. {
  9164. $lp_id = intval($lp_id);
  9165. $files_to_export = array();
  9166. $course_data = api_get_course_info($this->cc);
  9167. if (!empty($course_data)) {
  9168. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9169. $list = self::get_flat_ordered_items_list($lp_id);
  9170. if (!empty($list)) {
  9171. foreach ($list as $item_id) {
  9172. $item = $this->items[$item_id];
  9173. switch ($item->type) {
  9174. case 'document':
  9175. //Getting documents from a LP with chamilo documents
  9176. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9177. // Try loading document from the base course.
  9178. if (empty($file_data) && !empty($sessionId)) {
  9179. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc, false, 0);
  9180. }
  9181. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  9182. if (file_exists($file_path)) {
  9183. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  9184. }
  9185. break;
  9186. case 'asset': //commes from a scorm package generated by chamilo
  9187. case 'sco':
  9188. $file_path = $scorm_path.'/'.$item->path;
  9189. if (file_exists($file_path)) {
  9190. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  9191. }
  9192. break;
  9193. case 'dokeos_chapter':
  9194. case 'dir':
  9195. case 'chapter':
  9196. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  9197. break;
  9198. }
  9199. }
  9200. }
  9201. $pdf = new PDF();
  9202. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9203. return $result;
  9204. }
  9205. return false;
  9206. }
  9207. /**
  9208. * Temp function to be moved in main_api or the best place around for this.
  9209. * Creates a file path if it doesn't exist
  9210. * @param string $path
  9211. */
  9212. public function create_path($path)
  9213. {
  9214. $path_bits = explode('/', dirname($path));
  9215. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9216. $path_built = IS_WINDOWS_OS ? '' : '/';
  9217. foreach ($path_bits as $bit) {
  9218. if (!empty ($bit)) {
  9219. $new_path = $path_built . $bit;
  9220. if (is_dir($new_path)) {
  9221. $path_built = $new_path . '/';
  9222. } else {
  9223. mkdir($new_path, api_get_permissions_for_new_directories());
  9224. $path_built = $new_path . '/';
  9225. }
  9226. }
  9227. }
  9228. }
  9229. /**
  9230. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9231. * @return boolean The results of the unlink function, or false if there was no image to start with
  9232. */
  9233. public function delete_lp_image()
  9234. {
  9235. $img = $this->get_preview_image();
  9236. if ($img != '') {
  9237. $del_file = $this->get_preview_image_path(null, 'sys');
  9238. if (isset($del_file) && file_exists($del_file)) {
  9239. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9240. if (file_exists($del_file_2)) {
  9241. unlink($del_file_2);
  9242. }
  9243. $this->set_preview_image('');
  9244. return @unlink($del_file);
  9245. }
  9246. }
  9247. return false;
  9248. }
  9249. /**
  9250. * Uploads an author image to the upload/learning_path/images directory
  9251. * @param array The image array, coming from the $_FILES superglobal
  9252. * @return boolean True on success, false on error
  9253. */
  9254. public function upload_image($image_array)
  9255. {
  9256. $image_moved = false;
  9257. if (!empty ($image_array['name'])) {
  9258. $upload_ok = process_uploaded_file($image_array);
  9259. $has_attachment = true;
  9260. } else {
  9261. $image_moved = true;
  9262. }
  9263. if ($upload_ok) {
  9264. if ($has_attachment) {
  9265. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  9266. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9267. $updir = $sys_course_path . $courseDir;
  9268. // Try to add an extension to the file if it hasn't one.
  9269. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9270. if (!filter_extension($new_file_name)) {
  9271. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9272. $image_moved = false;
  9273. } else {
  9274. $file_extension = explode('.', $image_array['name']);
  9275. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9276. $filename = uniqid('');
  9277. $new_file_name = $filename.'.'.$file_extension;
  9278. $new_path = $updir.'/'.$new_file_name;
  9279. // Resize the image.
  9280. $temp = new Image($image_array['tmp_name']);
  9281. $temp->resize(104);
  9282. $result = $temp->send_image($new_path);
  9283. // Storing the image filename.
  9284. if ($result) {
  9285. $image_moved = true;
  9286. $this->set_preview_image($new_file_name);
  9287. //Resize to 64px to use on course homepage
  9288. $temp->resize(64);
  9289. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9290. return true;
  9291. }
  9292. }
  9293. }
  9294. }
  9295. return false;
  9296. }
  9297. /**
  9298. * @param int $lp_id
  9299. * @param string $status
  9300. */
  9301. public function set_autolaunch($lp_id, $status)
  9302. {
  9303. $course_id = api_get_course_int_id();
  9304. $lp_id = intval($lp_id);
  9305. $status = intval($status);
  9306. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9307. // Setting everything to autolaunch = 0
  9308. $attributes['autolaunch'] = 0;
  9309. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  9310. Database::update($lp_table, $attributes, $where);
  9311. if ($status == 1) {
  9312. //Setting my lp_id to autolaunch = 1
  9313. $attributes['autolaunch'] = 1;
  9314. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  9315. Database::update($lp_table, $attributes, $where );
  9316. }
  9317. }
  9318. /**
  9319. * Gets previous_item_id for the next element of the lp_item table
  9320. * @author Isaac flores paz
  9321. * @return integer Previous item ID
  9322. */
  9323. public function select_previous_item_id()
  9324. {
  9325. $course_id = api_get_course_int_id();
  9326. if ($this->debug > 0) {
  9327. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9328. }
  9329. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9330. // Get the max order of the items
  9331. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  9332. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  9333. $rs_max_order = Database::query($sql_max_order);
  9334. $row_max_order = Database::fetch_object($rs_max_order);
  9335. $max_order = $row_max_order->display_order;
  9336. // Get the previous item ID
  9337. $sql = "SELECT id as previous FROM $table_lp_item
  9338. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  9339. $rs_max = Database::query($sql);
  9340. $row_max = Database::fetch_object($rs_max);
  9341. // Return the previous item ID
  9342. return $row_max->previous;
  9343. }
  9344. /**
  9345. * Copies an LP
  9346. */
  9347. public function copy()
  9348. {
  9349. $main_path = api_get_path(SYS_CODE_PATH);
  9350. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9351. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9352. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9353. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9354. //Course builder
  9355. $cb = new CourseBuilder();
  9356. //Setting tools that will be copied
  9357. $cb->set_tools_to_build(array('learnpaths'));
  9358. //Setting elements that will be copied
  9359. $cb->set_tools_specific_id_list(
  9360. array('learnpaths' => array($this->lp_id))
  9361. );
  9362. $course = $cb->build();
  9363. //Course restorer
  9364. $course_restorer = new CourseRestorer($course);
  9365. $course_restorer->set_add_text_in_items(true);
  9366. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9367. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9368. }
  9369. public function verify_document_size($s)
  9370. {
  9371. $post_max = ini_get('post_max_size');
  9372. if (substr($post_max, -1, 1) == 'M') {
  9373. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024;
  9374. } elseif (substr($post_max, -1, 1) == 'G') {
  9375. $post_max = intval(substr($post_max, 0, -1)) * 1024 * 1024 * 1024;
  9376. }
  9377. $upl_max = ini_get('upload_max_filesize');
  9378. if (substr($upl_max, -1, 1) == 'M') {
  9379. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024;
  9380. } elseif (substr($upl_max, -1, 1) == 'G') {
  9381. $upl_max = intval(substr($upl_max, 0, -1)) * 1024 * 1024 * 1024;
  9382. }
  9383. $documents_total_space = DocumentManager::documents_total_space();
  9384. $course_max_space = DocumentManager::get_course_quota();
  9385. $total_size = filesize($s) + $documents_total_space;
  9386. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  9387. return true;
  9388. } else{
  9389. return false;
  9390. }
  9391. }
  9392. /**
  9393. * Clear LP prerequisites
  9394. */
  9395. public function clear_prerequisites()
  9396. {
  9397. $course_id = $this->get_course_int_id();
  9398. if ($this->debug > 0) {
  9399. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9400. }
  9401. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9402. $lp_id = $this->get_id();
  9403. //Cleaning prerequisites
  9404. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9405. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9406. Database::query($sql);
  9407. //Cleaning mastery score for exercises
  9408. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9409. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9410. Database::query($sql);
  9411. }
  9412. public function set_previous_step_as_prerequisite_for_all_items()
  9413. {
  9414. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9415. $course_id = $this->get_course_int_id();
  9416. $lp_id = $this->get_id();
  9417. if (!empty($this->items)) {
  9418. $previous_item_id = null;
  9419. $previous_item_max = 0;
  9420. $previous_item_type = null;
  9421. $last_item_not_chapter = null;
  9422. $last_item_not_chapter_type = null;
  9423. $last_item_not_chapter_max = null;
  9424. foreach ($this->items as $item) {
  9425. // if there was a previous item... (otherwise jump to set it)
  9426. if (!empty($previous_item_id)) {
  9427. $current_item_id = $item->get_id(); //save current id
  9428. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9429. // Current item is not a folder, so it qualifies to get a prerequisites
  9430. if ($last_item_not_chapter_type == 'quiz') {
  9431. // if previous is quiz, mark its max score as default score to be achieved
  9432. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max'
  9433. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  9434. Database::query($sql);
  9435. }
  9436. // now simply update the prerequisite to set it to the last non-chapter item
  9437. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter'
  9438. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9439. Database::query($sql);
  9440. // record item as 'non-chapter' reference
  9441. $last_item_not_chapter = $item->get_id();
  9442. $last_item_not_chapter_type = $item->get_type();
  9443. $last_item_not_chapter_max = $item->get_max();
  9444. }
  9445. } else {
  9446. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  9447. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  9448. $last_item_not_chapter = $item->get_id();
  9449. $last_item_not_chapter_type = $item->get_type();
  9450. $last_item_not_chapter_max = $item->get_max();
  9451. }
  9452. }
  9453. // Saving the item as "previous item" for the next loop
  9454. $previous_item_id = $item->get_id();
  9455. $previous_item_max = $item->get_max();
  9456. $previous_item_type = $item->get_type();
  9457. }
  9458. }
  9459. }
  9460. /**
  9461. * @param array $params
  9462. */
  9463. public static function createCategory($params)
  9464. {
  9465. $em = Database::getManager();
  9466. $item = new CLpCategory();
  9467. $item->setName($params['name']);
  9468. $item->setCId($params['c_id']);
  9469. $em->persist($item);
  9470. $em->flush();
  9471. }
  9472. /**
  9473. * @param array $params
  9474. */
  9475. public static function updateCategory($params)
  9476. {
  9477. $em = Database::getManager();
  9478. /** @var CLpCategory $item */
  9479. $item = $em->find('ChamiloCourseBundle:CLpCategory', $params['id']);
  9480. if ($item) {
  9481. $item->setName($params['name']);
  9482. $em->merge($item);
  9483. $em->flush();
  9484. }
  9485. }
  9486. /**
  9487. * @param int $id
  9488. */
  9489. public static function moveUpCategory($id)
  9490. {
  9491. $em = Database::getManager();
  9492. /** @var CLpCategory $item */
  9493. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9494. if ($item) {
  9495. $position = $item->getPosition() - 1;
  9496. $item->setPosition($position);
  9497. $em->persist($item);
  9498. $em->flush();
  9499. }
  9500. }
  9501. /**
  9502. * @param int $id
  9503. */
  9504. public static function moveDownCategory($id)
  9505. {
  9506. $em = Database::getManager();
  9507. /** @var CLpCategory $item */
  9508. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9509. if ($item) {
  9510. $position = $item->getPosition() + 1;
  9511. $item->setPosition($position);
  9512. $em->persist($item);
  9513. $em->flush();
  9514. }
  9515. }
  9516. /**
  9517. * @param int $courseId
  9518. * @return int|mixed
  9519. */
  9520. public static function getCountCategories($courseId)
  9521. {
  9522. if (empty($course_id)) {
  9523. return 0;
  9524. }
  9525. $em = Database::getManager();
  9526. $query = $em->createQuery('SELECT COUNT(u.id) FROM ChamiloCourseBundle:CLpCategory u WHERE u.cId = :id');
  9527. $query->setParameter('id', $courseId);
  9528. return $query->getSingleScalarResult();
  9529. }
  9530. /**
  9531. * @param int $courseId
  9532. *
  9533. * @return mixed
  9534. */
  9535. public static function getCategories($courseId)
  9536. {
  9537. $em = Database::getManager();
  9538. //Default behaviour
  9539. /*$items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(
  9540. array('cId' => $course_id),
  9541. array('name' => 'ASC')
  9542. );*/
  9543. // Using doctrine extensions
  9544. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->getBySortableGroupsQuery(
  9545. array('cId' => $courseId)
  9546. )->getResult();
  9547. return $items;
  9548. }
  9549. /**
  9550. * @param int $id
  9551. *
  9552. * @return mixed
  9553. */
  9554. public static function getCategory($id)
  9555. {
  9556. $em = Database::getManager();
  9557. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9558. return $item;
  9559. }
  9560. /**
  9561. * @param int $courseId
  9562. * @return array
  9563. */
  9564. public static function getCategoryByCourse($courseId)
  9565. {
  9566. $em = Database::getManager();
  9567. $items = $em->getRepository('ChamiloCourseBundle:CLpCategory')->findBy(array('cId' => $courseId));
  9568. return $items;
  9569. }
  9570. /**
  9571. * @param int $id
  9572. *
  9573. * @return mixed
  9574. */
  9575. public static function deleteCategory($id)
  9576. {
  9577. $em = Database::getManager();
  9578. $item = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  9579. if ($item) {
  9580. $courseId = $item->getCId();
  9581. $query = $em->createQuery('SELECT u FROM ChamiloCourseBundle:CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9582. $query->setParameter('id', $courseId);
  9583. $query->setParameter('catId', $item->getId());
  9584. $lps = $query->getResult();
  9585. // Setting category = 0.
  9586. if ($lps) {
  9587. foreach ($lps as $lpItem) {
  9588. $lpItem->setCategoryId(0);
  9589. }
  9590. }
  9591. // Removing category.
  9592. $em->remove($item);
  9593. $em->flush();
  9594. }
  9595. }
  9596. /**
  9597. * @param int $courseId
  9598. * @param bool $addSelectOption
  9599. *
  9600. * @return mixed
  9601. */
  9602. static function getCategoryFromCourseIntoSelect($courseId, $addSelectOption = false)
  9603. {
  9604. $items = self::getCategoryByCourse($courseId);
  9605. $cats = array();
  9606. if ($addSelectOption) {
  9607. $cats = array(get_lang('SelectACategory'));
  9608. }
  9609. if (!empty($items)) {
  9610. foreach ($items as $cat) {
  9611. $cats[$cat->getId()] = $cat->getName();
  9612. }
  9613. }
  9614. return $cats;
  9615. }
  9616. /**
  9617. * Return the scorm item type object with spaces replaced with _
  9618. * The return result is use to build a css classname like scorm_type_$return
  9619. * @param $in_type
  9620. * @return mixed
  9621. */
  9622. private static function format_scorm_type_item($in_type)
  9623. {
  9624. return str_replace(' ', '_', $in_type);
  9625. }
  9626. /**
  9627. * @return \learnpath
  9628. */
  9629. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  9630. {
  9631. $lpObject = Session::read('lpobject');
  9632. $learnPath = null;
  9633. if (isset($lpObject)) {
  9634. $learnPath = unserialize($lpObject);
  9635. }
  9636. if (!is_object($learnPath)) {
  9637. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  9638. }
  9639. return $learnPath;
  9640. }
  9641. /**
  9642. * @param int $itemId
  9643. * @return learnpathItem|false
  9644. */
  9645. public function getItem($itemId)
  9646. {
  9647. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  9648. return $this->items[$itemId];
  9649. }
  9650. return false;
  9651. }
  9652. /**
  9653. * @return int
  9654. */
  9655. public function getCategoryId()
  9656. {
  9657. return $this->categoryId;
  9658. }
  9659. /**
  9660. * @param int $categoryId
  9661. * @return bool
  9662. */
  9663. public function setCategoryId($categoryId)
  9664. {
  9665. $this->categoryId = intval($categoryId);
  9666. $courseId = api_get_course_int_id();
  9667. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9668. $lp_id = $this->get_id();
  9669. $sql = "UPDATE $lp_table SET category_id = ".$this->categoryId."
  9670. WHERE c_id = $courseId AND id = $lp_id";
  9671. Database::query($sql);
  9672. return true;
  9673. }
  9674. /**
  9675. * Get whether this is a learning path with the possibility to subscribe
  9676. * users or not
  9677. * @return int
  9678. */
  9679. public function getSubscribeUsers()
  9680. {
  9681. return $this->subscribeUsers;
  9682. }
  9683. /**
  9684. * Set whether this is a learning path with the possibility to subscribe
  9685. * users or not
  9686. * @param int $subscribeUsers (0 = false, 1 = true)
  9687. */
  9688. public function setSubscribeUsers($value)
  9689. {
  9690. if ($this->debug > 0) {
  9691. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  9692. }
  9693. $this->subscribeUsers = intval($value);;
  9694. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  9695. $lp_id = $this->get_id();
  9696. $sql = "UPDATE $lp_table SET subscribe_users = ".$this->subscribeUsers."
  9697. WHERE c_id = ".$this->course_int_id." AND id = $lp_id";
  9698. Database::query($sql);
  9699. return true;
  9700. }
  9701. /**
  9702. * Calculate the count of stars for a user in this LP
  9703. * This calculation is based on the following rules:
  9704. * - the student gets one star when he gets to 50% of the learning path
  9705. * - the student gets a second star when the average score of all tests inside the learning path >= 50%
  9706. * - the student gets a third star when the average score of all tests inside the learning path >= 80%
  9707. * - the student gets the final star when the score for the *last* test is >= 80%
  9708. * @param int $sessionId Optional. The session ID
  9709. * @return int The count of stars
  9710. */
  9711. public function getCalculateStars($sessionId = 0)
  9712. {
  9713. $stars = 0;
  9714. $progress = self::getProgress($this->lp_id, $this->user_id, $this->course_int_id, $sessionId);
  9715. if ($progress >= 50) {
  9716. $stars++;
  9717. }
  9718. // Calculate stars chapters evaluation
  9719. $exercisesItems = $this->getExercisesItems();
  9720. if (!empty($exercisesItems)) {
  9721. $totalResult = 0;
  9722. foreach ($exercisesItems as $exerciseItem) {
  9723. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9724. $this->user_id,
  9725. $exerciseItem->path,
  9726. $this->course_int_id,
  9727. $sessionId,
  9728. $this->lp_id,
  9729. $exerciseItem->db_id
  9730. );
  9731. $exerciseResult = 0;
  9732. foreach ($exerciseResultInfo as $result) {
  9733. $exerciseResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9734. }
  9735. $exerciseAverage = $exerciseResult / (count($exerciseResultInfo) > 0 ? count($exerciseResultInfo) : 1);
  9736. $totalResult += $exerciseAverage;
  9737. }
  9738. $totalExerciseAverage = $totalResult / (count($exercisesItems) > 0 ? count($exercisesItems) : 1);
  9739. if ($totalExerciseAverage >= 50) {
  9740. $stars++;
  9741. }
  9742. if ($totalExerciseAverage >= 80) {
  9743. $stars++;
  9744. }
  9745. }
  9746. // Calculate star for final evaluation
  9747. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9748. if (!empty($finalEvaluationItem)) {
  9749. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9750. $this->user_id,
  9751. $finalEvaluationItem->path,
  9752. $this->course_int_id,
  9753. $sessionId,
  9754. $this->lp_id,
  9755. $finalEvaluationItem->db_id
  9756. );
  9757. $evaluationResult = 0;
  9758. foreach ($evaluationResultInfo as $result) {
  9759. $evaluationResult += $result['exe_result'] * 100 / $result['exe_weighting'];
  9760. }
  9761. $averageDivisor = count($evaluationResultInfo) > 0 ? count($evaluationResultInfo) : 1;
  9762. $evaluationAverage = $evaluationResult / $averageDivisor;
  9763. if ($evaluationAverage >= 80) {
  9764. $stars++;
  9765. }
  9766. }
  9767. return $stars;
  9768. }
  9769. /**
  9770. * Get the items of exercise type
  9771. * @return array The items. Otherwise return false
  9772. */
  9773. public function getExercisesItems()
  9774. {
  9775. $exercises = [];
  9776. foreach ($this->items as $item) {
  9777. if ($item->type != 'quiz') {
  9778. continue;
  9779. }
  9780. $exercises[] = $item;
  9781. }
  9782. array_pop($exercises);
  9783. return $exercises;
  9784. }
  9785. /**
  9786. * Get the item of exercise type (evaluation type)
  9787. * @return array The final evaluation. Otherwise return false
  9788. */
  9789. public function getFinalEvaluationItem()
  9790. {
  9791. $exercises = [];
  9792. foreach ($this->items as $item) {
  9793. if ($item->type != 'quiz') {
  9794. continue;
  9795. }
  9796. $exercises[] = $item;
  9797. }
  9798. return array_pop($exercises);
  9799. }
  9800. /**
  9801. * Calculate the total points achieved for the current user in this learning path
  9802. * @param int $sessionId Optional. The session Id
  9803. * @return int
  9804. */
  9805. public function getCalculateScore($sessionId = 0)
  9806. {
  9807. // Calculate stars chapters evaluation
  9808. $exercisesItems = $this->getExercisesItems();
  9809. $finalEvaluationItem = $this->getFinalEvaluationItem();
  9810. $totalExercisesResult = 0;
  9811. $totalEvaluationResult = 0;
  9812. if ($exercisesItems !== false) {
  9813. foreach ($exercisesItems as $exerciseItem) {
  9814. $exerciseResultInfo = Event::getExerciseResultsByUser(
  9815. $this->user_id,
  9816. $exerciseItem->path,
  9817. $this->course_int_id,
  9818. $sessionId,
  9819. $this->lp_id,
  9820. $exerciseItem->db_id
  9821. );
  9822. $exerciseResult = 0;
  9823. foreach ($exerciseResultInfo as $result) {
  9824. $exerciseResult += $result['exe_result'];
  9825. }
  9826. $totalExercisesResult += $exerciseResult;
  9827. }
  9828. }
  9829. if (!empty($finalEvaluationItem)) {
  9830. $evaluationResultInfo = Event::getExerciseResultsByUser(
  9831. $this->user_id,
  9832. $finalEvaluationItem->path,
  9833. $this->course_int_id,
  9834. $sessionId,
  9835. $this->lp_id,
  9836. $finalEvaluationItem->db_id
  9837. );
  9838. foreach ($evaluationResultInfo as $result) {
  9839. $totalEvaluationResult += $result['exe_result'];
  9840. }
  9841. }
  9842. return $totalExercisesResult + $totalEvaluationResult;
  9843. }
  9844. /**
  9845. * Check if URL is not allowed to be show in a iframe
  9846. * @param string $src
  9847. *
  9848. * @return string
  9849. */
  9850. public function fixBlockedLinks($src)
  9851. {
  9852. $urlInfo = parse_url($src);
  9853. //$platformProtocol = api_get_protocol();
  9854. $platformProtocol = 'https';
  9855. if (strpos(api_get_path(WEB_CODE_PATH), 'https') === false) {
  9856. $platformProtocol = 'http';
  9857. }
  9858. $protocolFixApplied = false;
  9859. //Scheme validation to avoid "Notices" when the lesson doesn't contain a valid scheme
  9860. $scheme = isset($urlInfo['scheme']) ? $urlInfo['scheme'] : null;
  9861. if ($platformProtocol != $scheme) {
  9862. Session::write('x_frame_source', $src);
  9863. $src = 'blank.php?error=x_frames_options';
  9864. $protocolFixApplied = true;
  9865. }
  9866. if ($protocolFixApplied == false) {
  9867. if (strpos($src, api_get_path(WEB_CODE_PATH)) === false) {
  9868. // Check X-Frame-Options
  9869. $ch = curl_init();
  9870. $options = array(
  9871. CURLOPT_URL => $src,
  9872. CURLOPT_RETURNTRANSFER => true,
  9873. CURLOPT_HEADER => true,
  9874. CURLOPT_FOLLOWLOCATION => true,
  9875. CURLOPT_ENCODING => "",
  9876. CURLOPT_AUTOREFERER => true,
  9877. CURLOPT_CONNECTTIMEOUT => 120,
  9878. CURLOPT_TIMEOUT => 120,
  9879. CURLOPT_MAXREDIRS => 10,
  9880. );
  9881. curl_setopt_array($ch, $options);
  9882. $response = curl_exec($ch);
  9883. $httpCode = curl_getinfo($ch);
  9884. $headers = substr($response, 0, $httpCode['header_size']);
  9885. $error = false;
  9886. if (stripos($headers, 'X-Frame-Options: DENY') > -1 ||
  9887. stripos($headers, 'X-Frame-Options: SAMEORIGIN') > -1
  9888. ) {
  9889. $error = true;
  9890. }
  9891. if ($error) {
  9892. Session::write('x_frame_source', $src);
  9893. $src = 'blank.php?error=x_frames_options';
  9894. }
  9895. }
  9896. }
  9897. return $src;
  9898. }
  9899. /**
  9900. * Check if this LP has a created forum in the basis course
  9901. * @return boolean
  9902. */
  9903. public function lpHasForum()
  9904. {
  9905. $forumTable = Database::get_course_table(TABLE_FORUM);
  9906. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9907. $fakeFrom = "
  9908. $forumTable f
  9909. INNER JOIN $itemProperty ip
  9910. ON (f.forum_id = ip.ref AND f.c_id = ip.c_id)
  9911. ";
  9912. $resultData = Database::select(
  9913. 'COUNT(f.iid) AS qty',
  9914. $fakeFrom,
  9915. [
  9916. 'where' => [
  9917. 'ip.visibility != ? AND ' => 2,
  9918. 'ip.tool = ? AND ' => TOOL_FORUM,
  9919. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9920. 'f.lp_id = ?' => intval($this->lp_id)
  9921. ]
  9922. ],
  9923. 'first'
  9924. );
  9925. if ($resultData['qty'] > 0) {
  9926. return true;
  9927. }
  9928. return false;
  9929. }
  9930. /**
  9931. * Get the forum for this learning path
  9932. * @return boolean
  9933. */
  9934. public function getForum($sessionId = 0)
  9935. {
  9936. $forumTable = Database::get_course_table(TABLE_FORUM);
  9937. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  9938. $fakeFrom = "$forumTable f
  9939. INNER JOIN $itemProperty ip ";
  9940. if ($this->lp_session_id == 0) {
  9941. $fakeFrom .= "
  9942. ON (
  9943. f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
  9944. f.session_id = ip.session_id OR ip.session_id IS NULL
  9945. )
  9946. )
  9947. ";
  9948. } else {
  9949. $fakeFrom .= "
  9950. ON (
  9951. f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
  9952. )
  9953. ";
  9954. }
  9955. $resultData = Database::select(
  9956. 'f.*',
  9957. $fakeFrom,
  9958. [
  9959. 'where' => [
  9960. 'ip.visibility != ? AND ' => 2,
  9961. 'ip.tool = ? AND ' => TOOL_FORUM,
  9962. 'f.session_id = ? AND ' => $sessionId,
  9963. 'f.c_id = ? AND ' => intval($this->course_int_id),
  9964. 'f.lp_id = ?' => intval($this->lp_id)
  9965. ]
  9966. ],
  9967. 'first'
  9968. );
  9969. if (empty($resultData)) {
  9970. return false;
  9971. }
  9972. return $resultData;
  9973. }
  9974. /**
  9975. * Create a forum for this learning path
  9976. * @param type $forumCategoryId
  9977. * @return int The forum ID if was created. Otherwise return false
  9978. */
  9979. public function createForum($forumCategoryId)
  9980. {
  9981. require_once api_get_path(SYS_CODE_PATH).'/forum/forumfunction.inc.php';
  9982. $forumId = store_forum(
  9983. [
  9984. 'lp_id' => $this->lp_id,
  9985. 'forum_title' => $this->name,
  9986. 'forum_comment' => null,
  9987. 'forum_category' => intval($forumCategoryId),
  9988. 'students_can_edit_group' => ['students_can_edit' => 0],
  9989. 'allow_new_threads_group' => ['allow_new_threads' => 0],
  9990. 'default_view_type_group' => ['default_view_type' => 'flat'],
  9991. 'group_forum' => 0,
  9992. 'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
  9993. ],
  9994. [],
  9995. true
  9996. );
  9997. return $forumId;
  9998. }
  9999. /**
  10000. * Check and obtain the lp final item if exist
  10001. *
  10002. * @return array lp items
  10003. */
  10004. private function getFinalItem()
  10005. {
  10006. if (empty($this->items)) {
  10007. return null;
  10008. }
  10009. foreach ($this->items as $item) {
  10010. if ($item->type !== 'final_item') {
  10011. continue;
  10012. }
  10013. return $item;
  10014. }
  10015. }
  10016. /**
  10017. * Get the LP Final Item Template
  10018. *
  10019. * @return html
  10020. */
  10021. private function getFinalItemTemplate()
  10022. {
  10023. return file_get_contents(api_get_path(SYS_CODE_PATH) . 'newscorm/final_item_template/template.html');
  10024. }
  10025. /**
  10026. * Get the LP Final Item Url
  10027. *
  10028. * @return String
  10029. */
  10030. private function getSavedFinalItem()
  10031. {
  10032. $finalItem = $this->getFinalItem();
  10033. $doc = DocumentManager::get_document_data_by_id($finalItem->path, $this->cc);
  10034. return file_get_contents($doc['absolute_path']);
  10035. }
  10036. /**
  10037. * Get the LP Final Item form
  10038. *
  10039. * @return html
  10040. */
  10041. public function getFinalItemForm()
  10042. {
  10043. $finalItem = $this->getFinalItem();
  10044. $title = '';
  10045. $content = '';
  10046. if ($finalItem) {
  10047. $title = $finalItem->title;
  10048. $buttonText = get_lang('Save');
  10049. $content = $this->getSavedFinalItem();
  10050. } else {
  10051. $buttonText = get_lang('LPCreateDocument');
  10052. $content = $this->getFinalItemTemplate();
  10053. }
  10054. $courseInfo = api_get_course_info();
  10055. $result = $this->generate_lp_folder($courseInfo);
  10056. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  10057. $relative_prefix = '../../';
  10058. $editorConfig = [
  10059. 'ToolbarSet' => 'LearningPathDocuments',
  10060. 'Width' => '100%',
  10061. 'Height' => '500',
  10062. 'FullPage' => true,
  10063. 'CreateDocumentDir' => $relative_prefix,
  10064. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/',
  10065. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path() . '/document/' . $relative_path
  10066. ];
  10067. $url = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
  10068. 'type' => 'document',
  10069. 'lp_id' => $this->lp_id
  10070. ]);
  10071. $form = new FormValidator('final_item', 'POST', $url);
  10072. $form->addText('title', get_lang('Title'));
  10073. $form->addButtonSave($buttonText);
  10074. $form->addHtml('<div class="alert alert-info">Variables :</br></br> <b>((certificate))</b> </br> <b>((skill))</b></div>');
  10075. $renderer = $form->defaultRenderer();
  10076. $renderer->setElementTemplate('<div class="editor-lp">&nbsp;{label}{element}</div>', 'content_lp');
  10077. $form->addHtmlEditor('content_lp', null, null, true, $editorConfig, true);
  10078. $form->addHidden('action', 'add_final_item');
  10079. $form->addHidden('path', isset($_SESSION['pathItem']) ? $_SESSION['pathItem'] : '');
  10080. $form->addHidden('previous', $this->get_last());
  10081. $form->setDefaults(['title' => $title, 'content_lp' => $content]);
  10082. if ($form->validate()) {
  10083. $values = $form->exportValues();
  10084. $lastItemId = $this->get_last();
  10085. if (!$finalItem) {
  10086. $documentId = $this->create_document($this->course_info, $values['content_lp'], $values['title']);
  10087. $this->add_item(
  10088. 0,
  10089. $lastItemId,
  10090. 'final_item',
  10091. $documentId,
  10092. $values['title'],
  10093. ''
  10094. );
  10095. } else {
  10096. $this->edit_document($this->course_info);
  10097. }
  10098. }
  10099. return $form->returnForm();
  10100. }
  10101. /**
  10102. * Returns an HTML-formatted link to a resource, to incorporate directly into
  10103. * the new learning path tool.
  10104. *
  10105. * The function is a big switch on tool type.
  10106. * In each case, we query the corresponding table for information and build the link
  10107. * with that information.
  10108. * @author Yannick Warnier <ywarnier@beeznest.org> - rebranding based on previous work (display_addedresource_link_in_learnpath())
  10109. * @param int $course_id Course code
  10110. * @param int $learnpath_id The learning path ID (in lp table)
  10111. * @param int $id_in_path the unique index in the items table
  10112. * @param int $lpViewId
  10113. */
  10114. public static function rl_get_resource_link_for_learnpath($course_id, $learnpath_id, $id_in_path, $lpViewId)
  10115. {
  10116. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10117. $course_info = api_get_course_info_by_id($course_id);
  10118. $course_id = $course_info['real_id'];
  10119. $course_code = $course_info['code'];
  10120. $session_id = api_get_session_id();
  10121. $learnpath_id = intval($learnpath_id);
  10122. $id_in_path = intval($id_in_path);
  10123. $lpViewId = intval($lpViewId);
  10124. $sql = "SELECT * FROM $tbl_lp_item
  10125. WHERE
  10126. c_id = $course_id AND
  10127. lp_id = $learnpath_id AND
  10128. id = $id_in_path
  10129. ";
  10130. $res_item = Database::query($sql);
  10131. if (Database::num_rows($res_item) < 1) {
  10132. return -1;
  10133. }
  10134. $row_item = Database::fetch_array($res_item, 'ASSOC');
  10135. $type = strtolower($row_item['item_type']);
  10136. $id = (strcmp($row_item['path'], '') == 0) ? '0' : $row_item['path'];
  10137. $origin = 'learnpath';
  10138. $main_dir_path = api_get_path(WEB_CODE_PATH);
  10139. $main_course_path = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  10140. $link = '';
  10141. switch ($type) {
  10142. case 'dokeos_chapter':
  10143. $link .= $main_dir_path.'newscorm/blank.php';
  10144. case TOOL_CALENDAR_EVENT:
  10145. $link .= $main_dir_path.'calendar/agenda.php?origin='.$origin.'&agenda_id='.$id;
  10146. break;
  10147. case TOOL_ANNOUNCEMENT:
  10148. $link .= $main_dir_path.'announcements/announcements.php?origin='.$origin.'&ann_id='.$id;
  10149. break;
  10150. case TOOL_LINK:
  10151. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10152. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10153. $myrow = Database::fetch_array($result);
  10154. $thelink = $myrow["url"];
  10155. $link .= $thelink;
  10156. break;
  10157. case TOOL_QUIZ:
  10158. if (!empty($id)) {
  10159. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10160. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id";
  10161. $result= Database::query($sql);
  10162. $myrow=Database::fetch_array($result);
  10163. if ($row_item['title'] != '') {
  10164. $myrow['title'] = $row_item['title'];
  10165. }
  10166. $link .= $main_dir_path.'exercice/overview.php?cidReq='.$course_code.'&session_id='.$session_id.'&lp_init=1&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$id_in_path.'&exerciseId='.$id;
  10167. }
  10168. break;
  10169. case 'hotpotatoes': //lowercase because of strtolower above
  10170. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  10171. $result = Database::query("SELECT * FROM ".$TBL_DOCUMENT." WHERE c_id = $course_id AND id=$id");
  10172. $myrow = Database::fetch_array($result);
  10173. $path = $myrow['path'];
  10174. $link .= $main_dir_path.'exercice/showinframes.php?file='.$path.'' .
  10175. '&origin='.$origin.'&cid='.$course_code.'&uid='.api_get_user_id().'' .
  10176. '&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$id_in_path.'&lp_view_id='.$lpViewId;
  10177. break;
  10178. case TOOL_FORUM:
  10179. $link .= $main_dir_path.'forum/viewforum.php?forum='.$id.'&lp=true&origin=learnpath';
  10180. break;
  10181. case TOOL_THREAD: //forum post
  10182. $tbl_topics = Database::get_course_table(TABLE_FORUM_THREAD);
  10183. if (!empty($id)) {
  10184. $sql = "SELECT * FROM $tbl_topics WHERE c_id = $course_id AND thread_id=$id";
  10185. $result = Database::query($sql);
  10186. $myrow = Database::fetch_array($result);
  10187. $link .= $main_dir_path.'forum/viewthread.php?origin=learnpath&thread='.$id.'' .
  10188. '&forum='.$myrow['forum_id'].'&lp=true';
  10189. }
  10190. break;
  10191. case TOOL_POST:
  10192. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10193. $result = Database::query("SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=$id");
  10194. $myrow = Database::fetch_array($result);
  10195. $title = $myrow['post_title'];
  10196. //$desc = $row_item['description'];
  10197. $posternom = $myrow['poster_name'];
  10198. $posttime = $myrow['post_date'];
  10199. $posttext = $myrow['post_text'];
  10200. $posttitle = $title;
  10201. $posttext = str_replace('"', "'", $posttext);
  10202. $link .= $main_dir_path.'forum/viewthread.php?post='.$id.'' .
  10203. '&thread='.$myrow['thread_id'].'&forum='.$myrow['forum_id'].'' .
  10204. '&lp=true';
  10205. break;
  10206. case TOOL_DOCUMENT:
  10207. $documentInfo = DocumentManager::get_document_data_by_id(
  10208. $id,
  10209. $course_code,
  10210. true,
  10211. $session_id
  10212. );
  10213. $documentPathInfo = pathinfo($documentInfo['absolute_path']);
  10214. $jplayer_supported_files = ['mp4', 'ogv', 'flv', 'm4v'];
  10215. $showDirectUrl = !in_array($documentPathInfo['extension'], $jplayer_supported_files);
  10216. if ($showDirectUrl) {
  10217. $link = $documentInfo['direct_url'] . '?';
  10218. $link .= http_build_query(['cidReq' => $course_code, 'id_session' => $session_id]);
  10219. } else {
  10220. $link = $documentInfo['url'] . '&' . http_build_query(['origin' => 'learnpathitem']);
  10221. }
  10222. $openmethod = 2;
  10223. $officedoc = false;
  10224. Session::write('openmethod',$openmethod);
  10225. Session::write('officedoc',$officedoc);
  10226. break;
  10227. case 'assignments':
  10228. $link .= $main_dir_path.'work/work.php?origin='.$origin;
  10229. break;
  10230. case TOOL_DROPBOX:
  10231. $link .= $main_dir_path.'dropbox/index.php?origin=learnpath';
  10232. break;
  10233. case 'introduction_text': //DEPRECATED
  10234. $link .= '';
  10235. break;
  10236. case TOOL_COURSE_DESCRIPTION:
  10237. $link .= $main_dir_path.'course_description?origin=learnpath';
  10238. break;
  10239. case TOOL_GROUP:
  10240. $link .= $main_dir_path.'group/group.php?origin='.$origin;
  10241. break;
  10242. case TOOL_USER:
  10243. $link .= $main_dir_path.'user/user.php?origin='.$origin;
  10244. break;
  10245. case TOOL_STUDENTPUBLICATION:
  10246. $link .= $main_dir_path.'work/work_list.php?'.api_get_cidreq().'&id='.$row_item['path'].'&origin=learnpath';
  10247. break;
  10248. } //end switch
  10249. return $link;
  10250. }
  10251. /**
  10252. * Gets the name of a resource (generally used in learnpath when no name is provided)
  10253. *
  10254. * @author Yannick Warnier <ywarnier@beeznest.org>, Dokeos - rebranding
  10255. * @param string Course code
  10256. * @param string The tool type (using constants declared in main_api.lib.php)
  10257. * @param integer The resource ID
  10258. */
  10259. public static function rl_get_resource_name($course_code, $learnpath_id, $id_in_path)
  10260. {
  10261. $_course = api_get_course_info($course_code);
  10262. $course_id = $_course['real_id'];
  10263. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  10264. $learnpath_id = intval($learnpath_id);
  10265. $id_in_path = intval($id_in_path);
  10266. $sql_item = "SELECT item_type, title, ref FROM $tbl_lp_item
  10267. WHERE c_id = $course_id AND lp_id = $learnpath_id AND id = $id_in_path";
  10268. $res_item = Database::query($sql_item);
  10269. if (Database::num_rows($res_item) < 1) {
  10270. return '';
  10271. }
  10272. $row_item = Database::fetch_array($res_item);
  10273. $type = strtolower($row_item['item_type']);
  10274. $id = $row_item['ref'];
  10275. $output = '';
  10276. switch ($type) {
  10277. case TOOL_CALENDAR_EVENT:
  10278. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  10279. $result = Database::query("SELECT * FROM $TABLEAGENDA WHERE c_id = $course_id AND id=$id");
  10280. $myrow = Database::fetch_array($result);
  10281. $output = $myrow['title'];
  10282. break;
  10283. case TOOL_ANNOUNCEMENT:
  10284. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  10285. $result = Database::query("SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id=$id");
  10286. $myrow = Database::fetch_array($result);
  10287. $output = $myrow['title'];
  10288. break;
  10289. case TOOL_LINK:
  10290. // Doesn't take $target into account.
  10291. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  10292. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$id");
  10293. $myrow = Database::fetch_array($result);
  10294. $output = $myrow['title'];
  10295. break;
  10296. case TOOL_QUIZ:
  10297. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  10298. $result = Database::query("SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND id=$id");
  10299. $myrow = Database::fetch_array($result);
  10300. $output = $myrow['title'];
  10301. break;
  10302. case TOOL_FORUM:
  10303. $TBL_FORUMS = Database::get_course_table(TABLE_FORUM);
  10304. $result = Database::query("SELECT * FROM $TBL_FORUMS WHERE c_id = $course_id AND forum_id=$id");
  10305. $myrow = Database::fetch_array($result);
  10306. $output = $myrow['forum_name'];
  10307. break;
  10308. case TOOL_THREAD: //=topics
  10309. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10310. // Grabbing the title of the post.
  10311. $sql_title = "SELECT * FROM $tbl_post WHERE c_id = $course_id AND post_id=".$id;
  10312. $result_title = Database::query($sql_title);
  10313. $myrow_title = Database::fetch_array($result_title);
  10314. $output = $myrow_title['post_title'];
  10315. break;
  10316. case TOOL_POST:
  10317. $tbl_post = Database::get_course_table(TABLE_FORUM_POST);
  10318. //$tbl_post_text = Database::get_course_table(FORUM_POST_TEXT_TABLE);
  10319. $sql = "SELECT * FROM $tbl_post p WHERE c_id = $course_id AND p.post_id = $id";
  10320. $result = Database::query($sql);
  10321. $post = Database::fetch_array($result);
  10322. $output = $post['post_title'];
  10323. break;
  10324. case 'dokeos_chapter':
  10325. $title = $row_item['title'];
  10326. if (!empty($title)) {
  10327. $output = $title;
  10328. } else {
  10329. $output = '-';
  10330. }
  10331. break;
  10332. case TOOL_DOCUMENT:
  10333. $title = $row_item['title'];
  10334. if (!empty($title)) {
  10335. $output = $title;
  10336. } else {
  10337. $output = '-';
  10338. }
  10339. break;
  10340. case 'hotpotatoes':
  10341. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  10342. $result = Database::query("SELECT * FROM $tbl_doc WHERE c_id = $course_id AND id=$id");
  10343. $myrow = Database::fetch_array($result);
  10344. $pathname = explode('/', $myrow['path']); // Making a correct name for the link.
  10345. $last = count($pathname) - 1; // Making a correct name for the link.
  10346. $filename = $pathname[$last]; // Making a correct name for the link.
  10347. $image = choose_image($filename);
  10348. $ext = explode('.', $filename);
  10349. $ext = strtolower($ext[sizeof($ext) - 1]);
  10350. $myrow['path'] = rawurlencode($myrow['path']);
  10351. $in_frames = in_array($ext, array('htm', 'html', 'gif', 'jpg', 'jpeg', 'png'));
  10352. $output = $filename;
  10353. break;
  10354. /*
  10355. case 'externallink':
  10356. $output = '<img src="../img/links.gif" align="middle" /> <a href="'.$id.'"'.$styling.' '.$target.'>'.$id."</a><br />\n";
  10357. break;
  10358. */
  10359. }
  10360. return stripslashes($output);
  10361. }
  10362. }
  10363. if (!function_exists('trim_value')) {
  10364. function trim_value(& $value) {
  10365. $value = trim($value);
  10366. }
  10367. }