tracking.lib.php 321 KB

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