tracking.lib.php 285 KB

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