tracking.lib.php 306 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. use CpChart\Chart\Cache as pCache;
  5. use CpChart\Chart\Data as pData;
  6. use CpChart\Chart\Image as pImage;
  7. use Chamilo\UserBundle\Entity\User;
  8. use Chamilo\CoreBundle\Entity\Course;
  9. use Chamilo\CoreBundle\Entity\Session;
  10. /**
  11. * Class Tracking
  12. *
  13. * @author Julio Montoya <gugli100@gmail.com>
  14. * @package chamilo.library
  15. */
  16. class Tracking
  17. {
  18. /**
  19. * Get group reporting
  20. * @param int $course_id
  21. * @param int $sessionId
  22. * @param int $group_id
  23. * @param string $type
  24. * @param int $start
  25. * @param int $limit
  26. * @param int $sidx
  27. * @param string $sord
  28. * @param array $where_condition
  29. * @return array|null
  30. */
  31. public static function get_group_reporting(
  32. $course_id,
  33. $sessionId = null,
  34. $group_id = null,
  35. $type = 'all',
  36. $start = 0,
  37. $limit = 1000,
  38. $sidx = 1,
  39. $sord = 'desc',
  40. $where_condition = array()
  41. ) {
  42. if (empty($course_id)) {
  43. return null;
  44. }
  45. $course_info = api_get_course_info_by_id($course_id);
  46. $sessionId = (int) $sessionId;
  47. $table_group = Database::get_course_table(TABLE_GROUP);
  48. $course_id = intval($course_id);
  49. $select = ' * ';
  50. if ($type == 'count') {
  51. $select = ' count(id) as count ';
  52. }
  53. if (empty($sessionId)) {
  54. $default_where = array('c_id = ? AND (session_id = 0 or session_id IS NULL)' => array($course_id));
  55. } else {
  56. $default_where = array('c_id = ? AND session_id = ? ' => array($course_id, $sessionId));
  57. }
  58. $result = Database::select(
  59. $select,
  60. $table_group,
  61. array(
  62. 'limit' => " $start, $limit",
  63. 'where' => $default_where,
  64. 'order' => "$sidx $sord",
  65. )
  66. );
  67. if ($type == 'count') {
  68. return $result[0]['count'];
  69. }
  70. $parsed_result = array();
  71. if (!empty($result)) {
  72. foreach ($result as $group) {
  73. $users = GroupManager::get_users($group['id'], true);
  74. $time = 0;
  75. $avg_student_score = 0;
  76. $avg_student_progress = 0;
  77. $work = 0;
  78. $messages = 0;
  79. foreach ($users as $user_data) {
  80. $time += self::get_time_spent_on_the_course($user_data['user_id'], $course_info['code'], $sessionId);
  81. $avg_student_score += self::get_avg_student_score($user_data['user_id'], $course_info['code'], array(), $sessionId);
  82. $avg_student_progress += self::get_avg_student_progress($user_data['user_id'], $course_info['code'], array(), $sessionId);
  83. $work += self::count_student_assignments($user_data['user_id'], $course_info['code'], $sessionId);
  84. $messages += self::count_student_messages($user_data['user_id'], $course_info['code'], $sessionId);
  85. }
  86. $countUsers = count($users);
  87. $averageProgress = empty($countUsers) ? 0 : $avg_student_progress/$countUsers;
  88. $averageScore = empty($countUsers) ? 0 : $avg_student_score/$countUsers;
  89. $group_item = array(
  90. 'id' => $group['id'],
  91. 'name' => $group['name'],
  92. 'time' => api_time_to_hms($time),
  93. 'progress' => $averageProgress,
  94. 'score' => $averageScore,
  95. 'works' => $work,
  96. 'messages' => $messages,
  97. );
  98. $parsed_result[] = $group_item;
  99. }
  100. }
  101. return $parsed_result;
  102. }
  103. /**
  104. * @param int $user_id
  105. * @param array $courseInfo
  106. * @param int $session_id
  107. * @param string $origin
  108. * @param bool $export_csv
  109. * @param int $lp_id
  110. * @param int $lp_item_id
  111. * @param int $extendId
  112. * @param int $extendAttemptId
  113. * @param string $extendedAttempt
  114. * @param string $extendedAll
  115. * @param string $type classic or simple
  116. * @param boolean $allowExtend Optional. Allow or not extend te results
  117. * @return null|string
  118. */
  119. public static function getLpStats(
  120. $user_id,
  121. $courseInfo,
  122. $session_id,
  123. $origin,
  124. $export_csv,
  125. $lp_id,
  126. $lp_item_id = null,
  127. $extendId = null,
  128. $extendAttemptId = null,
  129. $extendedAttempt = null,
  130. $extendedAll = null,
  131. $type = 'classic',
  132. $allowExtend = true
  133. ) {
  134. if (empty($courseInfo) || empty($lp_id)) {
  135. return null;
  136. }
  137. $hideTime = api_get_configuration_value('hide_lp_time');
  138. $lp_id = intval($lp_id);
  139. $lp_item_id = intval($lp_item_id);
  140. $user_id = intval($user_id);
  141. $session_id = intval($session_id);
  142. $origin = Security::remove_XSS($origin);
  143. $list = learnpath::get_flat_ordered_items_list($lp_id, 0, $courseInfo['real_id']);
  144. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  145. $course_id = $courseInfo['real_id'];
  146. $courseCode = $courseInfo['code'];
  147. $session_condition = api_get_session_condition($session_id);
  148. // Extend all button
  149. $output = null;
  150. $extend_all = 0;
  151. if ($origin == 'tracking') {
  152. $url_suffix = '&session_id=' . $session_id . '&course=' . $courseCode . '&student_id=' . $user_id . '&lp_id=' . $lp_id . '&origin=' . $origin;
  153. } else {
  154. $url_suffix = '&lp_id=' . $lp_id;
  155. }
  156. if (!empty($extendedAll)) {
  157. $extend_all_link = Display::url(
  158. Display::return_icon('view_less_stats.gif', get_lang('HideAllAttempts')),
  159. api_get_self() . '?action=stats' . $url_suffix
  160. );
  161. $extend_all = 1;
  162. } else {
  163. $extend_all_link = Display::url(
  164. Display::return_icon('view_more_stats.gif', get_lang('ShowAllAttempts')),
  165. api_get_self() . '?action=stats&extend_all=1' . $url_suffix
  166. );
  167. }
  168. if ($origin != 'tracking') {
  169. $output .= '<div class="section-status">';
  170. $output .= Display::page_header(get_lang('ScormMystatus'));
  171. $output .= '</div>';
  172. }
  173. $actionColumn = null;
  174. if ($type == 'classic') {
  175. $actionColumn = ' <th>' . get_lang('Actions') . '</th>';
  176. }
  177. $timeHeader = '<th class="lp_time" colspan="2">'.get_lang('ScormTime').'</th>';
  178. if ($hideTime) {
  179. $timeHeader = '';
  180. }
  181. $output .= '<div class="table-responsive">';
  182. $output .= '<table id="lp_tracking" class="table tracking">
  183. <thead>
  184. <tr class="table-header">
  185. <th width="16">' . ($allowExtend == true ? $extend_all_link : '&nbsp;') . '</th>
  186. <th colspan="4">
  187. ' . get_lang('ScormLessonTitle') .'
  188. </th>
  189. <th colspan="2">
  190. ' . get_lang('ScormStatus') . '
  191. </th>
  192. <th colspan="2">
  193. ' . get_lang('ScormScore') . '
  194. </th>
  195. '.$timeHeader.'
  196. '.$actionColumn.'
  197. </tr>
  198. </thead>
  199. <tbody>
  200. ';
  201. // Going through the items using the $items[] array instead of the database order ensures
  202. // we get them in the same order as in the imsmanifest file, which is rather random when using
  203. // the database table.
  204. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  205. $TBL_LP_ITEM_VIEW = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  206. $TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW);
  207. $tbl_quiz_questions = Database::get_course_table(TABLE_QUIZ_QUESTION);
  208. $TBL_QUIZ = Database::get_course_table(TABLE_QUIZ_TEST);
  209. $tbl_stats_exercices = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  210. $tbl_stats_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  211. $sql = "SELECT max(view_count)
  212. FROM $TBL_LP_VIEW
  213. WHERE
  214. c_id = $course_id AND
  215. lp_id = $lp_id AND
  216. user_id = $user_id
  217. $session_condition";
  218. $res = Database::query($sql);
  219. $view = '';
  220. if (Database::num_rows($res) > 0) {
  221. $myrow = Database::fetch_array($res);
  222. $view = $myrow[0];
  223. }
  224. $counter = 0;
  225. $total_time = 0;
  226. $h = get_lang('h');
  227. if (!empty($export_csv)) {
  228. $csv_content[] = array(
  229. get_lang('ScormLessonTitle'),
  230. get_lang('ScormStatus'),
  231. get_lang('ScormScore')
  232. );
  233. if ($hideTime === false) {
  234. $csv_content[] = get_lang('ScormTime');
  235. }
  236. }
  237. $result_disabled_ext_all = true;
  238. $chapterTypes = learnpath::getChapterTypes();
  239. // Show lp items
  240. if (is_array($list) && count($list) > 0) {
  241. foreach ($list as $my_item_id) {
  242. $extend_this = 0;
  243. $order = 'DESC';
  244. if ((!empty($extendId) && $extendId == $my_item_id) || $extend_all) {
  245. $extend_this = 1;
  246. $order = 'ASC';
  247. }
  248. // Prepare statement to go through each attempt.
  249. $viewCondition = null;
  250. if (!empty($view)) {
  251. $viewCondition = " AND v.view_count = $view ";
  252. }
  253. $sql = "SELECT
  254. iv.status as mystatus,
  255. v.view_count as mycount,
  256. iv.score as myscore,
  257. iv.total_time as mytime,
  258. i.id as myid,
  259. i.lp_id as mylpid,
  260. iv.lp_view_id as mylpviewid,
  261. i.title as mytitle,
  262. i.max_score as mymaxscore,
  263. iv.max_score as myviewmaxscore,
  264. i.item_type as item_type,
  265. iv.view_count as iv_view_count,
  266. iv.id as iv_id,
  267. path
  268. FROM $TBL_LP_ITEM as i
  269. INNER JOIN $TBL_LP_ITEM_VIEW as iv
  270. ON (i.id = iv.lp_item_id AND i.c_id = iv.c_id)
  271. INNER JOIN $TBL_LP_VIEW as v
  272. ON (iv.lp_view_id = v.id AND v.c_id = iv.c_id)
  273. WHERE
  274. v.c_id = $course_id AND
  275. i.id = $my_item_id AND
  276. i.lp_id = $lp_id AND
  277. v.user_id = $user_id AND
  278. v.session_id = $session_id
  279. $viewCondition
  280. ORDER BY iv.view_count $order ";
  281. $result = Database::query($sql);
  282. $num = Database::num_rows($result);
  283. $time_for_total = 'NaN';
  284. // Extend all
  285. if (($extend_this || $extend_all) && $num > 0) {
  286. $row = Database::fetch_array($result);
  287. $result_disabled_ext_all = false;
  288. if ($row['item_type'] == 'quiz') {
  289. // Check results_disabled in quiz table.
  290. $my_path = Database::escape_string($row['path']);
  291. $sql = "SELECT results_disabled
  292. FROM $TBL_QUIZ
  293. WHERE
  294. c_id = $course_id AND
  295. id ='" . $my_path . "'";
  296. $res_result_disabled = Database::query($sql);
  297. $row_result_disabled = Database::fetch_row($res_result_disabled);
  298. if (Database::num_rows($res_result_disabled) > 0 &&
  299. (int) $row_result_disabled[0] === 1
  300. ) {
  301. $result_disabled_ext_all = true;
  302. }
  303. }
  304. // If there are several attempts, and the link to extend has been clicked, show each attempt...
  305. if (($counter % 2) == 0) {
  306. $oddclass = 'row_odd';
  307. } else {
  308. $oddclass = 'row_even';
  309. }
  310. $extend_link = '';
  311. if (!empty($inter_num)) {
  312. $extend_link = Display::url(
  313. Display::return_icon(
  314. 'visible.gif',
  315. get_lang('HideAttemptView')
  316. ),
  317. api_get_self().'?action=stats&fold_id='.$my_item_id.$url_suffix
  318. );
  319. }
  320. $title = $row['mytitle'];
  321. if (empty($title)) {
  322. $title = learnpath::rl_get_resource_name($courseInfo['code'], $lp_id, $row['myid']);
  323. }
  324. if (in_array($row['item_type'], $chapterTypes)) {
  325. $title = "<h4> $title </h4>";
  326. }
  327. $lesson_status = $row['mystatus'];
  328. $title = Security::remove_XSS($title);
  329. $counter++;
  330. $action = null;
  331. if ($type == 'classic') {
  332. $action = '<td></td>';
  333. }
  334. if (in_array($row['item_type'], $chapterTypes)) {
  335. $output .= '<tr class="'.$oddclass.'">
  336. <td>'.$extend_link.'</td>
  337. <td colspan="4">
  338. '.$title.'
  339. </td>
  340. <td colspan="2">'.learnpathItem::humanize_status($lesson_status, true, $type).'</td>
  341. <td colspan="2"></td>
  342. <td colspan="2"></td>
  343. '.$action.'
  344. </tr>';
  345. continue;
  346. } else {
  347. $output .= '<tr class="'.$oddclass.'">
  348. <td>'.$extend_link.'</td>
  349. <td colspan="4">
  350. '.$title.'
  351. </td>
  352. <td colspan="2"></td>
  353. <td colspan="2"></td>
  354. <td colspan="2"></td>
  355. '.$action.'
  356. </tr>';
  357. }
  358. $attemptCount = 1;
  359. do {
  360. // Check if there are interactions below.
  361. $extend_attempt_link = '';
  362. $extend_this_attempt = 0;
  363. if ((learnpath::get_interactions_count_from_db($row['iv_id'], $course_id) > 0 ||
  364. learnpath::get_objectives_count_from_db($row['iv_id'], $course_id) > 0) &&
  365. !$extend_all
  366. ) {
  367. if ($extendAttemptId == $row['iv_id']) {
  368. // The extend button for this attempt has been clicked.
  369. $extend_this_attempt = 1;
  370. $extend_attempt_link = Display::url(
  371. Display::return_icon('visible.gif', get_lang('HideAttemptView')),
  372. api_get_self() . '?action=stats&extend_id=' . $my_item_id . '&fold_attempt_id=' . $row['iv_id'] . $url_suffix
  373. );
  374. } else { // Same case if fold_attempt_id is set, so not implemented explicitly.
  375. // The extend button for this attempt has not been clicked.
  376. $extend_attempt_link = Display::url(
  377. Display::return_icon('invisible.gif', get_lang('ExtendAttemptView')),
  378. api_get_self() . '?action=stats&extend_id=' . $my_item_id . '&extend_attempt_id=' . $row['iv_id'] . $url_suffix
  379. );
  380. }
  381. }
  382. if (($counter % 2) == 0) {
  383. $oddclass = 'row_odd';
  384. } else {
  385. $oddclass = 'row_even';
  386. }
  387. $lesson_status = $row['mystatus'];
  388. $score = $row['myscore'];
  389. $time_for_total = $row['mytime'];
  390. $time = learnpathItem::getScormTimeFromParameter('js', $row['mytime']);
  391. if ($score == 0) {
  392. $maxscore = $row['mymaxscore'];
  393. } else {
  394. if ($row['item_type'] == 'sco') {
  395. if (!empty($row['myviewmaxscore']) && $row['myviewmaxscore'] > 0) {
  396. $maxscore = $row['myviewmaxscore'];
  397. } elseif ($row['myviewmaxscore'] === '') {
  398. $maxscore = 0;
  399. } else {
  400. $maxscore = $row['mymaxscore'];
  401. }
  402. } else {
  403. $maxscore = $row['mymaxscore'];
  404. }
  405. }
  406. // Remove "NaN" if any (@todo: locate the source of these NaN)
  407. $time = str_replace('NaN', '00' . $h . '00\'00"', $time);
  408. if ($row['item_type'] != 'dir') {
  409. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  410. $view_score = Display::return_icon(
  411. 'invisible.gif',
  412. get_lang('ResultsHiddenByExerciseSetting')
  413. );
  414. } else {
  415. switch ($row['item_type']) {
  416. case 'sco':
  417. if ($maxscore == 0) {
  418. $view_score = $score;
  419. } else {
  420. $view_score = ExerciseLib::show_score($score, $maxscore, false);
  421. }
  422. break;
  423. case 'document':
  424. $view_score = ($score == 0 ? '/' : ExerciseLib::show_score($score, $maxscore, false));
  425. break;
  426. default:
  427. $view_score = ExerciseLib::show_score($score, $maxscore, false);
  428. break;
  429. }
  430. }
  431. $action = null;
  432. if ($type == 'classic') {
  433. $action = '<td></td>';
  434. }
  435. $timeRow = '<td class="lp_time" colspan="2">'.$time.'</td>';
  436. if ($hideTime) {
  437. $timeRow = '';
  438. }
  439. $output .= '<tr class="' . $oddclass . '">
  440. <td></td>
  441. <td>' . $extend_attempt_link . '</td>
  442. <td colspan="3">' . get_lang('Attempt') . ' ' . $attemptCount . '</td>
  443. <td colspan="2">' . learnpathItem::humanize_status($lesson_status, true, $type) . '</td>
  444. <td colspan="2">' . $view_score . '</td>
  445. '.$timeRow.'
  446. '.$action.'
  447. </tr>';
  448. $attemptCount++;
  449. if (!empty($export_csv)) {
  450. $temp = array();
  451. $temp[] = $title = Security::remove_XSS($title);
  452. $temp[] = Security::remove_XSS(
  453. learnpathItem::humanize_status($lesson_status, false, $type)
  454. );
  455. if ($row['item_type'] == 'quiz') {
  456. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  457. $temp[] = '/';
  458. } else {
  459. $temp[] = ($score == 0 ? '0/' . $maxscore : ($maxscore == 0 ? $score : $score . '/' . float_format($maxscore, 1)));
  460. }
  461. } else {
  462. $temp[] = ($score == 0 ? '/' : ($maxscore == 0 ? $score : $score . '/' . float_format($maxscore, 1)));
  463. }
  464. if ($hideTime === false) {
  465. $temp[] = $time;
  466. }
  467. $csv_content[] = $temp;
  468. }
  469. }
  470. $counter++;
  471. $action = null;
  472. if ($type == 'classic') {
  473. $action = '<td></td>';
  474. }
  475. if ($extend_this_attempt || $extend_all) {
  476. $list1 = learnpath::get_iv_interactions_array($row['iv_id']);
  477. foreach ($list1 as $id => $interaction) {
  478. if (($counter % 2) == 0) {
  479. $oddclass = 'row_odd';
  480. } else {
  481. $oddclass = 'row_even';
  482. }
  483. $student_response = urldecode($interaction['student_response']);
  484. $content_student_response = explode('__|', $student_response);
  485. if (count($content_student_response) > 0) {
  486. if (count($content_student_response) >= 3) {
  487. // Pop the element off the end of array.
  488. array_pop($content_student_response);
  489. }
  490. $student_response = implode(',', $content_student_response);
  491. }
  492. $timeRow = '<td class="lp_time">'.$interaction['time'].'</td>';
  493. if ($hideTime) {
  494. $timeRow = '';
  495. }
  496. $output .= '<tr class="'.$oddclass.'">
  497. <td></td>
  498. <td></td>
  499. <td></td>
  500. <td>'.$interaction['order_id'] . '</td>
  501. <td>'.$interaction['id'] . '</td>
  502. <td colspan="2">' . $interaction['type'].'</td>
  503. <td>'.$student_response . '</td>
  504. <td>'.$interaction['result'] . '</td>
  505. <td>'.$interaction['latency'] . '</td>
  506. '.$timeRow.'
  507. '.$action.'
  508. </tr>';
  509. $counter++;
  510. }
  511. $list2 = learnpath::get_iv_objectives_array($row['iv_id']);
  512. foreach ($list2 as $id => $interaction) {
  513. if (($counter % 2) == 0) {
  514. $oddclass = 'row_odd';
  515. } else {
  516. $oddclass = 'row_even';
  517. }
  518. $output .= '<tr class="'.$oddclass.'">
  519. <td></td>
  520. <td></td>
  521. <td></td>
  522. <td>' . $interaction['order_id'] . '</td>
  523. <td colspan="2">' . $interaction['objective_id'] . '</td>
  524. <td colspan="2">' . $interaction['status'] .'</td>
  525. <td>' . $interaction['score_raw'] . '</td>
  526. <td>' . $interaction['score_max'] . '</td>
  527. <td>' . $interaction['score_min'] . '</td>
  528. '.$action.'
  529. </tr>';
  530. $counter++;
  531. }
  532. }
  533. } while ($row = Database::fetch_array($result));
  534. } elseif ($num > 0) {
  535. // Not extended.
  536. $row = Database::fetch_array($result, 'ASSOC');
  537. $my_id = $row['myid'];
  538. $my_lp_id = $row['mylpid'];
  539. $my_lp_view_id = $row['mylpviewid'];
  540. $my_path = $row['path'];
  541. $result_disabled_ext_all = false;
  542. if ($row['item_type'] == 'quiz') {
  543. // Check results_disabled in quiz table.
  544. $my_path = Database::escape_string($my_path);
  545. $sql = "SELECT results_disabled
  546. FROM $TBL_QUIZ
  547. WHERE c_id = $course_id AND id ='" . $my_path . "'";
  548. $res_result_disabled = Database::query($sql);
  549. $row_result_disabled = Database::fetch_row($res_result_disabled);
  550. if (Database::num_rows($res_result_disabled) > 0 &&
  551. (int) $row_result_disabled[0] === 1
  552. ) {
  553. $result_disabled_ext_all = true;
  554. }
  555. }
  556. // Check if there are interactions below
  557. $extend_this_attempt = 0;
  558. $inter_num = learnpath::get_interactions_count_from_db($row['iv_id'], $course_id);
  559. $objec_num = learnpath::get_objectives_count_from_db($row['iv_id'], $course_id);
  560. $extend_attempt_link = '';
  561. if ($inter_num > 0 || $objec_num > 0) {
  562. if (!empty($extendAttemptId) && $extendAttemptId == $row['iv_id']) {
  563. // The extend button for this attempt has been clicked.
  564. $extend_this_attempt = 1;
  565. $extend_attempt_link = Display::url(
  566. Display::return_icon('visible.gif', get_lang('HideAttemptView')),
  567. api_get_self() . '?action=stats&extend_id=' . $my_item_id . '&fold_attempt_id=' . $row['iv_id'] . $url_suffix
  568. );
  569. } else {
  570. // Same case if fold_attempt_id is set, so not implemented explicitly.
  571. // The extend button for this attempt has not been clicked.
  572. $extend_attempt_link = Display::url(
  573. Display::return_icon('invisible.gif', get_lang('ExtendAttemptView')),
  574. api_get_self() . '?action=stats&extend_id=' . $my_item_id . '&extend_attempt_id=' . $row['iv_id'] . $url_suffix
  575. );
  576. }
  577. }
  578. if (($counter % 2) == 0) {
  579. $oddclass = 'row_odd';
  580. } else {
  581. $oddclass = 'row_even';
  582. }
  583. $extend_link = '';
  584. if ($inter_num > 1) {
  585. $extend_link = Display::url(
  586. Display::return_icon('invisible.gif', get_lang('ExtendAttemptView')),
  587. api_get_self() . '?action=stats&extend_id=' . $my_item_id . '&extend_attempt_id=' . $row['iv_id'] . $url_suffix
  588. );
  589. }
  590. $lesson_status = $row['mystatus'];
  591. $score = $row['myscore'];
  592. $subtotal_time = $row['mytime'];
  593. while ($tmp_row = Database::fetch_array($result)) {
  594. $subtotal_time += $tmp_row['mytime'];
  595. }
  596. $title = $row['mytitle'];
  597. // Selecting the exe_id from stats attempts tables in order to look the max score value.
  598. $sql = 'SELECT * FROM ' . $tbl_stats_exercices . '
  599. WHERE
  600. exe_exo_id="' . $row['path'] . '" AND
  601. exe_user_id="' . $user_id . '" AND
  602. orig_lp_id = "' . $lp_id . '" AND
  603. orig_lp_item_id = "' . $row['myid'] . '" AND
  604. c_id = ' . $course_id . ' AND
  605. status <> "incomplete" AND
  606. session_id = ' . $session_id . '
  607. ORDER BY exe_date DESC
  608. LIMIT 1';
  609. $resultLastAttempt = Database::query($sql);
  610. $num = Database::num_rows($resultLastAttempt);
  611. $id_last_attempt = null;
  612. if ($num > 0) {
  613. while ($rowLA = Database::fetch_array($resultLastAttempt)) {
  614. $id_last_attempt = $rowLA['exe_id'];
  615. }
  616. }
  617. switch ($row['item_type']) {
  618. case 'sco':
  619. if (!empty($row['myviewmaxscore']) and $row['myviewmaxscore'] > 0) {
  620. $maxscore = $row['myviewmaxscore'];
  621. } elseif ($row['myviewmaxscore'] === '') {
  622. $maxscore = 0;
  623. } else {
  624. $maxscore = $row['mymaxscore'];
  625. }
  626. break;
  627. case 'quiz':
  628. // Get score and total time from last attempt of a exercise en lp.
  629. $sql = "SELECT score
  630. FROM $TBL_LP_ITEM_VIEW
  631. WHERE
  632. c_id = $course_id AND
  633. lp_item_id = '" . (int) $my_id . "' AND
  634. lp_view_id = '" . (int) $my_lp_view_id . "'
  635. ORDER BY view_count DESC limit 1";
  636. $res_score = Database::query($sql);
  637. $row_score = Database::fetch_array($res_score);
  638. $sql = "SELECT SUM(total_time) as total_time
  639. FROM $TBL_LP_ITEM_VIEW
  640. WHERE
  641. c_id = $course_id AND
  642. lp_item_id = '" . (int) $my_id . "' AND
  643. lp_view_id = '" . (int) $my_lp_view_id . "'";
  644. $res_time = Database::query($sql);
  645. $row_time = Database::fetch_array($res_time);
  646. if (Database::num_rows($res_score) > 0 &&
  647. Database::num_rows($res_time) > 0
  648. ) {
  649. $score = (float) $row_score['score'];
  650. $subtotal_time = (int) $row_time['total_time'];
  651. } else {
  652. $score = 0;
  653. $subtotal_time = 0;
  654. }
  655. // Selecting the max score from an attempt.
  656. $sql = "SELECT SUM(t.ponderation) as maxscore
  657. FROM (
  658. SELECT DISTINCT
  659. question_id, marks, ponderation
  660. FROM $tbl_stats_attempts as at
  661. INNER JOIN $tbl_quiz_questions as q
  662. ON (q.id = at.question_id AND q.c_id = $course_id)
  663. WHERE exe_id ='$id_last_attempt'
  664. ) as t";
  665. $result = Database::query($sql);
  666. $row_max_score = Database::fetch_array($result);
  667. $maxscore = $row_max_score['maxscore'];
  668. break;
  669. default:
  670. $maxscore = $row['mymaxscore'];
  671. break;
  672. }
  673. $time_for_total = $subtotal_time;
  674. $time = learnpathItem::getScormTimeFromParameter(
  675. 'js',
  676. $subtotal_time
  677. );
  678. if (empty($title)) {
  679. $title = learnpath::rl_get_resource_name(
  680. $courseInfo['code'],
  681. $lp_id,
  682. $row['myid']
  683. );
  684. }
  685. $action = null;
  686. if ($type == 'classic') {
  687. $action = '<td></td>';
  688. }
  689. if (in_array($row['item_type'], $chapterTypes)) {
  690. $title = Security::remove_XSS($title);
  691. $output .= '<tr class="'.$oddclass.'">
  692. <td>'.$extend_link.'</td>
  693. <td colspan="4">
  694. <h4>'.$title.'</h4>
  695. </td>
  696. <td colspan="2">'.learnpathitem::humanize_status($lesson_status).'</td>
  697. <td colspan="2"></td>
  698. <td colspan="2"></td>
  699. '.$action.'
  700. </tr>';
  701. } else {
  702. $correct_test_link = '-';
  703. if ($row['item_type'] == 'quiz') {
  704. $my_url_suffix = '&course=' . $courseCode . '&student_id=' . $user_id . '&lp_id=' . intval($row['mylpid']) . '&origin=' . $origin;
  705. $sql = 'SELECT * FROM ' . $tbl_stats_exercices . '
  706. WHERE
  707. exe_exo_id="' . $row['path'] . '" AND
  708. exe_user_id="' . $user_id . '" AND
  709. orig_lp_id = "' . $lp_id . '" AND
  710. orig_lp_item_id = "' . $row['myid'] . '" AND
  711. c_id = ' . $course_id . ' AND
  712. status <> "incomplete" AND
  713. session_id = ' . $session_id . '
  714. ORDER BY exe_date DESC ';
  715. $resultLastAttempt = Database::query($sql);
  716. $num = Database::num_rows($resultLastAttempt);
  717. if ($num > 0) {
  718. $linkId = 'link_'.$my_id;
  719. if ($extendedAttempt == 1 &&
  720. $lp_id == $my_lp_id &&
  721. $lp_item_id == $my_id
  722. ) {
  723. $correct_test_link = Display::url(
  724. Display::return_icon(
  725. 'view_less_stats.gif',
  726. get_lang('HideAllAttempts')
  727. ),
  728. api_get_self().'?action=stats'.$my_url_suffix.'&session_id='.$session_id.'&lp_item_id='.$my_id.'#'.$linkId,
  729. ['id' => $linkId]
  730. );
  731. } else {
  732. $correct_test_link = Display::url(
  733. Display::return_icon(
  734. 'view_more_stats.gif',
  735. get_lang(
  736. 'ShowAllAttemptsByExercise'
  737. )
  738. ),
  739. api_get_self().'?action=stats&extend_attempt=1'.$my_url_suffix.'&session_id='.$session_id.'&lp_item_id='.$my_id.'#'.$linkId,
  740. ['id' => $linkId]
  741. );
  742. }
  743. }
  744. }
  745. $title = Security::remove_XSS($title);
  746. $action = null;
  747. if ($type == 'classic') {
  748. $action = '<td>' . $correct_test_link . '</td>';
  749. }
  750. if ($lp_id == $my_lp_id && false) {
  751. $output .= '<tr class =' . $oddclass . '>
  752. <td>' . $extend_link . '</td>
  753. <td colspan="4">' . $title . '</td>
  754. <td colspan="2">&nbsp;</td>
  755. <td colspan="2">&nbsp;</td>
  756. <td colspan="2">&nbsp;</td>
  757. '.$action.'
  758. </tr>';
  759. $output .= '</tr>';
  760. } else {
  761. if ($lp_id == $my_lp_id && $lp_item_id == $my_id) {
  762. $output .= "<tr class='$oddclass'>";
  763. } else {
  764. $output .= "<tr class='$oddclass'>";
  765. }
  766. $scoreItem = null;
  767. if ($row['item_type'] == 'quiz') {
  768. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  769. $scoreItem .= Display::return_icon(
  770. 'invisible.gif',
  771. get_lang('ResultsHiddenByExerciseSetting')
  772. );
  773. } else {
  774. $scoreItem .= ExerciseLib::show_score($score, $maxscore, false);
  775. }
  776. } else {
  777. $scoreItem .= $score == 0 ? '/' : ($maxscore == 0 ? $score : $score . '/' . $maxscore);
  778. }
  779. $timeRow = '<td class="lp_time" colspan="2">'.$time.'</td>';
  780. if ($hideTime) {
  781. $timeRow = '';
  782. }
  783. $output .= '
  784. <td>'.$extend_link.'</td>
  785. <td colspan="4">' . $title . '</td>
  786. <td colspan="2">' . learnpathitem::humanize_status($lesson_status) .'</td>
  787. <td colspan="2">'.$scoreItem.'</td>
  788. '.$timeRow.'
  789. '.$action.'
  790. ';
  791. $output .= '</tr>';
  792. }
  793. if (!empty($export_csv)) {
  794. $temp = array();
  795. $temp[] = api_html_entity_decode($title, ENT_QUOTES);
  796. $temp[] = api_html_entity_decode($lesson_status, ENT_QUOTES);
  797. if ($row['item_type'] == 'quiz') {
  798. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  799. $temp[] = '/';
  800. } else {
  801. $temp[] = ($score == 0 ? '0/' . $maxscore : ($maxscore == 0 ? $score : $score . '/' . float_format($maxscore, 1)));
  802. }
  803. } else {
  804. $temp[] = ($score == 0 ? '/' : ($maxscore == 0 ? $score : $score . '/' . float_format($maxscore, 1)));
  805. }
  806. if ($hideTime === false) {
  807. $temp[] = $time;
  808. }
  809. $csv_content[] = $temp;
  810. }
  811. }
  812. $counter++;
  813. $action = null;
  814. if ($type == 'classic') {
  815. $action = '<td></td>';
  816. }
  817. if ($extend_this_attempt || $extend_all) {
  818. $list1 = learnpath::get_iv_interactions_array($row['iv_id']);
  819. foreach ($list1 as $id => $interaction) {
  820. if (($counter % 2) == 0) {
  821. $oddclass = 'row_odd';
  822. } else {
  823. $oddclass = 'row_even';
  824. }
  825. $timeRow = '<td class="lp_time">'.$interaction['time'].'</td>';
  826. if ($hideTime) {
  827. $timeRow = '';
  828. }
  829. $output .= '<tr class="'.$oddclass.'">
  830. <td></td>
  831. <td></td>
  832. <td></td>
  833. <td>'.$interaction['order_id'].'</td>
  834. <td>'.$interaction['id'].'</td>
  835. <td colspan="2">' . $interaction['type'].'</td>
  836. <td>'.urldecode($interaction['student_response']).'</td>
  837. <td>'.$interaction['result'].'</td>
  838. <td>'.$interaction['latency'].'</td>
  839. '.$timeRow.'
  840. '.$action.'
  841. </tr>';
  842. $counter++;
  843. }
  844. $list2 = learnpath::get_iv_objectives_array($row['iv_id']);
  845. foreach ($list2 as $id => $interaction) {
  846. if (($counter % 2) == 0) {
  847. $oddclass = 'row_odd';
  848. } else {
  849. $oddclass = 'row_even';
  850. }
  851. $output .= '<tr class="'.$oddclass.'">
  852. <td></td>
  853. <td></td>
  854. <td></td>
  855. <td>' . $interaction['order_id'] . '</td>
  856. <td colspan="2">'.$interaction['objective_id'] . '</td>
  857. <td colspan="2">' . $interaction['status'] . '</td>
  858. <td>' . $interaction['score_raw'].'</td>
  859. <td>' . $interaction['score_max'] .'</td>
  860. <td>' . $interaction['score_min'].'</td>
  861. '.$action.'
  862. </tr>';
  863. $counter++;
  864. }
  865. }
  866. // Attempts listing by exercise.
  867. if ($lp_id == $my_lp_id && $lp_item_id== $my_id && $extendedAttempt) {
  868. // Get attempts of a exercise.
  869. if (!empty($lp_id) &&
  870. !empty($lp_item_id) &&
  871. $row['item_type'] === 'quiz'
  872. ) {
  873. $sql = "SELECT path FROM $TBL_LP_ITEM
  874. WHERE
  875. c_id = $course_id AND
  876. id = '$lp_item_id' AND
  877. lp_id = '$lp_id'";
  878. $res_path = Database::query($sql);
  879. $row_path = Database::fetch_array($res_path);
  880. if (Database::num_rows($res_path) > 0) {
  881. $sql = 'SELECT * FROM ' . $tbl_stats_exercices . '
  882. WHERE
  883. exe_exo_id="' . (int) $row_path['path'] . '" AND
  884. status <> "incomplete" AND
  885. exe_user_id="' . $user_id . '" AND
  886. orig_lp_id = "' . (int) $lp_id . '" AND
  887. orig_lp_item_id = "' . (int) $lp_item_id . '" AND
  888. c_id = ' . $course_id . ' AND
  889. session_id = ' . $session_id . '
  890. ORDER BY exe_date';
  891. $res_attempts = Database::query($sql);
  892. $num_attempts = Database::num_rows($res_attempts);
  893. if ($num_attempts > 0) {
  894. $n = 1;
  895. while ($row_attempts = Database::fetch_array($res_attempts)) {
  896. $my_score = $row_attempts['exe_result'];
  897. $my_maxscore = $row_attempts['exe_weighting'];
  898. $my_exe_id = $row_attempts['exe_id'];
  899. $my_orig_lp = $row_attempts['orig_lp_id'];
  900. $my_orig_lp_item = $row_attempts['orig_lp_item_id'];
  901. $my_exo_exe_id = $row_attempts['exe_exo_id'];
  902. $mktime_start_date = api_strtotime($row_attempts['start_date'], 'UTC');
  903. $mktime_exe_date = api_strtotime($row_attempts['exe_date'], 'UTC');
  904. if ($mktime_start_date && $mktime_exe_date) {
  905. $mytime = ((int) $mktime_exe_date - (int) $mktime_start_date);
  906. $time_attemp = learnpathItem::getScormTimeFromParameter('js', $mytime);
  907. $time_attemp = str_replace('NaN', '00' . $h . '00\'00"', $time_attemp);
  908. } else {
  909. $time_attemp = ' - ';
  910. }
  911. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  912. $view_score = Display::return_icon('invisible.gif', get_lang('ResultsHiddenByExerciseSetting'));
  913. } else {
  914. // Show only float when need it
  915. if ($my_score == 0) {
  916. $view_score = ExerciseLib::show_score(0, $my_maxscore, false);
  917. } else {
  918. if ($my_maxscore == 0) {
  919. $view_score = $my_score;
  920. } else {
  921. $view_score = ExerciseLib::show_score($my_score, $my_maxscore, false);
  922. }
  923. }
  924. }
  925. $my_lesson_status = $row_attempts['status'];
  926. if ($my_lesson_status == '') {
  927. $my_lesson_status = learnpathitem::humanize_status('completed');
  928. } elseif ($my_lesson_status == 'incomplete') {
  929. $my_lesson_status = learnpathitem::humanize_status('incomplete');
  930. }
  931. $timeRow = '<td class="lp_time" colspan="2">'.$time_attemp . '</td>';
  932. if ($hideTime) {
  933. $timeRow = '';
  934. }
  935. $output .= '<tr class="' . $oddclass . '" >
  936. <td></td>
  937. <td>' . $extend_attempt_link . '</td>
  938. <td colspan="3">' . get_lang('Attempt').' '. $n.'</td>
  939. <td colspan="2">' . $my_lesson_status . '</td>
  940. <td colspan="2">'.$view_score . '</td>
  941. '.$timeRow;
  942. if ($action == 'classic') {
  943. if ($origin != 'tracking') {
  944. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  945. $output .= '<td>
  946. <img src="' . Display::returnIconPath('quiz_na.gif').'" alt="' . get_lang('ShowAttempt') . '" title="' . get_lang('ShowAttempt') . '">
  947. </td>';
  948. } else {
  949. $output .= '<td>
  950. <a href="../exercise/exercise_show.php?origin=' . $origin . '&id=' . $my_exe_id . '&cidReq=' . $courseCode . '" target="_parent">
  951. <img src="' . Display::returnIconPath('quiz.png').'" alt="' . get_lang('ShowAttempt') . '" title="' . get_lang('ShowAttempt') . '">
  952. </a></td>';
  953. }
  954. } else {
  955. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  956. $output .= '<td>
  957. <img src="' . Display::returnIconPath('quiz_na.gif').'" alt="' . get_lang('ShowAndQualifyAttempt') . '" title="' . get_lang('ShowAndQualifyAttempt') . '"></td>';
  958. } else {
  959. $output .= '<td>
  960. <a href="../exercise/exercise_show.php?cidReq=' . $courseCode . '&origin=correct_exercise_in_lp&id=' . $my_exe_id . '" target="_parent">
  961. <img src="' . Display::returnIconPath('quiz.gif').'" alt="' . get_lang('ShowAndQualifyAttempt') . '" title="' . get_lang('ShowAndQualifyAttempt') . '"></a></td>';
  962. }
  963. }
  964. }
  965. $output .= '</tr>';
  966. $n++;
  967. }
  968. }
  969. $output .= '<tr><td colspan="12">&nbsp;</td></tr>';
  970. }
  971. }
  972. }
  973. }
  974. $total_time += $time_for_total;
  975. // QUIZZ IN LP
  976. $a_my_id = array();
  977. if (!empty($my_lp_id)) {
  978. $a_my_id[] = $my_lp_id;
  979. }
  980. }
  981. }
  982. // NOT Extend all "left green cross"
  983. if (!empty($a_my_id)) {
  984. if ($extendedAttempt) {
  985. // "Right green cross" extended
  986. $total_score = self::get_avg_student_score(
  987. $user_id,
  988. $course_id,
  989. $a_my_id,
  990. $session_id,
  991. false,
  992. false
  993. );
  994. } else {
  995. // "Left green cross" extended
  996. $total_score = self::get_avg_student_score(
  997. $user_id,
  998. $course_id,
  999. $a_my_id,
  1000. $session_id,
  1001. false,
  1002. true
  1003. );
  1004. }
  1005. } else {
  1006. // Extend all "left green cross"
  1007. $total_score = self::get_avg_student_score(
  1008. $user_id,
  1009. $course_id,
  1010. array($lp_id),
  1011. $session_id,
  1012. false,
  1013. false
  1014. );
  1015. }
  1016. $total_time = learnpathItem::getScormTimeFromParameter('js', $total_time);
  1017. $total_time = str_replace('NaN', '00' . $h . '00\'00"', $total_time);
  1018. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  1019. $final_score = Display::return_icon('invisible.gif', get_lang('ResultsHiddenByExerciseSetting'));
  1020. } else {
  1021. if (is_numeric($total_score)) {
  1022. $final_score = $total_score . '%';
  1023. } else {
  1024. $final_score = $total_score;
  1025. }
  1026. }
  1027. $progress = learnpath::getProgress($lp_id, $user_id, $course_id, $session_id);
  1028. if (($counter % 2) == 0) {
  1029. $oddclass = 'row_odd';
  1030. } else {
  1031. $oddclass = 'row_even';
  1032. }
  1033. $action = null;
  1034. if ($type == 'classic') {
  1035. $action = '<td></td>';
  1036. }
  1037. $timeTotal = '<td class="lp_time" colspan="2">'.$total_time.'</div>';
  1038. if ($hideTime) {
  1039. $timeTotal = '';
  1040. }
  1041. $output .= '<tr class="'.$oddclass.'">
  1042. <td></td>
  1043. <td colspan="4">
  1044. <i>' . get_lang('AccomplishedStepsTotal') .'</i>
  1045. </td>
  1046. <td colspan="2">'.$progress.'%</td>
  1047. <td colspan="2">' . $final_score.'</td>
  1048. '.$timeTotal.'
  1049. '.$action.'
  1050. </tr>';
  1051. $output .= '
  1052. </tbody>
  1053. </table>
  1054. </div>
  1055. ';
  1056. if (!empty($export_csv)) {
  1057. $temp = array(
  1058. '',
  1059. '',
  1060. '',
  1061. ''
  1062. );
  1063. $csv_content[] = $temp;
  1064. $temp = array(
  1065. get_lang('AccomplishedStepsTotal'),
  1066. '',
  1067. $final_score
  1068. );
  1069. if ($hideTime === false) {
  1070. $temp[] = $total_time;
  1071. }
  1072. $csv_content[] = $temp;
  1073. ob_end_clean();
  1074. Export::arrayToCsv($csv_content, 'reporting_learning_path_details');
  1075. exit;
  1076. }
  1077. return $output;
  1078. }
  1079. /**
  1080. * @param int $userId
  1081. *
  1082. * @return array
  1083. */
  1084. public static function getStats($userId)
  1085. {
  1086. $courses = array();
  1087. $assignedCourses = array();
  1088. if (api_is_drh() && api_drh_can_access_all_session_content()) {
  1089. $studentList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1090. 'drh_all',
  1091. $userId,
  1092. false,
  1093. null,
  1094. null,
  1095. null,
  1096. null,
  1097. null,
  1098. null,
  1099. null,
  1100. array(),
  1101. array(),
  1102. STUDENT
  1103. );
  1104. $students = array();
  1105. if (is_array($studentList)) {
  1106. foreach ($studentList as $studentData) {
  1107. $students[] = $studentData['user_id'];
  1108. }
  1109. }
  1110. $studentBossesList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1111. 'drh_all',
  1112. $userId,
  1113. false,
  1114. null,
  1115. null,
  1116. null,
  1117. null,
  1118. null,
  1119. null,
  1120. null,
  1121. array(),
  1122. array(),
  1123. STUDENT_BOSS
  1124. );
  1125. $studentBosses = array();
  1126. if (is_array($studentBossesList)) {
  1127. foreach ($studentBossesList as $studentBossData) {
  1128. $studentBosses[] = $studentBossData['user_id'];
  1129. }
  1130. }
  1131. $teacherList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1132. 'drh_all',
  1133. $userId,
  1134. false,
  1135. null,
  1136. null,
  1137. null,
  1138. null,
  1139. null,
  1140. null,
  1141. null,
  1142. array(),
  1143. array(),
  1144. COURSEMANAGER
  1145. );
  1146. $teachers = array();
  1147. foreach ($teacherList as $teacherData) {
  1148. $teachers[] = $teacherData['user_id'];
  1149. }
  1150. $humanResources = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1151. 'drh_all',
  1152. $userId,
  1153. false,
  1154. null,
  1155. null,
  1156. null,
  1157. null,
  1158. null,
  1159. null,
  1160. null,
  1161. array(),
  1162. array(),
  1163. DRH
  1164. );
  1165. $humanResourcesList = array();
  1166. if (is_array($humanResources)) {
  1167. foreach ($humanResources as $item) {
  1168. $humanResourcesList[] = $item['user_id'];
  1169. }
  1170. }
  1171. $platformCourses = SessionManager::getAllCoursesFollowedByUser(
  1172. $userId,
  1173. null,
  1174. null,
  1175. null,
  1176. null,
  1177. null
  1178. );
  1179. foreach ($platformCourses as $course) {
  1180. $courses[$course['code']] = $course['code'];
  1181. }
  1182. $sessions = SessionManager::get_sessions_followed_by_drh($userId);
  1183. } else {
  1184. $studentList = UserManager::getUsersFollowedByUser(
  1185. $userId,
  1186. STUDENT,
  1187. false,
  1188. false,
  1189. false,
  1190. null,
  1191. null,
  1192. null,
  1193. null,
  1194. null,
  1195. null,
  1196. COURSEMANAGER
  1197. );
  1198. $students = array();
  1199. foreach ($studentList as $studentData) {
  1200. $students[] = $studentData['user_id'];
  1201. }
  1202. $studentBossesList = UserManager::getUsersFollowedByUser(
  1203. $userId,
  1204. STUDENT_BOSS,
  1205. false,
  1206. false,
  1207. false,
  1208. null,
  1209. null,
  1210. null,
  1211. null,
  1212. null,
  1213. null,
  1214. COURSEMANAGER
  1215. );
  1216. $studentBosses = array();
  1217. foreach ($studentBossesList as $studentBossData) {
  1218. $studentBosses[] = $studentBossData['user_id'];
  1219. }
  1220. $teacherList = UserManager::getUsersFollowedByUser(
  1221. $userId,
  1222. COURSEMANAGER,
  1223. false,
  1224. false,
  1225. false,
  1226. null,
  1227. null,
  1228. null,
  1229. null,
  1230. null,
  1231. null,
  1232. COURSEMANAGER
  1233. );
  1234. $teachers = array();
  1235. foreach ($teacherList as $teacherData) {
  1236. $teachers[] = $teacherData['user_id'];
  1237. }
  1238. $humanResources = UserManager::getUsersFollowedByUser(
  1239. $userId,
  1240. DRH,
  1241. false,
  1242. false,
  1243. false,
  1244. null,
  1245. null,
  1246. null,
  1247. null,
  1248. null,
  1249. null,
  1250. COURSEMANAGER
  1251. );
  1252. $humanResourcesList = array();
  1253. foreach ($humanResources as $item) {
  1254. $humanResourcesList[] = $item['user_id'];
  1255. }
  1256. $platformCourses = CourseManager::getCoursesFollowedByUser(
  1257. $userId,
  1258. COURSEMANAGER,
  1259. null,
  1260. null,
  1261. null,
  1262. null,
  1263. false,
  1264. null,
  1265. null,
  1266. true
  1267. );
  1268. foreach ($platformCourses as $course) {
  1269. $assignedCourses[$course['code']] = $course['code'];
  1270. }
  1271. $platformCourses = CourseManager::getCoursesFollowedByUser(
  1272. $userId,
  1273. COURSEMANAGER
  1274. );
  1275. foreach ($platformCourses as $course) {
  1276. $courses[$course['code']] = $course['code'];
  1277. }
  1278. $sessions = SessionManager::getSessionsFollowedByUser(
  1279. $userId,
  1280. COURSEMANAGER
  1281. );
  1282. }
  1283. return array(
  1284. 'drh' => $humanResourcesList,
  1285. 'teachers' => $teachers,
  1286. 'students' => $students,
  1287. 'studentBosses' => $studentBosses,
  1288. 'courses' => $courses,
  1289. 'sessions' => $sessions,
  1290. 'assignedCourses' => $assignedCourses
  1291. );
  1292. }
  1293. /**
  1294. * Calculates the time spent on the platform by a user
  1295. * @param int|array User id
  1296. * @param string $timeFilter type of time filter: 'last_week' or 'custom'
  1297. * @param string $start_date start date date('Y-m-d H:i:s')
  1298. * @param string $end_date end date date('Y-m-d H:i:s')
  1299. *
  1300. * @return int $nb_seconds
  1301. */
  1302. public static function get_time_spent_on_the_platform(
  1303. $userId,
  1304. $timeFilter = 'last_7_days',
  1305. $start_date = null,
  1306. $end_date = null
  1307. ) {
  1308. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1309. $condition_time = '';
  1310. if (is_array($userId)) {
  1311. $userList = array_map('intval', $userId);
  1312. $userCondition = " login_user_id IN ('".implode("','", $userList)."')";
  1313. } else {
  1314. $userCondition = " login_user_id = ".intval($userId);
  1315. }
  1316. if (empty($timeFilter)) {
  1317. $timeFilter = 'last_week';
  1318. }
  1319. $today = new DateTime('now', new DateTimeZone('UTC'));
  1320. switch ($timeFilter) {
  1321. case 'last_7_days':
  1322. $newDate = new DateTime('-7 day', new DateTimeZone('UTC'));
  1323. $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'";
  1324. $condition_time .= " AND logout_date <= '{$today->format('Y-m-d H:i:s')}') ";
  1325. break;
  1326. case 'last_30_days':
  1327. $newDate = new DateTime('-30 days', new DateTimeZone('UTC'));
  1328. $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'";
  1329. $condition_time .= "AND logout_date <= '{$today->format('Y-m-d H:i:s')}') ";
  1330. break;
  1331. case 'custom':
  1332. if (!empty($start_date) && !empty($end_date)) {
  1333. $start_date = Database::escape_string($start_date);
  1334. $end_date = Database::escape_string($end_date);
  1335. $condition_time = ' AND (login_date >= "'.$start_date.'" AND logout_date <= "'.$end_date.'" ) ';
  1336. }
  1337. break;
  1338. }
  1339. $sql = 'SELECT SUM(TIMESTAMPDIFF(SECOND, login_date, logout_date)) diff
  1340. FROM '.$tbl_track_login.'
  1341. WHERE '.$userCondition.$condition_time;
  1342. $rs = Database::query($sql);
  1343. $row = Database::fetch_array($rs, 'ASSOC');
  1344. $diff = $row['diff'];
  1345. if ($diff >= 0) {
  1346. return $diff;
  1347. } else {
  1348. return -1;
  1349. }
  1350. }
  1351. /**
  1352. * Calculates the time spent on the course
  1353. * @param integer $user_id
  1354. * @param integer $courseId
  1355. * @param int Session id (optional)
  1356. *
  1357. * @return int Time in seconds
  1358. */
  1359. public static function get_time_spent_on_the_course($user_id, $courseId, $session_id = 0)
  1360. {
  1361. $courseId = intval($courseId);
  1362. $session_id = intval($session_id);
  1363. $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1364. if (is_array($user_id)) {
  1365. $user_id = array_map('intval', $user_id);
  1366. $condition_user = " AND user_id IN (".implode(',', $user_id).") ";
  1367. } else {
  1368. $user_id = intval($user_id);
  1369. $condition_user = " AND user_id = $user_id ";
  1370. }
  1371. $sql = "SELECT
  1372. SUM(UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  1373. FROM $tbl_track_course
  1374. WHERE UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date) ";
  1375. if ($courseId != 0) {
  1376. $sql .= "AND c_id = '$courseId' ";
  1377. }
  1378. if ($session_id != -1) {
  1379. $sql .= "AND session_id = '$session_id' ";
  1380. }
  1381. $sql .= $condition_user;
  1382. $rs = Database::query($sql);
  1383. $row = Database::fetch_array($rs);
  1384. return $row['nb_seconds'];
  1385. }
  1386. /**
  1387. * Get first connection date for a student
  1388. * @param int $student_id
  1389. *
  1390. * @return string|bool Date format long without day or false if there are no connections
  1391. */
  1392. public static function get_first_connection_date($student_id)
  1393. {
  1394. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1395. $sql = 'SELECT login_date
  1396. FROM ' . $tbl_track_login . '
  1397. WHERE login_user_id = ' . intval($student_id) . '
  1398. ORDER BY login_date ASC
  1399. LIMIT 0,1';
  1400. $rs = Database::query($sql);
  1401. if (Database::num_rows($rs)>0) {
  1402. if ($first_login_date = Database::result($rs, 0, 0)) {
  1403. return api_convert_and_format_date(
  1404. $first_login_date,
  1405. DATE_FORMAT_SHORT,
  1406. date_default_timezone_get()
  1407. );
  1408. }
  1409. }
  1410. return false;
  1411. }
  1412. /**
  1413. * Get las connection date for a student
  1414. * @param int $student_id
  1415. * @param bool $warning_message Show a warning message (optional)
  1416. * @param bool $return_timestamp True for returning results in timestamp (optional)
  1417. * @return string|int|bool Date format long without day, false if there are no connections or
  1418. * timestamp if parameter $return_timestamp is true
  1419. */
  1420. public static function get_last_connection_date(
  1421. $student_id,
  1422. $warning_message = false,
  1423. $return_timestamp = false
  1424. ) {
  1425. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1426. $sql = 'SELECT login_date
  1427. FROM ' . $table . '
  1428. WHERE login_user_id = ' . intval($student_id) . '
  1429. ORDER BY login_date
  1430. DESC LIMIT 0,1';
  1431. $rs = Database::query($sql);
  1432. if (Database::num_rows($rs) > 0) {
  1433. if ($last_login_date = Database::result($rs, 0, 0)) {
  1434. $last_login_date = api_get_local_time($last_login_date);
  1435. if ($return_timestamp) {
  1436. return api_strtotime($last_login_date, 'UTC');
  1437. } else {
  1438. if (!$warning_message) {
  1439. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  1440. } else {
  1441. $timestamp = api_strtotime($last_login_date, 'UTC');
  1442. $currentTimestamp = time();
  1443. //If the last connection is > than 7 days, the text is red
  1444. //345600 = 7 days in seconds
  1445. if ($currentTimestamp - $timestamp > 604800) {
  1446. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . '</span>';
  1447. } else {
  1448. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  1449. }
  1450. }
  1451. }
  1452. }
  1453. }
  1454. return false;
  1455. }
  1456. /**
  1457. * Get las connection date for a student
  1458. * @param array $studentList Student id array
  1459. * @param int $days
  1460. * @param bool $getCount
  1461. * @return int
  1462. */
  1463. public static function getInactiveUsers($studentList, $days, $getCount = true)
  1464. {
  1465. if (empty($studentList)) {
  1466. return 0;
  1467. }
  1468. $days = intval($days);
  1469. $date = api_get_utc_datetime(strtotime($days.' days ago'));
  1470. $studentList = array_map('intval', $studentList);
  1471. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1472. $select = " SELECT login_user_id ";
  1473. if ($getCount) {
  1474. $select = " SELECT count(DISTINCT login_user_id) as count";
  1475. }
  1476. $sql = "$select
  1477. FROM $tbl_track_login
  1478. WHERE
  1479. login_user_id IN (' ". implode("','", $studentList) . "' ) AND
  1480. login_date < '$date'
  1481. ";
  1482. $rs = Database::query($sql);
  1483. if (Database::num_rows($rs) > 0) {
  1484. if ($getCount) {
  1485. $count = Database::fetch_array($rs);
  1486. return $count['count'];
  1487. }
  1488. return Database::store_result($rs, 'ASSOC');
  1489. }
  1490. return false;
  1491. }
  1492. /**
  1493. * Get first user's connection date on the course
  1494. * @param int User id
  1495. * @param int $courseId
  1496. * @param int Session id (optional, default=0)
  1497. * @return string|bool Date with format long without day or false if there is no date
  1498. */
  1499. public static function get_first_connection_date_on_the_course(
  1500. $student_id,
  1501. $courseId,
  1502. $session_id = 0,
  1503. $convert_date = true
  1504. ) {
  1505. $student_id = intval($student_id);
  1506. $courseId = intval($courseId);
  1507. $session_id = intval($session_id);
  1508. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1509. $sql = 'SELECT login_course_date
  1510. FROM '.$tbl_track_login.'
  1511. WHERE
  1512. user_id = '.$student_id.' AND
  1513. c_id = '.$courseId.' AND
  1514. session_id = '.$session_id.'
  1515. ORDER BY login_course_date ASC
  1516. LIMIT 0,1';
  1517. $rs = Database::query($sql);
  1518. if (Database::num_rows($rs) > 0) {
  1519. if ($first_login_date = Database::result($rs, 0, 0)) {
  1520. if ($convert_date) {
  1521. return api_convert_and_format_date(
  1522. $first_login_date,
  1523. DATE_FORMAT_SHORT
  1524. );
  1525. } else {
  1526. return $first_login_date;
  1527. }
  1528. }
  1529. }
  1530. return false;
  1531. }
  1532. /**
  1533. * Get last user's connection date on the course
  1534. * @param int User id
  1535. * @param array $courseInfo real_id and code are used
  1536. * @param int Session id (optional, default=0)
  1537. * @return string|bool Date with format long without day or false if there is no date
  1538. */
  1539. public static function get_last_connection_date_on_the_course(
  1540. $student_id,
  1541. $courseInfo,
  1542. $session_id = 0,
  1543. $convert_date = true
  1544. ) {
  1545. // protect data
  1546. $student_id = intval($student_id);
  1547. $courseId = $courseInfo['real_id'];
  1548. $session_id = intval($session_id);
  1549. $tbl_track_e_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1550. $sql = 'SELECT access_date
  1551. FROM '.$tbl_track_e_access.'
  1552. WHERE access_user_id = '.$student_id.' AND
  1553. c_id = "'.$courseId.'" AND
  1554. access_session_id = '.$session_id.'
  1555. ORDER BY access_date DESC
  1556. LIMIT 0,1';
  1557. $rs = Database::query($sql);
  1558. if (Database::num_rows($rs) > 0) {
  1559. if ($last_login_date = Database::result($rs, 0, 0)) {
  1560. if (empty($last_login_date)) {
  1561. return false;
  1562. }
  1563. //see #5736
  1564. $last_login_date_timestamp = api_strtotime($last_login_date);
  1565. $now = time();
  1566. //If the last connection is > than 7 days, the text is red
  1567. //345600 = 7 days in seconds
  1568. if ($now - $last_login_date_timestamp > 604800) {
  1569. if ($convert_date) {
  1570. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT);
  1571. $icon = api_is_allowed_to_edit() ?
  1572. '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?action=add&remind_inactive='.$student_id.'&cidReq='.$courseInfo['code'].'" title="'.get_lang('RemindInactiveUser').'">
  1573. '.Display::return_icon('messagebox_warning.gif').'
  1574. </a>'
  1575. : null;
  1576. return $icon. Display::label($last_login_date, 'warning');
  1577. } else {
  1578. return $last_login_date;
  1579. }
  1580. } else {
  1581. if ($convert_date) {
  1582. return api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT);
  1583. } else {
  1584. return $last_login_date;
  1585. }
  1586. }
  1587. }
  1588. }
  1589. return false;
  1590. }
  1591. /**
  1592. * Get count of the connections to the course during a specified period
  1593. * @param int $courseId
  1594. * @param int Session id (optional)
  1595. * @param int Datetime from which to collect data (defaults to 0)
  1596. * @param int Datetime to which to collect data (defaults to now)
  1597. * @return int count connections
  1598. */
  1599. public static function get_course_connections_count($courseId, $session_id = 0, $start = 0, $stop = null)
  1600. {
  1601. if ($start < 0) {
  1602. $start = 0;
  1603. }
  1604. if (!isset($stop) or ($stop < 0)) {
  1605. $stop = api_get_utc_datetime();
  1606. }
  1607. // Given we're storing in cache, round the start and end times
  1608. // to the lower minute
  1609. $roundedStart = substr($start, 0, -2) . '00';
  1610. $roundedStop = substr($stop, 0, -2) . '00';
  1611. $roundedStart = Database::escape_string($roundedStart);
  1612. $roundedStop = Database::escape_string($roundedStop);
  1613. $month_filter = " AND login_course_date > '$roundedStart' AND login_course_date < '$roundedStop' ";
  1614. $courseId = intval($courseId);
  1615. $session_id = intval($session_id);
  1616. $count = 0;
  1617. $tbl_track_e_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1618. $sql = "SELECT count(*) as count_connections
  1619. FROM $tbl_track_e_course_access
  1620. WHERE
  1621. c_id = $courseId AND
  1622. session_id = $session_id
  1623. $month_filter";
  1624. //This query can be very slow (several seconds on an indexed table
  1625. // with 14M rows). As such, we'll try to use APCu if it is
  1626. // available to store the resulting value for a few seconds
  1627. $cacheAvailable = api_get_configuration_value('apc');
  1628. if ($cacheAvailable === true) {
  1629. $apc = apcu_cache_info(true);
  1630. $apc_end = $apc['start_time'] + $apc['ttl'];
  1631. $apc_var = api_get_configuration_value('apc_prefix') . 'course_access_' . $courseId . '_' . $session_id . '_' . strtotime($roundedStart) . '_' . strtotime($roundedStop);
  1632. if (apcu_exists($apc_var) && (time() < $apc_end) &&
  1633. apcu_fetch($apc_var) > 0
  1634. ) {
  1635. $count = apcu_fetch($apc_var);
  1636. } else {
  1637. $rs = Database::query($sql);
  1638. if (Database::num_rows($rs)>0) {
  1639. $row = Database::fetch_object($rs);
  1640. $count = $row->count_connections;
  1641. }
  1642. apcu_clear_cache();
  1643. apcu_store($apc_var, $count, 60);
  1644. }
  1645. } else {
  1646. $rs = Database::query($sql);
  1647. if (Database::num_rows($rs)>0) {
  1648. $row = Database::fetch_object($rs);
  1649. $count = $row->count_connections;
  1650. }
  1651. }
  1652. return $count;
  1653. }
  1654. /**
  1655. * Get count courses per student
  1656. * @param int Student id
  1657. * @param bool Include sessions (optional)
  1658. * @return int count courses
  1659. */
  1660. public static function count_course_per_student($user_id, $include_sessions = true)
  1661. {
  1662. $user_id = intval($user_id);
  1663. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1664. $tbl_session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1665. $sql = 'SELECT DISTINCT c_id
  1666. FROM ' . $tbl_course_rel_user . '
  1667. WHERE user_id = ' . $user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH;
  1668. $rs = Database::query($sql);
  1669. $nb_courses = Database::num_rows($rs);
  1670. if ($include_sessions) {
  1671. $sql = 'SELECT DISTINCT c_id
  1672. FROM ' . $tbl_session_course_rel_user . '
  1673. WHERE user_id = ' . $user_id;
  1674. $rs = Database::query($sql);
  1675. $nb_courses += Database::num_rows($rs);
  1676. }
  1677. return $nb_courses;
  1678. }
  1679. /**
  1680. * Gets the score average from all tests in a course by student
  1681. *
  1682. * @param $student_id
  1683. * @param $course_code
  1684. * @param int $exercise_id
  1685. * @param null $session_id
  1686. * @param int $active_filter 2 for consider all tests
  1687. * 1 for active <> -1
  1688. * 0 for active <> 0
  1689. * @param int $into_lp 1 for all exercises
  1690. * 0 for without LP
  1691. * @internal param \Student $mixed id
  1692. * @internal param \Course $string code
  1693. * @internal param \Exercise $int id (optional), filtered by exercise
  1694. * @internal param \Session $int id (optional), if param $session_id is null
  1695. * it'll return results including sessions, 0 = session is not filtered
  1696. * @return string value (number %) Which represents a round integer about the score average.
  1697. */
  1698. public static function get_avg_student_exercise_score(
  1699. $student_id,
  1700. $course_code,
  1701. $exercise_id = 0,
  1702. $session_id = null,
  1703. $active_filter = 1,
  1704. $into_lp = 0
  1705. ) {
  1706. $course_code = Database::escape_string($course_code);
  1707. $course_info = api_get_course_info($course_code);
  1708. if (!empty($course_info)) {
  1709. // table definition
  1710. $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  1711. $tbl_stats_exercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1712. // Compose a filter based on optional exercise given
  1713. $condition_quiz = "";
  1714. if (!empty($exercise_id)) {
  1715. $exercise_id = intval($exercise_id);
  1716. $condition_quiz =" AND id = $exercise_id ";
  1717. }
  1718. // Compose a filter based on optional session id given
  1719. $condition_session = '';
  1720. if (isset($session_id)) {
  1721. $session_id = intval($session_id);
  1722. $condition_session = " AND session_id = $session_id ";
  1723. }
  1724. if ($active_filter == 1) {
  1725. $condition_active = 'AND active <> -1';
  1726. } elseif ($active_filter == 0) {
  1727. $condition_active = 'AND active <> 0';
  1728. } else {
  1729. $condition_active = '';
  1730. }
  1731. $condition_into_lp = '';
  1732. $select_lp_id = '';
  1733. if ($into_lp == 0) {
  1734. $condition_into_lp = 'AND orig_lp_id = 0 AND orig_lp_item_id = 0';
  1735. } else {
  1736. $select_lp_id = ', orig_lp_id as lp_id ';
  1737. }
  1738. $sql = "SELECT count(id) FROM $tbl_course_quiz
  1739. WHERE c_id = {$course_info['real_id']} $condition_active $condition_quiz ";
  1740. $count_quiz = 0;
  1741. $countQuizResult = Database::query($sql);
  1742. if (!empty($countQuizResult)) {
  1743. $count_quiz = Database::fetch_row($countQuizResult);
  1744. }
  1745. if (!empty($count_quiz[0]) && !empty($student_id)) {
  1746. if (is_array($student_id)) {
  1747. $student_id = array_map('intval', $student_id);
  1748. $condition_user = " AND exe_user_id IN (".implode(',', $student_id).") ";
  1749. } else {
  1750. $student_id = intval($student_id);
  1751. $condition_user = " AND exe_user_id = '$student_id' ";
  1752. }
  1753. if (empty($exercise_id)) {
  1754. $sql = "SELECT id FROM $tbl_course_quiz
  1755. WHERE c_id = {$course_info['real_id']} $condition_active $condition_quiz";
  1756. $result = Database::query($sql);
  1757. $exercise_list = array();
  1758. $exercise_id = null;
  1759. if (!empty($result) && Database::num_rows($result)) {
  1760. while ($row = Database::fetch_array($result)) {
  1761. $exercise_list[] = $row['id'];
  1762. }
  1763. }
  1764. if (!empty($exercise_list)) {
  1765. $exercise_id = implode("','",$exercise_list);
  1766. }
  1767. }
  1768. $count_quiz = Database::fetch_row(Database::query($sql));
  1769. $sql = "SELECT
  1770. SUM(exe_result/exe_weighting*100) as avg_score,
  1771. COUNT(*) as num_attempts
  1772. $select_lp_id
  1773. FROM $tbl_stats_exercise
  1774. WHERE
  1775. exe_exo_id IN ('".$exercise_id."')
  1776. $condition_user AND
  1777. status = '' AND
  1778. c_id = {$course_info['real_id']}
  1779. $condition_session
  1780. $condition_into_lp
  1781. ORDER BY exe_date DESC";
  1782. $res = Database::query($sql);
  1783. $row = Database::fetch_array($res);
  1784. $quiz_avg_score = null;
  1785. if (!empty($row['avg_score'])) {
  1786. $quiz_avg_score = round($row['avg_score'],2);
  1787. }
  1788. if(!empty($row['num_attempts'])) {
  1789. $quiz_avg_score = round($quiz_avg_score / $row['num_attempts'], 2);
  1790. }
  1791. if (is_array($student_id)) {
  1792. $quiz_avg_score = round($quiz_avg_score / count($student_id), 2);
  1793. }
  1794. if ($into_lp == 0) {
  1795. return $quiz_avg_score;
  1796. } else {
  1797. if (!empty($row['lp_id'])) {
  1798. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  1799. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1800. $sql = "SELECT lp.name
  1801. FROM $tbl_lp as lp, $tbl_course as c
  1802. WHERE
  1803. c.code = '$course_code' AND
  1804. lp.id = ".$row['lp_id']." AND
  1805. lp.c_id = c.id
  1806. LIMIT 1;
  1807. ";
  1808. $result = Database::query($sql);
  1809. $row_lp = Database::fetch_row($result);
  1810. $lp_name = $row_lp[0];
  1811. return array($quiz_avg_score, $lp_name);
  1812. } else {
  1813. return array($quiz_avg_score, null);
  1814. }
  1815. }
  1816. }
  1817. }
  1818. return null;
  1819. }
  1820. /**
  1821. * Get count student's exercise COMPLETED attempts
  1822. * @param int $student_id
  1823. * @param int $courseId
  1824. * @param int $exercise_id
  1825. * @param int $lp_id
  1826. * @param int $lp_item_id
  1827. * @param int $session_id
  1828. * @param int $find_all_lp 0 = just LP specified
  1829. * 1 = LP specified or whitout LP,
  1830. * 2 = all rows
  1831. * @internal param \Student $int id
  1832. * @internal param \Course $string code
  1833. * @internal param \Exercise $int id
  1834. * @internal param \Learning $int path id (optional),
  1835. * for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  1836. * @internal param \Learning $int path item id (optional),
  1837. * for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  1838. * @return int count of attempts
  1839. */
  1840. public static function count_student_exercise_attempts(
  1841. $student_id,
  1842. $courseId,
  1843. $exercise_id,
  1844. $lp_id = 0,
  1845. $lp_item_id = 0,
  1846. $session_id = 0,
  1847. $find_all_lp = 0
  1848. ) {
  1849. $courseId = intval($courseId);
  1850. $student_id = intval($student_id);
  1851. $exercise_id = intval($exercise_id);
  1852. $session_id = intval($session_id);
  1853. $lp_id = intval($lp_id);
  1854. $lp_item_id = intval($lp_item_id);
  1855. $tbl_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1856. $sql = "SELECT COUNT(ex.exe_id) as essais FROM $tbl_stats_exercises AS ex
  1857. WHERE ex.c_id = $courseId
  1858. AND ex.exe_exo_id = $exercise_id
  1859. AND status = ''
  1860. AND exe_user_id= $student_id
  1861. AND session_id = $session_id ";
  1862. if ($find_all_lp == 1) {
  1863. $sql .= "AND (orig_lp_id = $lp_id OR orig_lp_id = 0)
  1864. AND (orig_lp_item_id = $lp_item_id OR orig_lp_item_id = 0)";
  1865. } elseif ($find_all_lp == 0) {
  1866. $sql .= "AND orig_lp_id = $lp_id
  1867. AND orig_lp_item_id = $lp_item_id";
  1868. }
  1869. $rs = Database::query($sql);
  1870. $row = Database::fetch_row($rs);
  1871. $count_attempts = $row[0];
  1872. return $count_attempts;
  1873. }
  1874. /**
  1875. * Get count student's exercise progress
  1876. *
  1877. * @param array $exercise_list
  1878. * @param int $user_id
  1879. * @param int $courseId
  1880. * @param int $session_id
  1881. */
  1882. public static function get_exercise_student_progress($exercise_list, $user_id, $courseId, $session_id)
  1883. {
  1884. $courseId = intval($courseId);
  1885. $user_id = intval($user_id);
  1886. $session_id = intval($session_id);
  1887. if (empty($exercise_list)) {
  1888. return '0%';
  1889. }
  1890. $tbl_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1891. $exercise_list = array_keys($exercise_list);
  1892. $exercise_list = array_map('intval', $exercise_list);
  1893. $exercise_list_imploded = implode("' ,'", $exercise_list);
  1894. $sql = "SELECT COUNT(DISTINCT ex.exe_exo_id)
  1895. FROM $tbl_stats_exercises AS ex
  1896. WHERE
  1897. ex.c_id = $courseId AND
  1898. ex.session_id = $session_id AND
  1899. ex.exe_user_id = $user_id AND
  1900. ex.exe_exo_id IN ('$exercise_list_imploded') ";
  1901. $rs = Database::query($sql);
  1902. $count = 0;
  1903. if ($rs) {
  1904. $row = Database::fetch_row($rs);
  1905. $count = $row[0];
  1906. }
  1907. $count = ($count != 0 ) ? 100*round(intval($count)/count($exercise_list), 2) .'%' : '0%';
  1908. return $count;
  1909. }
  1910. /**
  1911. * @param array $exercise_list
  1912. * @param int $user_id
  1913. * @param int $courseId
  1914. * @param int $session_id
  1915. * @return string
  1916. */
  1917. public static function get_exercise_student_average_best_attempt($exercise_list, $user_id, $courseId, $session_id)
  1918. {
  1919. $result = 0;
  1920. if (!empty($exercise_list)) {
  1921. foreach ($exercise_list as $exercise_data) {
  1922. $exercise_id = $exercise_data['id'];
  1923. $best_attempt = Event::get_best_attempt_exercise_results_per_user(
  1924. $user_id,
  1925. $exercise_id,
  1926. $courseId,
  1927. $session_id
  1928. );
  1929. if (!empty($best_attempt) && !empty($best_attempt['exe_weighting'])) {
  1930. $result += $best_attempt['exe_result']/$best_attempt['exe_weighting'];
  1931. }
  1932. }
  1933. $result = $result / count($exercise_list);
  1934. $result = round($result, 2) * 100;
  1935. }
  1936. return $result.'%';
  1937. }
  1938. /**
  1939. * get teacher progress by course and session
  1940. * @param int course id
  1941. * @param int session id
  1942. * @return array
  1943. */
  1944. static function get_teachers_progress_by_course($courseId, $sessionId)
  1945. {
  1946. $course = api_get_course_info_by_id($courseId);
  1947. $sessionId = intval($sessionId);
  1948. $courseId = intval($courseId);
  1949. $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1950. $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
  1951. //get teachers
  1952. $sql = "SELECT scu.session_id, scu.user_id, s.name
  1953. FROM $sessionCourseUserTable scu, $sessionTable s
  1954. WHERE
  1955. scu.session_id = s.id
  1956. AND scu.status = 2
  1957. AND scu.visibility = 1
  1958. AND scu.c_id = '%s'
  1959. AND scu.session_id = %s";
  1960. $query = sprintf($sql, intval($courseId), $sessionId);
  1961. $rs = Database::query($query);
  1962. $teachers = array();
  1963. while ($teacher = Database::fetch_array($rs,'ASSOC')) {
  1964. $teachers[] = $teacher;
  1965. }
  1966. $data = array();
  1967. foreach ($teachers as $teacher) {
  1968. //total documents added
  1969. $sql = "SELECT count(*) as total
  1970. FROM c_item_property
  1971. WHERE lastedit_type = 'DocumentAdded'
  1972. AND c_id = %s
  1973. AND insert_user_id = %s
  1974. AND session_id = %s";
  1975. $query = sprintf($sql,
  1976. $courseId,
  1977. $teacher['user_id'],
  1978. $teacher['session_id']
  1979. );
  1980. $rs = Database::query($query);
  1981. $totalDocuments = 0;
  1982. if ($rs) {
  1983. $row = Database::fetch_row($rs);
  1984. $totalDocuments = $row[0];
  1985. }
  1986. //total links added
  1987. $sql = "SELECT count(*) as total
  1988. FROM c_item_property
  1989. WHERE lastedit_type = 'LinkAdded'
  1990. AND c_id = %s
  1991. AND insert_user_id = %s
  1992. AND session_id = %s";
  1993. $query = sprintf($sql,
  1994. $courseId,
  1995. $teacher['user_id'],
  1996. $teacher['session_id']
  1997. );
  1998. $rs = Database::query($query);
  1999. $totalLinks = 0;
  2000. if ($rs) {
  2001. $row = Database::fetch_row($rs);
  2002. $totalLinks = $row[0];
  2003. }
  2004. //total forums added
  2005. $sql = "SELECT count(*) as total
  2006. FROM c_item_property
  2007. WHERE lastedit_type = 'ForumthreadVisible'
  2008. AND c_id = %s
  2009. AND insert_user_id = %s
  2010. AND session_id = %s";
  2011. $query = sprintf($sql,
  2012. $courseId,
  2013. $teacher['user_id'],
  2014. $teacher['session_id']
  2015. );
  2016. $rs = Database::query($query);
  2017. $totalForums = 0;
  2018. if ($rs) {
  2019. $row = Database::fetch_row($rs);
  2020. $totalForums = $row[0];
  2021. }
  2022. //total wikis added
  2023. $sql = "SELECT COUNT(DISTINCT(ref)) as total
  2024. FROM c_item_property
  2025. WHERE lastedit_type = 'WikiAdded'
  2026. AND c_id = %s
  2027. AND insert_user_id = %s
  2028. AND session_id = %s";
  2029. $query = sprintf($sql,
  2030. $courseId,
  2031. $teacher['user_id'],
  2032. $teacher['session_id']
  2033. );
  2034. $rs = Database::query($query);
  2035. $totalWikis = 0;
  2036. if ($rs) {
  2037. $row = Database::fetch_row($rs);
  2038. $totalWikis = $row[0];
  2039. }
  2040. //total works added
  2041. $sql = "SELECT COUNT(*) as total
  2042. FROM c_item_property
  2043. WHERE lastedit_type = 'DirectoryCreated'
  2044. AND tool = 'work'
  2045. AND c_id = %s
  2046. AND insert_user_id = %s
  2047. AND session_id = %s";
  2048. $query = sprintf($sql,
  2049. $courseId,
  2050. $teacher['user_id'],
  2051. $teacher['session_id']
  2052. );
  2053. $rs = Database::query($query);
  2054. $totalWorks = 0;
  2055. if ($rs) {
  2056. $row = Database::fetch_row($rs);
  2057. $totalWorks = $row[0];
  2058. }
  2059. //total announcements added
  2060. $sql = "SELECT COUNT(*) as total
  2061. FROM c_item_property
  2062. WHERE lastedit_type = 'AnnouncementAdded'
  2063. AND c_id = %s
  2064. AND insert_user_id = %s
  2065. AND session_id = %s";
  2066. $query = sprintf($sql,
  2067. $courseId,
  2068. $teacher['user_id'],
  2069. $teacher['session_id']
  2070. );
  2071. $rs = Database::query($query);
  2072. $totalAnnouncements = 0;
  2073. if ($rs) {
  2074. $row = Database::fetch_row($rs);
  2075. $totalAnnouncements = $row[0];
  2076. }
  2077. $tutor = api_get_user_info($teacher['user_id']);
  2078. $data[] = array(
  2079. 'course' => $course['title'],
  2080. 'session' => $teacher['name'],
  2081. 'tutor' => $tutor['username'] . ' - ' . $tutor['lastname'] . ' ' . $tutor['firstname'],
  2082. 'documents' => $totalDocuments,
  2083. 'links' => $totalLinks,
  2084. 'forums' => $totalForums,
  2085. 'works' => $totalWorks,
  2086. 'wikis' => $totalWikis,
  2087. 'announcements' => $totalAnnouncements,
  2088. );
  2089. }
  2090. return $data;
  2091. }
  2092. /**
  2093. * Returns the average student progress in the learning paths of the given
  2094. * course.
  2095. * @param int|array $studentId
  2096. * @param string $courseCode
  2097. * @param array $lpIdList Limit average to listed lp ids
  2098. * @param int $sessionId Session id (optional),
  2099. * if parameter $session_id is null(default) it'll return results including
  2100. * sessions, 0 = session is not filtered
  2101. * @param bool $returnArray Will return an array of the type:
  2102. * [sum_of_progresses, number] if it is set to true
  2103. * @param boolean $onlySeriousGame Optional. Limit average to lp on seriousgame mode
  2104. * @return double Average progress of the user in this course
  2105. */
  2106. public static function get_avg_student_progress(
  2107. $studentId,
  2108. $courseCode = null,
  2109. $lpIdList = array(),
  2110. $sessionId = null,
  2111. $returnArray = false,
  2112. $onlySeriousGame = false
  2113. ) {
  2114. // If there is at least one learning path and one student.
  2115. if (empty($studentId)) {
  2116. return false;
  2117. }
  2118. $sessionId = intval($sessionId);
  2119. $courseInfo = api_get_course_info($courseCode);
  2120. if (empty($courseInfo)) {
  2121. return false;
  2122. }
  2123. $lPTable = Database::get_course_table(TABLE_LP_MAIN);
  2124. $lpViewTable = Database::get_course_table(TABLE_LP_VIEW);
  2125. $lpConditions = [];
  2126. $lpConditions['c_id = ? '] = $courseInfo['real_id'];
  2127. if ($sessionId > 0) {
  2128. $lpConditions['AND (session_id = ? OR session_id = 0 OR session_id IS NULL)'] = $sessionId;
  2129. } else {
  2130. $lpConditions['AND session_id = ?'] = $sessionId;
  2131. }
  2132. if (is_array($lpIdList) && count($lpIdList) > 0) {
  2133. $placeHolders = [];
  2134. for ($i = 0; $i < count($lpIdList); $i++) {
  2135. $placeHolders[] = '?';
  2136. }
  2137. $lpConditions['AND id IN(' . implode(', ', $placeHolders) . ') '] = $lpIdList;
  2138. }
  2139. if ($onlySeriousGame) {
  2140. $lpConditions['AND seriousgame_mode = ? '] = true;
  2141. }
  2142. $resultLP = Database::select(
  2143. 'id',
  2144. $lPTable,
  2145. ['where' => $lpConditions]
  2146. );
  2147. $filteredLP = array_keys($resultLP);
  2148. if (empty($filteredLP)) {
  2149. return false;
  2150. }
  2151. $conditions = [
  2152. " c_id = {$courseInfo['real_id']} ",
  2153. " lp_view.lp_id IN(" . implode(', ', $filteredLP) . ") "
  2154. ];
  2155. $groupBy = 'GROUP BY lp_id';
  2156. if (is_array($studentId)) {
  2157. $studentId = array_map('intval', $studentId);
  2158. $conditions[] = " lp_view.user_id IN (" . implode(',', $studentId) . ") ";
  2159. } else {
  2160. $studentId = intval($studentId);
  2161. $conditions[] = " lp_view.user_id = '$studentId' ";
  2162. if (empty($lpIdList)) {
  2163. $lpList = new LearnpathList($studentId, $courseCode, $sessionId);
  2164. $lpList = $lpList->get_flat_list();
  2165. if (!empty($lpList)) {
  2166. /** @var $lp */
  2167. foreach ($lpList as $lpId => $lp) {
  2168. $lpIdList[] = $lpId;
  2169. }
  2170. }
  2171. }
  2172. }
  2173. if (!empty($sessionId)) {
  2174. $conditions[] = " session_id = $sessionId ";
  2175. }
  2176. $conditionToString = implode('AND', $conditions);
  2177. // Get last view for each student (in case of multi-attempt)
  2178. // Also filter on LPs of this session
  2179. /*$sql = " SELECT
  2180. MAX(view_count),
  2181. AVG(progress) average,
  2182. SUM(progress) sum_progress,
  2183. count(progress) count_progress
  2184. FROM $lpViewTable lp_view
  2185. WHERE
  2186. $conditionToString
  2187. $groupBy";*/
  2188. $sql = "
  2189. SELECT lp_id, view_count, progress FROM $lpViewTable lp_view
  2190. WHERE
  2191. $conditionToString
  2192. $groupBy
  2193. ORDER BY view_count DESC
  2194. ";
  2195. $result = Database::query($sql);
  2196. $progress = array();
  2197. $viewCount = array();
  2198. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2199. if (!isset($viewCount[$row['lp_id']])) {
  2200. $progress[$row['lp_id']] = $row['progress'];
  2201. }
  2202. $viewCount[$row['lp_id']] = $row['view_count'];
  2203. }
  2204. // Fill with lp ids
  2205. if (!empty($lpIdList)) {
  2206. foreach ($lpIdList as $lpId) {
  2207. if (!isset($progress[$lpId])) {
  2208. $progress[$lpId] = 0;
  2209. }
  2210. }
  2211. }
  2212. if (!empty($progress)) {
  2213. $sum = array_sum($progress);
  2214. $average = $sum / count($progress);
  2215. } else {
  2216. $average = 0;
  2217. $sum = 0;
  2218. }
  2219. if ($returnArray) {
  2220. return [
  2221. $sum,
  2222. count($progress)
  2223. ];
  2224. }
  2225. return round($average, 1);
  2226. }
  2227. /**
  2228. * This function gets:
  2229. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores.
  2230. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores.
  2231. * 3. And finally it will return the average between 1. and 2.
  2232. * @todo improve performance, when loading 1500 users with 20 lps the script dies
  2233. * This function does not take the results of a Test out of a LP
  2234. *
  2235. * @param mixed $student_id Array of user ids or an user id
  2236. * @param string $course_code
  2237. * @param array $lp_ids List of LP ids
  2238. * @param int $session_id Session id (optional),
  2239. * if param $session_id is null(default) it'll return results
  2240. * including sessions, 0 = session is not filtered
  2241. * @param bool $return_array Returns an array of the
  2242. * type [sum_score, num_score] if set to true
  2243. * @param bool $get_only_latest_attempt_results get only the latest attempts or ALL attempts
  2244. *
  2245. * @return string Value (number %) Which represents a round integer explain in got in 3.
  2246. */
  2247. public static function get_avg_student_score(
  2248. $student_id,
  2249. $course_code,
  2250. $lp_ids = array(),
  2251. $session_id = null,
  2252. $return_array = false,
  2253. $get_only_latest_attempt_results = false
  2254. ) {
  2255. $debug = false;
  2256. if (empty($lp_ids)) {
  2257. $debug = false;
  2258. }
  2259. if ($debug) echo '<h1>Tracking::get_avg_student_score</h1>';
  2260. $tbl_stats_exercices = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2261. $tbl_stats_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2262. $course = api_get_course_info($course_code);
  2263. if (!empty($course)) {
  2264. // Get course tables names
  2265. $tbl_quiz_questions = Database::get_course_table(TABLE_QUIZ_QUESTION);
  2266. $lp_table = Database:: get_course_table(TABLE_LP_MAIN);
  2267. $lp_item_table = Database:: get_course_table(TABLE_LP_ITEM);
  2268. $lp_view_table = Database:: get_course_table(TABLE_LP_VIEW);
  2269. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2270. $course_id = $course['real_id'];
  2271. // Compose a filter based on optional learning paths list given
  2272. $condition_lp = "";
  2273. if (count($lp_ids) > 0) {
  2274. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  2275. }
  2276. // Compose a filter based on optional session id
  2277. $session_id = intval($session_id);
  2278. if (count($lp_ids) > 0) {
  2279. $condition_session = " AND session_id = $session_id ";
  2280. } else {
  2281. $condition_session = " WHERE session_id = $session_id ";
  2282. }
  2283. // Check the real number of LPs corresponding to the filter in the
  2284. // database (and if no list was given, get them all)
  2285. if (empty($session_id)) {
  2286. $sql = "SELECT DISTINCT(id), use_max_score
  2287. FROM $lp_table
  2288. WHERE c_id = $course_id AND (session_id = 0 OR session_id IS NULL ) $condition_lp ";
  2289. } else {
  2290. $sql = "SELECT DISTINCT(id), use_max_score
  2291. FROM $lp_table
  2292. WHERE c_id = $course_id $condition_lp ";
  2293. }
  2294. $res_row_lp = Database::query($sql);
  2295. $count_row_lp = Database::num_rows($res_row_lp);
  2296. $lp_list = $use_max_score = array();
  2297. while ($row_lp = Database::fetch_array($res_row_lp)) {
  2298. $lp_list[] = $row_lp['id'];
  2299. $use_max_score[$row_lp['id']] = $row_lp['use_max_score'];
  2300. }
  2301. if ($debug) {
  2302. echo 'Use max score or not list '; var_dump($use_max_score);
  2303. }
  2304. // prepare filter on users
  2305. if (is_array($student_id)) {
  2306. array_walk($student_id, 'intval');
  2307. $condition_user1 =" AND user_id IN (".implode(',', $student_id).") ";
  2308. } else {
  2309. $condition_user1 =" AND user_id = $student_id ";
  2310. }
  2311. if ($count_row_lp > 0 && !empty($student_id)) {
  2312. // Getting latest LP result for a student
  2313. //@todo problem when a course have more than 1500 users
  2314. $sql = "SELECT MAX(view_count) as vc, id, progress, lp_id, user_id
  2315. FROM $lp_view_table
  2316. WHERE
  2317. c_id = $course_id AND
  2318. lp_id IN (".implode(',', $lp_list).")
  2319. $condition_user1 AND
  2320. session_id = $session_id
  2321. GROUP BY lp_id, user_id";
  2322. if ($debug) echo $sql;
  2323. $rs_last_lp_view_id = Database::query($sql);
  2324. $global_result = 0;
  2325. if (Database::num_rows($rs_last_lp_view_id) > 0) {
  2326. // Cycle through each line of the results (grouped by lp_id, user_id)
  2327. while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
  2328. $count_items = 0;
  2329. $lp_partial_total = 0;
  2330. $list = array();
  2331. $lp_view_id = $row_lp_view['id'];
  2332. $lp_id = $row_lp_view['lp_id'];
  2333. $user_id = $row_lp_view['user_id'];
  2334. if ($debug) echo '<h2>LP id '.$lp_id.'</h2>';
  2335. if ($get_only_latest_attempt_results) {
  2336. //Getting lp_items done by the user
  2337. $sql = "SELECT DISTINCT lp_item_id
  2338. FROM $lp_item_view_table
  2339. WHERE
  2340. c_id = $course_id AND
  2341. lp_view_id = $lp_view_id
  2342. ORDER BY lp_item_id";
  2343. $res_lp_item = Database::query($sql);
  2344. while ($row_lp_item = Database::fetch_array($res_lp_item,'ASSOC')) {
  2345. $my_lp_item_id = $row_lp_item['lp_item_id'];
  2346. // Getting the most recent attempt
  2347. $sql = "SELECT lp_iv.id as lp_item_view_id,
  2348. lp_iv.score as score,
  2349. lp_i.max_score,
  2350. lp_iv.max_score as max_score_item_view,
  2351. lp_i.path,
  2352. lp_i.item_type,
  2353. lp_i.id as iid
  2354. FROM $lp_item_view_table as lp_iv
  2355. INNER JOIN $lp_item_table as lp_i
  2356. ON lp_i.id = lp_iv.lp_item_id AND
  2357. lp_iv.c_id = $course_id AND
  2358. lp_i.c_id = $course_id AND
  2359. (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  2360. WHERE
  2361. lp_item_id = $my_lp_item_id AND
  2362. lp_view_id = $lp_view_id
  2363. ORDER BY view_count DESC
  2364. LIMIT 1";
  2365. $res_lp_item_result = Database::query($sql);
  2366. while ($row_max_score = Database::fetch_array($res_lp_item_result,'ASSOC')) {
  2367. $list[]= $row_max_score;
  2368. }
  2369. }
  2370. } else {
  2371. // For the currently analysed view, get the score and
  2372. // max_score of each item if it is a sco or a TOOL_QUIZ
  2373. $sql = "SELECT
  2374. lp_iv.id as lp_item_view_id,
  2375. lp_iv.score as score,
  2376. lp_i.max_score,
  2377. lp_iv.max_score as max_score_item_view,
  2378. lp_i.path,
  2379. lp_i.item_type,
  2380. lp_i.id as iid
  2381. FROM $lp_item_view_table as lp_iv
  2382. INNER JOIN $lp_item_table as lp_i
  2383. ON lp_i.id = lp_iv.lp_item_id AND
  2384. lp_iv.c_id = $course_id AND
  2385. lp_i.c_id = $course_id AND
  2386. (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  2387. WHERE lp_view_id = $lp_view_id ";
  2388. if ($debug) echo $sql.'<br />';
  2389. $res_max_score = Database::query($sql);
  2390. while ($row_max_score = Database::fetch_array($res_max_score,'ASSOC')) {
  2391. $list[]= $row_max_score;
  2392. }
  2393. }
  2394. // Go through each scorable element of this view
  2395. $score_of_scorm_calculate = 0;
  2396. foreach ($list as $row_max_score) {
  2397. // Came from the original lp_item
  2398. $max_score = $row_max_score['max_score'];
  2399. // Came from the lp_item_view
  2400. $max_score_item_view = $row_max_score['max_score_item_view'];
  2401. $score = $row_max_score['score'];
  2402. if ($debug) echo '<h3>Item Type: ' .$row_max_score['item_type'].'</h3>';
  2403. if ($row_max_score['item_type'] == 'sco') {
  2404. /* Check if it is sco (easier to get max_score)
  2405. when there's no max score, we assume 100 as the max score,
  2406. as the SCORM 1.2 says that the value should always be between 0 and 100.
  2407. */
  2408. if ($max_score == 0 || is_null($max_score) || $max_score == '') {
  2409. // Chamilo style
  2410. if ($use_max_score[$lp_id]) {
  2411. $max_score = 100;
  2412. } else {
  2413. // Overwrites max score = 100 to use the one that came in the lp_item_view see BT#1613
  2414. $max_score = $max_score_item_view;
  2415. }
  2416. }
  2417. // Avoid division by zero errors
  2418. if (!empty($max_score)) {
  2419. $lp_partial_total += $score/$max_score;
  2420. }
  2421. if ($debug) echo '<b>$lp_partial_total, $score, $max_score '.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  2422. } else {
  2423. // Case of a TOOL_QUIZ element
  2424. $item_id = $row_max_score['iid'];
  2425. $item_path = $row_max_score['path'];
  2426. $lp_item_view_id = $row_max_score['lp_item_view_id'];
  2427. // Get last attempt to this exercise through
  2428. // the current lp for the current user
  2429. $sql = "SELECT exe_id
  2430. FROM $tbl_stats_exercices
  2431. WHERE
  2432. exe_exo_id = '$item_path' AND
  2433. exe_user_id = $user_id AND
  2434. orig_lp_item_id = $item_id AND
  2435. orig_lp_item_view_id = $lp_item_view_id AND
  2436. c_id = $course_id AND
  2437. session_id = $session_id AND
  2438. status = ''
  2439. ORDER BY exe_date DESC
  2440. LIMIT 1";
  2441. if ($debug) echo $sql .'<br />';
  2442. $result_last_attempt = Database::query($sql);
  2443. $num = Database::num_rows($result_last_attempt);
  2444. if ($num > 0 ) {
  2445. $id_last_attempt = Database::result($result_last_attempt, 0, 0);
  2446. if ($debug) echo $id_last_attempt.'<br />';
  2447. // Within the last attempt number tracking, get the sum of
  2448. // the max_scores of all questions that it was
  2449. // made of (we need to make this call dynamic because of random questions selection)
  2450. $sql = "SELECT SUM(t.ponderation) as maxscore FROM
  2451. (
  2452. SELECT DISTINCT
  2453. question_id,
  2454. marks,
  2455. ponderation
  2456. FROM $tbl_stats_attempts AS at
  2457. INNER JOIN $tbl_quiz_questions AS q
  2458. ON (q.id = at.question_id)
  2459. WHERE
  2460. exe_id ='$id_last_attempt' AND
  2461. q.c_id = $course_id
  2462. )
  2463. AS t";
  2464. if ($debug) echo '$sql: '.$sql.' <br />';
  2465. $res_max_score_bis = Database::query($sql);
  2466. $row_max_score_bis = Database::fetch_array($res_max_score_bis);
  2467. if (!empty($row_max_score_bis['maxscore'])) {
  2468. $max_score = $row_max_score_bis['maxscore'];
  2469. }
  2470. if (!empty($max_score) && floatval($max_score) > 0) {
  2471. $lp_partial_total += $score/$max_score;
  2472. }
  2473. if ($debug) echo '$lp_partial_total, $score, $max_score <b>'.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  2474. }
  2475. }
  2476. if (in_array($row_max_score['item_type'], array('quiz','sco'))) {
  2477. // Normal way
  2478. if ($use_max_score[$lp_id]) {
  2479. $count_items++;
  2480. } else {
  2481. if ($max_score != '') {
  2482. $count_items++;
  2483. }
  2484. }
  2485. if ($debug) echo '$count_items: '.$count_items;
  2486. }
  2487. } //end for
  2488. $score_of_scorm_calculate += $count_items ? (($lp_partial_total / $count_items) * 100) : 0;
  2489. if ($debug) echo '<h3>$count_items '.$count_items.'</h3>';
  2490. if ($debug) echo '<h3>$score_of_scorm_calculate '.$score_of_scorm_calculate.'</h3>';
  2491. $global_result += $score_of_scorm_calculate;
  2492. if ($debug) echo '<h3>$global_result '.$global_result.'</h3>';
  2493. } // end while
  2494. }
  2495. $lp_with_quiz = 0;
  2496. foreach ($lp_list as $lp_id) {
  2497. // Check if LP have a score we assume that all SCO have an score
  2498. $sql = "SELECT count(id) as count
  2499. FROM $lp_item_table
  2500. WHERE
  2501. c_id = $course_id AND
  2502. (item_type = 'quiz' OR item_type = 'sco') AND
  2503. lp_id = ".$lp_id;
  2504. if ($debug) echo $sql;
  2505. $result_have_quiz = Database::query($sql);
  2506. if (Database::num_rows($result_have_quiz) > 0 ) {
  2507. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  2508. if (is_numeric($row['count']) && $row['count'] != 0) {
  2509. $lp_with_quiz++;
  2510. }
  2511. }
  2512. }
  2513. if ($debug) echo '<h3>$lp_with_quiz '.$lp_with_quiz.' </h3>';
  2514. if ($debug) echo '<h3>Final return</h3>';
  2515. if ($lp_with_quiz != 0) {
  2516. if (!$return_array) {
  2517. $score_of_scorm_calculate = round(($global_result/$lp_with_quiz),2);
  2518. if ($debug) var_dump($score_of_scorm_calculate);
  2519. if (empty($lp_ids)) {
  2520. if ($debug) echo '<h2>All lps fix: '.$score_of_scorm_calculate.'</h2>';
  2521. }
  2522. return $score_of_scorm_calculate;
  2523. } else {
  2524. if ($debug) var_dump($global_result, $lp_with_quiz);
  2525. return array($global_result, $lp_with_quiz);
  2526. }
  2527. } else {
  2528. return '-';
  2529. }
  2530. }
  2531. }
  2532. return null;
  2533. }
  2534. /**
  2535. * This function gets:
  2536. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores.
  2537. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores.
  2538. * 3. And finally it will return the average between 1. and 2.
  2539. * This function does not take the results of a Test out of a LP
  2540. *
  2541. * @param int|array Array of user ids or an user id
  2542. * @param string $course_code Course code
  2543. * @param array $lp_ids List of LP ids
  2544. * @param int $session_id Session id (optional), if param $session_id is 0(default)
  2545. * it'll return results including sessions, 0 = session is not filtered
  2546. * @param bool Returns an array of the type [sum_score, num_score] if set to true
  2547. * @param bool get only the latest attempts or ALL attempts
  2548. * @return string Value (number %) Which represents a round integer explain in got in 3.
  2549. */
  2550. public static function getAverageStudentScore(
  2551. $student_id,
  2552. $course_code = '',
  2553. $lp_ids = array(),
  2554. $session_id = 0
  2555. ) {
  2556. if (empty($student_id)) {
  2557. return 0;
  2558. }
  2559. $conditions = array();
  2560. if (!empty($course_code)) {
  2561. $course = api_get_course_info($course_code);
  2562. $courseId = $course['real_id'];
  2563. $conditions[] = " c_id = $courseId";
  2564. }
  2565. // Get course tables names
  2566. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  2567. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  2568. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  2569. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2570. // Compose a filter based on optional learning paths list given
  2571. if (!empty($lp_ids) && count($lp_ids) > 0) {
  2572. $conditions[] = " id IN(".implode(',', $lp_ids).") ";
  2573. }
  2574. // Compose a filter based on optional session id
  2575. $session_id = intval($session_id);
  2576. if (!empty($session_id)) {
  2577. $conditions[] = " session_id = $session_id ";
  2578. }
  2579. if (is_array($student_id)) {
  2580. array_walk($student_id, 'intval');
  2581. $conditions[] =" lp_view.user_id IN (".implode(',', $student_id).") ";
  2582. } else {
  2583. $conditions[] =" lp_view.user_id = $student_id ";
  2584. }
  2585. $conditionsToString = implode('AND ', $conditions);
  2586. $sql = "SELECT
  2587. SUM(lp_iv.score) sum_score,
  2588. SUM(lp_i.max_score) sum_max_score
  2589. FROM $lp_table as lp
  2590. INNER JOIN $lp_item_table as lp_i
  2591. ON lp.id = lp_id AND lp.c_id = lp_i.c_id
  2592. INNER JOIN $lp_view_table as lp_view
  2593. ON lp_view.lp_id = lp_i.lp_id AND lp_view.c_id = lp_i.c_id
  2594. INNER JOIN $lp_item_view_table as lp_iv
  2595. ON lp_i.id = lp_iv.lp_item_id AND lp_view.c_id = lp_iv.c_id AND lp_iv.lp_view_id = lp_view.id
  2596. WHERE (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."') AND
  2597. $conditionsToString
  2598. ";
  2599. $result = Database::query($sql);
  2600. $row = Database::fetch_array($result, 'ASSOC');
  2601. if (empty($row['sum_max_score'])) {
  2602. return 0;
  2603. }
  2604. return ($row['sum_score'] / $row['sum_max_score']) * 100;
  2605. }
  2606. /**
  2607. * This function gets time spent in learning path for a student inside a course
  2608. * @param int|array Student id(s)
  2609. * @param string Course code
  2610. * @param array Limit average to listed lp ids
  2611. * @param int Session id (optional), if param $session_id is
  2612. * null(default) it'll return results including sessions, 0 = session is not filtered
  2613. * @return int Total time
  2614. */
  2615. public static function get_time_spent_in_lp($student_id, $course_code, $lp_ids = array(), $session_id = null)
  2616. {
  2617. $course = api_get_course_info($course_code);
  2618. $student_id = intval($student_id);
  2619. $total_time = 0;
  2620. if (!empty($course)) {
  2621. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  2622. $t_lpv = Database::get_course_table(TABLE_LP_VIEW);
  2623. $t_lpiv = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2624. $course_id = $course['real_id'];
  2625. // Compose a filter based on optional learning paths list given
  2626. $condition_lp = "";
  2627. if (count($lp_ids) > 0) {
  2628. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  2629. }
  2630. // Compose a filter based on optional session id
  2631. $session_id = intval($session_id);
  2632. $condition_session = "";
  2633. if (isset($session_id)) {
  2634. $condition_session = " AND session_id = $session_id ";
  2635. }
  2636. // Check the real number of LPs corresponding to the filter in the
  2637. // database (and if no list was given, get them all)
  2638. $sql = "SELECT DISTINCT(id) FROM $lp_table WHERE c_id = $course_id $condition_lp";
  2639. $res_row_lp = Database::query($sql);
  2640. $count_row_lp = Database::num_rows($res_row_lp);
  2641. // calculates time
  2642. if ($count_row_lp > 0) {
  2643. while ($row_lp = Database::fetch_array($res_row_lp)) {
  2644. $lp_id = intval($row_lp['id']);
  2645. $sql = 'SELECT SUM(total_time)
  2646. FROM '.$t_lpiv.' AS item_view
  2647. INNER JOIN '.$t_lpv.' AS view
  2648. ON (
  2649. item_view.lp_view_id = view.id AND
  2650. item_view.c_id = view.c_id
  2651. )
  2652. WHERE
  2653. item_view.c_id = '.$course_id.' AND
  2654. view.c_id = '.$course_id.' AND
  2655. view.lp_id = '.$lp_id.' AND
  2656. view.user_id = '.$student_id.' AND
  2657. session_id = '.$session_id;
  2658. $rs = Database::query($sql);
  2659. if (Database::num_rows($rs) > 0) {
  2660. $total_time += Database::result($rs, 0, 0);
  2661. }
  2662. }
  2663. }
  2664. }
  2665. return $total_time;
  2666. }
  2667. /**
  2668. * This function gets last connection time to one learning path
  2669. * @param int|array $student_id Student id(s)
  2670. * @param string $course_code Course code
  2671. * @param int $lp_id Learning path id
  2672. * @param int $session_id
  2673. * @return int Total time
  2674. */
  2675. public static function get_last_connection_time_in_lp(
  2676. $student_id,
  2677. $course_code,
  2678. $lp_id,
  2679. $session_id = 0
  2680. ) {
  2681. $course = CourseManager::get_course_information($course_code);
  2682. $student_id = intval($student_id);
  2683. $lp_id = intval($lp_id);
  2684. $last_time = 0;
  2685. $session_id = intval($session_id);
  2686. if (!empty($course)) {
  2687. $course_id = $course['real_id'];
  2688. $lp_table = Database:: get_course_table(TABLE_LP_MAIN);
  2689. $t_lpv = Database:: get_course_table(TABLE_LP_VIEW);
  2690. $t_lpiv = Database:: get_course_table(TABLE_LP_ITEM_VIEW);
  2691. // Check the real number of LPs corresponding to the filter in the
  2692. // database (and if no list was given, get them all)
  2693. $sql = "SELECT id FROM $lp_table WHERE c_id = $course_id AND id = $lp_id ";
  2694. $res_row_lp = Database::query($sql);
  2695. $count_row_lp = Database::num_rows($res_row_lp);
  2696. // calculates last connection time
  2697. if ($count_row_lp > 0) {
  2698. $sql = 'SELECT MAX(start_time)
  2699. FROM ' . $t_lpiv . ' AS item_view
  2700. INNER JOIN ' . $t_lpv . ' AS view
  2701. ON (item_view.lp_view_id = view.id AND item_view.c_id = view.c_id)
  2702. WHERE
  2703. item_view.c_id = '.$course_id.' AND
  2704. view.c_id = '.$course_id.' AND
  2705. view.lp_id = '.$lp_id.' AND
  2706. view.user_id = '.$student_id.' AND
  2707. view.session_id = '.$session_id;
  2708. $rs = Database::query($sql);
  2709. if (Database::num_rows($rs) > 0) {
  2710. $last_time = Database::result($rs, 0, 0);
  2711. }
  2712. }
  2713. }
  2714. return $last_time;
  2715. }
  2716. /**
  2717. * gets the list of students followed by coach
  2718. * @param int $coach_id Coach id
  2719. * @return array List of students
  2720. */
  2721. public static function get_student_followed_by_coach($coach_id)
  2722. {
  2723. $coach_id = intval($coach_id);
  2724. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2725. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2726. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  2727. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2728. $students = [];
  2729. // At first, courses where $coach_id is coach of the course //
  2730. $sql = 'SELECT session_id, c_id
  2731. FROM ' . $tbl_session_course_user . '
  2732. WHERE user_id=' . $coach_id.' AND status=2';
  2733. if (api_is_multiple_url_enabled()) {
  2734. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  2735. $access_url_id = api_get_current_access_url_id();
  2736. if ($access_url_id != -1) {
  2737. $sql = 'SELECT scu.session_id, scu.c_id
  2738. FROM ' . $tbl_session_course_user . ' scu
  2739. INNER JOIN '.$tbl_session_rel_access_url.' sru
  2740. ON (scu.session_id=sru.session_id)
  2741. WHERE
  2742. scu.user_id=' . $coach_id.' AND
  2743. scu.status=2 AND
  2744. sru.access_url_id = '.$access_url_id;
  2745. }
  2746. }
  2747. $result = Database::query($sql);
  2748. while ($a_courses = Database::fetch_array($result)) {
  2749. $courseId = $a_courses['c_id'];
  2750. $id_session = $a_courses['session_id'];
  2751. $sql = "SELECT DISTINCT srcru.user_id
  2752. FROM $tbl_session_course_user AS srcru
  2753. INNER JOIN $tbl_session_user sru
  2754. ON (srcru.user_id = sru.user_id AND srcru.session_id = sru.session_id)
  2755. WHERE
  2756. sru.relation_type<>".SESSION_RELATION_TYPE_RRHH." AND
  2757. srcru.c_id = '$courseId' AND
  2758. srcru.session_id = '$id_session'";
  2759. $rs = Database::query($sql);
  2760. while ($row = Database::fetch_array($rs)) {
  2761. $students[$row['user_id']] = $row['user_id'];
  2762. }
  2763. }
  2764. // Then, courses where $coach_id is coach of the session //
  2765. $sql = 'SELECT session_course_user.user_id
  2766. FROM ' . $tbl_session_course_user . ' as session_course_user
  2767. INNER JOIN '.$tbl_session_user.' sru
  2768. ON session_course_user.user_id = sru.user_id AND session_course_user.session_id = sru.session_id
  2769. INNER JOIN ' . $tbl_session_course . ' as session_course
  2770. ON session_course.c_id = session_course_user.c_id
  2771. AND session_course_user.session_id = session_course.session_id
  2772. INNER JOIN ' . $tbl_session . ' as session
  2773. ON session.id = session_course.session_id
  2774. AND session.id_coach = ' . $coach_id;
  2775. if (api_is_multiple_url_enabled()) {
  2776. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  2777. $access_url_id = api_get_current_access_url_id();
  2778. if ($access_url_id != -1){
  2779. $sql = 'SELECT session_course_user.user_id
  2780. FROM ' . $tbl_session_course_user . ' as session_course_user
  2781. INNER JOIN '.$tbl_session_user.' sru
  2782. ON session_course_user.user_id = sru.user_id AND
  2783. session_course_user.session_id = sru.session_id
  2784. INNER JOIN ' . $tbl_session_course . ' as session_course
  2785. ON session_course.c_id = session_course_user.c_id AND
  2786. session_course_user.session_id = session_course.session_id
  2787. INNER JOIN ' . $tbl_session . ' as session
  2788. ON session.id = session_course.session_id AND
  2789. session.id_coach = ' . $coach_id.'
  2790. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  2791. ON session.id = session_rel_url.session_id WHERE access_url_id = '.$access_url_id;
  2792. }
  2793. }
  2794. $result = Database::query($sql);
  2795. while ($row = Database::fetch_array($result)) {
  2796. $students[$row['user_id']] = $row['user_id'];
  2797. }
  2798. return $students;
  2799. }
  2800. /**
  2801. * Get student followed by a coach inside a session
  2802. * @param int Session id
  2803. * @param int Coach id
  2804. * @return array students list
  2805. */
  2806. public static function get_student_followed_by_coach_in_a_session($id_session, $coach_id)
  2807. {
  2808. $coach_id = intval($coach_id);
  2809. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2810. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2811. $students = [];
  2812. // At first, courses where $coach_id is coach of the course //
  2813. $sql = 'SELECT c_id FROM ' . $tbl_session_course_user . '
  2814. WHERE session_id="' . $id_session . '" AND user_id=' . $coach_id.' AND status=2';
  2815. $result = Database::query($sql);
  2816. while ($a_courses = Database::fetch_array($result)) {
  2817. $courseId = $a_courses["c_id"];
  2818. $sql = "SELECT DISTINCT srcru.user_id
  2819. FROM $tbl_session_course_user AS srcru
  2820. WHERE
  2821. c_id = '$courseId' AND
  2822. session_id = '" . $id_session . "'";
  2823. $rs = Database::query($sql);
  2824. while ($row = Database::fetch_array($rs)) {
  2825. $students[$row['user_id']] = $row['user_id'];
  2826. }
  2827. }
  2828. // Then, courses where $coach_id is coach of the session
  2829. $sql = 'SELECT id_coach FROM ' . $tbl_session . '
  2830. WHERE id="' . $id_session.'" AND id_coach="' . $coach_id . '"';
  2831. $result = Database::query($sql);
  2832. //He is the session_coach so we select all the users in the session
  2833. if (Database::num_rows($result) > 0) {
  2834. $sql = 'SELECT DISTINCT srcru.user_id
  2835. FROM ' . $tbl_session_course_user . ' AS srcru
  2836. WHERE session_id="' . $id_session . '"';
  2837. $result = Database::query($sql);
  2838. while ($row = Database::fetch_array($result)) {
  2839. $students[$row['user_id']] = $row['user_id'];
  2840. }
  2841. }
  2842. return $students;
  2843. }
  2844. /**
  2845. * Check if a coach is allowed to follow a student
  2846. * @param int Coach id
  2847. * @param int Student id
  2848. * @return bool
  2849. */
  2850. public static function is_allowed_to_coach_student($coach_id, $student_id)
  2851. {
  2852. $coach_id = intval($coach_id);
  2853. $student_id = intval($student_id);
  2854. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2855. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2856. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2857. // At first, courses where $coach_id is coach of the course //
  2858. $sql = 'SELECT 1 FROM ' . $tbl_session_course_user . '
  2859. WHERE user_id=' . $coach_id .' AND status=2';
  2860. $result = Database::query($sql);
  2861. if (Database::num_rows($result) > 0) {
  2862. return true;
  2863. }
  2864. // Then, courses where $coach_id is coach of the session
  2865. $sql = 'SELECT session_course_user.user_id
  2866. FROM ' . $tbl_session_course_user . ' as session_course_user
  2867. INNER JOIN ' . $tbl_session_course . ' as session_course
  2868. ON session_course.c_id = session_course_user.c_id
  2869. INNER JOIN ' . $tbl_session . ' as session
  2870. ON session.id = session_course.session_id
  2871. AND session.id_coach = ' . $coach_id . '
  2872. WHERE user_id = ' . $student_id;
  2873. $result = Database::query($sql);
  2874. if (Database::num_rows($result) > 0) {
  2875. return true;
  2876. }
  2877. return false;
  2878. }
  2879. /**
  2880. * Get courses followed by coach
  2881. * @param int Coach id
  2882. * @param int Session id (optional)
  2883. * @return array Courses list
  2884. */
  2885. public static function get_courses_followed_by_coach($coach_id, $id_session = null)
  2886. {
  2887. $coach_id = intval($coach_id);
  2888. if (!empty($id_session)) {
  2889. $id_session = intval($id_session);
  2890. }
  2891. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2892. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2893. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2894. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2895. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2896. // At first, courses where $coach_id is coach of the course.
  2897. $sql = 'SELECT DISTINCT c.code
  2898. FROM ' . $tbl_session_course_user . ' sc
  2899. INNER JOIN '.$tbl_course.' c
  2900. ON (c.id = sc.c_id)
  2901. WHERE user_id = ' . $coach_id.' AND status = 2';
  2902. if (api_is_multiple_url_enabled()) {
  2903. $access_url_id = api_get_current_access_url_id();
  2904. if ($access_url_id != -1){
  2905. $sql = 'SELECT DISTINCT c.code
  2906. FROM ' . $tbl_session_course_user . ' scu
  2907. INNER JOIN '.$tbl_course.' c
  2908. ON (c.code = scu.c_id)
  2909. INNER JOIN '.$tbl_course_rel_access_url.' cru
  2910. ON (c.id = cru.c_id)
  2911. WHERE
  2912. scu.user_id=' . $coach_id.' AND
  2913. scu.status=2 AND
  2914. cru.access_url_id = '.$access_url_id;
  2915. }
  2916. }
  2917. if (!empty($id_session)) {
  2918. $sql .= ' AND session_id=' . $id_session;
  2919. }
  2920. $courseList = array();
  2921. $result = Database::query($sql);
  2922. while ($row = Database::fetch_array($result)) {
  2923. $courseList[$row['code']] = $row['code'];
  2924. }
  2925. // Then, courses where $coach_id is coach of the session
  2926. $sql = 'SELECT DISTINCT course.code
  2927. FROM ' . $tbl_session_course . ' as session_course
  2928. INNER JOIN ' . $tbl_session . ' as session
  2929. ON session.id = session_course.session_id
  2930. AND session.id_coach = ' . $coach_id . '
  2931. INNER JOIN ' . $tbl_course . ' as course
  2932. ON course.id = session_course.c_id';
  2933. if (api_is_multiple_url_enabled()) {
  2934. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2935. $access_url_id = api_get_current_access_url_id();
  2936. if ($access_url_id != -1){
  2937. $sql = 'SELECT DISTINCT c.code
  2938. FROM ' . $tbl_session_course . ' as session_course
  2939. INNER JOIN '.$tbl_course.' c
  2940. ON (c.id = session_course.c_id)
  2941. INNER JOIN ' . $tbl_session . ' as session
  2942. ON session.id = session_course.session_id
  2943. AND session.id_coach = ' . $coach_id . '
  2944. INNER JOIN ' . $tbl_course . ' as course
  2945. ON course.id = session_course.c_id
  2946. INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url
  2947. ON (course_rel_url.c_id = c.id)';
  2948. }
  2949. }
  2950. if (!empty ($id_session)) {
  2951. $sql .= ' WHERE session_course.session_id=' . $id_session;
  2952. if (api_is_multiple_url_enabled())
  2953. $sql .= ' AND access_url_id = '.$access_url_id;
  2954. } else {
  2955. if (api_is_multiple_url_enabled())
  2956. $sql .= ' WHERE access_url_id = '.$access_url_id;
  2957. }
  2958. $result = Database::query($sql);
  2959. while ($row = Database::fetch_array($result)) {
  2960. $courseList[$row['code']] = $row['code'];
  2961. }
  2962. return $courseList;
  2963. }
  2964. /**
  2965. * Get sessions coached by user
  2966. * @param $coach_id
  2967. * @param int $start
  2968. * @param int $limit
  2969. * @param bool $getCount
  2970. * @param string $keyword
  2971. * @param string $description
  2972. * @return mixed
  2973. */
  2974. public static function get_sessions_coached_by_user(
  2975. $coach_id,
  2976. $start = 0,
  2977. $limit = 0,
  2978. $getCount = false,
  2979. $keyword = '',
  2980. $description = ''
  2981. ) {
  2982. // table definition
  2983. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2984. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2985. $coach_id = intval($coach_id);
  2986. $select = " SELECT * FROM ";
  2987. if ($getCount) {
  2988. $select = " SELECT count(DISTINCT id) as count FROM ";
  2989. }
  2990. $limitCondition = null;
  2991. if (!empty($start) && !empty($limit)) {
  2992. $limitCondition = " LIMIT ".intval($start).", ".intval($limit);
  2993. }
  2994. $keywordCondition = null;
  2995. if (!empty($keyword)) {
  2996. $keyword = Database::escape_string($keyword);
  2997. $keywordCondition = " AND (name LIKE '%$keyword%' ) ";
  2998. if (!empty($description)) {
  2999. $description = Database::escape_string($description);
  3000. $keywordCondition = " AND (name LIKE '%$keyword%' OR description LIKE '%$description%' ) ";
  3001. }
  3002. }
  3003. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  3004. $access_url_id = api_get_current_access_url_id();
  3005. $sql = "
  3006. $select
  3007. (
  3008. SELECT DISTINCT
  3009. id,
  3010. name,
  3011. access_start_date,
  3012. access_end_date
  3013. FROM $tbl_session session
  3014. INNER JOIN $tbl_session_rel_access_url session_rel_url
  3015. ON (session.id = session_rel_url.session_id)
  3016. WHERE
  3017. id_coach = $coach_id AND
  3018. access_url_id = $access_url_id
  3019. $keywordCondition
  3020. UNION
  3021. SELECT DISTINCT
  3022. session.id,
  3023. session.name,
  3024. session.access_start_date,
  3025. session.access_end_date
  3026. FROM $tbl_session as session
  3027. INNER JOIN $tbl_session_course_user as session_course_user
  3028. ON
  3029. session.id = session_course_user.session_id AND
  3030. session_course_user.user_id = $coach_id AND
  3031. session_course_user.status = 2
  3032. INNER JOIN $tbl_session_rel_access_url session_rel_url
  3033. ON (session.id = session_rel_url.session_id)
  3034. WHERE
  3035. access_url_id = $access_url_id
  3036. $keywordCondition
  3037. ) as sessions $limitCondition
  3038. ";
  3039. $rs = Database::query($sql);
  3040. if ($getCount) {
  3041. $row = Database::fetch_array($rs);
  3042. return $row['count'];
  3043. }
  3044. $sessions = [];
  3045. while ($row = Database::fetch_array($rs)) {
  3046. if ($row['access_start_date'] == '0000-00-00 00:00:00') {
  3047. $row['access_start_date'] = null;
  3048. }
  3049. $sessions[$row['id']] = $row;
  3050. }
  3051. if (!empty($sessions)) {
  3052. foreach ($sessions as & $session) {
  3053. if (empty($session['access_start_date'])
  3054. ) {
  3055. $session['status'] = get_lang('SessionActive');
  3056. }
  3057. else {
  3058. $time_start = api_strtotime($session['access_start_date'], 'UTC');
  3059. $time_end = api_strtotime($session['access_end_date'], 'UTC');
  3060. if ($time_start < time() && time() < $time_end) {
  3061. $session['status'] = get_lang('SessionActive');
  3062. } else {
  3063. if (time() < $time_start) {
  3064. $session['status'] = get_lang('SessionFuture');
  3065. } else {
  3066. if (time() > $time_end) {
  3067. $session['status'] = get_lang('SessionPast');
  3068. }
  3069. }
  3070. }
  3071. }
  3072. }
  3073. }
  3074. return $sessions;
  3075. }
  3076. /**
  3077. * Get courses list from a session
  3078. * @param int Session id
  3079. * @return array Courses list
  3080. */
  3081. public static function get_courses_list_from_session($session_id)
  3082. {
  3083. $session_id = intval($session_id);
  3084. // table definition
  3085. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  3086. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3087. $sql = "SELECT DISTINCT code, c_id
  3088. FROM $tbl_session_course sc
  3089. INNER JOIN $courseTable c
  3090. ON sc.c_id = c.id
  3091. WHERE session_id= $session_id";
  3092. $result = Database::query($sql);
  3093. $courses = array();
  3094. while ($row = Database::fetch_array($result)) {
  3095. $courses[$row['code']] = $row;
  3096. }
  3097. return $courses;
  3098. }
  3099. /**
  3100. * Count the number of documents that an user has uploaded to a course
  3101. * @param int|array Student id(s)
  3102. * @param string Course code
  3103. * @param int Session id (optional),
  3104. * if param $session_id is null(default)
  3105. * return count of assignments including sessions, 0 = session is not filtered
  3106. * @return int Number of documents
  3107. */
  3108. public static function count_student_uploaded_documents($student_id, $course_code, $session_id = null)
  3109. {
  3110. // get the information of the course
  3111. $a_course = CourseManager::get_course_information($course_code);
  3112. if (!empty($a_course)) {
  3113. // table definition
  3114. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3115. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  3116. $course_id = $a_course['real_id'];
  3117. if (is_array($student_id)) {
  3118. $studentList = array_map('intval', $student_id);
  3119. $condition_user = " AND ip.insert_user_id IN ('".implode(',', $studentList)."') ";
  3120. } else {
  3121. $student_id = intval($student_id);
  3122. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  3123. }
  3124. $condition_session = null;
  3125. if (isset($session_id)) {
  3126. $session_id = intval($session_id);
  3127. $condition_session = " AND pub.session_id = $session_id ";
  3128. }
  3129. $sql = "SELECT count(ip.tool) AS count
  3130. FROM $tbl_item_property ip
  3131. INNER JOIN $tbl_document pub
  3132. ON (ip.ref = pub.id AND ip.c_id = pub.c_id)
  3133. WHERE
  3134. ip.c_id = $course_id AND
  3135. pub.c_id = $course_id AND
  3136. pub.filetype ='file' AND
  3137. ip.tool = 'document'
  3138. $condition_user $condition_session ";
  3139. $rs = Database::query($sql);
  3140. $row = Database::fetch_array($rs, 'ASSOC');
  3141. return $row['count'];
  3142. }
  3143. return null;
  3144. }
  3145. /**
  3146. * Count assignments per student
  3147. * @param $student_id
  3148. * @param null $course_code
  3149. * @param null $session_id
  3150. * @return int Count of assignments
  3151. * @internal param array|int $Student id(s)
  3152. * @internal param Course $string code
  3153. * @internal param Session $int id (optional),
  3154. * if param $session_id is null(default) return count of assignments
  3155. * including sessions, 0 = session is not filtered
  3156. */
  3157. public static function count_student_assignments($student_id, $course_code = null, $session_id = null)
  3158. {
  3159. if (empty($student_id)) {
  3160. return 0;
  3161. }
  3162. $conditions = array();
  3163. // Get the information of the course
  3164. $a_course = api_get_course_info($course_code);
  3165. if (!empty($a_course)) {
  3166. $course_id = $a_course['real_id'];
  3167. $conditions[]= " ip.c_id = $course_id AND pub.c_id = $course_id ";
  3168. }
  3169. // table definition
  3170. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3171. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  3172. if (is_array($student_id)) {
  3173. $studentList = array_map('intval', $student_id);
  3174. $conditions[]= " ip.insert_user_id IN ('".implode("','", $studentList)."') ";
  3175. } else {
  3176. $student_id = intval($student_id);
  3177. $conditions[]= " ip.insert_user_id = '$student_id' ";
  3178. }
  3179. if (isset($session_id)) {
  3180. $session_id = intval($session_id);
  3181. $conditions[]= " pub.session_id = $session_id ";
  3182. }
  3183. $conditions[] = ' pub.active <> 2 ';
  3184. $conditionToString = implode('AND', $conditions);
  3185. $sql = "SELECT count(ip.tool) as count
  3186. FROM $tbl_item_property ip
  3187. INNER JOIN $tbl_student_publication pub
  3188. ON (ip.ref = pub.id AND ip.c_id = pub.c_id)
  3189. WHERE
  3190. ip.tool='work' AND
  3191. $conditionToString";
  3192. $rs = Database::query($sql);
  3193. $row = Database::fetch_array($rs, 'ASSOC');
  3194. return $row['count'];
  3195. }
  3196. /**
  3197. * Count messages per student inside forum tool
  3198. * @param int|array Student id
  3199. * @param string Course code
  3200. * @param int Session id (optional), if param $session_id is
  3201. * null(default) return count of messages including sessions, 0 = session is not filtered
  3202. * @return int Count of messages
  3203. */
  3204. public static function count_student_messages($student_id, $courseCode = null, $session_id = null)
  3205. {
  3206. if (empty($student_id)) {
  3207. return 0;
  3208. }
  3209. // Table definition.
  3210. $tbl_forum_post = Database::get_course_table(TABLE_FORUM_POST);
  3211. $tbl_forum = Database::get_course_table(TABLE_FORUM);
  3212. $conditions = array();
  3213. if (is_array($student_id)) {
  3214. $studentList = array_map('intval', $student_id);
  3215. $conditions[]= " post.poster_id IN ('".implode("','", $studentList)."') ";
  3216. } else {
  3217. $student_id = intval($student_id);
  3218. $conditions[]= " post.poster_id = '$student_id' ";
  3219. }
  3220. $conditionsToString = implode('AND ', $conditions);
  3221. if (empty($courseCode)) {
  3222. $sql = "SELECT count(poster_id) as count
  3223. FROM $tbl_forum_post post
  3224. INNER JOIN $tbl_forum forum
  3225. ON (forum.forum_id = post.forum_id AND forum.c_id = post.c_id)
  3226. WHERE $conditionsToString";
  3227. $rs = Database::query($sql);
  3228. $row = Database::fetch_array($rs, 'ASSOC');
  3229. return $row['count'];
  3230. }
  3231. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  3232. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  3233. $courseInfo = api_get_course_info($courseCode);
  3234. $forums = [];
  3235. if (!empty($courseInfo)) {
  3236. $forums = get_forums('', $courseCode, true, $session_id);
  3237. $course_id = $courseInfo['real_id'];
  3238. $conditions[]= " post.c_id = $course_id ";
  3239. }
  3240. if (!empty($forums)) {
  3241. $idList = array_column($forums, 'forum_id');
  3242. $idListToString = implode("', '", $idList);
  3243. $conditions[]= " post.forum_id IN ('$idListToString')";
  3244. }
  3245. $conditionsToString = implode('AND ', $conditions);
  3246. $sql = "SELECT count(poster_id) as count
  3247. FROM $tbl_forum_post post
  3248. WHERE $conditionsToString";
  3249. $rs = Database::query($sql);
  3250. $row = Database::fetch_array($rs, 'ASSOC');
  3251. $count = $row['count'];
  3252. return $count;
  3253. }
  3254. /**
  3255. * This function counts the number of post by course
  3256. * @param string Course code
  3257. * @param int Session id (optional), if param $session_id is
  3258. * null(default) it'll return results including sessions,
  3259. * 0 = session is not filtered
  3260. * @param int $groupId
  3261. * @return int The number of post by course
  3262. */
  3263. public static function count_number_of_posts_by_course($course_code, $session_id = null, $groupId = 0)
  3264. {
  3265. $courseInfo = api_get_course_info($course_code);
  3266. if (!empty($courseInfo)) {
  3267. $tbl_posts = Database::get_course_table(TABLE_FORUM_POST);
  3268. $tbl_forums = Database::get_course_table(TABLE_FORUM);
  3269. $condition_session = '';
  3270. if (isset($session_id)) {
  3271. $session_id = intval($session_id);
  3272. $condition_session = api_get_session_condition($session_id, true, false, 'f.session_id');
  3273. }
  3274. $course_id = $courseInfo['real_id'];
  3275. $groupId = intval($groupId);
  3276. if (!empty($groupId)) {
  3277. $groupCondition = " i.to_group_id = $groupId ";
  3278. } else {
  3279. $groupCondition = " (i.to_group_id = 0 OR i.to_group_id IS NULL) ";
  3280. }
  3281. $item = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3282. $sql = "SELECT count(*) FROM $tbl_posts p
  3283. INNER JOIN $tbl_forums f
  3284. ON f.forum_id = p.forum_id AND p.c_id = f.c_id
  3285. INNER JOIN $item i
  3286. ON (tool = '".TOOL_FORUM."' AND f.c_id = i.c_id AND f.iid = i.ref)
  3287. WHERE
  3288. p.c_id = $course_id AND
  3289. f.c_id = $course_id AND
  3290. $groupCondition
  3291. $condition_session
  3292. ";
  3293. $result = Database::query($sql);
  3294. $row = Database::fetch_row($result);
  3295. $count = $row[0];
  3296. return $count;
  3297. } else {
  3298. return null;
  3299. }
  3300. }
  3301. /**
  3302. * This function counts the number of threads by course
  3303. * @param string Course code
  3304. * @param int Session id (optional),
  3305. * if param $session_id is null(default) it'll return results including
  3306. * sessions, 0 = session is not filtered
  3307. * @param int $groupId
  3308. * @return int The number of threads by course
  3309. */
  3310. public static function count_number_of_threads_by_course($course_code, $session_id = null, $groupId = 0)
  3311. {
  3312. $course_info = api_get_course_info($course_code);
  3313. if (empty($course_info)) {
  3314. return null;
  3315. }
  3316. $course_id = $course_info['real_id'];
  3317. $tbl_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3318. $tbl_forums = Database::get_course_table(TABLE_FORUM);
  3319. $condition_session = '';
  3320. if (isset($session_id)) {
  3321. $session_id = intval($session_id);
  3322. $condition_session = ' AND f.session_id = '. $session_id;
  3323. }
  3324. $groupId = intval($groupId);
  3325. if (!empty($groupId)) {
  3326. $groupCondition = " i.to_group_id = $groupId ";
  3327. } else {
  3328. $groupCondition = " (i.to_group_id = 0 OR i.to_group_id IS NULL) ";
  3329. }
  3330. $item = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3331. $sql = "SELECT count(*)
  3332. FROM $tbl_threads t
  3333. INNER JOIN $tbl_forums f
  3334. ON f.iid = t.forum_id AND f.c_id = t.c_id
  3335. INNER JOIN $item i
  3336. ON (
  3337. tool = '".TOOL_FORUM_THREAD."' AND
  3338. f.c_id = i.c_id AND
  3339. t.iid = i.ref
  3340. )
  3341. WHERE
  3342. t.c_id = $course_id AND
  3343. f.c_id = $course_id AND
  3344. $groupCondition
  3345. $condition_session
  3346. ";
  3347. $result = Database::query($sql);
  3348. if (Database::num_rows($result)) {
  3349. $row = Database::fetch_row($result);
  3350. $count = $row[0];
  3351. return $count;
  3352. } else {
  3353. return null;
  3354. }
  3355. }
  3356. /**
  3357. * This function counts the number of forums by course
  3358. * @param string Course code
  3359. * @param int Session id (optional),
  3360. * if param $session_id is null(default) it'll return results
  3361. * including sessions, 0 = session is not filtered
  3362. * @param int $groupId
  3363. * @return int The number of forums by course
  3364. */
  3365. public static function count_number_of_forums_by_course($course_code, $session_id = null, $groupId = 0)
  3366. {
  3367. $course_info = api_get_course_info($course_code);
  3368. if (empty($course_info)) {
  3369. return null;
  3370. }
  3371. $course_id = $course_info['real_id'];
  3372. $condition_session = '';
  3373. if (isset($session_id)) {
  3374. $session_id = intval($session_id);
  3375. $condition_session = ' AND f.session_id = '. $session_id;
  3376. }
  3377. $groupId = intval($groupId);
  3378. if (!empty($groupId)) {
  3379. $groupCondition = " i.to_group_id = $groupId ";
  3380. } else {
  3381. $groupCondition = " (i.to_group_id = 0 OR i.to_group_id IS NULL) ";
  3382. }
  3383. $tbl_forums = Database::get_course_table(TABLE_FORUM);
  3384. $item = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3385. $sql = "SELECT count(*)
  3386. FROM $tbl_forums f
  3387. INNER JOIN $item i
  3388. ON f.c_id = i.c_id AND f.iid = i.ref AND tool = '".TOOL_FORUM."'
  3389. WHERE
  3390. f.c_id = $course_id AND
  3391. $groupCondition
  3392. $condition_session
  3393. ";
  3394. $result = Database::query($sql);
  3395. if (Database::num_rows($result)) {
  3396. $row = Database::fetch_row($result);
  3397. $count = $row[0];
  3398. return $count;
  3399. } else {
  3400. return null;
  3401. }
  3402. }
  3403. /**
  3404. * This function counts the chat last connections by course in x days
  3405. * @param string Course code
  3406. * @param int Last x days
  3407. * @param int Session id (optional)
  3408. * @return int Chat last connections by course in x days
  3409. */
  3410. public static function chat_connections_during_last_x_days_by_course($course_code, $last_days, $session_id = 0)
  3411. {
  3412. $course_info = api_get_course_info($course_code);
  3413. if (empty($course_info)) {
  3414. return null;
  3415. }
  3416. $course_id = $course_info['real_id'];
  3417. //protect data
  3418. $last_days = intval($last_days);
  3419. $session_id = intval($session_id);
  3420. $tbl_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3421. $now = api_get_utc_datetime();
  3422. $sql = "SELECT count(*) FROM $tbl_stats_access
  3423. WHERE
  3424. DATE_SUB('$now',INTERVAL $last_days DAY) <= access_date AND
  3425. c_id = '$course_id' AND
  3426. access_tool='".TOOL_CHAT."' AND
  3427. access_session_id='$session_id' ";
  3428. $result = Database::query($sql);
  3429. if (Database::num_rows($result)) {
  3430. $row = Database::fetch_row($result);
  3431. $count = $row[0];
  3432. return $count;
  3433. } else {
  3434. return null;
  3435. }
  3436. }
  3437. /**
  3438. * This function gets the last student's connection in chat
  3439. * @param int Student id
  3440. * @param string Course code
  3441. * @param int Session id (optional)
  3442. * @return string datetime formatted without day (e.g: February 23, 2010 10:20:50 )
  3443. */
  3444. public static function chat_last_connection($student_id, $courseId, $session_id = 0)
  3445. {
  3446. $student_id = intval($student_id);
  3447. $courseId = intval($courseId);
  3448. $session_id = intval($session_id);
  3449. $date_time = '';
  3450. // table definition
  3451. $tbl_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  3452. $sql = "SELECT access_date
  3453. FROM $tbl_stats_access
  3454. WHERE
  3455. access_tool='".TOOL_CHAT."' AND
  3456. access_user_id='$student_id' AND
  3457. c_id = $courseId AND
  3458. access_session_id = '$session_id'
  3459. ORDER BY access_date DESC limit 1";
  3460. $rs = Database::query($sql);
  3461. if (Database::num_rows($rs) > 0) {
  3462. $row = Database::fetch_array($rs);
  3463. $date_time = api_convert_and_format_date(
  3464. $row['access_date'],
  3465. null,
  3466. date_default_timezone_get()
  3467. );
  3468. }
  3469. return $date_time;
  3470. }
  3471. /**
  3472. * Get count student's visited links
  3473. * @param int $student_id Student id
  3474. * @param int $courseId
  3475. * @param int $session_id Session id (optional)
  3476. * @return int count of visited links
  3477. */
  3478. public static function count_student_visited_links($student_id, $courseId, $session_id = 0)
  3479. {
  3480. $student_id = intval($student_id);
  3481. $courseId = intval($courseId);
  3482. $session_id = intval($session_id);
  3483. // table definition
  3484. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
  3485. $sql = 'SELECT 1
  3486. FROM '.$table.'
  3487. WHERE
  3488. links_user_id= '.$student_id.' AND
  3489. c_id = "'.$courseId.'" AND
  3490. links_session_id = '.$session_id.' ';
  3491. $rs = Database::query($sql);
  3492. return Database::num_rows($rs);
  3493. }
  3494. /**
  3495. * Get count student downloaded documents
  3496. * @param int Student id
  3497. * @param int $courseId
  3498. * @param int Session id (optional)
  3499. * @return int Count downloaded documents
  3500. */
  3501. public static function count_student_downloaded_documents($student_id, $courseId, $session_id = 0)
  3502. {
  3503. $student_id = intval($student_id);
  3504. $courseId = intval($courseId);
  3505. $session_id = intval($session_id);
  3506. // table definition
  3507. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3508. $sql = 'SELECT 1
  3509. FROM ' . $table . '
  3510. WHERE down_user_id = '.$student_id.'
  3511. AND c_id = "'.$courseId.'"
  3512. AND down_session_id = '.$session_id.' ';
  3513. $rs = Database::query($sql);
  3514. return Database::num_rows($rs);
  3515. }
  3516. /**
  3517. * Get course list inside a session from a student
  3518. * @param int $user_id Student id
  3519. * @param int $id_session Session id (optional)
  3520. * @return array Courses list
  3521. */
  3522. public static function get_course_list_in_session_from_student($user_id, $id_session = 0)
  3523. {
  3524. $user_id = intval($user_id);
  3525. $id_session = intval($id_session);
  3526. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3527. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3528. $sql = "SELECT c.code
  3529. FROM $tbl_session_course_user sc
  3530. INNER JOIN $courseTable c
  3531. WHERE
  3532. user_id= $user_id AND
  3533. session_id = $id_session";
  3534. $result = Database::query($sql);
  3535. $courses = array();
  3536. while ($row = Database::fetch_array($result)) {
  3537. $courses[$row['code']] = $row['code'];
  3538. }
  3539. return $courses;
  3540. }
  3541. /**
  3542. * Get inactive students in course
  3543. * @param int $courseId
  3544. * @param string $since Since login course date (optional, default = 'never')
  3545. * @param int $session_id (optional)
  3546. * @return array Inactive users
  3547. */
  3548. public static function getInactiveStudentsInCourse($courseId, $since = 'never', $session_id = 0)
  3549. {
  3550. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  3551. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3552. $table_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3553. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  3554. $now = api_get_utc_datetime();
  3555. $courseId = intval($courseId);
  3556. if (empty($courseId)) {
  3557. return false;
  3558. }
  3559. if (empty($session_id)) {
  3560. $inner = '
  3561. INNER JOIN '.$table_course_rel_user.' course_user
  3562. ON course_user.user_id = stats_login.user_id AND course_user.c_id = c.id
  3563. ';
  3564. } else {
  3565. $inner = '
  3566. INNER JOIN '.$tbl_session_course_user.' session_course_user
  3567. ON
  3568. c.id = session_course_user.c_id AND
  3569. session_course_user.session_id = '.intval($session_id).' AND
  3570. session_course_user.user_id = stats_login.user_id ';
  3571. }
  3572. $sql = 'SELECT stats_login.user_id, MAX(login_course_date) max_date
  3573. FROM '.$tbl_track_login.' stats_login
  3574. INNER JOIN '.$tableCourse.' c
  3575. ON (c.id = stats_login.c_id)
  3576. '.$inner.'
  3577. WHERE c.id = '.$courseId.'
  3578. GROUP BY stats_login.user_id
  3579. HAVING DATE_SUB( "' . $now . '", INTERVAL '.$since.' DAY) > max_date ';
  3580. if ($since == 'never') {
  3581. if (empty($session_id)) {
  3582. $sql = 'SELECT course_user.user_id
  3583. FROM ' . $table_course_rel_user . ' course_user
  3584. LEFT JOIN ' . $tbl_track_login . ' stats_login
  3585. ON course_user.user_id = stats_login.user_id AND
  3586. relation_type<>' . COURSE_RELATION_TYPE_RRHH . '
  3587. INNER JOIN ' . $tableCourse . ' c
  3588. ON (c.id = course_user.c_id)
  3589. WHERE
  3590. course_user.c_id = ' . $courseId . ' AND
  3591. stats_login.login_course_date IS NULL
  3592. GROUP BY course_user.user_id';
  3593. } else {
  3594. $sql = 'SELECT session_course_user.user_id
  3595. FROM '.$tbl_session_course_user.' session_course_user
  3596. LEFT JOIN ' . $tbl_track_login . ' stats_login
  3597. ON session_course_user.user_id = stats_login.user_id
  3598. INNER JOIN ' . $tableCourse . ' c
  3599. ON (c.id = session_course_user.c_id)
  3600. WHERE
  3601. session_course_user.c_id = ' . $courseId . ' AND
  3602. stats_login.login_course_date IS NULL
  3603. GROUP BY session_course_user.user_id';
  3604. }
  3605. }
  3606. $rs = Database::query($sql);
  3607. $inactive_users = array();
  3608. while($user = Database::fetch_array($rs)) {
  3609. $inactive_users[] = $user['user_id'];
  3610. }
  3611. return $inactive_users;
  3612. }
  3613. /**
  3614. * Get count login per student
  3615. * @param int $student_id Student id
  3616. * @param int $courseId
  3617. * @param int $session_id Session id (optional)
  3618. * @return int count login
  3619. */
  3620. public static function count_login_per_student($student_id, $courseId, $session_id = 0)
  3621. {
  3622. $student_id = intval($student_id);
  3623. $courseId = intval($courseId);
  3624. $session_id = intval($session_id);
  3625. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3626. $sql = 'SELECT '.$student_id.'
  3627. FROM ' . $table . '
  3628. WHERE
  3629. access_user_id=' . $student_id . ' AND
  3630. c_id="' . $courseId . '" AND
  3631. access_session_id = "'.$session_id.'" ';
  3632. $rs = Database::query($sql);
  3633. $nb_login = Database::num_rows($rs);
  3634. return $nb_login;
  3635. }
  3636. /**
  3637. * Get students followed by a human resources manager
  3638. * @param int Drh id
  3639. * @return array Student list
  3640. */
  3641. public static function get_student_followed_by_drh($hr_dept_id)
  3642. {
  3643. $hr_dept_id = intval($hr_dept_id);
  3644. $a_students = array();
  3645. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  3646. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  3647. WHERE hr_dept_id='.$hr_dept_id;
  3648. $rs = Database::query($sql);
  3649. while($user = Database::fetch_array($rs)) {
  3650. $a_students[$user['user_id']] = $user['user_id'];
  3651. }
  3652. return $a_students;
  3653. }
  3654. /**
  3655. * get count clicks about tools most used by course
  3656. * @param int $courseId
  3657. * @param int Session id (optional),
  3658. * if param $session_id is null(default) it'll return results
  3659. * including sessions, 0 = session is not filtered
  3660. * @return array tools data
  3661. */
  3662. public static function get_tools_most_used_by_course($courseId, $session_id = null)
  3663. {
  3664. $courseId = intval($courseId);
  3665. $data = array();
  3666. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  3667. $condition_session = '';
  3668. if (isset($session_id)) {
  3669. $session_id = intval($session_id);
  3670. $condition_session = ' AND access_session_id = '. $session_id;
  3671. }
  3672. $sql = "SELECT
  3673. access_tool,
  3674. COUNT(DISTINCT access_user_id),
  3675. count(access_tool) as count_access_tool
  3676. FROM $TABLETRACK_ACCESS
  3677. WHERE
  3678. access_tool IS NOT NULL AND
  3679. access_tool != '' AND
  3680. c_id = '$courseId'
  3681. $condition_session
  3682. GROUP BY access_tool
  3683. ORDER BY count_access_tool DESC
  3684. LIMIT 0, 3";
  3685. $rs = Database::query($sql);
  3686. if (Database::num_rows($rs) > 0) {
  3687. while ($row = Database::fetch_array($rs)) {
  3688. $data[] = $row;
  3689. }
  3690. }
  3691. return $data;
  3692. }
  3693. /**
  3694. * Get total clicks
  3695. * THIS FUNCTION IS NOT BEEN USED, IT WAS MEANT TO BE USE WITH track_e_course_access.date_from and track_e_course_access.date_to,
  3696. * BUT NO ROW MATCH THE CONDITION, IT SHOULD BE FINE TO USE IT WHEN YOU USE USER DEFINED DATES AND NO CHAMILO DATES
  3697. * @param int User Id
  3698. * @param int Course Id
  3699. * @param int Session Id (optional), if param $session_id is 0 (default) it'll return results including sessions, 0 = session is not filtered
  3700. * @param string Date from
  3701. * @param string Date to
  3702. * @return array Data
  3703. * @author César Perales cesar.perales@beeznest.com 2014-01-16
  3704. */
  3705. public static function get_total_clicks($userId, $courseId, $sessionId = 0, $date_from = '', $date_to = '')
  3706. {
  3707. $course = api_get_course_info_by_id($courseId);
  3708. $tables = array(
  3709. TABLE_STATISTIC_TRACK_E_LASTACCESS => array(
  3710. 'course' => 'c_id',
  3711. 'session' => 'access_session_id',
  3712. 'user' => 'access_user_id',
  3713. 'start_date'=> 'access_date',
  3714. ),
  3715. TABLE_STATISTIC_TRACK_E_ACCESS => array(
  3716. 'course' => 'c_id',
  3717. 'session' => 'access_session_id',
  3718. 'user' => 'access_user_id',
  3719. 'start_date'=> 'access_date',
  3720. ),
  3721. #TABLE_STATISTIC_TRACK_E_LOGIN, array(,, 'login_date', 'logout_date');
  3722. TABLE_STATISTIC_TRACK_E_DOWNLOADS => array(
  3723. 'course' => 'c_id',
  3724. 'session' => 'down_session_id',
  3725. 'user' => 'down_user_id',
  3726. 'start_date'=> 'down_date',
  3727. ),
  3728. TABLE_STATISTIC_TRACK_E_LINKS => array(
  3729. 'course' => 'c_id',
  3730. 'session' => 'links_session_id',
  3731. 'user' => 'links_user_id',
  3732. 'start_date'=> 'links_date',
  3733. ),
  3734. TABLE_STATISTIC_TRACK_E_ONLINE => array(
  3735. 'course' => 'c_id',
  3736. 'session' => 'session_id',
  3737. 'user' => 'login_user_id',
  3738. 'start_date'=> 'login_date',
  3739. ),
  3740. #TABLE_STATISTIC_TRACK_E_HOTPOTATOES,
  3741. /*TABLE_STATISTIC_TRACK_E_COURSE_ACCESS => array(
  3742. 'course' => 'c_id',
  3743. 'session' => 'session_id',
  3744. 'user' => 'user_id',
  3745. 'start_date'=> 'login_course_date',
  3746. 'end_date' => 'logout_course_date',
  3747. ),*/
  3748. TABLE_STATISTIC_TRACK_E_EXERCISES => array(
  3749. 'course' => 'c_id',
  3750. 'session' => 'session_id',
  3751. 'user' => 'exe_user_id',
  3752. 'start_date'=> 'exe_date',
  3753. ),
  3754. TABLE_STATISTIC_TRACK_E_ATTEMPT => array(
  3755. 'course' => 'c_id',
  3756. 'session' => 'session_id',
  3757. 'user' => 'user_id',
  3758. 'start_date'=> 'tms',
  3759. ),
  3760. #TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING,
  3761. #TABLE_STATISTIC_TRACK_E_DEFAULT,
  3762. TABLE_STATISTIC_TRACK_E_UPLOADS => array(
  3763. 'course' => 'c_id',
  3764. 'session' => 'upload_session_id',
  3765. 'user' => 'upload_user_id',
  3766. 'start_date'=> 'upload_date',
  3767. ),
  3768. );
  3769. foreach ($tables as $tableName => $fields) {
  3770. //If session is defined, add it to query
  3771. $where = '';
  3772. if (isset($sessionId) && !empty($sessionId)) {
  3773. $sessionField = $fields['session'];
  3774. $where .= " AND $sessionField = $sessionId";
  3775. }
  3776. //filter by date
  3777. if (!empty($date_from) && !empty($date_to)) {
  3778. $fieldStartDate = $fields['start_date'];
  3779. if (!isset($fields['end_date'])) {
  3780. $where .= sprintf(" AND ($fieldStartDate BETWEEN '%s' AND '%s' )", $date_from, $date_to) ;
  3781. } else {
  3782. $fieldEndDate = $fields['end_date'];
  3783. $where .= sprintf(" AND fieldStartDate >= '%s'
  3784. AND $fieldEndDate <= '%s'", $date_from, $date_to);
  3785. }
  3786. }
  3787. //query
  3788. $sql = "SELECT %s as user, count(*) as total
  3789. FROM %s
  3790. WHERE %s = '%s'
  3791. AND %s = %s
  3792. $where
  3793. GROUP BY %s";
  3794. $sql = sprintf($sql,
  3795. $fields['user'], //user field
  3796. $tableName, //FROM
  3797. $fields['course'], //course condition
  3798. $course['real_id'], //course condition
  3799. $fields['user'], //user condition
  3800. $userId, //user condition
  3801. $fields['user'] //GROUP BY
  3802. );
  3803. $rs = Database::query($sql);
  3804. //iterate query
  3805. if (Database::num_rows($rs) > 0) {
  3806. while ($row = Database::fetch_array($rs)) {
  3807. $data[$row['user']] = (isset($data[$row['user']])) ? $data[$row['user']] + $row[total]: $row['total'];
  3808. }
  3809. }
  3810. }
  3811. return $data;
  3812. }
  3813. /**
  3814. * get documents most downloaded by course
  3815. * @param string Course code
  3816. * @param int Session id (optional),
  3817. * if param $session_id is null(default) it'll return results including
  3818. * sessions, 0 = session is not filtered
  3819. * @param int Limit (optional, default = 0, 0 = without limit)
  3820. * @return array documents downloaded
  3821. */
  3822. public static function get_documents_most_downloaded_by_course($course_code, $session_id = 0, $limit = 0)
  3823. {
  3824. //protect data
  3825. $courseId = api_get_course_int_id($course_code);
  3826. $data = array();
  3827. $TABLETRACK_DOWNLOADS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3828. $condition_session = '';
  3829. $session_id = intval($session_id);
  3830. if (!empty($session_id)) {
  3831. $condition_session = ' AND down_session_id = '. $session_id;
  3832. }
  3833. $sql = "SELECT
  3834. down_doc_path,
  3835. COUNT(DISTINCT down_user_id),
  3836. COUNT(down_doc_path) as count_down
  3837. FROM $TABLETRACK_DOWNLOADS
  3838. WHERE c_id = $courseId
  3839. $condition_session
  3840. GROUP BY down_doc_path
  3841. ORDER BY count_down DESC
  3842. LIMIT 0, $limit";
  3843. $rs = Database::query($sql);
  3844. if (Database::num_rows($rs) > 0) {
  3845. while ($row = Database::fetch_array($rs)) {
  3846. $data[] = $row;
  3847. }
  3848. }
  3849. return $data;
  3850. }
  3851. /**
  3852. * get links most visited by course
  3853. * @param string Course code
  3854. * @param int Session id (optional),
  3855. * if param $session_id is null(default) it'll
  3856. * return results including sessions, 0 = session is not filtered
  3857. * @return array links most visited
  3858. */
  3859. public static function get_links_most_visited_by_course($course_code, $session_id = null)
  3860. {
  3861. $course_code = Database::escape_string($course_code);
  3862. $course_info = api_get_course_info($course_code);
  3863. $course_id = $course_info['real_id'];
  3864. $data = array();
  3865. $TABLETRACK_LINKS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
  3866. $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK);
  3867. $condition_session = '';
  3868. if (isset($session_id)) {
  3869. $session_id = intval($session_id);
  3870. $condition_session = ' AND cl.session_id = '.$session_id;
  3871. }
  3872. $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title) as count_visits
  3873. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  3874. WHERE
  3875. cl.c_id = $course_id AND
  3876. sl.links_link_id = cl.id AND
  3877. sl.c_id = $course_id
  3878. $condition_session
  3879. GROUP BY cl.title, cl.url
  3880. ORDER BY count_visits DESC
  3881. LIMIT 0, 3";
  3882. $rs = Database::query($sql);
  3883. if (Database::num_rows($rs) > 0) {
  3884. while ($row = Database::fetch_array($rs)) {
  3885. $data[] = $row;
  3886. }
  3887. }
  3888. return $data;
  3889. }
  3890. /**
  3891. * Shows the user progress (when clicking in the Progress tab)
  3892. *
  3893. * @param int $user_id
  3894. * @param int $session_id
  3895. * @param string $extra_params
  3896. * @param bool $show_courses
  3897. * @param bool $showAllSessions
  3898. *
  3899. * @return string
  3900. */
  3901. public static function show_user_progress(
  3902. $user_id,
  3903. $session_id = 0,
  3904. $extra_params = '',
  3905. $show_courses = true,
  3906. $showAllSessions = true
  3907. ) {
  3908. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3909. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  3910. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3911. $tbl_access_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3912. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3913. $tbl_access_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  3914. $user_id = intval($user_id);
  3915. $session_id = intval($session_id);
  3916. if (api_is_multiple_url_enabled()) {
  3917. $sql = "SELECT c.code, title
  3918. FROM $tbl_course_user cu
  3919. INNER JOIN $tbl_course c
  3920. ON (cu.c_id = c.id)
  3921. INNER JOIN $tbl_access_rel_course a
  3922. ON (a.c_id = c.id)
  3923. WHERE
  3924. cu.user_id = $user_id AND
  3925. relation_type<> ".COURSE_RELATION_TYPE_RRHH." AND
  3926. access_url_id = ".api_get_current_access_url_id()."
  3927. ORDER BY title";
  3928. } else {
  3929. $sql = "SELECT c.code, title
  3930. FROM $tbl_course_user u
  3931. INNER JOIN $tbl_course c ON (c_id = c.id)
  3932. WHERE
  3933. u.user_id= $user_id AND
  3934. relation_type<>".COURSE_RELATION_TYPE_RRHH."
  3935. ORDER BY title";
  3936. }
  3937. $rs = Database::query($sql);
  3938. $courses = $course_in_session = $temp_course_in_session = array();
  3939. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3940. $courses[$row['code']] = $row['title'];
  3941. }
  3942. $orderBy = " ORDER BY name ";
  3943. $extraInnerJoin = null;
  3944. if (SessionManager::orderCourseIsEnabled() && !empty($session_id)) {
  3945. $orderBy = " ORDER BY s.id, position ";
  3946. $tableSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  3947. $extraInnerJoin = " INNER JOIN $tableSessionRelCourse src
  3948. ON (cu.c_id = src.c_id AND src.session_id = $session_id) ";
  3949. }
  3950. $sessionCondition = '';
  3951. if (!empty($session_id)) {
  3952. $sessionCondition = " AND s.id = $session_id";
  3953. }
  3954. // Get the list of sessions where the user is subscribed as student
  3955. if (api_is_multiple_url_enabled()) {
  3956. $sql = "SELECT DISTINCT c.code, s.id as session_id, name
  3957. FROM $tbl_session_course_user cu
  3958. INNER JOIN $tbl_access_rel_session a
  3959. ON (a.session_id = cu.session_id)
  3960. INNER JOIN $tbl_session s
  3961. ON (s.id = a.session_id)
  3962. INNER JOIN $tbl_course c
  3963. ON (c.id = cu.c_id)
  3964. $extraInnerJoin
  3965. WHERE
  3966. cu.user_id = $user_id AND
  3967. access_url_id = ".api_get_current_access_url_id()."
  3968. $sessionCondition
  3969. $orderBy ";
  3970. } else {
  3971. $sql = "SELECT DISTINCT c.code, s.id as session_id, name
  3972. FROM $tbl_session_course_user cu
  3973. INNER JOIN $tbl_session s
  3974. ON (s.id = cu.session_id)
  3975. INNER JOIN $tbl_course c
  3976. ON (c.id = cu.c_id)
  3977. $extraInnerJoin
  3978. WHERE
  3979. cu.user_id = $user_id
  3980. $sessionCondition
  3981. $orderBy ";
  3982. }
  3983. $rs = Database::query($sql);
  3984. $simple_session_array = array();
  3985. while ($row = Database::fetch_array($rs)) {
  3986. $course_info = CourseManager::get_course_information($row['code']);
  3987. $temp_course_in_session[$row['session_id']]['course_list'][$course_info['real_id']] = $course_info;
  3988. $temp_course_in_session[$row['session_id']]['name'] = $row['name'];
  3989. $simple_session_array[$row['session_id']] = $row['name'];
  3990. }
  3991. foreach ($simple_session_array as $my_session_id => $session_name) {
  3992. $course_list = $temp_course_in_session[$my_session_id]['course_list'];
  3993. $my_course_data = array();
  3994. foreach ($course_list as $course_data) {
  3995. $my_course_data[$course_data['id']] = $course_data['title'];
  3996. }
  3997. if (empty($session_id)) {
  3998. $my_course_data = utf8_sort($my_course_data);
  3999. }
  4000. $final_course_data = array();
  4001. foreach($my_course_data as $course_id => $value) {
  4002. $final_course_data[$course_id] = $course_list[$course_id];
  4003. }
  4004. $course_in_session[$my_session_id]['course_list'] = $final_course_data;
  4005. $course_in_session[$my_session_id]['name'] = $session_name;
  4006. }
  4007. $html = '';
  4008. // Course list
  4009. if ($show_courses) {
  4010. if (!empty($courses)) {
  4011. $html .= Display::page_subheader(
  4012. Display::return_icon('course.png', get_lang('MyCourses'), array(), ICON_SIZE_SMALL).' '.get_lang('MyCourses')
  4013. );
  4014. $html .= '<div class="table-responsive">';
  4015. $html .= '<table class="table table-striped table-hover">';
  4016. $html .= '<thead>';
  4017. $html .= '<tr>
  4018. '.Display::tag('th', get_lang('Course'), array('width'=>'300px')).'
  4019. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  4020. '.Display::tag('th', get_lang('Progress'), array('class'=>'head')).'
  4021. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array('align' => 'absmiddle', 'hspace' => '3px')),array('class'=>'head')).'
  4022. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  4023. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  4024. </tr>';
  4025. $html .= '</thead><tbody>';
  4026. foreach ($courses as $course_code => $course_title) {
  4027. $courseInfo = api_get_course_info($course_code);
  4028. $courseId = $courseInfo['real_id'];
  4029. $total_time_login = self::get_time_spent_on_the_course(
  4030. $user_id,
  4031. $courseId
  4032. );
  4033. $time = api_time_to_hms($total_time_login);
  4034. $progress = self::get_avg_student_progress(
  4035. $user_id,
  4036. $course_code
  4037. );
  4038. $percentage_score = self::get_avg_student_score(
  4039. $user_id,
  4040. $course_code,
  4041. array()
  4042. );
  4043. $last_connection = self::get_last_connection_date_on_the_course(
  4044. $user_id,
  4045. $courseInfo
  4046. );
  4047. if (is_null($progress)) {
  4048. $progress = '0%';
  4049. } else {
  4050. $progress = $progress.'%';
  4051. }
  4052. if (isset($_GET['course']) &&
  4053. $course_code == $_GET['course'] &&
  4054. empty($_GET['session_id'])
  4055. ) {
  4056. $html .= '<tr class="row_odd" style="background-color:#FBF09D">';
  4057. } else {
  4058. $html .= '<tr class="row_even">';
  4059. }
  4060. $url = api_get_course_url($course_code, $session_id);
  4061. $course_url = Display::url($course_title, $url, array('target'=>SESSION_LINK_TARGET));
  4062. $html .= '<td>'.$course_url.'</td>';
  4063. $html .= '<td align="center">'.$time.'</td>';
  4064. $html .= '<td align="center">'.$progress.'</td>';
  4065. $html .= '<td align="center">';
  4066. if (is_numeric($percentage_score)) {
  4067. $html .= $percentage_score.'%';
  4068. } else {
  4069. $html .= '0%';
  4070. }
  4071. $html .= '</td>';
  4072. $html .= '<td align="center">'.$last_connection.'</td>';
  4073. $html .= '<td align="center">';
  4074. if (isset($_GET['course']) &&
  4075. $course_code == $_GET['course'] &&
  4076. empty($_GET['session_id'])
  4077. ) {
  4078. $html .= '<a href="#course_session_header">';
  4079. $html .= Display::return_icon('2rightarrow_na.png', get_lang('Details'));
  4080. } else {
  4081. $html .= '<a href="'.api_get_self().'?course='.$course_code.$extra_params.'#course_session_header">';
  4082. $html .= Display::return_icon('2rightarrow.png', get_lang('Details'));
  4083. }
  4084. $html .= '</a>';
  4085. $html .= '</td></tr>';
  4086. }
  4087. $html .= '</tbody></table>';
  4088. $html .= '</div>';
  4089. }
  4090. }
  4091. // Session list
  4092. if (!empty($course_in_session)) {
  4093. $main_session_graph = '';
  4094. //Load graphics only when calling to an specific session
  4095. $session_graph = array();
  4096. $all_exercise_graph_name_list = array();
  4097. $my_results = array();
  4098. $all_exercise_graph_list = array();
  4099. $all_exercise_start_time = array();
  4100. foreach ($course_in_session as $my_session_id => $session_data) {
  4101. $course_list = $session_data['course_list'];
  4102. $user_count = count(SessionManager::get_users_by_session($my_session_id));
  4103. $exercise_graph_name_list = array();
  4104. //$user_results = array();
  4105. $exercise_graph_list = array();
  4106. foreach ($course_list as $course_data) {
  4107. $exercise_list = ExerciseLib::get_all_exercises(
  4108. $course_data,
  4109. $my_session_id,
  4110. false,
  4111. null,
  4112. false,
  4113. 1
  4114. );
  4115. foreach ($exercise_list as $exercise_data) {
  4116. $exercise_obj = new Exercise($course_data['id']);
  4117. $exercise_obj->read($exercise_data['id']);
  4118. //Exercise is not necessary to be visible to show results check the result_disable configuration instead
  4119. //$visible_return = $exercise_obj->is_visible();
  4120. if ($exercise_data['results_disabled'] == 0 || $exercise_data['results_disabled'] == 2) {
  4121. $best_average = intval(
  4122. ExerciseLib::get_best_average_score_by_exercise(
  4123. $exercise_data['id'],
  4124. $course_data['id'],
  4125. $my_session_id,
  4126. $user_count
  4127. )
  4128. );
  4129. $exercise_graph_list[] = $best_average;
  4130. $all_exercise_graph_list[] = $best_average;
  4131. $user_result_data = ExerciseLib::get_best_attempt_by_user(
  4132. api_get_user_id(),
  4133. $exercise_data['id'],
  4134. $course_data['real_id'],
  4135. $my_session_id
  4136. );
  4137. $score = 0;
  4138. if (!empty($user_result_data['exe_weighting']) && intval($user_result_data['exe_weighting']) != 0) {
  4139. $score = intval($user_result_data['exe_result']/$user_result_data['exe_weighting'] * 100);
  4140. }
  4141. $time = api_strtotime($exercise_data['start_time']) ? api_strtotime($exercise_data['start_time'], 'UTC') : 0;
  4142. $all_exercise_start_time[] = $time;
  4143. $my_results[] = $score;
  4144. if (count($exercise_list)<=10) {
  4145. $title = cut($course_data['title'], 30)." \n ".cut($exercise_data['title'], 30);
  4146. $exercise_graph_name_list[]= $title;
  4147. $all_exercise_graph_name_list[] = $title;
  4148. } else {
  4149. // if there are more than 10 results, space becomes difficult to find, so only show the title of the exercise, not the tool
  4150. $title = cut($exercise_data['title'], 30);
  4151. $exercise_graph_name_list[]= $title;
  4152. $all_exercise_graph_name_list[]= $title;
  4153. }
  4154. }
  4155. }
  4156. }
  4157. }
  4158. // Complete graph
  4159. if (!empty($my_results) && !empty($all_exercise_graph_list)) {
  4160. asort($all_exercise_start_time);
  4161. //Fix exams order
  4162. $final_all_exercise_graph_name_list = array();
  4163. $my_results_final = array();
  4164. $final_all_exercise_graph_list = array();
  4165. foreach ($all_exercise_start_time as $key => $time) {
  4166. $label_time = '';
  4167. if (!empty($time)) {
  4168. $label_time = date('d-m-y', $time);
  4169. }
  4170. $final_all_exercise_graph_name_list[] = $all_exercise_graph_name_list[$key].' '.$label_time;
  4171. $my_results_final[] = $my_results[$key];
  4172. $final_all_exercise_graph_list[] = $all_exercise_graph_list[$key];
  4173. }
  4174. $main_session_graph = self::generate_session_exercise_graph(
  4175. $final_all_exercise_graph_name_list,
  4176. $my_results_final,
  4177. $final_all_exercise_graph_list
  4178. );
  4179. }
  4180. $sessionIcon = Display::return_icon(
  4181. 'session.png',
  4182. get_lang('Sessions'),
  4183. array(),
  4184. ICON_SIZE_SMALL
  4185. );
  4186. $anchor = Display::url('', '', ['name' => 'course_session_header']);
  4187. $html .= $anchor.Display::page_subheader(
  4188. $sessionIcon.' '.get_lang('Sessions')
  4189. );
  4190. $html .= '<div class="table-responsive">';
  4191. $html .= '<table class="table table-striped table-hover">';
  4192. $html .= '<thead>';
  4193. $html .= '<tr>
  4194. '.Display::tag('th', get_lang('Session'), array('width'=>'300px')).'
  4195. '.Display::tag('th', get_lang('PublishedExercises'), array('width'=>'300px')).'
  4196. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  4197. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  4198. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  4199. </tr>';
  4200. $html .= '</thead>';
  4201. $html .= '<tbody>';
  4202. foreach ($course_in_session as $my_session_id => $session_data) {
  4203. $course_list = $session_data['course_list'];
  4204. $session_name = $session_data['name'];
  4205. if ($showAllSessions == false) {
  4206. if (isset($session_id) && !empty($session_id)) {
  4207. if ($session_id != $my_session_id) {
  4208. continue;
  4209. }
  4210. }
  4211. }
  4212. $all_exercises = 0;
  4213. $all_unanswered_exercises_by_user = 0;
  4214. $all_average = 0;
  4215. $stats_array = array();
  4216. foreach ($course_list as $course_data) {
  4217. //All exercises in the course @todo change for a real count
  4218. $exercises = ExerciseLib::get_all_exercises($course_data, $my_session_id);
  4219. $count_exercises = 0;
  4220. if (is_array($exercises) && !empty($exercises)) {
  4221. $count_exercises = count($exercises);
  4222. }
  4223. // Count of user results
  4224. $done_exercises = null;
  4225. $courseInfo = api_get_course_info($course_data['code']);
  4226. $answered_exercises = 0;
  4227. if (!empty($exercises)) {
  4228. foreach ($exercises as $exercise_item) {
  4229. $attempts = Event::count_exercise_attempts_by_user(
  4230. api_get_user_id(),
  4231. $exercise_item['id'],
  4232. $courseInfo['real_id'],
  4233. $my_session_id
  4234. );
  4235. if ($attempts > 1) {
  4236. $answered_exercises++;
  4237. }
  4238. }
  4239. }
  4240. // Average
  4241. $average = ExerciseLib::get_average_score_by_course($courseInfo['real_id'], $my_session_id);
  4242. $all_exercises += $count_exercises;
  4243. $all_unanswered_exercises_by_user += $count_exercises - $answered_exercises;
  4244. $all_average += $average;
  4245. }
  4246. $all_average = $all_average / count($course_list);
  4247. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  4248. $html .= '<tr style="background-color:#FBF09D">';
  4249. } else {
  4250. $html .= '<tr>';
  4251. }
  4252. $url = api_get_path(WEB_CODE_PATH)."session/index.php?session_id={$my_session_id}";
  4253. $html .= Display::tag('td', Display::url($session_name, $url, array('target'=>SESSION_LINK_TARGET)));
  4254. $html .= Display::tag('td', $all_exercises);
  4255. $html .= Display::tag('td', $all_unanswered_exercises_by_user);
  4256. //$html .= Display::tag('td', $all_done_exercise);
  4257. $html .= Display::tag('td', ExerciseLib::convert_to_percentage($all_average));
  4258. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  4259. $icon = Display::url(Display::return_icon('2rightarrow_na.png', get_lang('Details')), '?session_id='.$my_session_id);
  4260. } else {
  4261. $icon = Display::url(Display::return_icon('2rightarrow.png', get_lang('Details')), '?session_id='.$my_session_id);
  4262. }
  4263. $html .= Display::tag('td', $icon);
  4264. $html .= '</tr>';
  4265. }
  4266. $html .= '</tbody>';
  4267. $html .= '</table></div><br />';
  4268. $html .= Display::div($main_session_graph, array('id'=>'session_graph','class'=>'chart-session', 'style'=>'position:relative; text-align: center;') );
  4269. // Checking selected session.
  4270. if (isset($_GET['session_id'])) {
  4271. $session_id_from_get = intval($_GET['session_id']);
  4272. $session_data = $course_in_session[$session_id_from_get];
  4273. $course_list = $session_data['course_list'];
  4274. $html .= Display::tag('h3',$session_data['name'].' - '.get_lang('CourseList'));
  4275. $html .= '<div class="table-responsive">';
  4276. $html .= '<table class="table table-hover table-striped">';
  4277. //'.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  4278. $html .= '
  4279. <thead>
  4280. <tr>
  4281. <th width="300px">'.get_lang('Course').'</th>
  4282. '.Display::tag('th', get_lang('PublishedExercises'), array('class'=>'head')).'
  4283. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  4284. '.Display::tag('th', get_lang('MyAverage'), array('class'=>'head')).'
  4285. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  4286. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  4287. '.Display::tag('th', get_lang('LPProgress') , array('class'=>'head')).'
  4288. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array ('align' => 'absmiddle', 'hspace' => '3px')), array('class'=>'head')).'
  4289. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  4290. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  4291. </tr>
  4292. </thead>
  4293. <tbody>
  4294. ';
  4295. foreach ($course_list as $course_data) {
  4296. $course_code = $course_data['code'];
  4297. $course_title = $course_data['title'];
  4298. $courseInfo = api_get_course_info($course_code);
  4299. $courseId = $courseInfo['real_id'];
  4300. // All exercises in the course @todo change for a real count
  4301. $exercises = ExerciseLib::get_all_exercises($course_data, $session_id_from_get);
  4302. $count_exercises = 0;
  4303. if (!empty($exercises)) {
  4304. $count_exercises = count($exercises);
  4305. }
  4306. $answered_exercises = 0;
  4307. foreach($exercises as $exercise_item) {
  4308. $attempts = Event::count_exercise_attempts_by_user(
  4309. api_get_user_id(),
  4310. $exercise_item['id'],
  4311. $courseId,
  4312. $session_id_from_get
  4313. );
  4314. if ($attempts > 1) {
  4315. $answered_exercises++;
  4316. }
  4317. }
  4318. $unanswered_exercises = $count_exercises - $answered_exercises;
  4319. // Average
  4320. $average = ExerciseLib::get_average_score_by_course($courseId, $session_id_from_get);
  4321. $my_average = ExerciseLib::get_average_score_by_course_by_user(api_get_user_id(), $courseId, $session_id_from_get);
  4322. $stats_array[$course_code] = array(
  4323. 'exercises' => $count_exercises,
  4324. 'unanswered_exercises_by_user' => $unanswered_exercises,
  4325. 'done_exercises' => $done_exercises,
  4326. 'average' => $average,
  4327. 'my_average' => $my_average
  4328. );
  4329. $last_connection = self::get_last_connection_date_on_the_course(
  4330. $user_id,
  4331. $courseInfo,
  4332. $session_id_from_get
  4333. );
  4334. $progress = self::get_avg_student_progress(
  4335. $user_id,
  4336. $course_code,
  4337. array(),
  4338. $session_id_from_get
  4339. );
  4340. $total_time_login = self::get_time_spent_on_the_course(
  4341. $user_id,
  4342. $courseId,
  4343. $session_id_from_get
  4344. );
  4345. $time = api_time_to_hms($total_time_login);
  4346. $percentage_score = self::get_avg_student_score(
  4347. $user_id,
  4348. $course_code,
  4349. array(),
  4350. $session_id_from_get
  4351. );
  4352. $courseCodeFromGet = isset($_GET['course']) ? $_GET['course'] : null;
  4353. if ($course_code == $courseCodeFromGet && $_GET['session_id'] == $session_id_from_get) {
  4354. $html .= '<tr class="row_odd" style="background-color:#FBF09D" >';
  4355. } else {
  4356. $html .= '<tr class="row_even">';
  4357. }
  4358. $url = api_get_course_url($course_code, $session_id_from_get);
  4359. $course_url = Display::url($course_title, $url, array('target' => SESSION_LINK_TARGET));
  4360. $html .= Display::tag('td', $course_url);
  4361. $html .= Display::tag('td', $stats_array[$course_code]['exercises']);
  4362. $html .= Display::tag('td', $stats_array[$course_code]['unanswered_exercises_by_user']);
  4363. //$html .= Display::tag('td', $stats_array[$course_code]['done_exercises']);
  4364. $html .= Display::tag('td', ExerciseLib::convert_to_percentage($stats_array[$course_code]['my_average']));
  4365. $html .= Display::tag('td', $stats_array[$course_code]['average'] == 0 ? '-' : '('.ExerciseLib::convert_to_percentage($stats_array[$course_code]['average']).')');
  4366. $html .= Display::tag('td', $time, array('align'=>'center'));
  4367. if (is_numeric($progress)) {
  4368. $progress = $progress.'%';
  4369. } else {
  4370. $progress = '0%';
  4371. }
  4372. // Progress
  4373. $html .= Display::tag('td', $progress, array('align'=>'center'));
  4374. if (is_numeric($percentage_score)) {
  4375. $percentage_score = $percentage_score.'%';
  4376. } else {
  4377. $percentage_score = '0%';
  4378. }
  4379. //Score
  4380. $html .= Display::tag('td', $percentage_score, array('align'=>'center'));
  4381. if (empty($last_connection) or is_bool($last_connection)) {
  4382. $last_connection = '';
  4383. }
  4384. $html .= Display::tag('td', $last_connection, array('align' => 'center'));
  4385. if ($course_code == $courseCodeFromGet && $_GET['session_id'] == $session_id_from_get) {
  4386. $details = '<a href="#">';
  4387. $details .=Display::return_icon('2rightarrow_na.png', get_lang('Details'));
  4388. } else {
  4389. $details = '<a href="'.api_get_self().'?course='.$course_code.'&session_id='.$session_id_from_get.$extra_params.'">';
  4390. $details .= Display::return_icon('2rightarrow.png', get_lang('Details'));
  4391. }
  4392. $details .= '</a>';
  4393. $html .= Display::tag('td', $details, array('align'=>'center'));
  4394. $html .= '</tr>';
  4395. }
  4396. $html .= '</tbody></table></div>';
  4397. }
  4398. }
  4399. return $html;
  4400. }
  4401. /**
  4402. * Shows the user detail progress (when clicking in the details link)
  4403. * @param int $user_id
  4404. * @param string $course_code
  4405. * @param int $session_id
  4406. * @return string html code
  4407. */
  4408. public static function show_course_detail($user_id, $course_code, $session_id)
  4409. {
  4410. $html = '';
  4411. if (isset($course_code)) {
  4412. $user_id = intval($user_id);
  4413. $session_id = intval($session_id);
  4414. $course = Database::escape_string($course_code);
  4415. $course_info = CourseManager::get_course_information($course);
  4416. $html .= Display::page_subheader($course_info['title']);
  4417. $html .= '<div class="table-responsive">';
  4418. $html .= '<table class="table table-striped table-hover">';
  4419. //Course details
  4420. $html .= '
  4421. <thead>
  4422. <tr>
  4423. <th>'.get_lang('Exercises').'</th>
  4424. <th>'.get_lang('Attempts').'</th>
  4425. <th>'.get_lang('BestAttempt').'</th>
  4426. <th>'.get_lang('Ranking').'</th>
  4427. <th>'.get_lang('BestResultInCourse').'</th>
  4428. <th>'.get_lang('Statistics').' '.Display::return_icon('info3.gif', get_lang('OnlyBestResultsPerStudent'), array('align' => 'absmiddle', 'hspace' => '3px')).'</th>
  4429. </tr>
  4430. </thead>
  4431. <tbody>';
  4432. if (empty($session_id)) {
  4433. $user_list = CourseManager::get_user_list_from_course_code(
  4434. $course,
  4435. $session_id,
  4436. null,
  4437. null,
  4438. STUDENT
  4439. );
  4440. } else {
  4441. $user_list = CourseManager::get_user_list_from_course_code(
  4442. $course,
  4443. $session_id,
  4444. null,
  4445. null,
  4446. 0
  4447. );
  4448. }
  4449. // Show exercise results of invisible exercises? see BT#4091
  4450. $exercise_list = ExerciseLib::get_all_exercises(
  4451. $course_info,
  4452. $session_id,
  4453. false,
  4454. null,
  4455. false,
  4456. 2
  4457. );
  4458. $to_graph_exercise_result = array();
  4459. if (!empty($exercise_list)) {
  4460. $score = $weighting = $exe_id = 0;
  4461. foreach ($exercise_list as $exercices) {
  4462. $exercise_obj = new Exercise($course_info['real_id']);
  4463. $exercise_obj->read($exercices['id']);
  4464. $visible_return = $exercise_obj->is_visible();
  4465. $score = $weighting = $attempts = 0;
  4466. // Getting count of attempts by user
  4467. $attempts = Event::count_exercise_attempts_by_user(
  4468. api_get_user_id(),
  4469. $exercices['id'],
  4470. $course_info['real_id'],
  4471. $session_id
  4472. );
  4473. $html .= '<tr class="row_even">';
  4474. $url = api_get_path(WEB_CODE_PATH) . "exercise/overview.php?cidReq={$course_info['code']}&id_session=$session_id&exerciseId={$exercices['id']}";
  4475. if ($visible_return['value'] == true) {
  4476. $exercices['title'] = Display::url(
  4477. $exercices['title'],
  4478. $url,
  4479. array('target' => SESSION_LINK_TARGET)
  4480. );
  4481. } elseif ($exercices['active'] == -1) {
  4482. $exercices['title'] = sprintf(get_lang('XParenthesisDeleted'), $exercices['title']);
  4483. }
  4484. $html .= Display::tag('td', $exercices['title']);
  4485. // Exercise configuration show results or show only score
  4486. if ($exercices['results_disabled'] == 0 || $exercices['results_disabled'] == 2) {
  4487. //For graphics
  4488. $best_exercise_stats = Event::get_best_exercise_results_by_user(
  4489. $exercices['id'],
  4490. $course_info['real_id'],
  4491. $session_id
  4492. );
  4493. $to_graph_exercise_result[$exercices['id']] = array(
  4494. 'title' => $exercices['title'],
  4495. 'data' => $best_exercise_stats
  4496. );
  4497. $latest_attempt_url = '';
  4498. $best_score = $position = $percentage_score_result = '-';
  4499. $graph = $normal_graph = null;
  4500. // Getting best results
  4501. $best_score_data = ExerciseLib::get_best_attempt_in_course(
  4502. $exercices['id'],
  4503. $course_info['real_id'],
  4504. $session_id
  4505. );
  4506. $best_score = '';
  4507. if (!empty($best_score_data)) {
  4508. $best_score = ExerciseLib::show_score(
  4509. $best_score_data['exe_result'],
  4510. $best_score_data['exe_weighting']
  4511. );
  4512. }
  4513. if ($attempts > 0) {
  4514. $exercise_stat = ExerciseLib::get_best_attempt_by_user(
  4515. api_get_user_id(),
  4516. $exercices['id'],
  4517. $course_info['real_id'],
  4518. $session_id
  4519. );
  4520. if (!empty($exercise_stat)) {
  4521. // Always getting the BEST attempt
  4522. $score = $exercise_stat['exe_result'];
  4523. $weighting = $exercise_stat['exe_weighting'];
  4524. $exe_id = $exercise_stat['exe_id'];
  4525. $latest_attempt_url .= api_get_path(WEB_CODE_PATH) . 'exercise/result.php?id='.$exe_id.'&cidReq='.$course_info['code'].'&show_headers=1&id_session='.$session_id;
  4526. $percentage_score_result = Display::url(ExerciseLib::show_score($score, $weighting), $latest_attempt_url);
  4527. $my_score = 0;
  4528. if (!empty($weighting) && intval($weighting) != 0) {
  4529. $my_score = $score/$weighting;
  4530. }
  4531. //@todo this function slows the page
  4532. if (is_int($user_list)) {
  4533. $user_list = array($user_list);
  4534. }
  4535. $position = ExerciseLib::get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id, $user_list);
  4536. $graph = self::generate_exercise_result_thumbnail_graph($to_graph_exercise_result[$exercices['id']]);
  4537. $normal_graph = self::generate_exercise_result_graph($to_graph_exercise_result[$exercices['id']]);
  4538. }
  4539. }
  4540. $html .= Display::div(
  4541. $normal_graph,
  4542. array('id'=>'main_graph_'.$exercices['id'],'class'=>'dialog', 'style'=>'display:none')
  4543. );
  4544. if (empty($graph)) {
  4545. $graph = '-';
  4546. } else {
  4547. $graph = Display::url(
  4548. '<img src="' . $graph . '" >',
  4549. $normal_graph,
  4550. array(
  4551. 'id' => $exercices['id'],
  4552. 'class' => 'expand-image',
  4553. )
  4554. );
  4555. }
  4556. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  4557. $html .= Display::tag('td', $percentage_score_result, array('align'=>'center'));
  4558. $html .= Display::tag('td', $position, array('align'=>'center'));
  4559. $html .= Display::tag('td', $best_score, array('align'=>'center'));
  4560. $html .= Display::tag('td', $graph, array('align'=>'center'));
  4561. //$html .= Display::tag('td', $latest_attempt_url, array('align'=>'center', 'width'=>'25'));
  4562. } else {
  4563. // Exercise configuration NO results
  4564. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  4565. $html .= Display::tag('td', '-', array('align'=>'center'));
  4566. $html .= Display::tag('td', '-', array('align'=>'center'));
  4567. $html .= Display::tag('td', '-', array('align'=>'center'));
  4568. $html .= Display::tag('td', '-', array('align'=>'center'));
  4569. }
  4570. $html .= '</tr>';
  4571. }
  4572. } else {
  4573. $html .= '<tr><td colspan="5" align="center">'.get_lang('NoEx').'</td></tr>';
  4574. }
  4575. $html .= '</tbody></table></div>';
  4576. // LP table results
  4577. $html .= '<div class="table-responsive">';
  4578. $html .='<table class="table table-striped table-hover">';
  4579. $html .= '<thead><tr>';
  4580. $html .= Display::tag('th', get_lang('Learnpaths'), array('class'=>'head', 'style'=>'color:#000'));
  4581. $html .= Display::tag('th', get_lang('LatencyTimeSpent'), array('class'=>'head', 'style'=>'color:#000'));
  4582. $html .= Display::tag('th', get_lang('Progress'), array('class'=>'head', 'style'=>'color:#000'));
  4583. $html .= Display::tag('th', get_lang('Score'), array('class'=>'head', 'style'=>'color:#000'));
  4584. $html .= Display::tag('th', get_lang('LastConnexion'), array('class'=>'head', 'style'=>'color:#000'));
  4585. $html .= '</tr></thead><tbody>';
  4586. $list = new LearnpathList(
  4587. api_get_user_id(),
  4588. $course_info['code'],
  4589. $session_id,
  4590. 'lp.publicatedOn ASC',
  4591. true,
  4592. null,
  4593. true
  4594. );
  4595. $lp_list = $list->get_flat_list();
  4596. if (!empty($lp_list) > 0) {
  4597. foreach ($lp_list as $lp_id => $learnpath) {
  4598. $progress = self::get_avg_student_progress($user_id, $course, array($lp_id), $session_id);
  4599. $last_connection_in_lp = self::get_last_connection_time_in_lp($user_id, $course, $lp_id, $session_id);
  4600. $time_spent_in_lp = self::get_time_spent_in_lp($user_id, $course, array($lp_id), $session_id);
  4601. $percentage_score = self::get_avg_student_score($user_id, $course, array($lp_id), $session_id);
  4602. if (is_numeric($percentage_score)) {
  4603. $percentage_score = $percentage_score.'%';
  4604. } else {
  4605. $percentage_score = '0%';
  4606. }
  4607. $time_spent_in_lp = api_time_to_hms($time_spent_in_lp);
  4608. $html .= '<tr class="row_even">';
  4609. $url = api_get_path(WEB_CODE_PATH) . "lp/lp_controller.php?cidReq={$course_code}&id_session=$session_id&lp_id=$lp_id&action=view";
  4610. if ($learnpath['lp_visibility'] == 0) {
  4611. $html .= Display::tag('td', $learnpath['lp_name']);
  4612. } else {
  4613. $html .= Display::tag('td', Display::url($learnpath['lp_name'], $url, array('target'=>SESSION_LINK_TARGET)));
  4614. }
  4615. $html .= Display::tag('td', $time_spent_in_lp, array('align'=>'center'));
  4616. if (is_numeric($progress)) {
  4617. $progress = $progress.'%';
  4618. }
  4619. $html .= Display::tag('td', $progress, array('align'=>'center'));
  4620. $html .= Display::tag('td', $percentage_score);
  4621. $last_connection = '-';
  4622. if (!empty($last_connection_in_lp)) {
  4623. $last_connection = api_convert_and_format_date($last_connection_in_lp, DATE_TIME_FORMAT_LONG);
  4624. }
  4625. $html .= Display::tag('td', $last_connection, array('align'=>'center','width'=>'180px'));
  4626. $html .= "</tr>";
  4627. }
  4628. } else {
  4629. $html .= '<tr>
  4630. <td colspan="4" align="center">
  4631. '.get_lang('NoLearnpath').'
  4632. </td>
  4633. </tr>';
  4634. }
  4635. $html .='</tbody></table></div>';
  4636. $html .= self::displayUserSkills($user_id, $course_info['id'], $session_id);
  4637. }
  4638. return $html;
  4639. }
  4640. /**
  4641. * Generates an histogram
  4642. * @param array list of exercise names
  4643. * @param array my results 0 to 100
  4644. * @param array average scores 0-100
  4645. * @return string
  4646. */
  4647. static function generate_session_exercise_graph($names, $my_results, $average)
  4648. {
  4649. /* Create and populate the pData object */
  4650. $myData = new pData();
  4651. $myData->addPoints($names, 'Labels');
  4652. $myData->addPoints($my_results, 'Serie1');
  4653. $myData->addPoints($average, 'Serie2');
  4654. $myData->setSerieWeight('Serie1', 1);
  4655. $myData->setSerieTicks('Serie2', 4);
  4656. $myData->setSerieDescription('Labels', 'Months');
  4657. $myData->setAbscissa('Labels');
  4658. $myData->setSerieDescription('Serie1', get_lang('MyResults'));
  4659. $myData->setSerieDescription('Serie2', get_lang('AverageScore'));
  4660. $myData->setAxisUnit(0, '%');
  4661. $myData->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  4662. // Cache definition
  4663. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  4664. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  4665. $chartHash = $myCache->getHash($myData);
  4666. if ($myCache->isInCache($chartHash)) {
  4667. //if we already created the img
  4668. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  4669. $myCache->saveFromCache($chartHash, $imgPath);
  4670. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  4671. } else {
  4672. /* Define width, height and angle */
  4673. $mainWidth = 860;
  4674. $mainHeight = 500;
  4675. $angle = 50;
  4676. /* Create the pChart object */
  4677. $myPicture = new pImage($mainWidth, $mainHeight, $myData);
  4678. /* Turn of Antialiasing */
  4679. $myPicture->Antialias = false;
  4680. /* Draw the background */
  4681. $settings = array('R' => 255, 'G' => 255, 'B' => 255);
  4682. $myPicture->drawFilledRectangle(0, 0, $mainWidth, $mainHeight, $settings);
  4683. /* Add a border to the picture */
  4684. $myPicture->drawRectangle(
  4685. 0,
  4686. 0,
  4687. $mainWidth - 1,
  4688. $mainHeight - 1,
  4689. array('R' => 0, 'G' => 0, 'B' => 0)
  4690. );
  4691. /* Set the default font */
  4692. $myPicture->setFontProperties(
  4693. array(
  4694. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  4695. 'FontSize' => 10)
  4696. );
  4697. /* Write the chart title */
  4698. $myPicture->drawText(
  4699. $mainWidth / 2,
  4700. 30,
  4701. get_lang('ExercisesInTimeProgressChart'),
  4702. array(
  4703. 'FontSize' => 12,
  4704. 'Align' => TEXT_ALIGN_BOTTOMMIDDLE
  4705. )
  4706. );
  4707. /* Set the default font */
  4708. $myPicture->setFontProperties(
  4709. array(
  4710. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  4711. 'FontSize' => 6
  4712. )
  4713. );
  4714. /* Define the chart area */
  4715. $myPicture->setGraphArea(60, 60, $mainWidth - 60, $mainHeight - 150);
  4716. /* Draw the scale */
  4717. $scaleSettings = array(
  4718. 'XMargin' => 10,
  4719. 'YMargin' => 10,
  4720. 'Floating' => true,
  4721. 'GridR' => 200,
  4722. 'GridG' => 200,
  4723. 'GridB' => 200,
  4724. 'DrawSubTicks' => true,
  4725. 'CycleBackground' => true,
  4726. 'LabelRotation' => $angle,
  4727. 'Mode' => SCALE_MODE_ADDALL_START0,
  4728. );
  4729. $myPicture->drawScale($scaleSettings);
  4730. /* Turn on Antialiasing */
  4731. $myPicture->Antialias = true;
  4732. /* Enable shadow computing */
  4733. $myPicture->setShadow(
  4734. true,
  4735. array(
  4736. 'X' => 1,
  4737. 'Y' => 1,
  4738. 'R' => 0,
  4739. 'G' => 0,
  4740. 'B' => 0,
  4741. 'Alpha' => 10
  4742. )
  4743. );
  4744. /* Draw the line chart */
  4745. $myPicture->setFontProperties(
  4746. array(
  4747. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  4748. 'FontSize' => 10
  4749. )
  4750. );
  4751. $myPicture->drawSplineChart();
  4752. $myPicture->drawPlotChart(
  4753. array(
  4754. 'DisplayValues' => true,
  4755. 'PlotBorder' => true,
  4756. 'BorderSize' => 1,
  4757. 'Surrounding' => -60,
  4758. 'BorderAlpha' => 80
  4759. )
  4760. );
  4761. /* Write the chart legend */
  4762. $myPicture->drawLegend(
  4763. $mainWidth / 2 + 50,
  4764. 50,
  4765. array(
  4766. 'Style' => LEGEND_BOX,
  4767. 'Mode' => LEGEND_HORIZONTAL,
  4768. 'FontR' => 0,
  4769. 'FontG' => 0,
  4770. 'FontB' => 0,
  4771. 'R' => 220,
  4772. 'G' => 220,
  4773. 'B' => 220,
  4774. 'Alpha' => 100
  4775. )
  4776. );
  4777. $myCache->writeToCache($chartHash, $myPicture);
  4778. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  4779. $myCache->saveFromCache($chartHash, $imgPath);
  4780. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  4781. }
  4782. $html = '<img src="' . $imgPath . '">';
  4783. return $html;
  4784. }
  4785. /**
  4786. *
  4787. * Returns a thumbnail of the function generate_exercise_result_graph
  4788. * @param array $attempts
  4789. */
  4790. static function generate_exercise_result_thumbnail_graph($attempts)
  4791. {
  4792. //$exercise_title = $attempts['title'];
  4793. $attempts = $attempts['data'];
  4794. $my_exercise_result_array = $exercise_result = array();
  4795. if (empty($attempts)) {
  4796. return null;
  4797. }
  4798. foreach ($attempts as $attempt) {
  4799. if (api_get_user_id() == $attempt['exe_user_id']) {
  4800. if ($attempt['exe_weighting'] != 0 ) {
  4801. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  4802. }
  4803. } else {
  4804. if ($attempt['exe_weighting'] != 0 ) {
  4805. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  4806. }
  4807. }
  4808. }
  4809. //Getting best result
  4810. rsort($my_exercise_result_array);
  4811. $my_exercise_result = 0;
  4812. if (isset($my_exercise_result_array[0])) {
  4813. $my_exercise_result = $my_exercise_result_array[0] *100;
  4814. }
  4815. $max = 100;
  4816. $pieces = 5 ;
  4817. $part = round($max / $pieces);
  4818. $x_axis = array();
  4819. $final_array = array();
  4820. $my_final_array = array();
  4821. for ($i=1; $i <=$pieces; $i++) {
  4822. $sum = 1;
  4823. if ($i == 1) {
  4824. $sum = 0;
  4825. }
  4826. $min = ($i-1)*$part + $sum;
  4827. $max = ($i)*$part;
  4828. $x_axis[]= $min." - ".$max;
  4829. $count = 0;
  4830. foreach($exercise_result as $result) {
  4831. $percentage = $result*100;
  4832. //echo $percentage.' - '.$min.' - '.$max."<br />";
  4833. if ($percentage >= $min && $percentage <= $max) {
  4834. //echo ' is > ';
  4835. $count++;
  4836. }
  4837. }
  4838. //echo '<br />';
  4839. $final_array[]= $count;
  4840. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  4841. $my_final_array[] = 1;
  4842. } else {
  4843. $my_final_array[] = 0;
  4844. }
  4845. }
  4846. //Fix to remove the data of the user with my data
  4847. for($i = 0; $i<=count($my_final_array); $i++) {
  4848. if (!empty($my_final_array[$i])) {
  4849. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  4850. $final_array[$i] = 0;
  4851. }
  4852. }
  4853. // Dataset definition
  4854. $dataSet = new pData();
  4855. $dataSet->addPoints($final_array, 'Serie1');
  4856. $dataSet->addPoints($my_final_array, 'Serie2');
  4857. $dataSet->normalize(100, "%");
  4858. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  4859. // Cache definition
  4860. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  4861. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  4862. $chartHash = $myCache->getHash($dataSet);
  4863. if ($myCache->isInCache($chartHash)) {
  4864. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  4865. $myCache->saveFromCache($chartHash, $imgPath);
  4866. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  4867. } else {
  4868. /* Create the pChart object */
  4869. $widthSize = 80;
  4870. $heightSize = 35;
  4871. $fontSize = 2;
  4872. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  4873. /* Turn of Antialiasing */
  4874. $myPicture->Antialias = false;
  4875. /* Add a border to the picture */
  4876. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, array('R' => 0, 'G' => 0, 'B' => 0));
  4877. /* Set the default font */
  4878. $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => $fontSize));
  4879. /* Do not write the chart title */
  4880. /* Define the chart area */
  4881. $myPicture->setGraphArea(5, 5, $widthSize - 5, $heightSize - 5);
  4882. /* Draw the scale */
  4883. $scaleSettings = array(
  4884. 'GridR' => 200,
  4885. 'GridG' => 200,
  4886. 'GridB' => 200,
  4887. 'DrawSubTicks' => true,
  4888. 'CycleBackground' => true,
  4889. 'Mode' => SCALE_MODE_MANUAL,
  4890. 'ManualScale' => array(
  4891. '0' => array(
  4892. 'Min' => 0,
  4893. 'Max' => 100
  4894. )
  4895. )
  4896. );
  4897. $myPicture->drawScale($scaleSettings);
  4898. /* Turn on shadow computing */
  4899. $myPicture->setShadow(
  4900. true,
  4901. array(
  4902. 'X' => 1,
  4903. 'Y' => 1,
  4904. 'R' => 0,
  4905. 'G' => 0,
  4906. 'B' => 0,
  4907. 'Alpha' => 10
  4908. )
  4909. );
  4910. /* Draw the chart */
  4911. $myPicture->setShadow(
  4912. true,
  4913. array(
  4914. 'X' => 1,
  4915. 'Y' => 1,
  4916. 'R' => 0,
  4917. 'G' => 0,
  4918. 'B' => 0,
  4919. 'Alpha' => 10
  4920. )
  4921. );
  4922. $settings = array(
  4923. 'DisplayValues' => true,
  4924. 'DisplaySize' => $fontSize,
  4925. 'DisplayR' => 0,
  4926. 'DisplayG' => 0,
  4927. 'DisplayB' => 0,
  4928. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  4929. 'Gradient' => false,
  4930. 'Surrounding' => 5,
  4931. 'InnerSurrounding' => 5
  4932. );
  4933. $myPicture->drawStackedBarChart($settings);
  4934. /* Save and write in cache */
  4935. $myCache->writeToCache($chartHash, $myPicture);
  4936. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  4937. $myCache->saveFromCache($chartHash, $imgPath);
  4938. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  4939. }
  4940. return $imgPath;
  4941. }
  4942. /**
  4943. * Generates a big graph with the number of best results
  4944. * @param array
  4945. */
  4946. static function generate_exercise_result_graph($attempts)
  4947. {
  4948. $exercise_title = strip_tags($attempts['title']);
  4949. $attempts = $attempts['data'];
  4950. $my_exercise_result_array = $exercise_result = array();
  4951. if (empty($attempts)) {
  4952. return null;
  4953. }
  4954. foreach ($attempts as $attempt) {
  4955. if (api_get_user_id() == $attempt['exe_user_id']) {
  4956. if ($attempt['exe_weighting'] != 0 ) {
  4957. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  4958. }
  4959. } else {
  4960. if ($attempt['exe_weighting'] != 0 ) {
  4961. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  4962. }
  4963. }
  4964. }
  4965. //Getting best result
  4966. rsort($my_exercise_result_array);
  4967. $my_exercise_result = 0;
  4968. if (isset($my_exercise_result_array[0])) {
  4969. $my_exercise_result = $my_exercise_result_array[0] *100;
  4970. }
  4971. $max = 100;
  4972. $pieces = 5 ;
  4973. $part = round($max / $pieces);
  4974. $x_axis = array();
  4975. $final_array = array();
  4976. $my_final_array = array();
  4977. for ($i=1; $i <=$pieces; $i++) {
  4978. $sum = 1;
  4979. if ($i == 1) {
  4980. $sum = 0;
  4981. }
  4982. $min = ($i-1)*$part + $sum;
  4983. $max = ($i)*$part;
  4984. $x_axis[]= $min." - ".$max;
  4985. $count = 0;
  4986. foreach($exercise_result as $result) {
  4987. $percentage = $result*100;
  4988. if ($percentage >= $min && $percentage <= $max) {
  4989. $count++;
  4990. }
  4991. }
  4992. $final_array[]= $count;
  4993. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  4994. $my_final_array[] = 1;
  4995. } else {
  4996. $my_final_array[] = 0;
  4997. }
  4998. }
  4999. //Fix to remove the data of the user with my data
  5000. for($i = 0; $i<=count($my_final_array); $i++) {
  5001. if (!empty($my_final_array[$i])) {
  5002. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  5003. $final_array[$i] = 0;
  5004. }
  5005. }
  5006. // Dataset definition
  5007. $dataSet = new pData();
  5008. $dataSet->addPoints($final_array, 'Serie1');
  5009. $dataSet->addPoints($my_final_array, 'Serie2');
  5010. $dataSet->addPoints($x_axis, 'Serie3');
  5011. $dataSet->setSerieDescription('Serie1', get_lang('Score'));
  5012. $dataSet->setSerieDescription('Serie2', get_lang('MyResults'));
  5013. $dataSet->setAbscissa('Serie3');
  5014. $dataSet->setXAxisName(get_lang('Score'));
  5015. $dataSet->normalize(100, "%");
  5016. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  5017. // Cache definition
  5018. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  5019. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  5020. $chartHash = $myCache->getHash($dataSet);
  5021. if ($myCache->isInCache($chartHash)) {
  5022. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  5023. $myCache->saveFromCache($chartHash, $imgPath);
  5024. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  5025. } else {
  5026. /* Create the pChart object */
  5027. $widthSize = 480;
  5028. $heightSize = 250;
  5029. $fontSize = 8;
  5030. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  5031. /* Turn of Antialiasing */
  5032. $myPicture->Antialias = false;
  5033. /* Add a border to the picture */
  5034. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, array('R' => 0, 'G' => 0, 'B' => 0));
  5035. /* Set the default font */
  5036. $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => 10));
  5037. /* Write the chart title */
  5038. $myPicture->drawText(
  5039. 250,
  5040. 20,
  5041. $exercise_title,
  5042. array(
  5043. 'FontSize' => 12,
  5044. 'Align' => TEXT_ALIGN_BOTTOMMIDDLE
  5045. )
  5046. );
  5047. /* Define the chart area */
  5048. $myPicture->setGraphArea(50, 50, $widthSize - 20, $heightSize - 30);
  5049. /* Draw the scale */
  5050. $scaleSettings = array(
  5051. 'GridR' => 200,
  5052. 'GridG' => 200,
  5053. 'GridB' => 200,
  5054. 'DrawSubTicks' => true,
  5055. 'CycleBackground' => true,
  5056. 'Mode' => SCALE_MODE_MANUAL,
  5057. 'ManualScale' => array(
  5058. '0' => array(
  5059. 'Min' => 0,
  5060. 'Max' => 100
  5061. )
  5062. )
  5063. );
  5064. $myPicture->drawScale($scaleSettings);
  5065. /* Turn on shadow computing */
  5066. $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
  5067. /* Draw the chart */
  5068. $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
  5069. $settings = array(
  5070. 'DisplayValues' => true,
  5071. 'DisplaySize' => $fontSize,
  5072. 'DisplayR' => 0,
  5073. 'DisplayG' => 0,
  5074. 'DisplayB' => 0,
  5075. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  5076. 'Gradient' => false,
  5077. 'Surrounding' => 30,
  5078. 'InnerSurrounding' => 25
  5079. );
  5080. $myPicture->drawStackedBarChart($settings);
  5081. $legendSettings = array(
  5082. 'Mode' => LEGEND_HORIZONTAL,
  5083. 'Style' => LEGEND_NOBORDER,
  5084. );
  5085. $myPicture->drawLegend($widthSize / 2, 30, $legendSettings);
  5086. /* Write and save into cache */
  5087. $myCache->writeToCache($chartHash, $myPicture);
  5088. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  5089. $myCache->saveFromCache($chartHash, $imgPath);
  5090. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  5091. }
  5092. return $imgPath;
  5093. }
  5094. /**
  5095. * @param FormValidator $form
  5096. * @return mixed
  5097. */
  5098. public static function setUserSearchForm($form)
  5099. {
  5100. global $_configuration;
  5101. $form->addElement('text', 'keyword', get_lang('Keyword'));
  5102. $form->addElement(
  5103. 'select',
  5104. 'active',
  5105. get_lang('Status'),
  5106. array(1 => get_lang('Active'), 0 => get_lang('Inactive'))
  5107. );
  5108. $form->addElement(
  5109. 'select',
  5110. 'sleeping_days',
  5111. get_lang('InactiveDays'),
  5112. array(
  5113. '',
  5114. 1 => 1,
  5115. 5 => 5,
  5116. 15 => 15,
  5117. 30 => 30,
  5118. 60 => 60,
  5119. 90 => 90,
  5120. 120 => 120,
  5121. )
  5122. );
  5123. $form->addButtonSearch(get_lang('Search'));
  5124. return $form;
  5125. }
  5126. /**
  5127. * Get the progress of a exercise
  5128. * @param int $sessionId The session ID (session.id)
  5129. * @param int $courseId The course ID (course.id)
  5130. * @param int $exerciseId The quiz ID (c_quiz.id)
  5131. * @param string $date_from
  5132. * @param string $date_to
  5133. * @param array $options An array of options you can pass to the query (limit, where and order)
  5134. * @return array An array with the data of exercise(s) progress
  5135. */
  5136. public static function get_exercise_progress(
  5137. $sessionId = 0,
  5138. $courseId = 0,
  5139. $exerciseId = 0,
  5140. $date_from = null,
  5141. $date_to = null,
  5142. $options = array()
  5143. ) {
  5144. $sessionId = intval($sessionId);
  5145. $courseId = intval($courseId);
  5146. $exerciseId = intval($exerciseId);
  5147. $date_from = Database::escape_string($date_from);
  5148. $date_to = Database::escape_string($date_to);
  5149. /*
  5150. * This method gets the data by blocks, as previous attempts at one single
  5151. * query made it take ages. The logic of query division is described below
  5152. */
  5153. // Get tables names
  5154. $tuser = Database::get_main_table(TABLE_MAIN_USER);
  5155. $tquiz = Database::get_course_table(TABLE_QUIZ_TEST);
  5156. $tquiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  5157. $tquiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  5158. $tquiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  5159. $ttrack_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  5160. $ttrack_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  5161. $sessions = array();
  5162. $courses = array();
  5163. // if session ID is defined but course ID is empty, get all the courses
  5164. // from that session
  5165. if (!empty($sessionId) && empty($courseId)) {
  5166. // $courses is an array of course int id as index and course details hash as value
  5167. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  5168. $sessions[$sessionId] = api_get_session_info($sessionId);
  5169. } elseif (empty($sessionId) && !empty($courseId)) {
  5170. // if, to the contrary, course is defined but not sessions, get the sessions that include this course
  5171. // $sessions is an array like: [0] => ('id' => 3, 'name' => 'Session 35'), [1] => () etc;
  5172. $course = api_get_course_info_by_id($courseId);
  5173. $sessionsTemp = SessionManager::get_session_by_course($courseId);
  5174. $courses[$courseId] = $course;
  5175. foreach ($sessionsTemp as $sessionItem) {
  5176. $sessions[$sessionItem['id']] = $sessionItem;
  5177. }
  5178. } elseif (!empty($courseId) && !empty($sessionId)) {
  5179. //none is empty
  5180. $course = api_get_course_info_by_id($courseId);
  5181. $courses[$courseId] = array($course['code']);
  5182. $courses[$courseId]['code'] = $course['code'];
  5183. $sessions[$sessionId] = api_get_session_info($sessionId);
  5184. } else {
  5185. //both are empty, not enough data, return an empty array
  5186. return array();
  5187. }
  5188. // Now we have two arrays of courses and sessions with enough data to proceed
  5189. // If no course could be found, we shouldn't return anything.
  5190. // Sessions can be empty (then we only return the pure-course-context results)
  5191. if (count($courses) < 1) {
  5192. return array();
  5193. }
  5194. $data = array();
  5195. // The following loop is less expensive than what it seems:
  5196. // - if a course was defined, then we only loop through sessions
  5197. // - if a session was defined, then we only loop through courses
  5198. // - if a session and a course were defined, then we only loop once
  5199. foreach ($courses as $courseIdx => $courseData) {
  5200. $where = '';
  5201. $whereParams = array();
  5202. $whereSessionParams = '';
  5203. if (count($sessions > 0)) {
  5204. foreach ($sessions as $sessionIdx => $sessionData) {
  5205. if (!empty($sessionIdx)) {
  5206. $whereSessionParams .= $sessionIdx.',';
  5207. }
  5208. }
  5209. $whereSessionParams = substr($whereSessionParams,0,-1);
  5210. }
  5211. if (!empty($exerciseId)) {
  5212. $exerciseId = intval($exerciseId);
  5213. $where .= ' AND q.id = %d ';
  5214. $whereParams[] = $exerciseId;
  5215. }
  5216. /*
  5217. * This feature has been disabled for now, to avoid having to
  5218. * join two very large tables
  5219. //2 = show all questions (wrong and correct answered)
  5220. if ($answer != 2) {
  5221. $answer = intval($answer);
  5222. //$where .= ' AND qa.correct = %d';
  5223. //$whereParams[] = $answer;
  5224. }
  5225. */
  5226. $limit = '';
  5227. if (!empty($options['limit'])) {
  5228. $limit = " LIMIT ".$options['limit'];
  5229. }
  5230. if (!empty($options['where'])) {
  5231. $where .= ' AND '.Database::escape_string($options['where']);
  5232. }
  5233. $order = '';
  5234. if (!empty($options['order'])) {
  5235. $order = " ORDER BY ".$options['order'];
  5236. }
  5237. if (!empty($date_to) && !empty($date_from)) {
  5238. $where .= sprintf(" AND (te.start_date BETWEEN '%s 00:00:00' AND '%s 23:59:59')", $date_from, $date_to);
  5239. }
  5240. $sql = "SELECT
  5241. te.session_id,
  5242. ta.id as attempt_id,
  5243. te.exe_user_id as user_id,
  5244. te.exe_id as exercise_attempt_id,
  5245. ta.question_id,
  5246. ta.answer as answer_id,
  5247. ta.tms as time,
  5248. te.exe_exo_id as quiz_id,
  5249. CONCAT ('c', q.c_id, '_e', q.id) as exercise_id,
  5250. q.title as quiz_title,
  5251. qq.description as description
  5252. FROM $ttrack_exercises te
  5253. INNER JOIN $ttrack_attempt ta
  5254. ON ta.exe_id = te.exe_id
  5255. INNER JOIN $tquiz q
  5256. ON q.id = te.exe_exo_id
  5257. INNER JOIN $tquiz_rel_question rq
  5258. ON rq.exercice_id = q.id AND rq.c_id = q.c_id
  5259. INNER JOIN $tquiz_question qq
  5260. ON
  5261. qq.id = rq.question_id AND
  5262. qq.c_id = rq.c_id AND
  5263. qq.position = rq.question_order AND
  5264. ta.question_id = rq.question_id
  5265. WHERE
  5266. te.c_id = $courseIdx ".(empty($whereSessionParams)?'':"AND te.session_id IN ($whereSessionParams)")."
  5267. AND q.c_id = $courseIdx
  5268. $where $order $limit";
  5269. $sql_query = vsprintf($sql, $whereParams);
  5270. // Now browse through the results and get the data
  5271. $rs = Database::query($sql_query);
  5272. $userIds = array();
  5273. $questionIds = array();
  5274. $answerIds = array();
  5275. while ($row = Database::fetch_array($rs)) {
  5276. //only show if exercise is visible
  5277. if (api_get_item_visibility($courseData, 'quiz', $row['exercise_id'])) {
  5278. $userIds[$row['user_id']] = $row['user_id'];
  5279. $questionIds[$row['question_id']] = $row['question_id'];
  5280. $answerIds[$row['question_id']][$row['answer_id']] = $row['answer_id'];
  5281. $row['session'] = $sessions[$row['session_id']];
  5282. $data[] = $row;
  5283. }
  5284. }
  5285. // Now fill questions data. Query all questions and answers for this test to avoid
  5286. $sqlQuestions = "SELECT tq.c_id, tq.id as question_id, tq.question, tqa.id_auto,
  5287. tqa.answer, tqa.correct, tq.position, tqa.id_auto as answer_id
  5288. FROM $tquiz_question tq, $tquiz_answer tqa
  5289. WHERE
  5290. tqa.question_id = tq.id AND
  5291. tqa.c_id = tq.c_id AND
  5292. tq.c_id = $courseIdx AND
  5293. tq.id IN (".implode(',', $questionIds).")";
  5294. $resQuestions = Database::query($sqlQuestions);
  5295. $answer = array();
  5296. $question = array();
  5297. while ($rowQuestion = Database::fetch_assoc($resQuestions)) {
  5298. $questionId = $rowQuestion['question_id'];
  5299. $answerId = $rowQuestion['answer_id'];
  5300. $answer[$questionId][$answerId] = array(
  5301. 'position' => $rowQuestion['position'],
  5302. 'question' => $rowQuestion['question'],
  5303. 'answer' => $rowQuestion['answer'],
  5304. 'correct' => $rowQuestion['correct']
  5305. );
  5306. $question[$questionId]['question'] = $rowQuestion['question'];
  5307. }
  5308. // Now fill users data
  5309. $sqlUsers = "SELECT user_id, username, lastname, firstname
  5310. FROM $tuser
  5311. WHERE user_id IN (".implode(',',$userIds).")";
  5312. $resUsers = Database::query($sqlUsers);
  5313. while ($rowUser = Database::fetch_assoc($resUsers)) {
  5314. $users[$rowUser['user_id']] = $rowUser;
  5315. }
  5316. foreach ($data as $id => $row) {
  5317. $rowQuestId = $row['question_id'];
  5318. $rowAnsId = $row['answer_id'];
  5319. $data[$id]['session'] = $sessions[$row['session_id']]['name'];
  5320. $data[$id]['firstname'] = $users[$row['user_id']]['firstname'];
  5321. $data[$id]['lastname'] = $users[$row['user_id']]['lastname'];
  5322. $data[$id]['username'] = $users[$row['user_id']]['username'];
  5323. $data[$id]['answer'] = $answer[$rowQuestId][$rowAnsId]['answer'];
  5324. $data[$id]['correct'] = ($answer[$rowQuestId][$rowAnsId]['correct'] == 0 ? get_lang('No') : get_lang('Yes'));
  5325. $data[$id]['question'] = $question[$rowQuestId]['question'];
  5326. $data[$id]['question_id'] = $rowQuestId;
  5327. $data[$id]['description'] = $row['description'];
  5328. }
  5329. /*
  5330. The minimum expected array structure at the end is:
  5331. attempt_id,
  5332. session name,
  5333. exercise_id,
  5334. quiz_title,
  5335. username,
  5336. lastname,
  5337. firstname,
  5338. time,
  5339. question_id,
  5340. question,
  5341. answer,
  5342. */
  5343. }
  5344. return $data;
  5345. }
  5346. /**
  5347. * @param User $user
  5348. * @param string $tool
  5349. * @param Course $course
  5350. * @param Session|null $session Optional.
  5351. * @return \Chamilo\CourseBundle\Entity\CStudentPublication|null
  5352. * @throws \Doctrine\ORM\NonUniqueResultException
  5353. */
  5354. public static function getLastStudentPublication(User $user, $tool, Course $course, Session $session = null)
  5355. {
  5356. return Database::getManager()
  5357. ->createQuery("
  5358. SELECT csp
  5359. FROM ChamiloCourseBundle:CStudentPublication csp
  5360. INNER JOIN ChamiloCourseBundle:CItemProperty cip
  5361. WITH (
  5362. csp.iid = cip.ref AND
  5363. csp.sessionId = cip.session AND
  5364. csp.cId = cip.course AND
  5365. csp.userId = cip.lasteditUserId
  5366. )
  5367. WHERE
  5368. cip.session = :session AND cip.course = :course AND cip.lasteditUserId = :user AND cip.tool = :tool
  5369. ORDER BY csp.iid DESC
  5370. ")
  5371. ->setMaxResults(1)
  5372. ->setParameters([
  5373. 'tool' => $tool,
  5374. 'session' => $session,
  5375. 'course' => $course,
  5376. 'user' => $user
  5377. ])
  5378. ->getOneOrNullResult();
  5379. }
  5380. /**
  5381. * Get the HTML code for show a block with the achieved user skill on course/session
  5382. * @param int $userId
  5383. * @param int $courseId Optional.
  5384. * @param int $sessionId Optional.
  5385. * @return string
  5386. */
  5387. public static function displayUserSkills($userId, $courseId = 0, $sessionId = 0)
  5388. {
  5389. $userId = intval($userId);
  5390. $courseId = intval($courseId);
  5391. $sessionId = intval($sessionId);
  5392. if (api_get_setting('allow_skills_tool') !== 'true') {
  5393. return '';
  5394. }
  5395. $filter = ['user' => $userId];
  5396. $filter['course'] = $courseId ?: null;
  5397. $filter['session'] = $sessionId ?: null;
  5398. $em = Database::getManager();
  5399. $skillsRelUser = $em->getRepository('ChamiloCoreBundle:SkillRelUser')->findBy($filter);
  5400. $html = '
  5401. <div class="table-responsive">
  5402. <table class="table" id="skillList">
  5403. <thead>
  5404. <tr>
  5405. <th>' . get_lang('AchievedSkills') . '</th>
  5406. </tr>
  5407. </thead>
  5408. <tbody>
  5409. <tr>
  5410. <td>
  5411. ';
  5412. if (count($skillsRelUser)) {
  5413. $html .= '
  5414. <div class="scrollbar-inner badges-sidebar">
  5415. <ul class="list-unstyled list-badges">
  5416. ';
  5417. foreach ($skillsRelUser as $userSkill) {
  5418. $skill = $userSkill->getSkill();
  5419. $html .= '
  5420. <li class="thumbnail">
  5421. <a href="' . api_get_path(WEB_PATH) . 'badge/' . $userSkill->getId() . '/user/' . $userId . '" target="_blank">
  5422. <img class="img-responsive" title="' . $skill->getName() . '" src="' . $skill->getWebIconPath() . '" width="64" height="64">
  5423. <div class="caption">
  5424. <p class="text-center">' . $skill->getName() . '</p>
  5425. </div>
  5426. </a>
  5427. </li>
  5428. ';
  5429. }
  5430. $html .= '
  5431. </ul>
  5432. </div>
  5433. ';
  5434. } else {
  5435. $html .= get_lang('WithoutAchievedSkills');
  5436. }
  5437. $html .= '
  5438. </td>
  5439. </tr>
  5440. </tbody>
  5441. </table>
  5442. </div>
  5443. ';
  5444. return $html;
  5445. }
  5446. }
  5447. /**
  5448. * @todo move into a proper file
  5449. * @package chamilo.tracking
  5450. */
  5451. class TrackingCourseLog
  5452. {
  5453. /**
  5454. * @return mixed
  5455. */
  5456. public static function count_item_resources()
  5457. {
  5458. $session_id = api_get_session_id();
  5459. $course_id = api_get_course_int_id();
  5460. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5461. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  5462. $sql = "SELECT count(tool) AS total_number_of_items
  5463. FROM $table_item_property track_resource, $table_user user
  5464. WHERE
  5465. track_resource.c_id = $course_id AND
  5466. track_resource.insert_user_id = user.user_id AND
  5467. session_id " .(empty($session_id) ? ' IS NULL ' : " = $session_id ");
  5468. if (isset($_GET['keyword'])) {
  5469. $keyword = Database::escape_string(trim($_GET['keyword']));
  5470. $sql .= " AND (
  5471. user.username LIKE '%".$keyword."%' OR
  5472. lastedit_type LIKE '%".$keyword."%' OR
  5473. tool LIKE '%".$keyword."%'
  5474. )";
  5475. }
  5476. $sql .= " AND tool IN (
  5477. 'document',
  5478. 'learnpath',
  5479. 'quiz',
  5480. 'glossary',
  5481. 'link',
  5482. 'course_description',
  5483. 'announcement',
  5484. 'thematic',
  5485. 'thematic_advance',
  5486. 'thematic_plan'
  5487. )";
  5488. $res = Database::query($sql);
  5489. $obj = Database::fetch_object($res);
  5490. return $obj->total_number_of_items;
  5491. }
  5492. /**
  5493. * @param $from
  5494. * @param $number_of_items
  5495. * @param $column
  5496. * @param $direction
  5497. * @return array
  5498. */
  5499. public static function get_item_resources_data($from, $number_of_items, $column, $direction)
  5500. {
  5501. $session_id = api_get_session_id();
  5502. $course_id = api_get_course_int_id();
  5503. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5504. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  5505. $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
  5506. $session_id = intval($session_id);
  5507. $sql = "SELECT
  5508. tool as col0,
  5509. lastedit_type as col1,
  5510. ref as ref,
  5511. user.username as col3,
  5512. insert_date as col5,
  5513. visibility as col6,
  5514. user.user_id as user_id
  5515. FROM $table_item_property track_resource, $table_user user
  5516. WHERE
  5517. track_resource.c_id = $course_id AND
  5518. track_resource.insert_user_id = user.user_id AND
  5519. session_id " .(empty($session_id) ? ' IS NULL ' : " = $session_id ");
  5520. if (isset($_GET['keyword'])) {
  5521. $keyword = Database::escape_string(trim($_GET['keyword']));
  5522. $sql .= " AND (
  5523. user.username LIKE '%".$keyword."%' OR
  5524. lastedit_type LIKE '%".$keyword."%' OR
  5525. tool LIKE '%".$keyword."%'
  5526. ) ";
  5527. }
  5528. $sql .= " AND tool IN (
  5529. 'document',
  5530. 'learnpath',
  5531. 'quiz',
  5532. 'glossary',
  5533. 'link',
  5534. 'course_description',
  5535. 'announcement',
  5536. 'thematic',
  5537. 'thematic_advance',
  5538. 'thematic_plan'
  5539. )";
  5540. if ($column == 0) {
  5541. $column = '0';
  5542. }
  5543. if ($column != '' && $direction != '') {
  5544. if ($column != 2 && $column != 4) {
  5545. $sql .= " ORDER BY col$column $direction";
  5546. }
  5547. } else {
  5548. $sql .= " ORDER BY col5 DESC ";
  5549. }
  5550. $from = intval($from);
  5551. $number_of_items = intval($number_of_items);
  5552. $sql .= " LIMIT $from, $number_of_items ";
  5553. $res = Database::query($sql);
  5554. $resources = array();
  5555. $thematic_tools = array('thematic', 'thematic_advance', 'thematic_plan');
  5556. while ($row = Database::fetch_array($res)) {
  5557. $ref = $row['ref'];
  5558. $table_name = self::get_tool_name_table($row['col0']);
  5559. $table_tool = Database::get_course_table($table_name['table_name']);
  5560. $id = $table_name['id_tool'];
  5561. $recorset = false;
  5562. if (in_array($row['col0'], array('thematic_plan', 'thematic_advance'))) {
  5563. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  5564. $sql = "SELECT thematic_id FROM $table_tool
  5565. WHERE c_id = $course_id AND id = $ref";
  5566. $rs_thematic = Database::query($sql);
  5567. if (Database::num_rows($rs_thematic)) {
  5568. $row_thematic = Database::fetch_array($rs_thematic);
  5569. $thematic_id = $row_thematic['thematic_id'];
  5570. $sql = "SELECT session.id, session.name, user.username
  5571. FROM $tbl_thematic t, $table_session session, $table_user user
  5572. WHERE
  5573. t.c_id = $course_id AND
  5574. t.session_id = session.id AND
  5575. session.id_coach = user.user_id AND
  5576. t.id = $thematic_id";
  5577. $recorset = Database::query($sql);
  5578. }
  5579. } else {
  5580. $sql = "SELECT session.id, session.name, user.username
  5581. FROM $table_tool tool, $table_session session, $table_user user
  5582. WHERE
  5583. tool.c_id = $course_id AND
  5584. tool.session_id = session.id AND
  5585. session.id_coach = user.user_id AND
  5586. tool.$id = $ref";
  5587. $recorset = Database::query($sql);
  5588. }
  5589. if (!empty($recorset)) {
  5590. $obj = Database::fetch_object($recorset);
  5591. $name_session = '';
  5592. $coach_name = '';
  5593. if (!empty($obj)) {
  5594. $name_session = $obj->name;
  5595. $coach_name = $obj->username;
  5596. }
  5597. $url_tool = api_get_path(WEB_CODE_PATH).$table_name['link_tool'];
  5598. $row[0] = '';
  5599. if ($row['col6'] != 2) {
  5600. if (in_array($row['col0'], $thematic_tools)) {
  5601. $exp_thematic_tool = explode('_', $row['col0']);
  5602. $thematic_tool_title = '';
  5603. if (is_array($exp_thematic_tool)) {
  5604. foreach ($exp_thematic_tool as $exp) {
  5605. $thematic_tool_title .= api_ucfirst($exp);
  5606. }
  5607. } else {
  5608. $thematic_tool_title = api_ucfirst($row['col0']);
  5609. }
  5610. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'&action=thematic_details">'.get_lang($thematic_tool_title).'</a>';
  5611. } else {
  5612. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'">'.get_lang('Tool'.api_ucfirst($row['col0'])).'</a>';
  5613. }
  5614. } else {
  5615. $row[0] = api_ucfirst($row['col0']);
  5616. }
  5617. $row[1] = get_lang($row[1]);
  5618. $row[6] = api_convert_and_format_date($row['col5'], null, date_default_timezone_get());
  5619. $row[5] = '';
  5620. //@todo Improve this code please
  5621. switch ($table_name['table_name']) {
  5622. case 'document':
  5623. $sql = "SELECT tool.title as title FROM $table_tool tool
  5624. WHERE c_id = $course_id AND id = $ref";
  5625. $rs_document = Database::query($sql);
  5626. $obj_document = Database::fetch_object($rs_document);
  5627. if ($obj_document) {
  5628. $row[5] = $obj_document->title;
  5629. }
  5630. break;
  5631. case 'announcement':
  5632. $sql = "SELECT title FROM $table_tool
  5633. WHERE c_id = $course_id AND id = $ref";
  5634. $rs_document = Database::query($sql);
  5635. $obj_document = Database::fetch_object($rs_document);
  5636. if ($obj_document) {
  5637. $row[5] = $obj_document->title;
  5638. }
  5639. break;
  5640. case 'glossary':
  5641. $sql = "SELECT name FROM $table_tool
  5642. WHERE c_id = $course_id AND glossary_id = $ref";
  5643. $rs_document = Database::query($sql);
  5644. $obj_document = Database::fetch_object($rs_document);
  5645. if ($obj_document) {
  5646. $row[5] = $obj_document->name;
  5647. }
  5648. break;
  5649. case 'lp':
  5650. $sql = "SELECT name
  5651. FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  5652. $rs_document = Database::query($sql);
  5653. $obj_document = Database::fetch_object($rs_document);
  5654. $row[5] = $obj_document->name;
  5655. break;
  5656. case 'quiz':
  5657. $sql = "SELECT title FROM $table_tool
  5658. WHERE c_id = $course_id AND id = $ref";
  5659. $rs_document = Database::query($sql);
  5660. $obj_document = Database::fetch_object($rs_document);
  5661. if ($obj_document) {
  5662. $row[5] = $obj_document->title;
  5663. }
  5664. break;
  5665. case 'course_description':
  5666. $sql = "SELECT title FROM $table_tool
  5667. WHERE c_id = $course_id AND id = $ref";
  5668. $rs_document = Database::query($sql);
  5669. $obj_document = Database::fetch_object($rs_document);
  5670. if ($obj_document) {
  5671. $row[5] = $obj_document->title;
  5672. }
  5673. break;
  5674. case 'thematic':
  5675. $rs = Database::query("SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  5676. if (Database::num_rows($rs) > 0) {
  5677. $obj = Database::fetch_object($rs);
  5678. if ($obj) {
  5679. $row[5] = $obj->title;
  5680. }
  5681. }
  5682. break;
  5683. case 'thematic_advance':
  5684. $rs = Database::query("SELECT content FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  5685. if (Database::num_rows($rs) > 0) {
  5686. $obj = Database::fetch_object($rs);
  5687. if ($obj) {
  5688. $row[5] = $obj->content;
  5689. }
  5690. }
  5691. break;
  5692. case 'thematic_plan':
  5693. $rs = Database::query("SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  5694. if (Database::num_rows($rs) > 0) {
  5695. $obj = Database::fetch_object($rs);
  5696. if ($obj) {
  5697. $row[5] = $obj->title;
  5698. }
  5699. }
  5700. break;
  5701. default:
  5702. break;
  5703. }
  5704. $row2 = $name_session;
  5705. if (!empty($coach_name)) {
  5706. $row2 .= '<br />'.get_lang('Coach').': '.$coach_name;
  5707. }
  5708. $row[2] = $row2;
  5709. if (!empty($row['col3'])) {
  5710. $userInfo = api_get_user_info($row['user_id']);
  5711. $row['col3'] = Display::url(
  5712. $row['col3'],
  5713. $userInfo['profile_url']
  5714. );
  5715. $row[3] = $row['col3'];
  5716. $ip = TrackingUserLog::get_ip_from_user_event($row['user_id'], $row['col5'], true);
  5717. if (empty($ip)) {
  5718. $ip = get_lang('Unknown');
  5719. }
  5720. $row[4] = $ip;
  5721. }
  5722. $resources[] = $row;
  5723. }
  5724. }
  5725. return $resources;
  5726. }
  5727. /**
  5728. * @param string $tool
  5729. *
  5730. * @return array
  5731. */
  5732. public static function get_tool_name_table($tool)
  5733. {
  5734. switch ($tool) {
  5735. case 'document':
  5736. $table_name = TABLE_DOCUMENT;
  5737. $link_tool = 'document/document.php';
  5738. $id_tool = 'id';
  5739. break;
  5740. case 'learnpath':
  5741. $table_name = TABLE_LP_MAIN;
  5742. $link_tool = 'lp/lp_controller.php';
  5743. $id_tool = 'id';
  5744. break;
  5745. case 'quiz':
  5746. $table_name = TABLE_QUIZ_TEST;
  5747. $link_tool = 'exercise/exercise.php';
  5748. $id_tool = 'id';
  5749. break;
  5750. case 'glossary':
  5751. $table_name = TABLE_GLOSSARY;
  5752. $link_tool = 'glossary/index.php';
  5753. $id_tool = 'glossary_id';
  5754. break;
  5755. case 'link':
  5756. $table_name = TABLE_LINK;
  5757. $link_tool = 'link/link.php';
  5758. $id_tool = 'id';
  5759. break;
  5760. case 'course_description':
  5761. $table_name = TABLE_COURSE_DESCRIPTION;
  5762. $link_tool = 'course_description/';
  5763. $id_tool = 'id';
  5764. break;
  5765. case 'announcement':
  5766. $table_name = TABLE_ANNOUNCEMENT;
  5767. $link_tool = 'announcements/announcements.php';
  5768. $id_tool = 'id';
  5769. break;
  5770. case 'thematic':
  5771. $table_name = TABLE_THEMATIC;
  5772. $link_tool = 'course_progress/index.php';
  5773. $id_tool = 'id';
  5774. break;
  5775. case 'thematic_advance':
  5776. $table_name = TABLE_THEMATIC_ADVANCE;
  5777. $link_tool = 'course_progress/index.php';
  5778. $id_tool = 'id';
  5779. break;
  5780. case 'thematic_plan':
  5781. $table_name = TABLE_THEMATIC_PLAN;
  5782. $link_tool = 'course_progress/index.php';
  5783. $id_tool = 'id';
  5784. break;
  5785. default:
  5786. $table_name = $tool;
  5787. break;
  5788. }
  5789. return array(
  5790. 'table_name' => $table_name,
  5791. 'link_tool' => $link_tool,
  5792. 'id_tool' => $id_tool
  5793. );
  5794. }
  5795. public static function display_additional_profile_fields()
  5796. {
  5797. // getting all the extra profile fields that are defined by the platform administrator
  5798. $extra_fields = UserManager::get_extra_fields(0,50,5,'ASC');
  5799. // creating the form
  5800. $return = '<form action="courseLog.php" method="get" name="additional_profile_field_form" id="additional_profile_field_form">';
  5801. // the select field with the additional user profile fields (= this is where we select the field of which we want to see
  5802. // the information the users have entered or selected.
  5803. $return .= '<select name="additional_profile_field">';
  5804. $return .= '<option value="-">'.get_lang('SelectFieldToAdd').'</option>';
  5805. $extra_fields_to_show = 0;
  5806. foreach ($extra_fields as $key=>$field) {
  5807. // show only extra fields that are visible + and can be filtered, added by J.Montoya
  5808. if ($field[6]==1 && $field[8] == 1) {
  5809. if (isset($_GET['additional_profile_field']) && $field[0] == $_GET['additional_profile_field'] ) {
  5810. $selected = 'selected="selected"';
  5811. } else {
  5812. $selected = '';
  5813. }
  5814. $extra_fields_to_show++;
  5815. $return .= '<option value="'.$field[0].'" '.$selected.'>'.$field[3].'</option>';
  5816. }
  5817. }
  5818. $return .= '</select>';
  5819. // the form elements for the $_GET parameters (because the form is passed through GET
  5820. foreach ($_GET as $key=>$value){
  5821. if ($key <> 'additional_profile_field') {
  5822. $return .= '<input type="hidden" name="'.Security::remove_XSS($key).'" value="'.Security::remove_XSS($value).'" />';
  5823. }
  5824. }
  5825. // the submit button
  5826. $return .= '<button class="save" type="submit">'.get_lang('AddAdditionalProfileField').'</button>';
  5827. $return .= '</form>';
  5828. if ($extra_fields_to_show > 0) {
  5829. return $return;
  5830. } else {
  5831. return '';
  5832. }
  5833. }
  5834. /**
  5835. * This function gets all the information of a certrain ($field_id)
  5836. * additional profile field for a specific list of users is more efficent
  5837. * than get_addtional_profile_information_of_field() function
  5838. * It gets the information of all the users so that it can be displayed
  5839. * in the sortable table or in the csv or xls export
  5840. *
  5841. * @author Julio Montoya <gugli100@gmail.com>
  5842. * @param int field id
  5843. * @param array list of user ids
  5844. * @return array
  5845. * @since Nov 2009
  5846. * @version 1.8.6.2
  5847. */
  5848. public static function get_addtional_profile_information_of_field_by_user($field_id, $users)
  5849. {
  5850. // Database table definition
  5851. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  5852. $table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  5853. $extraField = Database::get_main_table(TABLE_EXTRA_FIELD);
  5854. $result_extra_field = UserManager::get_extra_field_information($field_id);
  5855. if (!empty($users)) {
  5856. if ($result_extra_field['field_type'] == UserManager::USER_FIELD_TYPE_TAG ) {
  5857. foreach($users as $user_id) {
  5858. $user_result = UserManager::get_user_tags($user_id, $field_id);
  5859. $tag_list = array();
  5860. foreach($user_result as $item) {
  5861. $tag_list[] = $item['tag'];
  5862. }
  5863. $return[$user_id][] = implode(', ',$tag_list);
  5864. }
  5865. } else {
  5866. $new_user_array = array();
  5867. foreach ($users as $user_id) {
  5868. $new_user_array[]= "'".$user_id."'";
  5869. }
  5870. $users = implode(',',$new_user_array);
  5871. $extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  5872. // Selecting only the necessary information NOT ALL the user list
  5873. $sql = "SELECT user.user_id, v.value
  5874. FROM $table_user user
  5875. INNER JOIN $table_user_field_values v
  5876. ON (user.user_id = v.item_id)
  5877. INNER JOIN $extraField f
  5878. ON (f.id = v.field_id)
  5879. WHERE
  5880. f.extra_field_type = $extraFieldType AND
  5881. v.field_id=".intval($field_id)." AND
  5882. user.user_id IN ($users)";
  5883. $result = Database::query($sql);
  5884. while($row = Database::fetch_array($result)) {
  5885. // get option value for field type double select by id
  5886. if (!empty($row['value'])) {
  5887. if ($result_extra_field['field_type'] ==
  5888. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  5889. ) {
  5890. $id_double_select = explode(';', $row['value']);
  5891. if (is_array($id_double_select)) {
  5892. $value1 = $result_extra_field['options'][$id_double_select[0]]['option_value'];
  5893. $value2 = $result_extra_field['options'][$id_double_select[1]]['option_value'];
  5894. $row['value'] = ($value1.';'.$value2);
  5895. }
  5896. }
  5897. }
  5898. // get other value from extra field
  5899. $return[$row['user_id']][] = $row['value'];
  5900. }
  5901. }
  5902. }
  5903. return $return;
  5904. }
  5905. /**
  5906. * count the number of students in this course (used for SortableTable)
  5907. * Deprecated
  5908. */
  5909. public function count_student_in_course()
  5910. {
  5911. global $nbStudents;
  5912. return $nbStudents;
  5913. }
  5914. public function sort_users($a, $b)
  5915. {
  5916. return strcmp(trim(api_strtolower($a[$_SESSION['tracking_column']])), trim(api_strtolower($b[$_SESSION['tracking_column']])));
  5917. }
  5918. public function sort_users_desc($a, $b)
  5919. {
  5920. return strcmp( trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
  5921. }
  5922. /**
  5923. * Get number of users for sortable with pagination
  5924. * @return int
  5925. */
  5926. public static function get_number_of_users()
  5927. {
  5928. global $user_ids;
  5929. return count($user_ids);
  5930. }
  5931. /**
  5932. * Get data for users list in sortable with pagination
  5933. * @param $from
  5934. * @param $number_of_items
  5935. * @param $column
  5936. * @param $direction
  5937. * @param $includeInvitedUsers boolean Whether include the invited users
  5938. * @return array
  5939. */
  5940. public static function get_user_data($from, $number_of_items, $column, $direction, $includeInvitedUsers = false)
  5941. {
  5942. global $user_ids, $course_code, $additional_user_profile_info, $export_csv, $is_western_name_order, $csv_content, $session_id;
  5943. $course_code = Database::escape_string($course_code);
  5944. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  5945. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  5946. $access_url_id = api_get_current_access_url_id();
  5947. // get all users data from a course for sortable with limit
  5948. if (is_array($user_ids)) {
  5949. $user_ids = array_map('intval', $user_ids);
  5950. $condition_user = " WHERE user.user_id IN (".implode(',',$user_ids).") ";
  5951. } else {
  5952. $user_ids = intval($user_ids);
  5953. $condition_user = " WHERE user.user_id = $user_ids ";
  5954. }
  5955. if (!empty($_GET['user_keyword'])) {
  5956. $keyword = trim(Database::escape_string($_GET['user_keyword']));
  5957. $condition_user .= " AND (
  5958. user.firstname LIKE '%".$keyword."%' OR
  5959. user.lastname LIKE '%".$keyword."%' OR
  5960. user.username LIKE '%".$keyword."%' OR
  5961. user.email LIKE '%".$keyword."%'
  5962. ) ";
  5963. }
  5964. $url_table = null;
  5965. $url_condition = null;
  5966. if (api_is_multiple_url_enabled()) {
  5967. $url_table = ", ".$tbl_url_rel_user." as url_users";
  5968. $url_condition = " AND user.user_id = url_users.user_id AND access_url_id='$access_url_id'";
  5969. }
  5970. $invitedUsersCondition = '';
  5971. if (!$includeInvitedUsers) {
  5972. $invitedUsersCondition = " AND user.status != " . INVITEE;
  5973. }
  5974. $sql = "SELECT user.user_id as user_id,
  5975. user.official_code as col0,
  5976. user.lastname as col1,
  5977. user.firstname as col2,
  5978. user.username as col3
  5979. FROM $tbl_user as user $url_table
  5980. $condition_user $url_condition $invitedUsersCondition";
  5981. if (!in_array($direction, array('ASC','DESC'))) {
  5982. $direction = 'ASC';
  5983. }
  5984. $column = intval($column);
  5985. $from = intval($from);
  5986. $number_of_items = intval($number_of_items);
  5987. $sql .= " ORDER BY col$column $direction ";
  5988. $sql .= " LIMIT $from,$number_of_items";
  5989. $res = Database::query($sql);
  5990. $users = array();
  5991. $course_info = api_get_course_info($course_code);
  5992. $total_surveys = 0;
  5993. $total_exercises = ExerciseLib::get_all_exercises(
  5994. $course_info,
  5995. $session_id,
  5996. false,
  5997. null,
  5998. false,
  5999. 3
  6000. );
  6001. if (empty($session_id)) {
  6002. $survey_user_list = array();
  6003. $survey_list = SurveyManager::get_surveys($course_code, $session_id);
  6004. $total_surveys = count($survey_list);
  6005. foreach ($survey_list as $survey) {
  6006. $user_list = SurveyManager::get_people_who_filled_survey(
  6007. $survey['survey_id'],
  6008. false,
  6009. $course_info['real_id']
  6010. );
  6011. foreach ($user_list as $user_id) {
  6012. isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id]++ : $survey_user_list[$user_id] = 1;
  6013. }
  6014. }
  6015. }
  6016. while ($user = Database::fetch_array($res, 'ASSOC')) {
  6017. $courseInfo = api_get_course_info($course_code);
  6018. $courseId = $courseInfo['real_id'];
  6019. $user['official_code'] = $user['col0'];
  6020. $user['lastname'] = $user['col1'];
  6021. $user['firstname'] = $user['col2'];
  6022. $user['username'] = $user['col3'];
  6023. $user['time'] = api_time_to_hms(
  6024. Tracking::get_time_spent_on_the_course(
  6025. $user['user_id'],
  6026. $courseId,
  6027. $session_id
  6028. )
  6029. );
  6030. $avg_student_score = Tracking::get_avg_student_score(
  6031. $user['user_id'],
  6032. $course_code,
  6033. array(),
  6034. $session_id
  6035. );
  6036. $avg_student_progress = Tracking::get_avg_student_progress(
  6037. $user['user_id'],
  6038. $course_code,
  6039. array(),
  6040. $session_id
  6041. );
  6042. if (empty($avg_student_progress)) {
  6043. $avg_student_progress = 0;
  6044. }
  6045. $user['average_progress'] = $avg_student_progress.'%';
  6046. $total_user_exercise = Tracking::get_exercise_student_progress(
  6047. $total_exercises,
  6048. $user['user_id'],
  6049. $courseId,
  6050. $session_id
  6051. );
  6052. $user['exercise_progress'] = $total_user_exercise;
  6053. $total_user_exercise = Tracking::get_exercise_student_average_best_attempt(
  6054. $total_exercises,
  6055. $user['user_id'],
  6056. $courseId,
  6057. $session_id
  6058. );
  6059. $user['exercise_average_best_attempt'] = $total_user_exercise;
  6060. if (is_numeric($avg_student_score)) {
  6061. $user['student_score'] = $avg_student_score.'%';
  6062. } else {
  6063. $user['student_score'] = $avg_student_score;
  6064. }
  6065. $user['count_assignments'] = Tracking::count_student_assignments(
  6066. $user['user_id'],
  6067. $course_code,
  6068. $session_id
  6069. );
  6070. $user['count_messages'] = Tracking::count_student_messages(
  6071. $user['user_id'],
  6072. $course_code,
  6073. $session_id
  6074. );
  6075. $user['first_connection'] = Tracking::get_first_connection_date_on_the_course(
  6076. $user['user_id'],
  6077. $courseId,
  6078. $session_id
  6079. );
  6080. $user['last_connection'] = Tracking::get_last_connection_date_on_the_course(
  6081. $user['user_id'],
  6082. $courseInfo,
  6083. $session_id
  6084. );
  6085. // we need to display an additional profile field
  6086. $user['additional'] = '';
  6087. if (isset($_GET['additional_profile_field']) && is_numeric($_GET['additional_profile_field'])) {
  6088. if (isset($additional_user_profile_info[$user['user_id']]) &&
  6089. is_array($additional_user_profile_info[$user['user_id']])
  6090. ) {
  6091. $user['additional'] = implode(', ', $additional_user_profile_info[$user['user_id']]);
  6092. }
  6093. }
  6094. if (empty($session_id)) {
  6095. $user['survey'] = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0) .' / '.$total_surveys;
  6096. }
  6097. $user['link'] = '<center>
  6098. <a href="../mySpace/myStudents.php?student='.$user['user_id'].'&details=true&course='.$course_code.'&origin=tracking_course&id_session='.$session_id.'">
  6099. '.Display::return_icon('2rightarrow.png').'
  6100. </a>
  6101. </center>';
  6102. // store columns in array $users
  6103. $is_western_name_order = api_is_western_name_order();
  6104. $user_row = array();
  6105. $user_row[]= $user['official_code']; //0
  6106. if ($is_western_name_order) {
  6107. $user_row[]= $user['firstname'];
  6108. $user_row[]= $user['lastname'];
  6109. } else {
  6110. $user_row[]= $user['lastname'];
  6111. $user_row[]= $user['firstname'];
  6112. }
  6113. $user_row[]= $user['username'];
  6114. $user_row[]= $user['time'];
  6115. $user_row[]= $user['average_progress'];
  6116. $user_row[]= $user['exercise_progress'];
  6117. $user_row[]= $user['exercise_average_best_attempt'];
  6118. $user_row[]= $user['student_score'];
  6119. $user_row[]= $user['count_assignments'];
  6120. $user_row[]= $user['count_messages'];
  6121. $userGroupManager = new UserGroup();
  6122. $user_row[] = $userGroupManager->getLabelsFromNameList($user['user_id'], UserGroup::NORMAL_CLASS);
  6123. if (empty($session_id)) {
  6124. $user_row[]= $user['survey'];
  6125. }
  6126. $user_row[]= $user['first_connection'];
  6127. $user_row[]= $user['last_connection'];
  6128. if (isset($_GET['additional_profile_field']) && is_numeric($_GET['additional_profile_field'])) {
  6129. $user_row[]= $user['additional'];
  6130. }
  6131. $user_row[]= $user['link'];
  6132. $users[] = $user_row;
  6133. if ($export_csv) {
  6134. if (empty($session_id)) {
  6135. $user_row = array_map('strip_tags', $user_row);
  6136. unset($user_row[14]);
  6137. unset($user_row[15]);
  6138. } else {
  6139. $user_row = array_map('strip_tags', $user_row);
  6140. unset($user_row[13]);
  6141. unset($user_row[14]);
  6142. }
  6143. $csv_content[] = $user_row;
  6144. }
  6145. }
  6146. return $users;
  6147. }
  6148. }
  6149. /**
  6150. * @package chamilo.tracking
  6151. */
  6152. class TrackingUserLog
  6153. {
  6154. /**
  6155. * Displays the number of logins every month for a specific user in a specific course.
  6156. * @param $view
  6157. * @param int $user_id
  6158. * @param int $course_id
  6159. * @param int $session_id
  6160. */
  6161. public static function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  6162. {
  6163. $MonthsLong = $GLOBALS['MonthsLong'];
  6164. // protected data
  6165. $user_id = intval($user_id);
  6166. $session_id = intval($session_id);
  6167. $course_id = Database::escape_string($course_id);
  6168. $track_access_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  6169. $tempView = $view;
  6170. if(substr($view,0,1) == '1') {
  6171. $new_view = substr_replace($view,'0',0,1);
  6172. echo "
  6173. <tr>
  6174. <td valign='top'>
  6175. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font>" .
  6176. "<b>".get_lang('LoginsAndAccessTools')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".$user_id."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=10000'>".get_lang('ExportAsCSV')."</a>]
  6177. </td>
  6178. </tr>
  6179. ";
  6180. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LoginsDetails')."<br>";
  6181. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  6182. FROM $track_access_table
  6183. WHERE access_user_id = $user_id
  6184. AND c_id = $course_id
  6185. AND access_session_id = $session_id
  6186. GROUP BY YEAR(access_date),MONTH(access_date)
  6187. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  6188. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  6189. $results = getManyResults3Col($sql);
  6190. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  6191. echo "<tr>
  6192. <td class='secLine'>
  6193. ".get_lang('LoginsTitleMonthColumn')."
  6194. </td>
  6195. <td class='secLine'>
  6196. ".get_lang('LoginsTitleCountColumn')."
  6197. </td>
  6198. </tr>";
  6199. $total = 0;
  6200. if (is_array($results)) {
  6201. for($j = 0 ; $j < count($results) ; $j++) {
  6202. echo "<tr>";
  6203. echo "<td class='content'><a href='logins_details.php?uInfo=".$user_id."&reqdate=".$results[$j][0]."&view=".Security::remove_XSS($view)."'>".$MonthsLong[date('n', $results[$j][0])-1].' '.date('Y', $results[$j][0])."</a></td>";
  6204. echo "<td valign='top' align='right' class='content'>".$results[$j][1]."</td>";
  6205. echo"</tr>";
  6206. $total = $total + $results[$j][1];
  6207. }
  6208. echo "<tr>";
  6209. echo "<td>".get_lang('Total')."</td>";
  6210. echo "<td align='right' class='content'>".$total."</td>";
  6211. echo"</tr>";
  6212. } else {
  6213. echo "<tr>";
  6214. echo "<td colspan='2'><center>".get_lang('NoResult')."</center></td>";
  6215. echo"</tr>";
  6216. }
  6217. echo "</table>";
  6218. echo "</td></tr>";
  6219. } else {
  6220. $new_view = substr_replace($view,'1',0,1);
  6221. echo "
  6222. <tr>
  6223. <td valign='top'>
  6224. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".$user_id."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('LoginsAndAccessTools')."</a>
  6225. </td>
  6226. </tr>
  6227. ";
  6228. }
  6229. }
  6230. /**
  6231. * Displays the exercise results for a specific user in a specific course.
  6232. * @param string $view
  6233. * @param int $user_id User ID
  6234. * @param string $courseCode Course code
  6235. * @return array
  6236. * @todo remove globals
  6237. */
  6238. public static function display_exercise_tracking_info($view, $user_id, $courseCode)
  6239. {
  6240. global $TBL_TRACK_HOTPOTATOES, $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $dateTimeFormatLong;
  6241. $courseId = api_get_course_int_id($courseCode);
  6242. if(substr($view,1,1) == '1') {
  6243. $new_view = substr_replace($view,'0',1,1);
  6244. echo "<tr>
  6245. <td valign='top'>
  6246. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('ExercicesResults')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=01000'>".get_lang('ExportAsCSV')."</a>]
  6247. </td>
  6248. </tr>";
  6249. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('ExercicesDetails')."<br />";
  6250. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  6251. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  6252. WHERE te.c_id = $courseId
  6253. AND te.exe_user_id = ".intval($user_id)."
  6254. AND te.exe_exo_id = ce.id
  6255. ORDER BY ce.title ASC, te.exe_date ASC";
  6256. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  6257. FROM $TBL_TRACK_HOTPOTATOES AS te
  6258. WHERE te.exe_user_id = '".intval($user_id)."' AND te.c_id = $courseId
  6259. ORDER BY te.c_id ASC, te.exe_date ASC";
  6260. $hpresults = StatsUtils::getManyResultsXCol($hpsql, 4);
  6261. $NoTestRes = 0;
  6262. $NoHPTestRes = 0;
  6263. echo "<tr>\n<td style='padding-left : 40px;padding-right : 40px;'>\n";
  6264. $results = StatsUtils::getManyResultsXCol($sql, 4);
  6265. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>\n";
  6266. echo "
  6267. <tr bgcolor='#E6E6E6'>
  6268. <td>
  6269. ".get_lang('ExercicesTitleExerciceColumn')."
  6270. </td>
  6271. <td>
  6272. ".get_lang('Date')."
  6273. </td>
  6274. <td>
  6275. ".get_lang('ExercicesTitleScoreColumn')."
  6276. </td>
  6277. </tr>";
  6278. if (is_array($results)) {
  6279. for($i = 0; $i < sizeof($results); $i++) {
  6280. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  6281. echo "<tr>\n";
  6282. echo "<td class='content'>".$results[$i][0]."</td>\n";
  6283. echo "<td class='content'>".$display_date."</td>\n";
  6284. echo "<td valign='top' align='right' class='content'>".$results[$i][1]." / ".$results[$i][2]."</td>\n";
  6285. echo "</tr>\n";
  6286. }
  6287. } else {
  6288. // istvan begin
  6289. $NoTestRes = 1;
  6290. }
  6291. // The Result of Tests
  6292. if (is_array($hpresults)) {
  6293. for($i = 0; $i < sizeof($hpresults); $i++) {
  6294. $title = GetQuizName($hpresults[$i][0],'');
  6295. if ($title == '')
  6296. $title = basename($hpresults[$i][0]);
  6297. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  6298. ?>
  6299. <tr>
  6300. <td class="content"><?php echo $title; ?></td>
  6301. <td class="content" align="center"><?php echo $display_date; ?></td>
  6302. <td class="content" align="center"><?php echo $hpresults[$i][1]; ?> / <?php echo $hpresults[$i][2]; ?>
  6303. </td>
  6304. </tr>
  6305. <?php
  6306. }
  6307. } else {
  6308. $NoHPTestRes = 1;
  6309. }
  6310. if ($NoTestRes == 1 && $NoHPTestRes == 1) {
  6311. echo "<tr>\n";
  6312. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>\n";
  6313. echo "</tr>\n";
  6314. }
  6315. echo "</table>";
  6316. echo "</td>\n</tr>\n";
  6317. } else {
  6318. $new_view = substr_replace($view,'1',1,1);
  6319. echo "
  6320. <tr>
  6321. <td valign='top'>
  6322. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=$user_id&view=".$new_view."' class='specialLink'>".get_lang('ExercicesResults')."</a>
  6323. </td>
  6324. </tr>";
  6325. }
  6326. }
  6327. /**
  6328. * Displays the student publications for a specific user in a specific course.
  6329. * @todo remove globals
  6330. */
  6331. public static function display_student_publications_tracking_info($view, $user_id, $course_id)
  6332. {
  6333. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK;
  6334. $_course = api_get_course_info_by_id($course_id);
  6335. if (substr($view,2,1) == '1') {
  6336. $new_view = substr_replace($view,'0',2,1);
  6337. echo "<tr>
  6338. <td valign='top'>
  6339. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('WorkUploads')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00100'>".get_lang('ExportAsCSV')."</a>]
  6340. </td>
  6341. </tr>";
  6342. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('WorksDetails')."<br>";
  6343. $sql = "SELECT u.upload_date, w.title, w.author,w.url
  6344. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  6345. WHERE u.upload_work_id = w.id
  6346. AND u.upload_user_id = '".intval($user_id)."'
  6347. AND u.c_id = '".intval($course_id)."'
  6348. ORDER BY u.upload_date DESC";
  6349. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  6350. $results = StatsUtils::getManyResultsXCol($sql,4);
  6351. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  6352. echo "<tr>
  6353. <td class='secLine' width='40%'>
  6354. ".get_lang('WorkTitle')."
  6355. </td>
  6356. <td class='secLine' width='30%'>
  6357. ".get_lang('WorkAuthors')."
  6358. </td>
  6359. <td class='secLine' width='30%'>
  6360. ".get_lang('Date')."
  6361. </td>
  6362. </tr>";
  6363. if (is_array($results)) {
  6364. for($j = 0 ; $j < count($results) ; $j++) {
  6365. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  6366. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  6367. echo "<tr>";
  6368. echo "<td class='content'>"
  6369. ."<a href ='".$pathToFile."'>".$results[$j][1]."</a>"
  6370. ."</td>";
  6371. echo "<td class='content'>".$results[$j][2]."</td>";
  6372. echo "<td class='content'>".$beautifulDate."</td>";
  6373. echo"</tr>";
  6374. }
  6375. } else {
  6376. echo "<tr>";
  6377. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>";
  6378. echo"</tr>";
  6379. }
  6380. echo "</table>";
  6381. echo "</td></tr>";
  6382. } else {
  6383. $new_view = substr_replace($view,'1',2,1);
  6384. echo "
  6385. <tr>
  6386. <td valign='top'>
  6387. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('WorkUploads')."</a>
  6388. </td>
  6389. </tr>
  6390. ";
  6391. }
  6392. }
  6393. /**
  6394. * Displays the links followed for a specific user in a specific course.
  6395. * @todo remove globals
  6396. */
  6397. public static function display_links_tracking_info($view, $user_id, $courseCode)
  6398. {
  6399. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  6400. $courseId = api_get_course_int_id($courseCode);
  6401. if (substr($view,3,1) == '1') {
  6402. $new_view = substr_replace($view,'0',3,1);
  6403. echo "
  6404. <tr>
  6405. <td valign='top'>
  6406. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('LinksAccess')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00010'>".get_lang('ExportAsCSV')."</a>]
  6407. </td>
  6408. </tr>
  6409. ";
  6410. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LinksDetails')."<br>";
  6411. $sql = "SELECT cl.title, cl.url
  6412. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  6413. WHERE sl.links_link_id = cl.id
  6414. AND sl.c_id = $courseId
  6415. AND sl.links_user_id = ".intval($user_id)."
  6416. GROUP BY cl.title, cl.url";
  6417. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  6418. $results = StatsUtils::getManyResults2Col($sql);
  6419. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  6420. echo "<tr>
  6421. <td class='secLine'>
  6422. ".get_lang('LinksTitleLinkColumn')."
  6423. </td>
  6424. </tr>";
  6425. if (is_array($results)) {
  6426. for($j = 0 ; $j < count($results) ; $j++) {
  6427. echo "<tr>";
  6428. echo "<td class='content'><a href='".$results[$j][1]."'>".$results[$j][0]."</a></td>";
  6429. echo"</tr>";
  6430. }
  6431. } else {
  6432. echo "<tr>";
  6433. echo "<td ><center>".get_lang('NoResult')."</center></td>";
  6434. echo"</tr>";
  6435. }
  6436. echo "</table>";
  6437. echo "</td></tr>";
  6438. } else {
  6439. $new_view = substr_replace($view,'1',3,1);
  6440. echo "
  6441. <tr>
  6442. <td valign='top'>
  6443. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('LinksAccess')."</a>
  6444. </td>
  6445. </tr>
  6446. ";
  6447. }
  6448. }
  6449. /**
  6450. * Displays the documents downloaded for a specific user in a specific course.
  6451. * @param string kind of view inside tracking info
  6452. * @param int User id
  6453. * @param string Course code
  6454. * @param int Session id (optional, default = 0)
  6455. * @return void
  6456. */
  6457. public static function display_document_tracking_info($view, $user_id, $course_code, $session_id = 0)
  6458. {
  6459. // protect data
  6460. $user_id = intval($user_id);
  6461. $courseId = api_get_course_int_id($course_code);
  6462. $session_id = intval($session_id);
  6463. $downloads_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  6464. if(substr($view,4,1) == '1') {
  6465. $new_view = substr_replace($view,'0',4,1);
  6466. echo "
  6467. <tr>
  6468. <td valign='top'>
  6469. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('DocumentsAccess')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00001'>".get_lang('ExportAsCSV')."</a>]
  6470. </td>
  6471. </tr>
  6472. ";
  6473. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('DocumentsDetails')."<br>";
  6474. $sql = "SELECT down_doc_path
  6475. FROM $downloads_table
  6476. WHERE c_id = $courseId
  6477. AND down_user_id = $user_id
  6478. AND down_session_id = $session_id
  6479. GROUP BY down_doc_path";
  6480. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  6481. $results = StatsUtils::getManyResults1Col($sql);
  6482. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>";
  6483. echo "<tr>
  6484. <td class='secLine'>
  6485. ".get_lang('DocumentsTitleDocumentColumn')."
  6486. </td>
  6487. </tr>";
  6488. if (is_array($results)) {
  6489. for($j = 0 ; $j < count($results) ; $j++) {
  6490. echo "<tr>";
  6491. echo "<td class='content'>".$results[$j]."</td>";
  6492. echo"</tr>";
  6493. }
  6494. } else {
  6495. echo "<tr>";
  6496. echo "<td><center>".get_lang('NoResult')."</center></td>";
  6497. echo"</tr>";
  6498. }
  6499. echo "</table>";
  6500. echo "</td></tr>";
  6501. } else {
  6502. $new_view = substr_replace($view,'1',4,1);
  6503. echo "
  6504. <tr>
  6505. <td valign='top'>
  6506. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('DocumentsAccess')."</a>
  6507. </td>
  6508. </tr>
  6509. ";
  6510. }
  6511. }
  6512. /**
  6513. * Gets the IP of a given user, using the last login before the given date
  6514. * @param int User ID
  6515. * @param string Datetime
  6516. * @param bool Whether to return the IP as a link or just as an IP
  6517. * @param string If defined and return_as_link if true, will be used as the text to be shown as the link
  6518. * @return string IP address (or false on error)
  6519. * @assert (0,0) === false
  6520. */
  6521. public static function get_ip_from_user_event($user_id, $event_date, $return_as_link = false, $body_replace = null)
  6522. {
  6523. if (empty($user_id) or empty($event_date)) {
  6524. return false;
  6525. }
  6526. $user_id = intval($user_id);
  6527. $event_date = Database::escape_string($event_date);
  6528. $table_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  6529. $sql_ip = "SELECT login_date, user_ip FROM $table_login
  6530. WHERE login_user_id = $user_id AND login_date < '$event_date'
  6531. ORDER BY login_date DESC LIMIT 1";
  6532. $ip = '';
  6533. $res_ip = Database::query($sql_ip);
  6534. if ($res_ip !== false && Database::num_rows($res_ip)>0) {
  6535. $row_ip = Database::fetch_row($res_ip);
  6536. if ($return_as_link) {
  6537. $ip = Display::url(
  6538. (empty($body_replace)?$row_ip[1]:$body_replace), 'http://www.whatsmyip.org/ip-geo-location/?ip='.$row_ip[1],
  6539. array('title'=>get_lang('TraceIP'), 'target'=>'_blank')
  6540. );
  6541. } else {
  6542. $ip = $row_ip[1];
  6543. }
  6544. }
  6545. return $ip;
  6546. }
  6547. }
  6548. /**
  6549. * @package chamilo.tracking
  6550. */
  6551. class TrackingUserLogCSV
  6552. {
  6553. /**
  6554. * Displays the number of logins every month for a specific user in a specific course.
  6555. * @param $view
  6556. * @param int $user_id
  6557. * @param int $course_id
  6558. * @param int $session_id
  6559. * @return array
  6560. */
  6561. public function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  6562. {
  6563. $MonthsLong = $GLOBALS['MonthsLong'];
  6564. $track_access_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  6565. // protected data
  6566. $user_id = intval($user_id);
  6567. $session_id = intval($session_id);
  6568. $course_id = intval($course_id);
  6569. $tempView = $view;
  6570. if (substr($view,0,1) == '1') {
  6571. $new_view = substr_replace($view,'0',0,1);
  6572. $title[1]= get_lang('LoginsAndAccessTools').get_lang('LoginsDetails');
  6573. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  6574. FROM $track_access_table
  6575. WHERE access_user_id = $user_id
  6576. AND c_id = $course_id
  6577. AND access_session_id = $session_id
  6578. GROUP BY YEAR(access_date),MONTH(access_date)
  6579. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  6580. //$results = getManyResults2Col($sql);
  6581. $results = getManyResults3Col($sql);
  6582. $title_line= get_lang('LoginsTitleMonthColumn').';'.get_lang('LoginsTitleCountColumn')."\n";
  6583. $line='';
  6584. $total = 0;
  6585. if (is_array($results)) {
  6586. for($j = 0 ; $j < count($results) ; $j++) {
  6587. $line .= $results[$j][0].';'.$results[$j][1]."\n";
  6588. $total = $total + $results[$j][1];
  6589. }
  6590. $line .= get_lang('Total').";".$total."\n";
  6591. } else {
  6592. $line= get_lang('NoResult')."</center></td>";
  6593. }
  6594. } else {
  6595. $new_view = substr_replace($view,'1',0,1);
  6596. }
  6597. return array($title_line, $line);
  6598. }
  6599. /**
  6600. * Displays the exercise results for a specific user in a specific course.
  6601. * @param string $view
  6602. * @param int $user_id User ID
  6603. * @param string $courseCode Course code
  6604. * @return array
  6605. * @todo remove globals
  6606. */
  6607. public function display_exercise_tracking_info($view, $userId, $courseCode)
  6608. {
  6609. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $TABLETRACK_HOTPOTATOES, $dateTimeFormatLong;
  6610. $courseId = api_get_course_int_id($courseCode);
  6611. $userId = intval($userId);
  6612. if (substr($view,1,1) == '1') {
  6613. $new_view = substr_replace($view,'0',1,1);
  6614. $title[1] = get_lang('ExercicesDetails');
  6615. $line = '';
  6616. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  6617. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  6618. WHERE te.c_id = $courseId
  6619. AND te.exe_user_id = $userId
  6620. AND te.exe_exo_id = ce.id
  6621. ORDER BY ce.title ASC, te.exe_date ASC";
  6622. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  6623. FROM $TABLETRACK_HOTPOTATOES AS te
  6624. WHERE te.exe_user_id = '$userId' AND te.c_id = $courseId
  6625. ORDER BY te.c_id ASC, te.exe_date ASC";
  6626. $hpresults = StatsUtils::getManyResultsXCol($hpsql, 4);
  6627. $NoTestRes = 0;
  6628. $NoHPTestRes = 0;
  6629. $results = StatsUtils::getManyResultsXCol($sql, 4);
  6630. $title_line = get_lang('ExercicesTitleExerciceColumn').";".get_lang('Date').';'.get_lang('ExercicesTitleScoreColumn')."\n";
  6631. if (is_array($results)) {
  6632. for($i = 0; $i < sizeof($results); $i++)
  6633. {
  6634. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  6635. $line .= $results[$i][0].";".$display_date.";".$results[$i][1]." / ".$results[$i][2]."\n";
  6636. }
  6637. } else {
  6638. // istvan begin
  6639. $NoTestRes = 1;
  6640. }
  6641. // The Result of Tests
  6642. if (is_array($hpresults)) {
  6643. for($i = 0; $i < sizeof($hpresults); $i++) {
  6644. $title = GetQuizName($hpresults[$i][0],'');
  6645. if ($title == '')
  6646. $title = basename($hpresults[$i][0]);
  6647. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  6648. $line .= $title.';'.$display_date.';'.$hpresults[$i][1].'/'.$hpresults[$i][2]."\n";
  6649. }
  6650. } else {
  6651. $NoHPTestRes = 1;
  6652. }
  6653. if ($NoTestRes == 1 && $NoHPTestRes == 1) {
  6654. $line=get_lang('NoResult');
  6655. }
  6656. } else {
  6657. $new_view = substr_replace($view,'1',1,1);
  6658. }
  6659. return array($title_line, $line);
  6660. }
  6661. /**
  6662. * Displays the student publications for a specific user in a specific course.
  6663. * @todo remove globals
  6664. */
  6665. public function display_student_publications_tracking_info($view, $user_id, $course_id)
  6666. {
  6667. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK;
  6668. $_course = api_get_course_info();
  6669. $user_id = intval($user_id);
  6670. $course_id = intval($course_id);
  6671. if (substr($view,2,1) == '1') {
  6672. $sql = "SELECT u.upload_date, w.title, w.author, w.url
  6673. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  6674. WHERE
  6675. u.upload_work_id = w.id AND
  6676. u.upload_user_id = '$user_id' AND
  6677. u.c_id = '$course_id'
  6678. ORDER BY u.upload_date DESC";
  6679. $results = StatsUtils::getManyResultsXCol($sql,4);
  6680. $title[1]=get_lang('WorksDetails');
  6681. $line='';
  6682. $title_line=get_lang('WorkTitle').";".get_lang('WorkAuthors').";".get_lang('Date')."\n";
  6683. if (is_array($results)) {
  6684. for($j = 0 ; $j < count($results) ; $j++) {
  6685. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  6686. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  6687. $line .= $results[$j][1].";".$results[$j][2].";".$beautifulDate."\n";
  6688. }
  6689. } else {
  6690. $line= get_lang('NoResult');
  6691. }
  6692. }
  6693. return array($title_line, $line);
  6694. }
  6695. /**
  6696. * Displays the links followed for a specific user in a specific course.
  6697. * @todo remove globals
  6698. */
  6699. public function display_links_tracking_info($view, $userId, $courseCode)
  6700. {
  6701. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  6702. $courseId = api_get_course_int_id($courseCode);
  6703. $userId = intval($userId);
  6704. $line = null;
  6705. if (substr($view,3,1) == '1') {
  6706. $new_view = substr_replace($view,'0',3,1);
  6707. $title[1]=get_lang('LinksDetails');
  6708. $sql = "SELECT cl.title, cl.url
  6709. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  6710. WHERE sl.links_link_id = cl.id
  6711. AND sl.c_id = $courseId
  6712. AND sl.links_user_id = $userId
  6713. GROUP BY cl.title, cl.url";
  6714. $results = StatsUtils::getManyResults2Col($sql);
  6715. $title_line= get_lang('LinksTitleLinkColumn')."\n";
  6716. if (is_array($results)) {
  6717. for ($j = 0 ; $j < count($results) ; $j++) {
  6718. $line .= $results[$j][0]."\n";
  6719. }
  6720. } else {
  6721. $line=get_lang('NoResult');
  6722. }
  6723. } else {
  6724. $new_view = substr_replace($view,'1',3,1);
  6725. }
  6726. return array($title_line, $line);
  6727. }
  6728. /**
  6729. * Displays the documents downloaded for a specific user in a specific course.
  6730. * @param string kind of view inside tracking info
  6731. * @param int User id
  6732. * @param string Course code
  6733. * @param int Session id (optional, default = 0)
  6734. * @return void
  6735. */
  6736. public function display_document_tracking_info($view, $user_id, $courseCode, $session_id = 0)
  6737. {
  6738. // protect data
  6739. $user_id = intval($user_id);
  6740. $courseId = api_get_course_int_id($courseCode);
  6741. $session_id = intval($session_id);
  6742. $downloads_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  6743. if (substr($view,4,1) == '1') {
  6744. $new_view = substr_replace($view,'0',4,1);
  6745. $title[1]= get_lang('DocumentsDetails');
  6746. $sql = "SELECT down_doc_path
  6747. FROM $downloads_table
  6748. WHERE c_id = $courseId
  6749. AND down_user_id = $user_id
  6750. AND down_session_id = $session_id
  6751. GROUP BY down_doc_path";
  6752. $results = StatsUtils::getManyResults1Col($sql);
  6753. $title_line = get_lang('DocumentsTitleDocumentColumn')."\n";
  6754. $line = null;
  6755. if (is_array($results)) {
  6756. for ($j = 0 ; $j < count($results) ; $j++) {
  6757. $line .= $results[$j]."\n";
  6758. }
  6759. } else {
  6760. $line = get_lang('NoResult');
  6761. }
  6762. } else {
  6763. $new_view = substr_replace($view,'1',4,1);
  6764. }
  6765. return array($title_line, $line);
  6766. }
  6767. /**
  6768. * @param $userId
  6769. * @param $courseInfo
  6770. * @param int $sessionId
  6771. * @return array
  6772. */
  6773. public static function getToolInformation(
  6774. $userId,
  6775. $courseInfo,
  6776. $sessionId = 0
  6777. ) {
  6778. $csvContent = array();
  6779. $courseToolInformation = null;
  6780. $headerTool = array(
  6781. array(get_lang('Title')),
  6782. array(get_lang('CreatedAt')),
  6783. array(get_lang('UpdatedAt')),
  6784. );
  6785. $headerListForCSV = array();
  6786. foreach ($headerTool as $item) {
  6787. $headerListForCSV[] = $item[0];
  6788. }
  6789. $courseForumInformationArray = getForumCreatedByUser(
  6790. $userId,
  6791. $courseInfo['real_id'],
  6792. $sessionId
  6793. );
  6794. if (!empty($courseForumInformationArray)) {
  6795. $csvContent[] = array();
  6796. $csvContent[] = get_lang('Forums');
  6797. $csvContent[] = $headerListForCSV;
  6798. foreach ($courseForumInformationArray as $row) {
  6799. $csvContent[] = $row;
  6800. }
  6801. $courseToolInformation .= Display::page_subheader2(
  6802. get_lang('Forums')
  6803. );
  6804. $courseToolInformation .= Display::return_sortable_table(
  6805. $headerTool,
  6806. $courseForumInformationArray
  6807. );
  6808. }
  6809. $courseWorkInformationArray = getWorkCreatedByUser(
  6810. $userId,
  6811. $courseInfo['real_id'],
  6812. $sessionId
  6813. );
  6814. if (!empty($courseWorkInformationArray)) {
  6815. $csvContent[] = null;
  6816. $csvContent[] = get_lang('Works');
  6817. $csvContent[] = $headerListForCSV;
  6818. foreach ($courseWorkInformationArray as $row) {
  6819. $csvContent[] = $row;
  6820. }
  6821. $csvContent[] = null;
  6822. $courseToolInformation .= Display::page_subheader2(
  6823. get_lang('Works')
  6824. );
  6825. $courseToolInformation .= Display::return_sortable_table(
  6826. $headerTool,
  6827. $courseWorkInformationArray
  6828. );
  6829. }
  6830. $courseToolInformationTotal = null;
  6831. if (!empty($courseToolInformation)) {
  6832. $sessionTitle = null;
  6833. if (!empty($sessionId)) {
  6834. $sessionTitle = ' ('.api_get_session_name($sessionId).')';
  6835. }
  6836. $courseToolInformationTotal .= Display::page_subheader(
  6837. $courseInfo['title'].$sessionTitle
  6838. );
  6839. $courseToolInformationTotal .= $courseToolInformation;
  6840. }
  6841. return array(
  6842. 'array' => $csvContent,
  6843. 'html' => $courseToolInformationTotal
  6844. );
  6845. }
  6846. }