tracking.lib.php 322 KB

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