tracking.lib.php 227 KB

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