tracking.lib.php 227 KB

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