tracking.lib.php 322 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180
  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. // Show lp items
  254. if (is_array($list) && count($list) > 0) {
  255. foreach ($list as $my_item_id) {
  256. $extend_this = 0;
  257. $order = 'DESC';
  258. if ((!empty($extendId) && $extendId == $my_item_id) || $extend_all) {
  259. $extend_this = 1;
  260. $order = 'ASC';
  261. }
  262. // Prepare statement to go through each attempt.
  263. $viewCondition = null;
  264. if (!empty($view)) {
  265. $viewCondition = " AND v.view_count = $view ";
  266. }
  267. $sql = "SELECT
  268. iv.status as mystatus,
  269. v.view_count as mycount,
  270. iv.score as myscore,
  271. iv.total_time as mytime,
  272. i.iid as myid,
  273. i.lp_id as mylpid,
  274. iv.lp_view_id as mylpviewid,
  275. i.title as mytitle,
  276. i.max_score as mymaxscore,
  277. iv.max_score as myviewmaxscore,
  278. i.item_type as item_type,
  279. iv.view_count as iv_view_count,
  280. iv.id as iv_id,
  281. path
  282. FROM $TBL_LP_ITEM as i
  283. INNER JOIN $TBL_LP_ITEM_VIEW as iv
  284. ON (i.iid = iv.lp_item_id AND i.c_id = iv.c_id)
  285. INNER JOIN $TBL_LP_VIEW as v
  286. ON (iv.lp_view_id = v.id AND v.c_id = iv.c_id)
  287. WHERE
  288. v.c_id = $course_id AND
  289. i.iid = $my_item_id AND
  290. i.lp_id = $lp_id AND
  291. v.user_id = $user_id AND
  292. v.session_id = $session_id
  293. $viewCondition
  294. ORDER BY iv.view_count $order ";
  295. $result = Database::query($sql);
  296. $num = Database::num_rows($result);
  297. $time_for_total = 0;
  298. // Extend all
  299. if (($extend_this || $extend_all) && $num > 0) {
  300. $row = Database::fetch_array($result);
  301. $result_disabled_ext_all = false;
  302. if ($row['item_type'] === 'quiz') {
  303. // Check results_disabled in quiz table.
  304. $my_path = Database::escape_string($row['path']);
  305. $sql = "SELECT results_disabled
  306. FROM $TBL_QUIZ
  307. WHERE
  308. c_id = $course_id AND
  309. id ='".$my_path."'";
  310. $res_result_disabled = Database::query($sql);
  311. $row_result_disabled = Database::fetch_row($res_result_disabled);
  312. if (Database::num_rows($res_result_disabled) > 0 &&
  313. (int) $row_result_disabled[0] === 1
  314. ) {
  315. $result_disabled_ext_all = true;
  316. }
  317. }
  318. // If there are several attempts, and the link to extend has been clicked, show each attempt...
  319. $oddclass = 'row_even';
  320. if (($counter % 2) == 0) {
  321. $oddclass = 'row_odd';
  322. }
  323. $extend_link = '';
  324. if (!empty($inter_num)) {
  325. $extend_link = Display::url(
  326. Display::return_icon(
  327. 'visible.png',
  328. get_lang('HideAttemptView')
  329. ),
  330. api_get_self().'?action=stats&fold_id='.$my_item_id.$url_suffix
  331. );
  332. }
  333. $title = $row['mytitle'];
  334. if (empty($title)) {
  335. $title = learnpath::rl_get_resource_name($courseInfo['code'], $lp_id, $row['myid']);
  336. }
  337. if (in_array($row['item_type'], $chapterTypes)) {
  338. $title = "<h4> $title </h4>";
  339. }
  340. $lesson_status = $row['mystatus'];
  341. $title = Security::remove_XSS($title);
  342. $counter++;
  343. $action = null;
  344. if ($type === 'classic') {
  345. $action = '<td></td>';
  346. }
  347. if (in_array($row['item_type'], $chapterTypes)) {
  348. $output .= '<tr class="'.$oddclass.'">
  349. <td>'.$extend_link.'</td>
  350. <td colspan="4">
  351. '.$title.'
  352. </td>
  353. <td colspan="2">'.learnpathItem::humanize_status($lesson_status, true, $type).'</td>
  354. <td colspan="2"></td>
  355. <td colspan="2"></td>
  356. '.$action.'
  357. </tr>';
  358. continue;
  359. } else {
  360. $output .= '<tr class="'.$oddclass.'">
  361. <td>'.$extend_link.'</td>
  362. <td colspan="4">'.$title.'</td>
  363. <td colspan="2"></td>
  364. <td colspan="2"></td>
  365. <td colspan="2"></td>
  366. '.$action.'
  367. </tr>';
  368. }
  369. $attemptCount = 1;
  370. do {
  371. // Check if there are interactions below.
  372. $extend_attempt_link = '';
  373. $extend_this_attempt = 0;
  374. if ((
  375. learnpath::get_interactions_count_from_db($row['iv_id'], $course_id) > 0 ||
  376. learnpath::get_objectives_count_from_db($row['iv_id'], $course_id) > 0
  377. ) &&
  378. !$extend_all
  379. ) {
  380. if ($extendAttemptId == $row['iv_id']) {
  381. // The extend button for this attempt has been clicked.
  382. $extend_this_attempt = 1;
  383. $extend_attempt_link = Display::url(
  384. Display::return_icon('visible.png', get_lang('HideAttemptView')),
  385. api_get_self().'?action=stats&extend_id='.$my_item_id.'&fold_attempt_id='.$row['iv_id'].$url_suffix
  386. );
  387. if (api_is_allowed_to_edit()) {
  388. $extend_attempt_link .= '&nbsp;'.
  389. Display::url(
  390. Display::return_icon('pdf.png', get_lang('ExportToPdf')),
  391. api_get_self(
  392. ).'?action=export_stats&extend_id='.$my_item_id.'&extend_attempt_id='.$row['iv_id'].$url_suffix
  393. );
  394. }
  395. } else { // Same case if fold_attempt_id is set, so not implemented explicitly.
  396. // The extend button for this attempt has not been clicked.
  397. $extend_attempt_link = Display::url(
  398. Display::return_icon('invisible.png', get_lang('ExtendAttemptView')),
  399. api_get_self().'?action=stats&extend_id='.$my_item_id.'&extend_attempt_id='.$row['iv_id'].$url_suffix
  400. );
  401. if (api_is_allowed_to_edit()) {
  402. $extend_attempt_link .= '&nbsp;'.
  403. Display::url(
  404. Display::return_icon('pdf.png', get_lang('ExportToPdf')),
  405. api_get_self(
  406. ).'?action=export_stats&extend_id='.$my_item_id.'&extend_attempt_id='.$row['iv_id'].$url_suffix
  407. );
  408. }
  409. }
  410. }
  411. $oddclass = 'row_even';
  412. if (($counter % 2) == 0) {
  413. $oddclass = 'row_odd';
  414. }
  415. $lesson_status = $row['mystatus'];
  416. $score = $row['myscore'];
  417. $time_for_total = $row['mytime'];
  418. if (self::minimunTimeAvailable($session_id, $course_id)) {
  419. $timeCourse = self::getCalculateTime($user_id, $course_id, $session_id);
  420. Session::write('trackTimeCourse', $timeCourse);
  421. $lp_time = $timeCourse[TOOL_LEARNPATH];
  422. $lpTime = (int) $lp_time[$lp_id];
  423. $time_for_total = $lpTime;
  424. }
  425. $time = learnpathItem::getScormTimeFromParameter('js', $row['mytime']);
  426. if ($score == 0) {
  427. $maxscore = $row['mymaxscore'];
  428. } else {
  429. if ($row['item_type'] === 'sco') {
  430. if (!empty($row['myviewmaxscore']) && $row['myviewmaxscore'] > 0) {
  431. $maxscore = $row['myviewmaxscore'];
  432. } elseif ($row['myviewmaxscore'] === '') {
  433. $maxscore = 0;
  434. } else {
  435. $maxscore = $row['mymaxscore'];
  436. }
  437. } else {
  438. $maxscore = $row['mymaxscore'];
  439. }
  440. }
  441. // Remove "NaN" if any (@todo: locate the source of these NaN)
  442. $time = str_replace('NaN', '00'.$h.'00\'00"', $time);
  443. if ($row['item_type'] != 'dir') {
  444. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  445. $view_score = Display::return_icon(
  446. 'invisible.png',
  447. get_lang('ResultsHiddenByExerciseSetting')
  448. );
  449. } else {
  450. switch ($row['item_type']) {
  451. case 'sco':
  452. if ($maxscore == 0) {
  453. $view_score = $score;
  454. } else {
  455. $view_score = ExerciseLib::show_score(
  456. $score,
  457. $maxscore,
  458. false
  459. );
  460. }
  461. break;
  462. case 'document':
  463. $view_score = ($score == 0 ? '/' : ExerciseLib::show_score($score, $maxscore, false));
  464. break;
  465. default:
  466. $view_score = ExerciseLib::show_score(
  467. $score,
  468. $maxscore,
  469. false
  470. );
  471. break;
  472. }
  473. }
  474. $action = null;
  475. if ($type == 'classic') {
  476. $action = '<td></td>';
  477. }
  478. $timeRow = '<td class="lp_time" colspan="2">'.$time.'</td>';
  479. if ($hideTime) {
  480. $timeRow = '';
  481. }
  482. $output .= '<tr class="'.$oddclass.'">
  483. <td></td>
  484. <td style="width:70px;float:left;">'.$extend_attempt_link.'</td>
  485. <td colspan="3">'.get_lang('Attempt').' '.$attemptCount.'</td>
  486. <td colspan="2">'.learnpathItem::humanize_status($lesson_status, true, $type).'</td>
  487. <td colspan="2">'.$view_score.'</td>
  488. '.$timeRow.'
  489. '.$action.'
  490. </tr>';
  491. $attemptCount++;
  492. if (!empty($export_csv)) {
  493. $temp = [];
  494. $temp[] = $title = Security::remove_XSS($title);
  495. $temp[] = Security::remove_XSS(
  496. learnpathItem::humanize_status($lesson_status, false, $type)
  497. );
  498. if ($row['item_type'] === 'quiz') {
  499. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  500. $temp[] = '/';
  501. } else {
  502. $temp[] = ($score == 0 ? '0/'.$maxscore : ($maxscore == 0 ? $score : $score.'/'.float_format($maxscore, 1)));
  503. }
  504. } else {
  505. $temp[] = ($score == 0 ? '/' : ($maxscore == 0 ? $score : $score.'/'.float_format($maxscore, 1)));
  506. }
  507. if ($hideTime === false) {
  508. $temp[] = $time;
  509. }
  510. $csv_content[] = $temp;
  511. }
  512. }
  513. $counter++;
  514. $action = null;
  515. if ($type == 'classic') {
  516. $action = '<td></td>';
  517. }
  518. if ($extend_this_attempt || $extend_all) {
  519. $list1 = learnpath::get_iv_interactions_array($row['iv_id'], $course_id);
  520. foreach ($list1 as $id => $interaction) {
  521. $oddclass = 'row_even';
  522. if (($counter % 2) == 0) {
  523. $oddclass = 'row_odd';
  524. }
  525. $timeRow = '<td class="lp_time">'.$interaction['time'].'</td>';
  526. if ($hideTime) {
  527. $timeRow = '';
  528. }
  529. $output .= '<tr class="'.$oddclass.'">
  530. <td></td>
  531. <td></td>
  532. <td></td>
  533. <td>'.$interaction['order_id'].'</td>
  534. <td>'.$interaction['id'].'</td>';
  535. $output .= '
  536. <td colspan="2">'.$interaction['type'].'</td>
  537. <td>'.$interaction['student_response_formatted'].'</td>
  538. <td>'.$interaction['result'].'</td>
  539. <td>'.$interaction['latency'].'</td>
  540. '.$timeRow.'
  541. '.$action.'
  542. </tr>';
  543. $counter++;
  544. }
  545. $list2 = learnpath::get_iv_objectives_array($row['iv_id'], $course_id);
  546. foreach ($list2 as $id => $interaction) {
  547. if (($counter % 2) == 0) {
  548. $oddclass = 'row_odd';
  549. } else {
  550. $oddclass = 'row_even';
  551. }
  552. $output .= '<tr class="'.$oddclass.'">
  553. <td></td>
  554. <td></td>
  555. <td></td>
  556. <td>'.$interaction['order_id'].'</td>
  557. <td colspan="2">'.$interaction['objective_id'].'</td>
  558. <td colspan="2">'.$interaction['status'].'</td>
  559. <td>'.$interaction['score_raw'].'</td>
  560. <td>'.$interaction['score_max'].'</td>
  561. <td>'.$interaction['score_min'].'</td>
  562. '.$action.'
  563. </tr>';
  564. $counter++;
  565. }
  566. }
  567. } while ($row = Database::fetch_array($result));
  568. } elseif ($num > 0) {
  569. // Not extended.
  570. $row = Database::fetch_array($result, 'ASSOC');
  571. $my_id = $row['myid'];
  572. $my_lp_id = $row['mylpid'];
  573. $my_lp_view_id = $row['mylpviewid'];
  574. $my_path = $row['path'];
  575. $result_disabled_ext_all = false;
  576. if ($row['item_type'] == 'quiz') {
  577. // Check results_disabled in quiz table.
  578. $my_path = Database::escape_string($my_path);
  579. $sql = "SELECT results_disabled
  580. FROM $TBL_QUIZ
  581. WHERE c_id = $course_id AND id ='".$my_path."'";
  582. $res_result_disabled = Database::query($sql);
  583. $row_result_disabled = Database::fetch_row($res_result_disabled);
  584. if (Database::num_rows($res_result_disabled) > 0 &&
  585. (int) $row_result_disabled[0] === 1
  586. ) {
  587. $result_disabled_ext_all = true;
  588. }
  589. }
  590. // Check if there are interactions below
  591. $extend_this_attempt = 0;
  592. $inter_num = learnpath::get_interactions_count_from_db($row['iv_id'], $course_id);
  593. $objec_num = learnpath::get_objectives_count_from_db($row['iv_id'], $course_id);
  594. $extend_attempt_link = '';
  595. if ($inter_num > 0 || $objec_num > 0) {
  596. if (!empty($extendAttemptId) && $extendAttemptId == $row['iv_id']) {
  597. // The extend button for this attempt has been clicked.
  598. $extend_this_attempt = 1;
  599. $extend_attempt_link = Display::url(
  600. Display::return_icon('visible.png', get_lang('HideAttemptView')),
  601. api_get_self().'?action=stats&extend_id='.$my_item_id.'&fold_attempt_id='.$row['iv_id'].$url_suffix
  602. );
  603. } else {
  604. // Same case if fold_attempt_id is set, so not implemented explicitly.
  605. // The extend button for this attempt has not been clicked.
  606. $extend_attempt_link = Display::url(
  607. Display::return_icon('invisible.png', get_lang('ExtendAttemptView')),
  608. api_get_self().'?action=stats&extend_id='.$my_item_id.'&extend_attempt_id='.$row['iv_id'].$url_suffix
  609. );
  610. }
  611. }
  612. $oddclass = 'row_even';
  613. if (($counter % 2) == 0) {
  614. $oddclass = 'row_odd';
  615. }
  616. $extend_link = '';
  617. if ($inter_num > 1) {
  618. $extend_link = Display::url(
  619. Display::return_icon('invisible.png', get_lang('ExtendAttemptView')),
  620. api_get_self().'?action=stats&extend_id='.$my_item_id.'&extend_attempt_id='.$row['iv_id'].$url_suffix
  621. );
  622. }
  623. $lesson_status = $row['mystatus'];
  624. $score = $row['myscore'];
  625. $subtotal_time = $row['mytime'];
  626. while ($tmp_row = Database::fetch_array($result)) {
  627. $subtotal_time += $tmp_row['mytime'];
  628. }
  629. $title = $row['mytitle'];
  630. // Selecting the exe_id from stats attempts tables in order to look the max score value.
  631. $sql = 'SELECT * FROM '.$tbl_stats_exercices.'
  632. WHERE
  633. exe_exo_id="'.$row['path'].'" AND
  634. exe_user_id="'.$user_id.'" AND
  635. orig_lp_id = "'.$lp_id.'" AND
  636. orig_lp_item_id = "'.$row['myid'].'" AND
  637. c_id = '.$course_id.' AND
  638. status <> "incomplete" AND
  639. session_id = '.$session_id.'
  640. ORDER BY exe_date DESC
  641. LIMIT 1';
  642. $resultLastAttempt = Database::query($sql);
  643. $num = Database::num_rows($resultLastAttempt);
  644. $id_last_attempt = null;
  645. if ($num > 0) {
  646. while ($rowLA = Database::fetch_array($resultLastAttempt)) {
  647. $id_last_attempt = $rowLA['exe_id'];
  648. }
  649. }
  650. switch ($row['item_type']) {
  651. case 'sco':
  652. if (!empty($row['myviewmaxscore']) && $row['myviewmaxscore'] > 0) {
  653. $maxscore = $row['myviewmaxscore'];
  654. } elseif ($row['myviewmaxscore'] === '') {
  655. $maxscore = 0;
  656. } else {
  657. $maxscore = $row['mymaxscore'];
  658. }
  659. break;
  660. case 'quiz':
  661. // Get score and total time from last attempt of a exercise en lp.
  662. $sql = "SELECT iid, score
  663. FROM $TBL_LP_ITEM_VIEW
  664. WHERE
  665. c_id = $course_id AND
  666. lp_item_id = '".(int) $my_id."' AND
  667. lp_view_id = '".(int) $my_lp_view_id."'
  668. ORDER BY view_count DESC
  669. LIMIT 1";
  670. $res_score = Database::query($sql);
  671. $row_score = Database::fetch_array($res_score);
  672. $sql = "SELECT SUM(total_time) as total_time
  673. FROM $TBL_LP_ITEM_VIEW
  674. WHERE
  675. c_id = $course_id AND
  676. lp_item_id = '".(int) $my_id."' AND
  677. lp_view_id = '".(int) $my_lp_view_id."'";
  678. $res_time = Database::query($sql);
  679. $row_time = Database::fetch_array($res_time);
  680. $score = 0;
  681. $subtotal_time = 0;
  682. if (Database::num_rows($res_score) > 0 &&
  683. Database::num_rows($res_time) > 0
  684. ) {
  685. $score = (float) $row_score['score'];
  686. $subtotal_time = (int) $row_time['total_time'];
  687. }
  688. // Selecting the max score from an attempt.
  689. $sql = "SELECT SUM(t.ponderation) as maxscore
  690. FROM (
  691. SELECT DISTINCT
  692. question_id, marks, ponderation
  693. FROM $tbl_stats_attempts as at
  694. INNER JOIN $tbl_quiz_questions as q
  695. ON (q.id = at.question_id AND q.c_id = $course_id)
  696. WHERE exe_id ='$id_last_attempt'
  697. ) as t";
  698. $result = Database::query($sql);
  699. $row_max_score = Database::fetch_array($result);
  700. $maxscore = $row_max_score['maxscore'];
  701. // Get duration time from track_e_exercises.exe_duration instead of lp_view_item.total_time
  702. $sql = 'SELECT SUM(exe_duration) exe_duration
  703. FROM '.$tbl_stats_exercices.'
  704. WHERE
  705. exe_exo_id="'.$row['path'].'" AND
  706. exe_user_id="'.$user_id.'" AND
  707. orig_lp_id = "'.$lp_id.'" AND
  708. orig_lp_item_id = "'.$row['myid'].'" AND
  709. c_id = '.$course_id.' AND
  710. status <> "incomplete" AND
  711. session_id = '.$session_id.'
  712. ORDER BY exe_date DESC ';
  713. $sumScoreResult = Database::query($sql);
  714. $durationRow = Database::fetch_array($sumScoreResult, 'ASSOC');
  715. if (!empty($durationRow['exe_duration'])) {
  716. $exeDuration = $durationRow['exe_duration'];
  717. if ($exeDuration != $subtotal_time &&
  718. !empty($row_score['iid']) &&
  719. !empty($exeDuration)
  720. ) {
  721. $subtotal_time = $exeDuration;
  722. // Update c_lp_item_view.total_time
  723. $sqlUpdate = "UPDATE $TBL_LP_ITEM_VIEW SET total_time = '$exeDuration'
  724. WHERE iid = ".$row_score['iid'];
  725. Database::query($sqlUpdate);
  726. }
  727. }
  728. break;
  729. default:
  730. $maxscore = $row['mymaxscore'];
  731. break;
  732. }
  733. $time_for_total = $subtotal_time;
  734. $time = learnpathItem::getScormTimeFromParameter('js', $subtotal_time);
  735. if (empty($title)) {
  736. $title = learnpath::rl_get_resource_name(
  737. $courseInfo['code'],
  738. $lp_id,
  739. $row['myid']
  740. );
  741. }
  742. $action = null;
  743. if ($type == 'classic') {
  744. $action = '<td></td>';
  745. }
  746. if (in_array($row['item_type'], $chapterTypes)) {
  747. $title = Security::remove_XSS($title);
  748. $output .= '<tr class="'.$oddclass.'">
  749. <td>'.$extend_link.'</td>
  750. <td colspan="4">
  751. <h4>'.$title.'</h4>
  752. </td>
  753. <td colspan="2">'.learnpathitem::humanize_status($lesson_status).'</td>
  754. <td colspan="2"></td>
  755. <td colspan="2"></td>
  756. '.$action.'
  757. </tr>';
  758. } else {
  759. $correct_test_link = '-';
  760. $showRowspan = false;
  761. if ($row['item_type'] == 'quiz') {
  762. $my_url_suffix = '&course='.$courseCode.'&student_id='.$user_id.'&lp_id='.intval($row['mylpid']).'&origin='.$origin;
  763. $sql = 'SELECT * FROM '.$tbl_stats_exercices.'
  764. WHERE
  765. exe_exo_id="'.$row['path'].'" AND
  766. exe_user_id="'.$user_id.'" AND
  767. orig_lp_id = "'.$lp_id.'" AND
  768. orig_lp_item_id = "'.$row['myid'].'" AND
  769. c_id = '.$course_id.' AND
  770. status <> "incomplete" AND
  771. session_id = '.$session_id.'
  772. ORDER BY exe_date DESC ';
  773. $resultLastAttempt = Database::query($sql);
  774. $num = Database::num_rows($resultLastAttempt);
  775. $showRowspan = false;
  776. if ($num > 0) {
  777. $linkId = 'link_'.$my_id;
  778. if ($extendedAttempt == 1 &&
  779. $lp_id == $my_lp_id &&
  780. $lp_item_id == $my_id
  781. ) {
  782. $showRowspan = true;
  783. $correct_test_link = Display::url(
  784. Display::return_icon(
  785. 'view_less_stats.gif',
  786. get_lang('HideAllAttempts')
  787. ),
  788. api_get_self().'?action=stats'.$my_url_suffix.'&session_id='.$session_id.'&lp_item_id='.$my_id.'#'.$linkId,
  789. ['id' => $linkId]
  790. );
  791. } else {
  792. $correct_test_link = Display::url(
  793. Display::return_icon(
  794. 'view_more_stats.gif',
  795. get_lang(
  796. 'ShowAllAttemptsByExercise'
  797. )
  798. ),
  799. api_get_self().'?action=stats&extend_attempt=1'.$my_url_suffix.'&session_id='.$session_id.'&lp_item_id='.$my_id.'#'.$linkId,
  800. ['id' => $linkId]
  801. );
  802. }
  803. }
  804. }
  805. $title = Security::remove_XSS($title);
  806. $action = null;
  807. if ($type == 'classic') {
  808. $action = '<td '.($showRowspan ? 'rowspan="2"' : '').'>'.$correct_test_link.'</td>';
  809. }
  810. if ($lp_id == $my_lp_id && false) {
  811. $output .= '<tr class ='.$oddclass.'>
  812. <td>'.$extend_link.'</td>
  813. <td colspan="4">'.$title.'</td>
  814. <td colspan="2">&nbsp;</td>
  815. <td colspan="2">&nbsp;</td>
  816. <td colspan="2">&nbsp;</td>
  817. '.$action.'
  818. </tr>';
  819. $output .= '</tr>';
  820. } else {
  821. if ($lp_id == $my_lp_id && $lp_item_id == $my_id) {
  822. $output .= "<tr class='$oddclass'>";
  823. } else {
  824. $output .= "<tr class='$oddclass'>";
  825. }
  826. $scoreItem = null;
  827. if ($row['item_type'] == 'quiz') {
  828. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  829. $scoreItem .= Display::return_icon(
  830. 'invisible.gif',
  831. get_lang('ResultsHiddenByExerciseSetting')
  832. );
  833. } else {
  834. $scoreItem .= ExerciseLib::show_score($score, $maxscore, false);
  835. }
  836. } else {
  837. $scoreItem .= $score == 0 ? '/' : ($maxscore == 0 ? $score : $score.'/'.$maxscore);
  838. }
  839. $timeRow = '<td class="lp_time" colspan="2">'.$time.'</td>';
  840. if ($hideTime) {
  841. $timeRow = '';
  842. }
  843. $output .= '
  844. <td>'.$extend_link.'</td>
  845. <td colspan="4">'.$title.'</td>
  846. <td colspan="2">'.learnpathitem::humanize_status($lesson_status).'</td>
  847. <td colspan="2">'.$scoreItem.'</td>
  848. '.$timeRow.'
  849. '.$action.'
  850. ';
  851. $output .= '</tr>';
  852. }
  853. if (!empty($export_csv)) {
  854. $temp = [];
  855. $temp[] = api_html_entity_decode($title, ENT_QUOTES);
  856. $temp[] = api_html_entity_decode($lesson_status, ENT_QUOTES);
  857. if ($row['item_type'] == 'quiz') {
  858. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  859. $temp[] = '/';
  860. } else {
  861. $temp[] = ($score == 0 ? '0/'.$maxscore : ($maxscore == 0 ? $score : $score.'/'.float_format($maxscore, 1)));
  862. }
  863. } else {
  864. $temp[] = ($score == 0 ? '/' : ($maxscore == 0 ? $score : $score.'/'.float_format($maxscore, 1)));
  865. }
  866. if ($hideTime === false) {
  867. $temp[] = $time;
  868. }
  869. $csv_content[] = $temp;
  870. }
  871. }
  872. $counter++;
  873. $action = null;
  874. if ($type == 'classic') {
  875. $action = '<td></td>';
  876. }
  877. if ($extend_this_attempt || $extend_all) {
  878. $list1 = learnpath::get_iv_interactions_array($row['iv_id'], $course_id);
  879. foreach ($list1 as $id => $interaction) {
  880. if (($counter % 2) == 0) {
  881. $oddclass = 'row_odd';
  882. } else {
  883. $oddclass = 'row_even';
  884. }
  885. $timeRow = '<td class="lp_time">'.$interaction['time'].'</td>';
  886. if ($hideTime) {
  887. $timeRow = '';
  888. }
  889. $output .= '<tr class="'.$oddclass.'">
  890. <td></td>
  891. <td></td>
  892. <td></td>
  893. <td>'.$interaction['order_id'].'</td>
  894. <td>'.$interaction['id'].'</td>
  895. <td colspan="2">'.$interaction['type'].'</td>
  896. <td>'.urldecode($interaction['student_response']).'</td>
  897. <td>'.$interaction['result'].'</td>
  898. <td>'.$interaction['latency'].'</td>
  899. '.$timeRow.'
  900. '.$action.'
  901. </tr>';
  902. $counter++;
  903. }
  904. $list2 = learnpath::get_iv_objectives_array($row['iv_id'], $course_id);
  905. foreach ($list2 as $id => $interaction) {
  906. if (($counter % 2) == 0) {
  907. $oddclass = 'row_odd';
  908. } else {
  909. $oddclass = 'row_even';
  910. }
  911. $output .= '<tr class="'.$oddclass.'">
  912. <td></td>
  913. <td></td>
  914. <td></td>
  915. <td>'.$interaction['order_id'].'</td>
  916. <td colspan="2">'.$interaction['objective_id'].'</td>
  917. <td colspan="2">'.$interaction['status'].'</td>
  918. <td>'.$interaction['score_raw'].'</td>
  919. <td>'.$interaction['score_max'].'</td>
  920. <td>'.$interaction['score_min'].'</td>
  921. '.$action.'
  922. </tr>';
  923. $counter++;
  924. }
  925. }
  926. // Attempts listing by exercise.
  927. if ($lp_id == $my_lp_id && $lp_item_id == $my_id && $extendedAttempt) {
  928. // Get attempts of a exercise.
  929. if (!empty($lp_id) &&
  930. !empty($lp_item_id) &&
  931. $row['item_type'] === 'quiz'
  932. ) {
  933. $sql = "SELECT path FROM $TBL_LP_ITEM
  934. WHERE
  935. c_id = $course_id AND
  936. iid = '$lp_item_id' AND
  937. lp_id = '$lp_id'";
  938. $res_path = Database::query($sql);
  939. $row_path = Database::fetch_array($res_path);
  940. if (Database::num_rows($res_path) > 0) {
  941. $sql = 'SELECT * FROM '.$tbl_stats_exercices.'
  942. WHERE
  943. exe_exo_id="'.(int) $row_path['path'].'" AND
  944. status <> "incomplete" AND
  945. exe_user_id="'.$user_id.'" AND
  946. orig_lp_id = "'.(int) $lp_id.'" AND
  947. orig_lp_item_id = "'.(int) $lp_item_id.'" AND
  948. c_id = '.$course_id.' AND
  949. session_id = '.$session_id.'
  950. ORDER BY exe_date';
  951. $res_attempts = Database::query($sql);
  952. $num_attempts = Database::num_rows($res_attempts);
  953. if ($num_attempts > 0) {
  954. $n = 1;
  955. while ($row_attempts = Database::fetch_array($res_attempts)) {
  956. $my_score = $row_attempts['exe_result'];
  957. $my_maxscore = $row_attempts['exe_weighting'];
  958. $my_exe_id = $row_attempts['exe_id'];
  959. $mktime_start_date = api_strtotime($row_attempts['start_date'], 'UTC');
  960. $mktime_exe_date = api_strtotime($row_attempts['exe_date'], 'UTC');
  961. $time_attemp = ' - ';
  962. if ($mktime_start_date && $mktime_exe_date) {
  963. $time_attemp = api_format_time($row_attempts['exe_duration'], 'js');
  964. }
  965. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  966. $view_score = Display::return_icon(
  967. 'invisible.png',
  968. get_lang(
  969. 'ResultsHiddenByExerciseSetting'
  970. )
  971. );
  972. } else {
  973. // Show only float when need it
  974. if ($my_score == 0) {
  975. $view_score = ExerciseLib::show_score(
  976. 0,
  977. $my_maxscore,
  978. false
  979. );
  980. } else {
  981. if ($my_maxscore == 0) {
  982. $view_score = $my_score;
  983. } else {
  984. $view_score = ExerciseLib::show_score(
  985. $my_score,
  986. $my_maxscore,
  987. false
  988. );
  989. }
  990. }
  991. }
  992. $my_lesson_status = $row_attempts['status'];
  993. if ($my_lesson_status == '') {
  994. $my_lesson_status = learnpathitem::humanize_status('completed');
  995. } elseif ($my_lesson_status == 'incomplete') {
  996. $my_lesson_status = learnpathitem::humanize_status('incomplete');
  997. }
  998. $timeRow = '<td class="lp_time" colspan="2">'.$time_attemp.'</td>';
  999. if ($hideTime) {
  1000. $timeRow = '';
  1001. }
  1002. $output .= '<tr class="'.$oddclass.'" >
  1003. <td></td>
  1004. <td>'.$extend_attempt_link.'</td>
  1005. <td colspan="3">'.get_lang('Attempt').' '.$n.'</td>
  1006. <td colspan="2">'.$my_lesson_status.'</td>
  1007. <td colspan="2">'.$view_score.'</td>
  1008. '.$timeRow;
  1009. if ($action == 'classic') {
  1010. if ($origin != 'tracking') {
  1011. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  1012. $output .= '<td>
  1013. <img src="'.Display::returnIconPath('quiz_na.gif').'" alt="'.get_lang('ShowAttempt').'" title="'.get_lang('ShowAttempt').'">
  1014. </td>';
  1015. } else {
  1016. $output .= '<td>
  1017. <a href="../exercise/exercise_show.php?origin='.$origin.'&id='.$my_exe_id.'&cidReq='.$courseCode.'" target="_parent">
  1018. <img src="'.Display::returnIconPath('quiz.png').'" alt="'.get_lang('ShowAttempt').'" title="'.get_lang('ShowAttempt').'">
  1019. </a></td>';
  1020. }
  1021. } else {
  1022. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  1023. $output .= '<td>
  1024. <img src="'.Display::returnIconPath('quiz_na.gif').'" alt="'.get_lang('ShowAndQualifyAttempt').'" title="'.get_lang('ShowAndQualifyAttempt').'"></td>';
  1025. } else {
  1026. $output .= '<td>
  1027. <a href="../exercise/exercise_show.php?cidReq='.$courseCode.'&origin=correct_exercise_in_lp&id='.$my_exe_id.'" target="_parent">
  1028. <img src="'.Display::returnIconPath('quiz.gif').'" alt="'.get_lang('ShowAndQualifyAttempt').'" title="'.get_lang('ShowAndQualifyAttempt').'"></a></td>';
  1029. }
  1030. }
  1031. }
  1032. $output .= '</tr>';
  1033. $n++;
  1034. }
  1035. }
  1036. $output .= '<tr><td colspan="12">&nbsp;</td></tr>';
  1037. }
  1038. }
  1039. }
  1040. }
  1041. $total_time += $time_for_total;
  1042. // QUIZZ IN LP
  1043. $a_my_id = [];
  1044. if (!empty($my_lp_id)) {
  1045. $a_my_id[] = $my_lp_id;
  1046. }
  1047. }
  1048. }
  1049. // NOT Extend all "left green cross"
  1050. if (!empty($a_my_id)) {
  1051. if ($extendedAttempt) {
  1052. // "Right green cross" extended
  1053. $total_score = self::get_avg_student_score(
  1054. $user_id,
  1055. $course_id,
  1056. $a_my_id,
  1057. $session_id,
  1058. false,
  1059. false
  1060. );
  1061. } else {
  1062. // "Left green cross" extended
  1063. $total_score = self::get_avg_student_score(
  1064. $user_id,
  1065. $course_id,
  1066. $a_my_id,
  1067. $session_id,
  1068. false,
  1069. true
  1070. );
  1071. }
  1072. } else {
  1073. // Extend all "left green cross"
  1074. $total_score = self::get_avg_student_score(
  1075. $user_id,
  1076. $course_id,
  1077. [$lp_id],
  1078. $session_id,
  1079. false,
  1080. false
  1081. );
  1082. }
  1083. $total_time = learnpathItem::getScormTimeFromParameter('js', $total_time);
  1084. $total_time = str_replace('NaN', '00'.$h.'00\'00"', $total_time);
  1085. if (!$is_allowed_to_edit && $result_disabled_ext_all) {
  1086. $final_score = Display::return_icon('invisible.png', get_lang('ResultsHiddenByExerciseSetting'));
  1087. $finalScoreToCsv = get_lang('ResultsHiddenByExerciseSetting');
  1088. } else {
  1089. if (is_numeric($total_score)) {
  1090. $final_score = $total_score.'%';
  1091. } else {
  1092. $final_score = $total_score;
  1093. }
  1094. $finalScoreToCsv = $final_score;
  1095. }
  1096. $progress = learnpath::getProgress($lp_id, $user_id, $course_id, $session_id);
  1097. $oddclass = 'row_even';
  1098. if (($counter % 2) == 0) {
  1099. $oddclass = 'row_odd';
  1100. }
  1101. $action = null;
  1102. if ($type == 'classic') {
  1103. $action = '<td></td>';
  1104. }
  1105. $timeTotal = '<td class="lp_time" colspan="2">'.$total_time.'</div>';
  1106. if ($hideTime) {
  1107. $timeTotal = '';
  1108. }
  1109. $output .= '<tr class="'.$oddclass.'">
  1110. <td></td>
  1111. <td colspan="4">
  1112. <i>'.get_lang('AccomplishedStepsTotal').'</i>
  1113. </td>
  1114. <td colspan="2">'.$progress.'%</td>
  1115. <td colspan="2">'.$final_score.'</td>
  1116. '.$timeTotal.'
  1117. '.$action.'
  1118. </tr>';
  1119. $output .= '
  1120. </tbody>
  1121. </table>
  1122. </div>
  1123. ';
  1124. if (!empty($export_csv)) {
  1125. $temp = [
  1126. '',
  1127. '',
  1128. '',
  1129. '',
  1130. ];
  1131. $csv_content[] = $temp;
  1132. $temp = [
  1133. get_lang('AccomplishedStepsTotal'),
  1134. '',
  1135. $finalScoreToCsv,
  1136. ];
  1137. if ($hideTime === false) {
  1138. $temp[] = $total_time;
  1139. }
  1140. $csv_content[] = $temp;
  1141. ob_end_clean();
  1142. Export::arrayToCsv($csv_content, 'reporting_learning_path_details');
  1143. exit;
  1144. }
  1145. return $output;
  1146. }
  1147. /**
  1148. * @param int $userId
  1149. * @param bool $getCount
  1150. *
  1151. * @return array
  1152. */
  1153. public static function getStats($userId, $getCount = false)
  1154. {
  1155. $courses = [];
  1156. $assignedCourses = [];
  1157. $drhCount = 0;
  1158. $teachersCount = 0;
  1159. $studentsCount = 0;
  1160. $studentBossCount = 0;
  1161. $courseCount = 0;
  1162. $sessionCount = 0;
  1163. $assignedCourseCount = 0;
  1164. if (api_is_drh() && api_drh_can_access_all_session_content()) {
  1165. $studentList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1166. 'drh_all',
  1167. $userId,
  1168. false,
  1169. null,
  1170. null,
  1171. null,
  1172. null,
  1173. null,
  1174. null,
  1175. null,
  1176. [],
  1177. [],
  1178. STUDENT
  1179. );
  1180. $students = [];
  1181. if (is_array($studentList)) {
  1182. foreach ($studentList as $studentData) {
  1183. $students[] = $studentData['user_id'];
  1184. }
  1185. }
  1186. $studentBossesList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1187. 'drh_all',
  1188. $userId,
  1189. $getCount,
  1190. null,
  1191. null,
  1192. null,
  1193. null,
  1194. null,
  1195. null,
  1196. null,
  1197. [],
  1198. [],
  1199. STUDENT_BOSS
  1200. );
  1201. if ($getCount) {
  1202. $studentBossCount = $studentBossesList;
  1203. } else {
  1204. $studentBosses = [];
  1205. if (is_array($studentBossesList)) {
  1206. foreach ($studentBossesList as $studentBossData) {
  1207. $studentBosses[] = $studentBossData['user_id'];
  1208. }
  1209. }
  1210. }
  1211. $teacherList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1212. 'drh_all',
  1213. $userId,
  1214. $getCount,
  1215. null,
  1216. null,
  1217. null,
  1218. null,
  1219. null,
  1220. null,
  1221. null,
  1222. [],
  1223. [],
  1224. COURSEMANAGER
  1225. );
  1226. if ($getCount) {
  1227. $teachersCount = $teacherList;
  1228. } else {
  1229. $teachers = [];
  1230. foreach ($teacherList as $teacherData) {
  1231. $teachers[] = $teacherData['user_id'];
  1232. }
  1233. }
  1234. $humanResources = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  1235. 'drh_all',
  1236. $userId,
  1237. $getCount,
  1238. null,
  1239. null,
  1240. null,
  1241. null,
  1242. null,
  1243. null,
  1244. null,
  1245. [],
  1246. [],
  1247. DRH
  1248. );
  1249. if ($getCount) {
  1250. $drhCount = $humanResources;
  1251. } else {
  1252. $humanResourcesList = [];
  1253. if (is_array($humanResources)) {
  1254. foreach ($humanResources as $item) {
  1255. $humanResourcesList[] = $item['user_id'];
  1256. }
  1257. }
  1258. }
  1259. $platformCourses = SessionManager::getAllCoursesFollowedByUser(
  1260. $userId,
  1261. null,
  1262. null,
  1263. null,
  1264. null,
  1265. null,
  1266. $getCount
  1267. );
  1268. if ($getCount) {
  1269. $courseCount = $platformCourses;
  1270. } else {
  1271. foreach ($platformCourses as $course) {
  1272. $courses[$course['code']] = $course['code'];
  1273. }
  1274. }
  1275. $sessions = SessionManager::get_sessions_followed_by_drh(
  1276. $userId,
  1277. null,
  1278. null,
  1279. false
  1280. );
  1281. } else {
  1282. $studentList = UserManager::getUsersFollowedByUser(
  1283. $userId,
  1284. STUDENT,
  1285. false,
  1286. false,
  1287. false,
  1288. null,
  1289. null,
  1290. null,
  1291. null,
  1292. null,
  1293. null,
  1294. COURSEMANAGER
  1295. );
  1296. $students = [];
  1297. if (is_array($studentList)) {
  1298. foreach ($studentList as $studentData) {
  1299. $students[] = $studentData['user_id'];
  1300. }
  1301. }
  1302. $studentBossesList = UserManager::getUsersFollowedByUser(
  1303. $userId,
  1304. STUDENT_BOSS,
  1305. false,
  1306. false,
  1307. $getCount,
  1308. null,
  1309. null,
  1310. null,
  1311. null,
  1312. null,
  1313. null,
  1314. COURSEMANAGER
  1315. );
  1316. if ($getCount) {
  1317. $studentBossCount = $studentBossesList;
  1318. } else {
  1319. $studentBosses = [];
  1320. if (is_array($studentBossesList)) {
  1321. foreach ($studentBossesList as $studentBossData) {
  1322. $studentBosses[] = $studentBossData['user_id'];
  1323. }
  1324. }
  1325. }
  1326. $teacherList = UserManager::getUsersFollowedByUser(
  1327. $userId,
  1328. COURSEMANAGER,
  1329. false,
  1330. false,
  1331. $getCount,
  1332. null,
  1333. null,
  1334. null,
  1335. null,
  1336. null,
  1337. null,
  1338. COURSEMANAGER
  1339. );
  1340. if ($getCount) {
  1341. $teachersCount = $teacherList;
  1342. } else {
  1343. $teachers = [];
  1344. foreach ($teacherList as $teacherData) {
  1345. $teachers[] = $teacherData['user_id'];
  1346. }
  1347. }
  1348. $humanResources = UserManager::getUsersFollowedByUser(
  1349. $userId,
  1350. DRH,
  1351. false,
  1352. false,
  1353. $getCount,
  1354. null,
  1355. null,
  1356. null,
  1357. null,
  1358. null,
  1359. null,
  1360. COURSEMANAGER
  1361. );
  1362. if ($getCount) {
  1363. $drhCount = $humanResources;
  1364. } else {
  1365. $humanResourcesList = [];
  1366. foreach ($humanResources as $item) {
  1367. $humanResourcesList[] = $item['user_id'];
  1368. }
  1369. }
  1370. $platformCourses = CourseManager::getCoursesFollowedByUser(
  1371. $userId,
  1372. COURSEMANAGER,
  1373. null,
  1374. null,
  1375. null,
  1376. null,
  1377. $getCount,
  1378. null,
  1379. null,
  1380. true
  1381. );
  1382. if ($getCount) {
  1383. $assignedCourseCount = $platformCourses;
  1384. } else {
  1385. foreach ($platformCourses as $course) {
  1386. $assignedCourses[$course['code']] = $course['code'];
  1387. }
  1388. }
  1389. $platformCourses = CourseManager::getCoursesFollowedByUser(
  1390. $userId,
  1391. COURSEMANAGER,
  1392. null,
  1393. null,
  1394. null,
  1395. null,
  1396. $getCount
  1397. );
  1398. if ($getCount) {
  1399. $courseCount = $platformCourses;
  1400. } else {
  1401. foreach ($platformCourses as $course) {
  1402. $courses[$course['code']] = $course['code'];
  1403. }
  1404. }
  1405. $sessions = SessionManager::getSessionsFollowedByUser(
  1406. $userId,
  1407. COURSEMANAGER,
  1408. null,
  1409. null,
  1410. false
  1411. );
  1412. }
  1413. if ($getCount) {
  1414. return [
  1415. 'drh' => $drhCount,
  1416. 'teachers' => $teachersCount,
  1417. 'student_count' => count($students),
  1418. 'student_list' => $students,
  1419. 'student_bosses' => $studentBossCount,
  1420. 'courses' => $courseCount,
  1421. 'session_count' => count($sessions),
  1422. 'session_list' => $sessions,
  1423. 'assigned_courses' => $assignedCourseCount,
  1424. ];
  1425. }
  1426. return [
  1427. 'drh' => $humanResourcesList,
  1428. 'teachers' => $teachers,
  1429. 'student_list' => $students,
  1430. 'student_bosses' => $studentBosses,
  1431. 'courses' => $courses,
  1432. 'sessions' => $sessions,
  1433. 'assigned_courses' => $assignedCourses,
  1434. ];
  1435. }
  1436. /**
  1437. * Calculates the time spent on the platform by a user.
  1438. *
  1439. * @param int|array $userId
  1440. * @param string $timeFilter type of time filter: 'last_week' or 'custom'
  1441. * @param string $start_date start date date('Y-m-d H:i:s')
  1442. * @param string $end_date end date date('Y-m-d H:i:s')
  1443. *
  1444. * @return int
  1445. */
  1446. public static function get_time_spent_on_the_platform(
  1447. $userId,
  1448. $timeFilter = 'last_7_days',
  1449. $start_date = null,
  1450. $end_date = null
  1451. ) {
  1452. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1453. $condition_time = '';
  1454. if (is_array($userId)) {
  1455. $userList = array_map('intval', $userId);
  1456. $userCondition = " login_user_id IN ('".implode("','", $userList)."')";
  1457. } else {
  1458. $userCondition = " login_user_id = ".intval($userId);
  1459. }
  1460. if (empty($timeFilter)) {
  1461. $timeFilter = 'last_week';
  1462. }
  1463. $today = new DateTime('now', new DateTimeZone('UTC'));
  1464. switch ($timeFilter) {
  1465. case 'last_7_days':
  1466. $newDate = new DateTime('-7 day', new DateTimeZone('UTC'));
  1467. $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'";
  1468. $condition_time .= " AND logout_date <= '{$today->format('Y-m-d H:i:s')}') ";
  1469. break;
  1470. case 'last_30_days':
  1471. $newDate = new DateTime('-30 days', new DateTimeZone('UTC'));
  1472. $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'";
  1473. $condition_time .= "AND logout_date <= '{$today->format('Y-m-d H:i:s')}') ";
  1474. break;
  1475. case 'custom':
  1476. if (!empty($start_date) && !empty($end_date)) {
  1477. $start_date = Database::escape_string($start_date);
  1478. $end_date = Database::escape_string($end_date);
  1479. $condition_time = ' AND (login_date >= "'.$start_date.'" AND logout_date <= "'.$end_date.'" ) ';
  1480. }
  1481. break;
  1482. }
  1483. $sql = 'SELECT SUM(TIMESTAMPDIFF(SECOND, login_date, logout_date)) diff
  1484. FROM '.$tbl_track_login.'
  1485. WHERE '.$userCondition.$condition_time;
  1486. $rs = Database::query($sql);
  1487. $row = Database::fetch_array($rs, 'ASSOC');
  1488. $diff = $row['diff'];
  1489. if ($diff >= 0) {
  1490. return $diff;
  1491. }
  1492. return -1;
  1493. }
  1494. /**
  1495. * Checks if the "lp_minimum_time" feature is available for the course.
  1496. *
  1497. * @param int $sessionId
  1498. * @param int $courseId
  1499. *
  1500. * @return bool
  1501. */
  1502. public static function minimunTimeAvailable($sessionId, $courseId)
  1503. {
  1504. if (!api_get_configuration_value('lp_minimum_time')) {
  1505. return false;
  1506. }
  1507. if (!empty($sessionId)) {
  1508. $extraFieldValue = new ExtraFieldValue('session');
  1509. $value = $extraFieldValue->get_values_by_handler_and_field_variable($sessionId, 'new_tracking_system');
  1510. if ($value && isset($value['value']) && $value['value'] == 1) {
  1511. return true;
  1512. }
  1513. } else {
  1514. if ($courseId) {
  1515. $extraFieldValue = new ExtraFieldValue('course');
  1516. $value = $extraFieldValue->get_values_by_handler_and_field_variable($courseId, 'new_tracking_system');
  1517. if ($value && isset($value['value']) && $value['value'] == 1) {
  1518. return true;
  1519. }
  1520. }
  1521. }
  1522. return false;
  1523. }
  1524. /**
  1525. * Calculates the time spent on the course.
  1526. *
  1527. * @param int $user_id
  1528. * @param int $courseId
  1529. * @param int Session id (optional)
  1530. *
  1531. * @return int Time in seconds
  1532. */
  1533. public static function get_time_spent_on_the_course(
  1534. $user_id,
  1535. $courseId,
  1536. $session_id = 0
  1537. ) {
  1538. $courseId = (int) $courseId;
  1539. if (empty($courseId) || empty($user_id)) {
  1540. return 0;
  1541. }
  1542. if (self::minimunTimeAvailable($session_id, $courseId)) {
  1543. $courseTime = self::getCalculateTime($user_id, $courseId, $session_id);
  1544. $time = isset($courseTime['total_time']) ? $courseTime['total_time'] : 0;
  1545. return $time;
  1546. }
  1547. $session_id = (int) $session_id;
  1548. if (is_array($user_id)) {
  1549. $user_id = array_map('intval', $user_id);
  1550. $conditionUser = " AND user_id IN (".implode(',', $user_id).") ";
  1551. } else {
  1552. $user_id = (int) $user_id;
  1553. $conditionUser = " AND user_id = $user_id ";
  1554. }
  1555. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1556. $sql = "SELECT
  1557. SUM(UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  1558. FROM $table
  1559. WHERE
  1560. UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date) AND
  1561. c_id = '$courseId' ";
  1562. if ($session_id != -1) {
  1563. $sql .= "AND session_id = '$session_id' ";
  1564. }
  1565. $sql .= $conditionUser;
  1566. $rs = Database::query($sql);
  1567. $row = Database::fetch_array($rs);
  1568. return $row['nb_seconds'];
  1569. }
  1570. /**
  1571. * Get first connection date for a student.
  1572. *
  1573. * @param int $student_id
  1574. *
  1575. * @return string|bool Date format long without day or false if there are no connections
  1576. */
  1577. public static function get_first_connection_date($student_id)
  1578. {
  1579. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1580. $sql = 'SELECT login_date
  1581. FROM '.$table.'
  1582. WHERE login_user_id = '.intval($student_id).'
  1583. ORDER BY login_date ASC
  1584. LIMIT 0,1';
  1585. $rs = Database::query($sql);
  1586. if (Database::num_rows($rs) > 0) {
  1587. if ($first_login_date = Database::result($rs, 0, 0)) {
  1588. return api_convert_and_format_date(
  1589. $first_login_date,
  1590. DATE_FORMAT_SHORT,
  1591. date_default_timezone_get()
  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['real_id'],
  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. }