tracking.lib.php 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279
  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. /**
  15. * Class
  16. * @package chamilo.library
  17. */
  18. class Tracking {
  19. /**
  20. * Calculates the time spent on the platform by a user
  21. * @param int User id
  22. * @param bool True for calculating only time spent last week (optional)
  23. * @param int Timestamp for filtering by Day (optional, default = 0)
  24. * @return timestamp $nb_seconds
  25. */
  26. public static function get_time_spent_on_the_platform($user_id, $last_week = false, $by_day = 0) {
  27. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  28. $condition_time = '';
  29. if ($last_week) {
  30. $a_last_week = get_last_week();
  31. $fday_last_week = date('Y-m-d H:i:s',$a_last_week[0]);
  32. $lday_last_week = date('Y-m-d H:i:s',$a_last_week[6]);
  33. $condition_time = ' AND (login_date >= "'.$fday_last_week.'" AND logout_date <= "'.$lday_last_week.'") ';
  34. } else if (!empty($by_day)) {
  35. $fdate_time = date('Y-m-d',$by_day).' 00:00:00';
  36. $ldate_time = date('Y-m-d',$by_day).' 23:59:59';
  37. $condition_time = ' AND (login_date >= "'.$fdate_time.'" AND logout_date <= "'.$ldate_time.'" ) ';
  38. }
  39. $sql = 'SELECT login_date, logout_date FROM '.$tbl_track_login.'
  40. WHERE login_user_id = '.intval($user_id).$condition_time;
  41. $rs = Database::query($sql);
  42. $nb_seconds = 0;
  43. $wrong_logout_dates = false;
  44. while ($a_connections = Database::fetch_array($rs)) {
  45. $s_login_date = $a_connections["login_date"];
  46. $s_logout_date = $a_connections["logout_date"];
  47. $i_timestamp_login_date = strtotime($s_login_date);
  48. $i_timestamp_logout_date = strtotime($s_logout_date);
  49. if ($i_timestamp_logout_date > 0) {
  50. // @TODO YW 20110708: for some reason the result here is often
  51. // negative, resulting in a negative time total. Considering the
  52. // logout_date is > 0, this can only mean that the database
  53. // contains items where the login_date is higher (=later) than
  54. // the logout date for a specific connexion. This has to be
  55. // analyzed and fixed. Also see the get_time_spent_on_the_course
  56. // for SQL summing.
  57. $nb_seconds += abs($i_timestamp_logout_date - $i_timestamp_login_date);
  58. } else { // there are wrong datas in db, then we can't give a wrong time
  59. $wrong_logout_dates = true;
  60. }
  61. }
  62. if($nb_seconds>0 || !$wrong_logout_dates)
  63. {
  64. return $nb_seconds;
  65. }
  66. else
  67. {
  68. return -1; //-1 means we have wrong datas in the db
  69. }
  70. }
  71. /**
  72. * Calculates the time spent on the course
  73. * @param integer User id
  74. * @param string Course code
  75. * @param int Session id (optional)
  76. * @return timestamp Time in seconds
  77. */
  78. public static function get_time_spent_on_the_course($user_id, $course_code, $session_id = 0) {
  79. // protect datas
  80. $course_code = Database::escape_string($course_code);
  81. $session_id = intval($session_id);
  82. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  83. $condition_user = "";
  84. if (is_array($user_id)) {
  85. $condition_user = " AND user_id IN (".implode(',',$user_id).") ";
  86. } else {
  87. $user_id = intval($user_id);
  88. $condition_user = " AND user_id = '$user_id' ";
  89. }
  90. $sql = " SELECT SUM(UNIX_TIMESTAMP(logout_course_date)-UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  91. FROM $tbl_track_course
  92. WHERE course_code='$course_code' AND session_id = '$session_id' $condition_user";
  93. $rs = Database::query($sql);
  94. $row = Database::fetch_array($rs);
  95. return $row['nb_seconds'];
  96. }
  97. /**
  98. * Get first connection date for a student
  99. * @param int Student id
  100. * @return string|bool Date format long without day or false if there are no connections
  101. */
  102. public static function get_first_connection_date($student_id) {
  103. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  104. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  105. WHERE login_user_id = ' . intval($student_id) . '
  106. ORDER BY login_date ASC LIMIT 0,1';
  107. $rs = Database::query($sql);
  108. if(Database::num_rows($rs)>0) {
  109. if ($first_login_date = Database::result($rs, 0, 0)) {
  110. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  111. }
  112. }
  113. return false;
  114. }
  115. /**
  116. * Get las connection date for a student
  117. * @param int Student id
  118. * @param bool Show a warning message (optional)
  119. * @param bool True for returning results in timestamp (optional)
  120. * @return string|int|bool Date format long without day, false if there are no connections or timestamp if parameter $return_timestamp is true
  121. */
  122. public static function get_last_connection_date($student_id, $warning_message = false, $return_timestamp = false) {
  123. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  124. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  125. WHERE login_user_id = ' . intval($student_id) . '
  126. ORDER BY login_date DESC LIMIT 0,1';
  127. $rs = Database::query($sql);
  128. if(Database::num_rows($rs)>0) {
  129. if ($last_login_date = Database::result($rs, 0, 0)) {
  130. $last_login_date = api_get_local_time($last_login_date);
  131. if ($return_timestamp) {
  132. return api_strtotime($last_login_date,'UTC');
  133. } else {
  134. if (!$warning_message) {
  135. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  136. } else {
  137. $timestamp = api_strtotime($last_login_date,'UTC');
  138. $currentTimestamp = time();
  139. //If the last connection is > than 7 days, the text is red
  140. //345600 = 7 days in seconds
  141. if ($currentTimestamp - $timestamp > 604800)
  142. {
  143. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . '</span>';
  144. }
  145. else
  146. {
  147. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. return false;
  154. }
  155. /**
  156. * Get first user's connection date on the course
  157. * @param int User id
  158. * @param string Course code
  159. * @param int Session id (optional, default=0)
  160. * @return string|bool Date with format long without day or false if there is no date
  161. */
  162. public static function get_first_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  163. // protect data
  164. $student_id = intval($student_id);
  165. $course_code = Database::escape_string($course_code);
  166. $session_id = intval($session_id);
  167. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  168. $sql = 'SELECT login_course_date FROM '.$tbl_track_login.'
  169. WHERE user_id = '.$student_id.'
  170. AND course_code = "'.$course_code.'"
  171. AND session_id = '.$session_id.'
  172. ORDER BY login_course_date ASC LIMIT 0,1';
  173. $rs = Database::query($sql);
  174. if (Database::num_rows($rs)>0) {
  175. if ($first_login_date = Database::result($rs, 0, 0)) {
  176. if ($convert_date) {
  177. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  178. } else {
  179. return $first_login_date;
  180. }
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * Get last user's connection date on the course
  187. * @param int User id
  188. * @param string Course code
  189. * @param int Session id (optional, default=0)
  190. * @return string|bool Date with format long without day or false if there is no date
  191. */
  192. public static function get_last_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  193. // protect data
  194. $student_id = intval($student_id);
  195. $course_code = Database::escape_string($course_code);
  196. $session_id = intval($session_id);
  197. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  198. $sql = 'SELECT login_course_date FROM '.$tbl_track_e_course_access.'
  199. WHERE user_id = '.$student_id.'
  200. AND course_code = "'.$course_code.'"
  201. AND session_id = '.$session_id.'
  202. ORDER BY login_course_date DESC LIMIT 0,1';
  203. $rs = Database::query($sql);
  204. if (Database::num_rows($rs)>0) {
  205. if ($last_login_date = Database::result($rs, 0, 0)) {
  206. $last_login_date = api_get_local_time($last_login_date, null, date_default_timezone_get());
  207. $timestamp = api_strtotime($last_login_date);
  208. $currentTimestamp = time();
  209. //If the last connection is > than 7 days, the text is red
  210. //345600 = 7 days in seconds
  211. if ($currentTimestamp - $timestamp > 604800) {
  212. if ($convert_date) {
  213. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . (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 align="middle" src="'.api_get_path(WEB_IMG_PATH).'messagebox_warning.gif" /></a>':'').'</span>';
  214. } else {
  215. return $last_login_date;
  216. }
  217. } else {
  218. if ($convert_date) {
  219. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  220. } else {
  221. return $last_login_date;
  222. }
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. /**
  229. * Get count of the connections to the course during a specified period
  230. * @param string Course code
  231. * @param int Session id (optional)
  232. * @param int Datetime from which to collect data (defaults to 0)
  233. * @param int Datetime to which to collect data (defaults to now)
  234. * @return int count connections
  235. */
  236. public static function get_course_connections_count($course_code, $session_id = 0, $start = 0, $stop = null) {
  237. // protect data
  238. $month_filter = '';
  239. if ($start < 0) {
  240. $start = 0;
  241. }
  242. if (!isset($stop) or ($stop < 0)) {
  243. $stop = api_get_utc_datetime();
  244. }
  245. $month_filter = " AND login_course_date > '$start' AND login_course_date < '$stop' ";
  246. $course_code = Database::escape_string($course_code);
  247. $session_id = intval($session_id);
  248. $count = 0;
  249. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  250. $sql = "SELECT count(*) as count_connections FROM $tbl_track_e_course_access WHERE course_code = '$course_code' AND session_id = $session_id $month_filter";
  251. $rs = Database::query($sql);
  252. if (Database::num_rows($rs)>0) {
  253. $row = Database::fetch_object($rs);
  254. $count = $row->count_connections;
  255. }
  256. return $count;
  257. }
  258. /**
  259. * Get count courses per student
  260. * @param int Student id
  261. * @param bool Include sessions (optional)
  262. * @return int count courses
  263. */
  264. public static function count_course_per_student($user_id, $include_sessions = true) {
  265. $user_id = intval($user_id);
  266. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  267. $tbl_session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  268. $sql = 'SELECT DISTINCT course_code
  269. FROM ' . $tbl_course_rel_user . '
  270. WHERE user_id = ' . $user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH;
  271. $rs = Database::query($sql);
  272. $nb_courses = Database::num_rows($rs);
  273. if ($include_sessions) {
  274. $sql = 'SELECT DISTINCT course_code
  275. FROM ' . $tbl_session_course_rel_user . '
  276. WHERE id_user = ' . $user_id;
  277. $rs = Database::query($sql);
  278. $nb_courses += Database::num_rows($rs);
  279. }
  280. return $nb_courses;
  281. }
  282. /**
  283. * Gets the score average from all tests in a course by student
  284. *
  285. * @param mixed Student(s) id
  286. * @param string Course code
  287. * @param int Exercise id (optional), filtered by exercise
  288. * @param int Session id (optional), if param $session_id is null it'll return results including sessions, 0 = session is not filtered
  289. * @return string value (number %) Which represents a round integer about the score average.
  290. */
  291. public static function get_avg_student_exercise_score($student_id, $course_code, $exercise_id = 0, $session_id = null) {
  292. // protect datas
  293. $course_code = Database::escape_string($course_code);
  294. // get the informations of the course
  295. $a_course = CourseManager :: get_course_information($course_code);
  296. if (!empty($a_course)) {
  297. // table definition
  298. $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  299. $tbl_stats_exercise = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  300. // Compose a filter based on optional exercise given
  301. $condition_quiz = "";
  302. if (!empty($exercise_id)) {
  303. $exercise_id = intval($exercise_id);
  304. $condition_quiz =" AND id = $exercise_id ";
  305. }
  306. // Compose a filter based on optional session id given
  307. $condition_session = "";
  308. if (isset($session_id)) {
  309. $session_id = intval($session_id);
  310. $condition_session = " AND session_id = $session_id ";
  311. }
  312. $sql = "SELECT count(id) FROM $tbl_course_quiz
  313. WHERE c_id = {$a_course['real_id']} AND active <> -1 $condition_quiz ";
  314. $count_quiz = Database::fetch_row(Database::query($sql));
  315. $quiz_avg_total_score = 0;
  316. if (!empty($count_quiz[0]) && !empty($student_id)) {
  317. $condition_user = "";
  318. if (is_array($student_id)) {
  319. $condition_user = " AND exe_user_id IN (".implode(',',$student_id).") ";
  320. } else {
  321. $condition_user = " AND exe_user_id = '$student_id' ";
  322. }
  323. if (empty($exercise_id)) {
  324. $sql = "SELECT id FROM $tbl_course_quiz
  325. WHERE c_id = {$a_course['real_id']} AND active <> -1 $condition_quiz";
  326. $exercises = Database::fetch_row(Database::query($sql));
  327. $exercise_list = array();
  328. $exercise_id = 0;
  329. if (!empty($exercises)) {
  330. foreach($exercises as $row) {
  331. $exercise_list[] = $row['id'];
  332. }
  333. $exercise_id = implode("','",$exercise_list);
  334. }
  335. }
  336. $count_quiz = Database::fetch_row(Database::query($sql));
  337. $sql = "SELECT SUM(exe_result/exe_weighting*100) as avg_score, COUNT(*) as num_attempts
  338. FROM $tbl_stats_exercise
  339. WHERE exe_exo_id IN ('".$exercise_id."')
  340. $condition_user
  341. AND orig_lp_id = 0
  342. AND exe_cours_id = '$course_code'
  343. AND orig_lp_item_id = 0 $condition_session
  344. ORDER BY exe_date DESC";
  345. $res = Database::query($sql);
  346. $row = Database::fetch_array($res);
  347. $quiz_avg_score = 0;
  348. if (!empty($row['avg_score'])) {
  349. $quiz_avg_score = round($row['avg_score'],2);
  350. }
  351. if(!empty($row['num_attempts'])) {
  352. $quiz_avg_score = round($quiz_avg_score / $row['num_attempts'], 2);
  353. }
  354. if (is_array($student_id)) {
  355. $quiz_avg_score = round($quiz_avg_score / count($student_id), 2);
  356. }
  357. return $quiz_avg_score;
  358. }
  359. }
  360. return null;
  361. }
  362. /**
  363. * Get count student's exercise attempts
  364. * @param int Student id
  365. * @param string Course code
  366. * @param int Exercise id
  367. * @param int Learning path id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  368. * @param int Learning path item id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  369. * @return int count of attempts
  370. */
  371. public function count_student_exercise_attempts($student_id, $course_code, $exercise_id, $lp_id = 0, $lp_item_id = 0, $session_id = 0) {
  372. $course_code = Database::escape_string($course_code);
  373. $course_info = CourseManager :: get_course_information($course_code);
  374. $student_id = intval($student_id);
  375. $exercise_id = intval($exercise_id);
  376. $session_id = intval($session_id);
  377. $count_attempts = 0;
  378. if (!empty($lp_id)) $lp_id = intval($lp_id);
  379. if (!empty($lp_item_id)) $lp_id = intval($lp_item_id);
  380. if (!empty($course_info['db_name'])) {
  381. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES, $course_info['db_name']);
  382. $sql = "SELECT COUNT(ex.exe_id) as essais FROM $tbl_stats_exercices AS ex
  383. WHERE ex.exe_cours_id = '$course_code'
  384. AND ex.exe_exo_id = $exercise_id
  385. AND orig_lp_id = $lp_id
  386. AND orig_lp_item_id = $lp_item_id
  387. AND exe_user_id= $student_id
  388. AND session_id = $session_id ";
  389. $rs = Database::query($sql);
  390. $row = Database::fetch_row($rs);
  391. $count_attempts = $row[0];
  392. }
  393. return $count_attempts;
  394. }
  395. /**
  396. * Returns the average student progress in the learning paths of the given
  397. * course.
  398. * @param int/array Student id(s)
  399. * @param string Course code
  400. * @param array Limit average to listed lp ids
  401. * @param int Session id (optional), if parameter $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  402. * @param bool Will return an array of the type: [sum_of_progresses, number] if it is set to true
  403. * @return double Average progress of the user in this course
  404. */
  405. public static function get_avg_student_progress($student_id, $course_code, $lp_ids = array(), $session_id = null, $return_array = false) {
  406. // get the informations of the course
  407. $a_course = CourseManager :: get_course_information($course_code);
  408. if (!empty($a_course)) {
  409. // table definition
  410. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  411. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN);
  412. // Compose a filter based on optional learning paths list given
  413. $condition_lp = "";
  414. if (!empty($lp_ids)) {
  415. if (count($lp_ids) > 0) {
  416. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  417. }
  418. }
  419. $session_id = intval($session_id);
  420. $sql = "SELECT id FROM $tbl_course_lp lp WHERE c_id = {$a_course['real_id']} $condition_lp";
  421. $res_count_lp = Database::query($sql);
  422. // count the number of learning paths
  423. $lp_id = array();
  424. while ($row_lp = Database::fetch_array($res_count_lp,'ASSOC')) {
  425. //$visibility = api_get_item_visibility($a_course, TOOL_LEARNPATH, $row_lp['id'], $session_id);
  426. // if ($visibility == 1) {
  427. $lp_id[] = $row_lp['id'];
  428. //}
  429. }
  430. $count_lp = count($lp_id);
  431. $avg_progress = 0;
  432. //if there is at least one learning path and one student
  433. if ($count_lp>0 && !empty($student_id)) {
  434. $condition_user = "";
  435. if (is_array($student_id)) {
  436. array_walk($student_id,'intval');
  437. $condition_user = " lp_view.user_id IN (".implode(',',$student_id).") AND ";
  438. } else {
  439. $student_id = intval($student_id);
  440. $condition_user = " lp_view.user_id = '$student_id' AND ";
  441. }
  442. // Get last view for each student (in case of multi-attempt)
  443. // Also filter on LPs of this session
  444. $sql_maxes = "SELECT MAX(view_count), progress FROM $tbl_course_lp_view lp_view ".
  445. "WHERE c_id = {$a_course['real_id']} AND
  446. $condition_user session_id = $session_id AND
  447. lp_view.lp_id IN (".implode(',',$lp_id).") ".
  448. "GROUP BY lp_id, user_id";
  449. $res_maxes = Database::query($sql_maxes);
  450. $sum = 0;
  451. while ($row_maxes = Database::fetch_array($res_maxes)) {
  452. $sum += $row_maxes[1];
  453. }
  454. // average progress = total sum divided by the number of views
  455. // summed up.
  456. $number_items = count($lp_id);
  457. if ($number_items == 0) {
  458. return 0; //not necessary to return something else if there is no view
  459. }
  460. if (!$return_array) {
  461. $avg_progress = round($sum / $number_items, 1);
  462. return $avg_progress;
  463. } else {
  464. return array($sum, $number_items);
  465. }
  466. }
  467. }
  468. return null;
  469. }
  470. /**
  471. * This function gets:
  472. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores.
  473. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores.
  474. * 3. And finally it will return the average between 1. and 2.
  475. * @todo improve performance, when loading 1500 users with 20 lps the script dies
  476. * This function does not take the results of a Test out of a LP
  477. *
  478. * @param mixed Array of user ids or an user id
  479. * @param string Course code
  480. * @param array List of LP ids
  481. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  482. * @param bool Returns an array of the type [sum_score, num_score] if set to true
  483. * @param bool get only the latest attempts or ALL attempts
  484. * @return string Value (number %) Which represents a round integer explain in got in 3.
  485. */
  486. public static function get_avg_student_score($student_id, $course_code, $lp_ids = array(), $session_id = null, $return_array = false, $get_only_latest_attempt_results = false) {
  487. $debug = false;
  488. if (empty($lp_ids)) {
  489. $debug = false;
  490. }
  491. if ($debug) echo '<h1>Tracking::get_avg_student_score</h1>';
  492. // get global tables names
  493. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  494. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  495. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  496. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  497. $tbl_stats_attempts = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  498. $course = CourseManager :: get_course_information($course_code);
  499. if (!empty($course)) {
  500. // get course tables names
  501. $tbl_quiz_questions = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  502. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  503. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  504. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  505. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  506. $course_id = $course['real_id'];
  507. // Compose a filter based on optional learning paths list given
  508. $condition_lp = "";
  509. if (count($lp_ids) > 0) {
  510. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  511. }
  512. // Compose a filter based on optional session id
  513. $condition_session = "";
  514. $session_id = intval($session_id);
  515. if (count($lp_ids) > 0) {
  516. $condition_session = " AND session_id = $session_id ";
  517. } else {
  518. $condition_session = " WHERE session_id = $session_id ";
  519. }
  520. // Check the real number of LPs corresponding to the filter in the
  521. // database (and if no list was given, get them all)
  522. if (empty($session_id)) {
  523. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE c_id = $course_id AND session_id = 0 $condition_lp ";
  524. } else {
  525. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE c_id = $course_id $condition_lp ";
  526. }
  527. $res_row_lp = Database::query($sql);
  528. $count_row_lp = Database::num_rows($res_row_lp);
  529. $lp_list = $use_max_score = array();
  530. while ($row_lp = Database::fetch_array($res_row_lp)) {
  531. $lp_list[] = $row_lp['id'];
  532. $use_max_score[$row_lp['id']] = $row_lp['use_max_score'];
  533. }
  534. if ($debug) {
  535. echo 'Use max score or not list '; var_dump($use_max_score);
  536. }
  537. // Init local variables that will be used through the calculation
  538. $progress = 0;
  539. // prepare filter on users
  540. $condition_user1 = "";
  541. if (is_array($student_id)) {
  542. array_walk($student_id, 'intval');
  543. $condition_user1 =" AND user_id IN (".implode(',', $student_id).") ";
  544. } else {
  545. $condition_user1 =" AND user_id = $student_id ";
  546. }
  547. if ($count_row_lp > 0 && !empty($student_id)) {
  548. // Getting latest LP result for a student
  549. //@todo problem when a course have more than 1500 users
  550. $sql = "SELECT MAX(view_count) as vc, id, progress, lp_id, user_id FROM $lp_view_table
  551. WHERE c_id = $course_id AND
  552. lp_id IN (".implode(',',$lp_list).")
  553. $condition_user1 AND
  554. session_id = $session_id
  555. GROUP BY lp_id, user_id";
  556. if ($debug) echo $sql;
  557. $rs_last_lp_view_id = Database::query($sql);
  558. $global_result = 0;
  559. if (Database::num_rows($rs_last_lp_view_id) > 0) {
  560. // Cycle through each line of the results (grouped by lp_id, user_id)
  561. $exe_list = array();
  562. while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
  563. $count_items = 0;
  564. $lp_partial_total = 0;
  565. $list = array();
  566. $lp_view_id = $row_lp_view['id'];
  567. $progress = $row_lp_view['progress'];
  568. $lp_id = $row_lp_view['lp_id'];
  569. $user_id = $row_lp_view['user_id'];
  570. if ($debug) echo '<h2>LP id '.$lp_id.'</h2>';
  571. if ($get_only_latest_attempt_results) {
  572. //if (1) {
  573. //Getting lp_items done by the user
  574. $sql = "SELECT DISTINCT lp_item_id FROM $lp_item_view_table
  575. WHERE c_id = $course_id AND lp_view_id = $lp_view_id ORDER BY lp_item_id";
  576. $res_lp_item = Database::query($sql);
  577. while ($row_lp_item = Database::fetch_array($res_lp_item,'ASSOC')) {
  578. $my_lp_item_id = $row_lp_item['lp_item_id'];
  579. //Getting the most recent attempt
  580. $sql = "SELECT lp_iv.id as lp_item_view_id, lp_iv.score as score,lp_i.max_score, lp_iv.max_score as max_score_item_view, lp_i.path, lp_i.item_type, lp_i.id as iid
  581. FROM $lp_item_view_table as lp_iv INNER JOIN $lp_item_table as lp_i ON lp_i.id = lp_iv.lp_item_id AND (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  582. WHERE lp_iv.c_id = $course_id AND
  583. lp_i.c_id = $course_id AND
  584. lp_item_id = $my_lp_item_id AND
  585. lp_view_id = $lp_view_id
  586. ORDER BY view_count DESC
  587. LIMIT 1";
  588. $res_lp_item_result = Database::query($sql);
  589. while ($row_max_score = Database::fetch_array($res_lp_item_result,'ASSOC')) {
  590. $list[]= $row_max_score;
  591. }
  592. }
  593. } else {
  594. // For the currently analysed view, get the score and
  595. // max_score of each item if it is a sco or a TOOL_QUIZ
  596. $sql_max_score = "SELECT lp_iv.id as lp_item_view_id, lp_iv.score as score,lp_i.max_score, lp_iv.max_score as max_score_item_view, lp_i.path, lp_i.item_type, lp_i.id as iid
  597. FROM $lp_item_view_table as lp_iv INNER JOIN $lp_item_table as lp_i ON lp_i.id = lp_iv.lp_item_id AND (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  598. WHERE lp_iv.c_id = $course_id AND
  599. lp_i.c_id = $course_id AND
  600. lp_view_id = $lp_view_id ";
  601. if ($debug) echo $sql_max_score.'<br />';
  602. $res_max_score = Database::query($sql_max_score);
  603. while ($row_max_score = Database::fetch_array($res_max_score,'ASSOC')) {
  604. $list[]= $row_max_score;
  605. }
  606. }
  607. $count_total_loop = 0;
  608. // Go through each scorable element of this view
  609. $score_of_scorm_calculate = 0;
  610. foreach ($list as $row_max_score) {
  611. $max_score = $row_max_score['max_score']; //Came from the original lp_item
  612. $max_score_item_view = $row_max_score['max_score_item_view']; //Came from the lp_item_view
  613. $score = $row_max_score['score'];
  614. if ($debug) echo '<h3>Item Type: ' .$row_max_score['item_type'].'</h3>';
  615. if ($row_max_score['item_type'] == 'sco') {
  616. //var_dump($row_max_score);
  617. // Check if it is sco (easier to get max_score)
  618. //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.
  619. if ($max_score == 0 || is_null($max_score) || $max_score == '') {
  620. //Chamilo style
  621. if ($use_max_score[$lp_id]) {
  622. $max_score = 100;
  623. } else {
  624. //Overwrites max score = 100 to use the one that came in the lp_item_view see BT#1613
  625. $max_score = $max_score_item_view;
  626. }
  627. }
  628. //Avoid division by zero errors
  629. if (!empty($max_score)) {
  630. $lp_partial_total += $score/$max_score;
  631. }
  632. if ($debug) echo '<b>$lp_partial_total, $score, $max_score '.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  633. } else {
  634. // Case of a TOOL_QUIZ element
  635. $item_id = $row_max_score['iid'];
  636. $item_path = $row_max_score['path'];
  637. $lp_item_view_id = $row_max_score['lp_item_view_id'];
  638. // Get last attempt to this exercise through
  639. // the current lp for the current user
  640. $sql_last_attempt = "SELECT exe_id FROM $tbl_stats_exercices WHERE
  641. exe_exo_id = '$item_path' AND
  642. exe_user_id = $user_id AND
  643. orig_lp_item_id = $item_id AND
  644. orig_lp_item_view_id = $lp_item_view_id AND
  645. exe_cours_id = '$course_code' AND
  646. session_id = $session_id
  647. ORDER BY exe_date DESC LIMIT 1";
  648. if ($debug) echo $sql_last_attempt .'<br />';
  649. $result_last_attempt = Database::query($sql_last_attempt);
  650. $num = Database :: num_rows($result_last_attempt);
  651. if ($num > 0 ) {
  652. $id_last_attempt = Database :: result($result_last_attempt, 0, 0);
  653. if ($debug) echo $id_last_attempt.'<br />';
  654. /* if (in_array($id_last_attempt, $exe_list)) {
  655. continue;
  656. }*/
  657. //$exe_list[] = $id_last_attempt;
  658. //echo $id_last_attempt .'<br />';
  659. // Within the last attempt number tracking, get the sum of
  660. // the max_scores of all questions that it was
  661. // made of (we need to make this call dynamic
  662. // because of random questions selection)
  663. $sql = "SELECT SUM(t.ponderation) as maxscore FROM
  664. ( SELECT distinct question_id, marks, ponderation FROM $tbl_stats_attempts AS at INNER JOIN $tbl_quiz_questions AS q ON (q.id = at.question_id)
  665. WHERE exe_id ='$id_last_attempt' ) AS t";
  666. $res_max_score_bis = Database::query($sql);
  667. $row_max_score_bis = Database :: fetch_array($res_max_score_bis);
  668. if (!empty($row_max_score_bis['maxscore'])) {
  669. $max_score = $row_max_score_bis['maxscore'];
  670. }
  671. if (!empty($max_score)) {
  672. $lp_partial_total += $score/$max_score;
  673. }
  674. if ($debug) echo '$lp_partial_total, $score, $max_score <b>'.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  675. }
  676. }
  677. if (in_array($row_max_score['item_type'], array('quiz','sco'))) {
  678. // Normal way
  679. if ($use_max_score[$lp_id]) {
  680. $count_items++;
  681. } else {
  682. if ($max_score != '') {
  683. $count_items++;
  684. }
  685. }
  686. if ($debug) echo '$count_items: '.$count_items;
  687. }
  688. } //end for
  689. $score_of_scorm_calculate += $count_items?(($lp_partial_total/$count_items)*100):0;
  690. if ($debug) echo '<h3>$count_items '.$count_items.'</h3>';
  691. if ($debug) echo '<h3>$score_of_scorm_calculate '.$score_of_scorm_calculate.'</h3>';
  692. // var_dump($score_of_scorm_calculate);
  693. $global_result += $score_of_scorm_calculate;
  694. if ($debug) echo '<h3>$global_result '.$global_result.'</h3>';
  695. } // end while
  696. }
  697. $lp_with_quiz = 0;
  698. if ($debug) var_dump($lp_list);
  699. foreach($lp_list as $lp_id) {
  700. //Check if LP have a score we asume that all SCO have an score
  701. $sql = "SELECT count(id) as count FROM $lp_item_table WHERE c_id = $course_id AND (item_type = 'quiz' OR item_type = 'sco') AND lp_id = ".$lp_id;
  702. if ($debug) echo $sql;
  703. $result_have_quiz = Database::query($sql);
  704. if (Database::num_rows($result_have_quiz) > 0 ) {
  705. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  706. if (is_numeric($row['count']) && $row['count'] != 0) {
  707. $lp_with_quiz ++;
  708. }
  709. }
  710. }
  711. if ($debug) echo '<h3>$lp_with_quiz '.$lp_with_quiz.' </h3>';
  712. if ($debug) echo '<h3>Final return</h3>';
  713. if ($lp_with_quiz != 0 ) {
  714. if (!$return_array) {
  715. $score_of_scorm_calculate = round(($global_result/$lp_with_quiz),2);
  716. if ($debug) var_dump($score_of_scorm_calculate);
  717. if (empty($lp_ids)) {
  718. //$score_of_scorm_calculate = round($score_of_scorm_calculate/count($lp_list),2);
  719. if ($debug) echo '<h2>All lps fix: '.$score_of_scorm_calculate.'</h2>';
  720. }
  721. return $score_of_scorm_calculate;
  722. } else {
  723. if ($debug) var_dump($global_result, $lp_with_quiz);
  724. return array($global_result, $lp_with_quiz);
  725. }
  726. } else {
  727. return '-';
  728. }
  729. }
  730. }
  731. return null;
  732. }
  733. /**
  734. * This function gets time spent in learning path for a student inside a course
  735. * @param int|array Student id(s)
  736. * @param string Course code
  737. * @param array Limit average to listed lp ids
  738. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  739. * @return int Total time
  740. */
  741. public static function get_time_spent_in_lp($student_id, $course_code, $lp_ids = array(), $session_id = null) {
  742. $course = CourseManager :: get_course_information($course_code);
  743. $student_id = intval($student_id);
  744. $total_time = 0;
  745. if (!empty($course)) {
  746. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  747. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW);
  748. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  749. $course_id = $course['real_id'];
  750. // Compose a filter based on optional learning paths list given
  751. $condition_lp = "";
  752. if(count($lp_ids) > 0) {
  753. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  754. }
  755. // Compose a filter based on optional session id
  756. $condition_session = "";
  757. $session_id = intval($session_id);
  758. if (isset($session_id)) {
  759. $condition_session = " AND session_id = $session_id ";
  760. }
  761. // Check the real number of LPs corresponding to the filter in the
  762. // database (and if no list was given, get them all)
  763. //$res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table $condition_lp $condition_session");
  764. $res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table WHERE c_id = $course_id $condition_lp");
  765. $count_row_lp = Database::num_rows($res_row_lp);
  766. // calculates time
  767. if ($count_row_lp > 0) {
  768. while ($row_lp = Database::fetch_array($res_row_lp)) {
  769. $lp_id = intval($row_lp['id']);
  770. $sql = 'SELECT SUM(total_time)
  771. FROM '.$t_lpiv.' AS item_view
  772. INNER JOIN '.$t_lpv.' AS view
  773. ON item_view.lp_view_id = view.id
  774. WHERE
  775. item_view.c_id = '.$course_id.' AND
  776. view.c_id = '.$course_id.' AND
  777. view.lp_id = '.$lp_id.'
  778. AND view.user_id = '.$student_id.' AND
  779. session_id = '.$session_id;
  780. $rs = Database::query($sql);
  781. if (Database :: num_rows($rs) > 0) {
  782. $total_time += Database :: result($rs, 0, 0);
  783. }
  784. }
  785. }
  786. }
  787. return $total_time;
  788. }
  789. /**
  790. * This function gets last connection time to one learning path
  791. * @param int|array Student id(s)
  792. * @param string Course code
  793. * @param int Learning path id
  794. * @return int Total time
  795. */
  796. public static function get_last_connection_time_in_lp($student_id, $course_code, $lp_id, $session_id = 0) {
  797. $course = CourseManager :: get_course_information($course_code);
  798. $student_id = intval($student_id);
  799. $lp_id = intval($lp_id);
  800. $last_time = 0;
  801. $session_id = intval($session_id);
  802. if (!empty($course)) {
  803. $course_id = $course['real_id'];
  804. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  805. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW);
  806. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  807. // Check the real number of LPs corresponding to the filter in the
  808. // database (and if no list was given, get them all)
  809. $res_row_lp = Database::query("SELECT id FROM $lp_table WHERE c_id = $course_id AND id = $lp_id ");
  810. $count_row_lp = Database::num_rows($res_row_lp);
  811. // calculates last connection time
  812. if ($count_row_lp > 0) {
  813. $sql = 'SELECT MAX(start_time)
  814. FROM ' . $t_lpiv . ' AS item_view
  815. INNER JOIN ' . $t_lpv . ' AS view
  816. ON item_view.lp_view_id = view.id
  817. WHERE
  818. item_view.c_id = '.$course_id.' AND
  819. view.c_id = '.$course_id.' AND
  820. view.lp_id = '.$lp_id.'
  821. AND view.user_id = '.$student_id.'
  822. AND view.session_id = '.$session_id;
  823. $rs = Database::query($sql);
  824. if (Database :: num_rows($rs) > 0) {
  825. $last_time = Database :: result($rs, 0, 0);
  826. }
  827. }
  828. }
  829. return $last_time;
  830. }
  831. /**
  832. * gets the list of students followed by coach
  833. * @param int Coach id
  834. * @return array List of students
  835. */
  836. public static function get_student_followed_by_coach($coach_id) {
  837. $coach_id = intval($coach_id);
  838. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  839. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  840. $tbl_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  841. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  842. $a_students = array ();
  843. // At first, courses where $coach_id is coach of the course //
  844. $sql = 'SELECT id_session, course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  845. global $_configuration;
  846. if ($_configuration['multiple_access_urls']) {
  847. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  848. $access_url_id = api_get_current_access_url_id();
  849. if ($access_url_id != -1) {
  850. $sql = 'SELECT scu.id_session, scu.course_code
  851. FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_session_rel_access_url.' sru
  852. ON (scu.id_session=sru.session_id)
  853. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND sru.access_url_id = '.$access_url_id;
  854. }
  855. }
  856. $result = Database::query($sql);
  857. while ($a_courses = Database::fetch_array($result)) {
  858. $course_code = $a_courses["course_code"];
  859. $id_session = $a_courses["id_session"];
  860. $sql = "SELECT distinct srcru.id_user
  861. FROM $tbl_session_course_user AS srcru, $tbl_session_user sru
  862. WHERE srcru.id_user = sru.id_user AND sru.relation_type<>".SESSION_RELATION_TYPE_RRHH." AND srcru.id_session = sru.id_session AND srcru.course_code='$course_code' AND srcru.id_session='$id_session'";
  863. $rs = Database::query($sql);
  864. while ($row = Database::fetch_array($rs)) {
  865. $a_students[$row['id_user']] = $row['id_user'];
  866. }
  867. }
  868. // Then, courses where $coach_id is coach of the session //
  869. $sql = 'SELECT session_course_user.id_user
  870. FROM ' . $tbl_session_course_user . ' as session_course_user
  871. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  872. INNER JOIN ' . $tbl_session_course . ' as session_course
  873. ON session_course.course_code = session_course_user.course_code
  874. AND session_course_user.id_session = session_course.id_session
  875. INNER JOIN ' . $tbl_session . ' as session
  876. ON session.id = session_course.id_session
  877. AND session.id_coach = ' . $coach_id;
  878. if ($_configuration['multiple_access_urls']) {
  879. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  880. $access_url_id = api_get_current_access_url_id();
  881. if ($access_url_id != -1){
  882. $sql = 'SELECT session_course_user.id_user
  883. FROM ' . $tbl_session_course_user . ' as session_course_user
  884. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  885. INNER JOIN ' . $tbl_session_course . ' as session_course
  886. ON session_course.course_code = session_course_user.course_code
  887. AND session_course_user.id_session = session_course.id_session
  888. INNER JOIN ' . $tbl_session . ' as session
  889. ON session.id = session_course.id_session
  890. AND session.id_coach = ' . $coach_id.'
  891. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  892. ON session.id = session_rel_url.session_id WHERE access_url_id = '.$access_url_id;
  893. }
  894. }
  895. $result = Database::query($sql);
  896. while ($row = Database::fetch_array($result)) {
  897. $a_students[$row['id_user']] = $row['id_user'];
  898. }
  899. return $a_students;
  900. }
  901. /**
  902. * Get student followed by a coach inside a session
  903. * @param int Session id
  904. * @param int Coach id
  905. * @return array students list
  906. */
  907. public static function get_student_followed_by_coach_in_a_session($id_session, $coach_id) {
  908. $coach_id = intval($coach_id);
  909. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  910. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  911. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  912. $a_students = array ();
  913. //////////////////////////////////////////////////////////////
  914. // At first, courses where $coach_id is coach of the course //
  915. //////////////////////////////////////////////////////////////
  916. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_session="' . $id_session . '" AND id_user=' . $coach_id.' AND status=2';
  917. $result = Database::query($sql);
  918. while ($a_courses = Database::fetch_array($result)) {
  919. $course_code = $a_courses["course_code"];
  920. $sql = "SELECT distinct srcru.id_user
  921. FROM $tbl_session_course_user AS srcru
  922. WHERE course_code='$course_code' and id_session = '" . $id_session . "'";
  923. $rs = Database::query($sql);
  924. while ($row = Database::fetch_array($rs)) {
  925. $a_students[$row['id_user']] = $row['id_user'];
  926. }
  927. }
  928. //////////////////////////////////////////////////////////////
  929. // Then, courses where $coach_id is coach of the session //
  930. //////////////////////////////////////////////////////////////
  931. $dsl_session_coach = 'SELECT id_coach FROM ' . $tbl_session . ' WHERE id="' . $id_session . '" AND id_coach="' . $coach_id . '"';
  932. $result = Database::query($dsl_session_coach);
  933. //He is the session_coach so we select all the users in the session
  934. if (Database::num_rows($result) > 0) {
  935. $sql = 'SELECT DISTINCT srcru.id_user FROM ' . $tbl_session_course_user . ' AS srcru WHERE id_session="' . $id_session . '"';
  936. $result = Database::query($sql);
  937. while ($row = Database::fetch_array($result)) {
  938. $a_students[$row['id_user']] = $row['id_user'];
  939. }
  940. }
  941. return $a_students;
  942. }
  943. /**
  944. * Check if a coach is allowed to follow a student
  945. * @param int Coach id
  946. * @param int Student id
  947. * @return bool
  948. */
  949. public static function is_allowed_to_coach_student($coach_id, $student_id) {
  950. $coach_id = intval($coach_id);
  951. $student_id = intval($student_id);
  952. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  953. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  954. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  955. //////////////////////////////////////////////////////////////
  956. // At first, courses where $coach_id is coach of the course //
  957. //////////////////////////////////////////////////////////////
  958. /*$sql = 'SELECT 1
  959. FROM ' . $tbl_session_course_user . ' AS session_course_user
  960. INNER JOIN ' . $tbl_session_course . ' AS session_course
  961. ON session_course.course_code = session_course_user.course_code
  962. AND id_coach=' . $coach_id . '
  963. WHERE id_user=' . $student_id;*/
  964. $sql = 'SELECT 1 FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id .' AND status=2';
  965. $result = Database::query($sql);
  966. if (Database::num_rows($result) > 0) {
  967. return true;
  968. }
  969. //////////////////////////////////////////////////////////////
  970. // Then, courses where $coach_id is coach of the session //
  971. //////////////////////////////////////////////////////////////
  972. $sql = 'SELECT session_course_user.id_user
  973. FROM ' . $tbl_session_course_user . ' as session_course_user
  974. INNER JOIN ' . $tbl_session_course . ' as session_course
  975. ON session_course.course_code = session_course_user.course_code
  976. INNER JOIN ' . $tbl_session . ' as session
  977. ON session.id = session_course.id_session
  978. AND session.id_coach = ' . $coach_id . '
  979. WHERE id_user = ' . $student_id;
  980. $result = Database::query($sql);
  981. if (Database::num_rows($result) > 0) {
  982. return true;
  983. }
  984. return false;
  985. }
  986. /**
  987. * Get courses followed by coach
  988. * @param int Coach id
  989. * @param int Session id (optional)
  990. * @return array Courses list
  991. */
  992. public static function get_courses_followed_by_coach($coach_id, $id_session = '')
  993. {
  994. $coach_id = intval($coach_id);
  995. if (!empty ($id_session))
  996. $id_session = intval($id_session);
  997. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  998. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  999. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1000. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  1001. //////////////////////////////////////////////////////////////
  1002. // At first, courses where $coach_id is coach of the course //
  1003. //////////////////////////////////////////////////////////////
  1004. $sql = 'SELECT DISTINCT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  1005. global $_configuration;
  1006. if ($_configuration['multiple_access_urls']) {
  1007. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1008. $access_url_id = api_get_current_access_url_id();
  1009. if ($access_url_id != -1){
  1010. $sql = 'SELECT DISTINCT scu.course_code FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_course_rel_access_url.' cru
  1011. ON (scu.course_code = cru.course_code)
  1012. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND cru.access_url_id = '.$access_url_id;
  1013. }
  1014. }
  1015. if (!empty ($id_session))
  1016. $sql .= ' AND id_session=' . $id_session;
  1017. $result = Database::query($sql);
  1018. while ($row = Database::fetch_array($result)) {
  1019. $a_courses[$row['course_code']] = $row['course_code'];
  1020. }
  1021. //////////////////////////////////////////////////////////////
  1022. // Then, courses where $coach_id is coach of the session //
  1023. //////////////////////////////////////////////////////////////
  1024. $sql = 'SELECT DISTINCT session_course.course_code
  1025. FROM ' . $tbl_session_course . ' as session_course
  1026. INNER JOIN ' . $tbl_session . ' as session
  1027. ON session.id = session_course.id_session
  1028. AND session.id_coach = ' . $coach_id . '
  1029. INNER JOIN ' . $tbl_course . ' as course
  1030. ON course.code = session_course.course_code';
  1031. if ($_configuration['multiple_access_urls']) {
  1032. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1033. $access_url_id = api_get_current_access_url_id();
  1034. if ($access_url_id != -1){
  1035. $sql = 'SELECT DISTINCT session_course.course_code
  1036. FROM ' . $tbl_session_course . ' as session_course
  1037. INNER JOIN ' . $tbl_session . ' as session
  1038. ON session.id = session_course.id_session
  1039. AND session.id_coach = ' . $coach_id . '
  1040. INNER JOIN ' . $tbl_course . ' as course
  1041. ON course.code = session_course.course_code
  1042. INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url
  1043. ON (session_course.course_code = course_rel_url.course_code)';
  1044. }
  1045. }
  1046. if (!empty ($id_session)) {
  1047. $sql .= ' WHERE session_course.id_session=' . $id_session;
  1048. if ($_configuration['multiple_access_urls'])
  1049. $sql .= ' AND access_url_id = '.$access_url_id;
  1050. } else {
  1051. if ($_configuration['multiple_access_urls'])
  1052. $sql .= ' WHERE access_url_id = '.$access_url_id;
  1053. }
  1054. $result = Database::query($sql);
  1055. while ($row = Database::fetch_array($result)) {
  1056. $a_courses[$row['course_code']] = $row['course_code'];
  1057. }
  1058. return $a_courses;
  1059. }
  1060. /**
  1061. * Get sessions coached by user
  1062. * @param int Coach id
  1063. * @return array Sessions list
  1064. */
  1065. public static function get_sessions_coached_by_user($coach_id) {
  1066. // table definition
  1067. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1068. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1069. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1070. // protect datas
  1071. $coach_id = intval($coach_id);
  1072. // session where we are general coach
  1073. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1074. FROM ' . $tbl_session . '
  1075. WHERE id_coach=' . $coach_id;
  1076. global $_configuration;
  1077. if ($_configuration['multiple_access_urls']) {
  1078. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1079. $access_url_id = api_get_current_access_url_id();
  1080. if ($access_url_id != -1){
  1081. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1082. FROM ' . $tbl_session . ' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1083. ON (session.id = session_rel_url.session_id)
  1084. WHERE id_coach=' . $coach_id.' AND access_url_id = '.$access_url_id;
  1085. }
  1086. }
  1087. $rs = Database::query($sql);
  1088. while ($row = Database::fetch_array($rs))
  1089. {
  1090. $a_sessions[$row["id"]] = $row;
  1091. }
  1092. // session where we are coach of a course
  1093. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1094. FROM ' . $tbl_session . ' as session
  1095. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1096. ON session.id = session_course_user.id_session
  1097. AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2';
  1098. global $_configuration;
  1099. if ($_configuration['multiple_access_urls']) {
  1100. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1101. $access_url_id = api_get_current_access_url_id();
  1102. if ($access_url_id != -1){
  1103. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1104. FROM ' . $tbl_session . ' as session
  1105. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1106. ON session.id = session_course_user.id_session AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2
  1107. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1108. ON (session.id = session_rel_url.session_id)
  1109. WHERE access_url_id = '.$access_url_id;
  1110. }
  1111. }
  1112. $rs = Database::query($sql);
  1113. while ($row = Database::fetch_array($rs))
  1114. {
  1115. $a_sessions[$row["id"]] = $row;
  1116. }
  1117. if (is_array($a_sessions)) {
  1118. foreach ($a_sessions as & $session) {
  1119. if ($session['date_start'] == '0000-00-00') {
  1120. $session['status'] = get_lang('SessionActive');
  1121. }
  1122. else {
  1123. $date_start = explode('-', $session['date_start']);
  1124. $time_start = mktime(0, 0, 0, $date_start[1], $date_start[2], $date_start[0]);
  1125. $date_end = explode('-', $session['date_end']);
  1126. $time_end = mktime(0, 0, 0, $date_end[1], $date_end[2], $date_end[0]);
  1127. if ($time_start < time() && time() < $time_end) {
  1128. $session['status'] = get_lang('SessionActive');
  1129. }
  1130. else{
  1131. if (time() < $time_start) {
  1132. $session['status'] = get_lang('SessionFuture');
  1133. }
  1134. else{
  1135. if (time() > $time_end) {
  1136. $session['status'] = get_lang('SessionPast');
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. return $a_sessions;
  1144. }
  1145. /**
  1146. * Get courses list from a session
  1147. * @param int Session id
  1148. * @return array Courses list
  1149. */
  1150. public static function get_courses_list_from_session($session_id) {
  1151. //protect datas
  1152. $session_id = intval($session_id);
  1153. // table definition
  1154. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1155. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1156. $sql = 'SELECT DISTINCT course_code
  1157. FROM ' . $tbl_session_course . '
  1158. WHERE id_session=' . $session_id;
  1159. $rs = Database::query($sql);
  1160. $a_courses = array ();
  1161. while ($row = Database::fetch_array($rs)) {
  1162. $a_courses[$row['course_code']] = $row;
  1163. }
  1164. return $a_courses;
  1165. }
  1166. /**
  1167. * Count the number of documents that an user has uploaded to a course
  1168. * @param int|array Student id(s)
  1169. * @param string Course code
  1170. * @param int Session id (optional), if param $session_id is null(default) return count of assignments including sessions, 0 = session is not filtered
  1171. * @return int Number of documents
  1172. */
  1173. public static function count_student_uploaded_documents($student_id, $course_code, $session_id = null){
  1174. // protect datas
  1175. $course_code = Database::escape_string($course_code);
  1176. // get the informations of the course
  1177. $a_course = CourseManager :: get_course_information($course_code);
  1178. if (!empty($a_course)) {
  1179. // table definition
  1180. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1181. $tbl_document = Database :: get_course_table(TABLE_DOCUMENT);
  1182. $course_id = $a_course['real_id'];
  1183. $condition_user = "";
  1184. if (is_array($student_id)) {
  1185. $condition_user = " AND ip.insert_user_id IN (".implode(',',$student_id).") ";
  1186. } else {
  1187. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  1188. }
  1189. $condition_session = "";
  1190. if (isset($session_id)) {
  1191. $session_id = intval($session_id);
  1192. $condition_session = " AND pub.session_id = $session_id ";
  1193. }
  1194. $sql = "SELECT count(ip.tool)
  1195. FROM $tbl_item_property ip INNER JOIN $tbl_document pub
  1196. ON ip.ref = pub.id
  1197. WHERE ip.c_id = $course_id AND
  1198. pub.c_id = $course_id AND
  1199. pub.filetype ='file' AND
  1200. ip.tool = 'document'
  1201. $condition_user $condition_session ";
  1202. $rs = Database::query($sql);
  1203. $row = Database::fetch_row($rs);
  1204. return $row[0];
  1205. }
  1206. return null;
  1207. }
  1208. /**
  1209. * Count assignments per student
  1210. * @param int|array Student id(s)
  1211. * @param string Course code
  1212. * @param int Session id (optional), if param $session_id is null(default) return count of assignments including sessions, 0 = session is not filtered
  1213. * @return int Count of assignments
  1214. */
  1215. public static function count_student_assignments($student_id, $course_code, $session_id = null) {
  1216. // protect datas
  1217. $course_code = Database::escape_string($course_code);
  1218. // get the informations of the course
  1219. $a_course = CourseManager :: get_course_information($course_code);
  1220. if (!empty($a_course)) {
  1221. // table definition
  1222. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1223. $tbl_student_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1224. $course_id = $a_course['real_id'];
  1225. $condition_user = "";
  1226. if (is_array($student_id)) {
  1227. $condition_user = " AND ip.insert_user_id IN (".implode(',',$student_id).") ";
  1228. } else {
  1229. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  1230. }
  1231. $condition_session = "";
  1232. if (isset($session_id)) {
  1233. $session_id = intval($session_id);
  1234. $condition_session = " AND pub.session_id = $session_id ";
  1235. }
  1236. $sql = "SELECT count(ip.tool)
  1237. FROM $tbl_item_property ip INNER JOIN $tbl_student_publication pub
  1238. ON ip.ref = pub.id
  1239. WHERE ip.c_id = $course_id AND
  1240. pub.c_id = $course_id AND
  1241. ip.tool='work'
  1242. $condition_user $condition_session ";
  1243. $rs = Database::query($sql);
  1244. $row = Database::fetch_row($rs);
  1245. return $row[0];
  1246. }
  1247. return null;
  1248. }
  1249. /**
  1250. * Count messages per student inside forum tool
  1251. * @param int Student id
  1252. * @param string Course code
  1253. * @param int Session id (optional), if param $session_id is null(default) return count of messages including sessions, 0 = session is not filtered
  1254. * @return int Count of messages
  1255. */
  1256. function count_student_messages($student_id, $course_code, $session_id = null) {
  1257. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  1258. // protect datas
  1259. $student_id = intval($student_id);
  1260. $course_code = addslashes($course_code);
  1261. // get the informations of the course
  1262. $a_course = CourseManager :: get_course_information($course_code);
  1263. if (!empty($a_course)) {
  1264. // table definition
  1265. $tbl_forum_post = Database :: get_course_table(TABLE_FORUM_POST);
  1266. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  1267. $course_id = $a_course['real_id'];
  1268. $condition_user = "";
  1269. if (is_array($student_id)) {
  1270. $condition_user = " AND post.poster_id IN (".implode(',',$student_id).") ";
  1271. } else {
  1272. $condition_user = " AND post.poster_id = '$student_id' ";
  1273. }
  1274. $condition_session = "";
  1275. if (isset($session_id)) {
  1276. $session_id = intval($session_id);
  1277. $condition_session = " AND forum.session_id = $session_id";
  1278. }
  1279. $sql = "SELECT 1 FROM $tbl_forum_post post INNER JOIN $tbl_forum forum
  1280. ON forum.forum_id = post.forum_id
  1281. WHERE post.c_id = $course_id AND
  1282. forum.c_id = $course_id
  1283. $condition_user $condition_session
  1284. ";
  1285. $rs = Database::query($sql);
  1286. return Database::num_rows($rs);
  1287. } else {
  1288. return null;
  1289. }
  1290. }
  1291. /**
  1292. * This function counts the number of post by course
  1293. * @param string Course code
  1294. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1295. * @return int The number of post by course
  1296. */
  1297. public static function count_number_of_posts_by_course($course_code, $session_id = null) {
  1298. //protect data
  1299. $course_code = Database::escape_string($course_code);
  1300. // get the informations of the course
  1301. $a_course = CourseManager :: get_course_information($course_code);
  1302. $count = 0;
  1303. if (!empty($a_course)) {
  1304. $tbl_posts = Database :: get_course_table(TABLE_FORUM_POST);
  1305. $tbl_forums = Database :: get_course_table(TABLE_FORUM);
  1306. $condition_session = '';
  1307. if (isset($session_id)) {
  1308. $session_id = intval($session_id);
  1309. $condition_session = ' AND f.session_id = '. $session_id;
  1310. }
  1311. $course_id = $a_course['real_id'];
  1312. $sql = "SELECT count(*) FROM $tbl_posts p INNER JOIN $tbl_forums f
  1313. ON f.forum_id = p.forum_id
  1314. WHERE p.c_id = $course_id AND
  1315. f.c_id = $course_id
  1316. $condition_session
  1317. ";
  1318. $result = Database::query($sql);
  1319. $row = Database::fetch_row($result);
  1320. $count = $row[0];
  1321. return $count;
  1322. } else {
  1323. return null;
  1324. }
  1325. }
  1326. /**
  1327. * This function counts the number of threads by course
  1328. * @param string Course code
  1329. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1330. * @return int The number of threads by course
  1331. */
  1332. public static function count_number_of_threads_by_course($course_code, $session_id = null) {
  1333. //protect data
  1334. $course_code = Database::escape_string($course_code);
  1335. // get the informations of the course
  1336. $a_course = CourseManager :: get_course_information($course_code);
  1337. $count = 0;
  1338. if (!empty($a_course['db_name'])) {
  1339. $tbl_threads = Database :: get_course_table(TABLE_FORUM_THREAD, $a_course['db_name']);
  1340. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1341. $condition_session = '';
  1342. if (isset($session_id)) {
  1343. $session_id = intval($session_id);
  1344. $condition_session = ' WHERE f.session_id = '. $session_id;
  1345. }
  1346. $sql = "SELECT count(*) FROM $tbl_threads t INNER JOIN $tbl_forums f ON f.forum_id = t.forum_id $condition_session ";
  1347. $result = Database::query($sql);
  1348. $row = Database::fetch_row($result);
  1349. $count = $row[0];
  1350. return $count;
  1351. } else {
  1352. return null;
  1353. }
  1354. }
  1355. /**
  1356. * This function counts the number of forums by course
  1357. * @param string Course code
  1358. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1359. * @return int The number of forums by course
  1360. */
  1361. public static function count_number_of_forums_by_course($course_code, $session_id = null) {
  1362. //protect data
  1363. $course_code = addslashes($course_code);
  1364. // get the informations of the course
  1365. $a_course = CourseManager :: get_course_information($course_code);
  1366. $count = 0;
  1367. if (!empty($a_course['db_name'])) {
  1368. $condition_session = '';
  1369. if (isset($session_id)) {
  1370. $session_id = intval($session_id);
  1371. $condition_session = ' WHERE session_id = '. $session_id;
  1372. }
  1373. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1374. $sql = "SELECT count(*) FROM $tbl_forums $condition_session";
  1375. $result = Database::query($sql);
  1376. $row = Database::fetch_row($result);
  1377. $count = $row[0];
  1378. return $count;
  1379. } else {
  1380. return null;
  1381. }
  1382. }
  1383. /**
  1384. * This function counts the chat last connections by course in x days
  1385. * @param string Course code
  1386. * @param int Last x days
  1387. * @param int Session id (optional)
  1388. * @return int Chat last connections by course in x days
  1389. */
  1390. public static function chat_connections_during_last_x_days_by_course($course_code,$last_days, $session_id = 0) {
  1391. //protect data
  1392. $last_days = intval($last_days);
  1393. $course_code = Database::escape_string($course_code);
  1394. $session_id = intval($session_id);
  1395. // get the informations of the course
  1396. $a_course = CourseManager :: get_course_information($course_code);
  1397. $count = 0;
  1398. if (!empty($a_course['db_name'])) {
  1399. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS, $a_course['db_name']);
  1400. $sql = "SELECT count(*) FROM $tbl_stats_access WHERE DATE_SUB(NOW(),INTERVAL $last_days DAY) <= access_date
  1401. AND access_cours_code = '$course_code' AND access_tool='".TOOL_CHAT."' AND access_session_id='$session_id' ";
  1402. $result = Database::query($sql);
  1403. $row = Database::fetch_row($result);
  1404. $count = $row[0];
  1405. return $count;
  1406. } else {
  1407. return null;
  1408. }
  1409. }
  1410. /**
  1411. * This function gets the last student's connection in chat
  1412. * @param int Student id
  1413. * @param string Course code
  1414. * @param int Session id (optional)
  1415. * @return string datetime formatted without day (e.g: February 23, 2010 10:20:50 )
  1416. */
  1417. public static function chat_last_connection($student_id, $course_code, $session_id = 0) {
  1418. //protect datas
  1419. $student_id = intval($student_id);
  1420. $course_code= Database::escape_string($course_code);
  1421. $session_id = intval($session_id);
  1422. $date_time = '';
  1423. // table definition
  1424. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1425. $sql = "SELECT access_date FROM $tbl_stats_access
  1426. WHERE access_tool='".TOOL_CHAT."' AND access_user_id='$student_id' AND access_cours_code = '$course_code' AND access_session_id = '$session_id' ORDER BY access_date DESC limit 1";
  1427. $rs = Database::query($sql);
  1428. if (Database::num_rows($rs) > 0) {
  1429. $row = Database::fetch_array($rs);
  1430. $date_time = api_convert_and_format_date($row['access_date'], null, date_default_timezone_get());
  1431. }
  1432. return $date_time;
  1433. }
  1434. /**
  1435. * Get count student's visited links
  1436. * @param int Student id
  1437. * @param string Course code
  1438. * @param int Session id (optional)
  1439. * @return int count of visited links
  1440. */
  1441. public static function count_student_visited_links($student_id, $course_code, $session_id = 0) {
  1442. // protect datas
  1443. $student_id = intval($student_id);
  1444. $course_code = Database::escape_string($course_code);
  1445. $session_id = intval($session_id);
  1446. // table definition
  1447. $tbl_stats_links = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1448. $sql = 'SELECT 1
  1449. FROM '.$tbl_stats_links.'
  1450. WHERE links_user_id= '.$student_id.'
  1451. AND links_cours_id = "'.$course_code.'"
  1452. AND links_session_id = '.$session_id.' ';
  1453. $rs = Database::query($sql);
  1454. return Database::num_rows($rs);
  1455. }
  1456. /**
  1457. * Get count student downloaded documents
  1458. * @param int Student id
  1459. * @param string Course code
  1460. * @param int Session id (optional)
  1461. * @return int Count downloaded documents
  1462. */
  1463. public static function count_student_downloaded_documents($student_id, $course_code, $session_id = 0) {
  1464. // protect datas
  1465. $student_id = intval($student_id);
  1466. $course_code = Database::escape_string($course_code);
  1467. $session_id = intval($session_id);
  1468. // table definition
  1469. $tbl_stats_documents = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1470. $sql = 'SELECT 1
  1471. FROM ' . $tbl_stats_documents . '
  1472. WHERE down_user_id = '.$student_id.'
  1473. AND down_cours_id = "'.$course_code.'"
  1474. AND down_session_id = '.$session_id.' ';
  1475. $rs = Database::query($sql);
  1476. return Database::num_rows($rs);
  1477. }
  1478. /**
  1479. * Get course list inside a session from a student
  1480. * @param int Student id
  1481. * @param int Session id (optional)
  1482. * @return array Courses list
  1483. */
  1484. public static function get_course_list_in_session_from_student($user_id, $id_session = 0) {
  1485. $user_id = intval($user_id);
  1486. $id_session = intval($id_session);
  1487. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1488. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user="' . $user_id . '" AND id_session="' . $id_session . '"';
  1489. $result = Database::query($sql);
  1490. $a_courses = array ();
  1491. while ($row = Database::fetch_array($result)) {
  1492. $a_courses[$row['course_code']] = $row['course_code'];
  1493. }
  1494. return $a_courses;
  1495. }
  1496. /**
  1497. * Get inactives students in course
  1498. * @param string Course code
  1499. * @param string Since login course date (optional, default = 'never')
  1500. * @param int Session id (optional)
  1501. * @return array Inactives users
  1502. */
  1503. public static function get_inactives_students_in_course($course_code, $since = 'never', $session_id=0) {
  1504. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1505. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1506. $table_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1507. $inner = '';
  1508. if($session_id!=0)
  1509. {
  1510. $inner = ' INNER JOIN '.$tbl_session_course_user.' session_course_user
  1511. ON stats_login.course_code = session_course_user.course_code
  1512. AND session_course_user.id_session = '.intval($session_id).'
  1513. AND session_course_user.id_user = stats_login.user_id ';
  1514. }
  1515. $sql = 'SELECT user_id, MAX(login_course_date) max_date FROM'.$tbl_track_login.' stats_login'.$inner.'
  1516. GROUP BY user_id
  1517. HAVING DATE_SUB( NOW(), INTERVAL '.$since.' DAY) > max_date ';
  1518. //HAVING DATE_ADD(max_date, INTERVAL '.$since.' DAY) < NOW() ';
  1519. if ($since == 'never') {
  1520. $sql = 'SELECT course_user.user_id FROM '.$table_course_rel_user.' course_user
  1521. LEFT JOIN '. $tbl_track_login.' stats_login
  1522. ON course_user.user_id = stats_login.user_id AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' '.
  1523. $inner.'
  1524. WHERE course_user.course_code = \''.Database::escape_string($course_code).'\'
  1525. AND stats_login.login_course_date IS NULL
  1526. GROUP BY course_user.user_id';
  1527. }
  1528. $rs = Database::query($sql);
  1529. $inactive_users = array();
  1530. while($user = Database::fetch_array($rs))
  1531. {
  1532. $inactive_users[] = $user['user_id'];
  1533. }
  1534. return $inactive_users;
  1535. }
  1536. /**
  1537. * Get count login per student
  1538. * @param int Student id
  1539. * @param string Course code
  1540. * @param int Session id (optional)
  1541. * @return int count login
  1542. */
  1543. public static function count_login_per_student($student_id, $course_code, $session_id = 0) {
  1544. $student_id = intval($student_id);
  1545. $course_code = Database::escape_string($course_code);
  1546. $session_id = intval($session_id);
  1547. $tbl_course_rel_user = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1548. $sql = 'SELECT '.$student_id.'
  1549. FROM ' . $tbl_course_rel_user . '
  1550. WHERE access_user_id=' . $student_id . '
  1551. AND access_cours_code="' . $course_code . '" AND access_session_id = "'.$session_id.'" ';
  1552. $rs = Database::query($sql);
  1553. $nb_login = Database::num_rows($rs);
  1554. return $nb_login;
  1555. }
  1556. /**
  1557. * Get students followed by a human resources manager
  1558. * @param int Drh id
  1559. * @return array Student list
  1560. */
  1561. public static function get_student_followed_by_drh($hr_dept_id) {
  1562. $hr_dept_id = intval($hr_dept_id);
  1563. $a_students = array ();
  1564. $tbl_organism = Database :: get_main_table(TABLE_MAIN_ORGANISM);
  1565. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1566. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  1567. WHERE hr_dept_id='.$hr_dept_id;
  1568. $rs = Database::query($sql);
  1569. while($user = Database :: fetch_array($rs))
  1570. {
  1571. $a_students[$user['user_id']] = $user['user_id'];
  1572. }
  1573. return $a_students;
  1574. }
  1575. /**
  1576. * Gets the average of test and scorm inside a learning path
  1577. * @param int User id
  1578. * @param string Course id
  1579. * @return float average of test
  1580. * @author isaac flores paz
  1581. * @deprecated get_avg_student_score should be use
  1582. */
  1583. public static function get_average_test_scorm_and_lp ($user_id,$course_id) {
  1584. //the score inside the Reporting table
  1585. $course_info = api_get_course_info($course_id);
  1586. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course_info['dbName']);
  1587. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course_info['dbName']);
  1588. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course_info['dbName']);
  1589. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_info['dbName']);
  1590. $sql_type='SELECT id, lp_type FROM '.$lp_table;
  1591. $rs_type=Database::query($sql_type);
  1592. $average_data=0;
  1593. $count_loop=0;
  1594. $lp_list = array();
  1595. while ($row_type=Database::fetch_array($rs_type)) {
  1596. $lp_list[] = $row_type['id'];
  1597. if ($row_type['lp_type']==1) {
  1598. //lp chamilo
  1599. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1600. $rs_last_lp_view_id = Database::query($sql);
  1601. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1602. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,(liw.score/li.max_score) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1603. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND li.item_type="quiz" AND liw.lp_view_id="'.$lp_view_id.'"';
  1604. $sum=0;
  1605. $tot=0;
  1606. $rs_list_view1=Database::query($sql_list_view);
  1607. while ($row_list_view=Database::fetch_array($rs_list_view1)) {
  1608. $sum=$sum+$row_list_view['sum_data'];
  1609. $tot++;
  1610. }
  1611. if ($tot==0) {
  1612. $tot=1;
  1613. }
  1614. $average_data1=$sum/$tot;
  1615. $sql_list_view='';
  1616. $rs_last_lp_view_id='';
  1617. } elseif ($row_type['lp_type']==2) {
  1618. //lp scorm
  1619. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1620. $rs_last_lp_view_id = Database::query($sql);
  1621. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1622. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,((liw.score/li.max_score)*100) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1623. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND (li.item_type="sco" OR li.item_type="quiz") AND liw.lp_view_id="'.$lp_view_id.'"';
  1624. $tot=0;
  1625. $sum=0;
  1626. $rs_list_view2=Database::query($sql_list_view);
  1627. while ($row_list_view=Database::fetch_array($rs_list_view2)) {
  1628. $sum=$sum+$row_list_view['sum_data'];
  1629. $tot++;
  1630. }
  1631. if ($tot==0) {
  1632. $tot=1;
  1633. }
  1634. $average_data2=$sum/$tot;
  1635. }
  1636. $average_data_sum=$average_data_sum+$average_data1+$average_data2;
  1637. $average_data2=0;
  1638. $average_data1=0;
  1639. $count_loop++;
  1640. }
  1641. //We only count the LP that have an exercise to get the average
  1642. $lp_with_quiz = 0;
  1643. foreach($lp_list as $lp_id) {
  1644. //check if LP have a score
  1645. $sql = "SELECT count(id) as count FROM $lp_item_table
  1646. WHERE item_type = 'quiz' AND lp_id = ".$lp_id." ";
  1647. $result_have_quiz = Database::query($sql);
  1648. if (Database::num_rows($result_have_quiz) > 0 ) {
  1649. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  1650. if (is_numeric($row['count']) && $row['count'] != 0) {
  1651. $lp_with_quiz++;
  1652. }
  1653. }
  1654. }
  1655. if ($lp_with_quiz > 0) {
  1656. $avg_student_score = round(($average_data_sum / $lp_with_quiz * 100), 2);
  1657. }
  1658. return $avg_student_score;
  1659. }
  1660. /**
  1661. * get count clicks about tools most used by course
  1662. * @param string Course code
  1663. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1664. * @return array tools data
  1665. */
  1666. public static function get_tools_most_used_by_course($course_code, $session_id = null) {
  1667. //protect data
  1668. $course_code = Database::escape_string($course_code);
  1669. $data = array();
  1670. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1671. $condition_session = '';
  1672. if (isset($session_id)) {
  1673. $session_id = intval($session_id);
  1674. $condition_session = ' AND access_session_id = '. $session_id;
  1675. }
  1676. $sql = "SELECT access_tool, COUNT(DISTINCT access_user_id),count( access_tool ) as count_access_tool
  1677. FROM $TABLETRACK_ACCESS
  1678. WHERE access_tool IS NOT NULL AND access_tool != '' AND access_cours_code = '$course_code' $condition_session
  1679. GROUP BY access_tool
  1680. ORDER BY count_access_tool DESC
  1681. LIMIT 0, 3";
  1682. $rs = Database::query($sql);
  1683. if (Database::num_rows($rs) > 0) {
  1684. while ($row = Database::fetch_array($rs)) {
  1685. $data[] = $row;
  1686. }
  1687. }
  1688. return $data;
  1689. }
  1690. /**
  1691. * get documents most downloaded by course
  1692. * @param string Course code
  1693. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1694. * @param int Limit (optional, default = 0, 0 = without limit)
  1695. * @return array documents downloaded
  1696. */
  1697. public static function get_documents_most_downloaded_by_course($course_code, $session_id = null, $limit = 0) {
  1698. //protect data
  1699. $course_code = Database::escape_string($course_code);
  1700. $data = array();
  1701. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1702. $condition_session = '';
  1703. if (isset($session_id)) {
  1704. $session_id = intval($session_id);
  1705. $condition_session = ' AND down_session_id = '. $session_id;
  1706. }
  1707. $sql = "SELECT down_doc_path, COUNT(DISTINCT down_user_id), COUNT(down_doc_path) as count_down
  1708. FROM $TABLETRACK_DOWNLOADS
  1709. WHERE down_cours_id = '$course_code'
  1710. $condition_session
  1711. GROUP BY down_doc_path
  1712. ORDER BY count_down DESC
  1713. LIMIT 0, $limit";
  1714. $rs = Database::query($sql);
  1715. if (Database::num_rows($rs) > 0) {
  1716. while ($row = Database::fetch_array($rs)) {
  1717. $data[] = $row;
  1718. }
  1719. }
  1720. return $data;
  1721. }
  1722. /**
  1723. * get links most visited by course
  1724. * @param string Course code
  1725. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1726. * @return array links most visited
  1727. */
  1728. public static function get_links_most_visited_by_course($course_code, $session_id = null) {
  1729. //protect data
  1730. $course_code = Database::escape_string($course_code);
  1731. $course_info = api_get_course_info($course_code);
  1732. $data = array();
  1733. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1734. $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK, $course_info['dbName']);
  1735. $condition_session = '';
  1736. if (isset($session_id)) {
  1737. $session_id = intval($session_id);
  1738. $condition_session = ' AND cl.session_id = '.$session_id;
  1739. }
  1740. $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title) as count_visits
  1741. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  1742. WHERE sl.links_link_id = cl.id
  1743. AND sl.links_cours_id = '$course_code'
  1744. $condition_session
  1745. GROUP BY cl.title, cl.url
  1746. ORDER BY count_visits DESC
  1747. LIMIT 0, 3";
  1748. $rs = Database::query($sql);
  1749. if (Database::num_rows($rs) > 0) {
  1750. while ($row = Database::fetch_array($rs)) {
  1751. $data[] = $row;
  1752. }
  1753. }
  1754. return $data;
  1755. }
  1756. /**
  1757. * Shows the user progress (when clicking in the Progress tab)
  1758. * @param int user id
  1759. * @return string html code
  1760. */
  1761. function show_user_progress($user_id, $session_id = 0, $extra_params = '', $show_courses = true) {
  1762. require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
  1763. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  1764. global $_configuration;
  1765. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  1766. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1767. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1768. $tbl_access_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1769. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1770. $tbl_access_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1771. $tbl_access_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1772. $user_id = intval($user_id);
  1773. // get course list
  1774. if ($_configuration['multiple_access_urls']) {
  1775. $sql = 'SELECT cu.course_code as code, title
  1776. FROM '.$tbl_course_user.' cu INNER JOIN '.$tbl_access_rel_course.' a ON(a.course_code = cu.course_code) INNER JOIN '.$tbl_course.' c ON( cu.course_code = c.code)
  1777. WHERE user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND access_url_id = '.api_get_current_access_url_id().' ORDER BY title ';
  1778. } else {
  1779. $sql = 'SELECT course_code as code, title FROM '.$tbl_course_user.' u INNER JOIN '.$tbl_course.' c ON(course_code = c.code)
  1780. WHERE u.user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' ORDER BY title ';
  1781. }
  1782. $rs = Database::query($sql);
  1783. $courses = $course_in_session = $temp_course_in_session = array();
  1784. $courses = array();
  1785. while($row = Database :: fetch_array($rs, 'ASSOC')) {
  1786. $courses[$row['code']] = $row['title'];
  1787. }
  1788. /*
  1789. if (!empty($courses)) {
  1790. //sort need to be improved
  1791. sort($courses);
  1792. }*/
  1793. // Get the list of sessions where the user is subscribed as student
  1794. if ($_configuration['multiple_access_urls']) {
  1795. $sql = 'SELECT DISTINCT cu.course_code, id_session as session_id, name FROM '.$tbl_session_course_user.' cu INNER JOIN '.$tbl_access_rel_session.' a ON(a.session_id = cu.id_session) INNER JOIN '.$tbl_session.' s ON(s.id = a.session_id)
  1796. WHERE id_user='.$user_id.' AND access_url_id = '.api_get_current_access_url_id().' ORDER BY name ';
  1797. } else {
  1798. $sql = 'SELECT DISTINCT course_code, id_session as session_id, name FROM '.$tbl_session_course_user.' u INNER JOIN '.$tbl_session.' s ON(s.id = u.id_session)
  1799. WHERE id_user='.$user_id.' ORDER BY name ';
  1800. }
  1801. $rs = Database::query($sql);
  1802. $simple_session_array = array();
  1803. while ($row = Database :: fetch_array($rs)) {
  1804. $course_info = CourseManager::get_course_information($row['course_code']);
  1805. $temp_course_in_session[$row['session_id']]['course_list'][$course_info['id']] = $course_info;
  1806. $temp_course_in_session[$row['session_id']]['name'] = $row['name'];
  1807. $simple_session_array[$row['session_id']] = $row['name'];
  1808. }
  1809. foreach($simple_session_array as $my_session_id => $session_name) {
  1810. $course_list = $temp_course_in_session[$my_session_id]['course_list'];
  1811. $my_course_data = array();
  1812. foreach($course_list as $course_data) {
  1813. $my_course_data[$course_data['id']] = $course_data['title'];
  1814. }
  1815. $my_course_data = utf8_sort($my_course_data);
  1816. $final_course_data = array();
  1817. foreach($my_course_data as $course_id => $value) {
  1818. $final_course_data[$course_id] = $course_list[$course_id];
  1819. }
  1820. $course_in_session[$my_session_id]['course_list'] = $final_course_data;
  1821. $course_in_session[$my_session_id]['name'] = $session_name;
  1822. }
  1823. $html = '';
  1824. // Course list
  1825. if ($show_courses) {
  1826. if (!empty($courses)) {
  1827. $html .= Display::tag('h1', Display::return_icon('course.png', get_lang('MyCourses'), array(), 22).' '.get_lang('MyCourses'));
  1828. $html .= '<table class="data_table" width="100%">';
  1829. $html .= '<tr>
  1830. '.Display::tag('th', get_lang('Course'), array('width'=>'300px')).'
  1831. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  1832. '.Display::tag('th', get_lang('Progress'), array('class'=>'head')).'
  1833. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array('align' => 'absmiddle', 'hspace' => '3px')),array('class'=>'head')).'
  1834. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  1835. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  1836. </tr>';
  1837. foreach ($courses as $course_code => $course_title) {
  1838. $weighting = 0;
  1839. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $course_code);
  1840. $time = api_time_to_hms($total_time_login);
  1841. $progress = Tracking :: get_avg_student_progress($user_id, $course_code);
  1842. $percentage_score = Tracking :: get_avg_student_score($user_id, $course_code, array());
  1843. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $course_code);
  1844. if ($course_code == $_GET['course'] && empty($_GET['session_id'])) {
  1845. $html .= '<tr class="row_odd" style="background-color:#FBF09D">';
  1846. } else {
  1847. $html .= '<tr class="row_even">';
  1848. }
  1849. $url = api_get_course_url($course_code, $session_id);
  1850. $course_url = Display::url($course_title, $url, array('target'=>SESSION_LINK_TARGET));
  1851. $html .= '<td>'.$course_url.'</td>';
  1852. $html .= '<td align="center">'.$time.'</td>';
  1853. $html .= '<td align="center">'.$progress.'%</td>';
  1854. $html .= '<td align="center">';
  1855. if (is_numeric($percentage_score)) {
  1856. $html .= $percentage_score.'%';
  1857. } else {
  1858. $html .= '0%';
  1859. }
  1860. $html .= '</td>';
  1861. $html .= '<td align="center">'.$last_connection.'</td>';
  1862. $html .= '<td align="center">';
  1863. if ($course_code == $_GET['course'] && empty($_GET['session_id'])) {
  1864. $html .= '<a href="#">';
  1865. $html .= Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  1866. } else {
  1867. $html .= '<a href="'.api_get_self().'?course='.$course_code.$extra_params.'">';
  1868. $html .= Display::return_icon('2rightarrow.gif', get_lang('Details'));
  1869. }
  1870. $html .= '</a>';
  1871. $html .= '</td></tr>';
  1872. }
  1873. $html .= '</table>';
  1874. }
  1875. }
  1876. // Session list
  1877. if (!empty($course_in_session)) {
  1878. $main_session_graph = '';
  1879. if (!isset($_GET['session_id']) && !isset($_GET['course'])) {
  1880. //Load graphics only when calling to an specific session
  1881. $session_graph = array();
  1882. $all_exercise_graph_name_list = array();
  1883. $all_user_results = array();
  1884. $all_exercise_graph_list = array();
  1885. $all_exercise_start_time = array();
  1886. foreach ($course_in_session as $my_session_id => $session_data) {
  1887. $course_list = $session_data['course_list'];
  1888. $session_name = $session_data['name'];
  1889. $user_count = count(SessionManager::get_users_by_session($my_session_id));
  1890. $exercise_graph_name_list = array();
  1891. $user_results = array();
  1892. $exercise_graph_list = array();
  1893. foreach ($course_list as $course_data) {
  1894. $exercise_list = get_all_exercises($course_data, $my_session_id);
  1895. foreach($exercise_list as $exercise_data) {
  1896. $exercise_obj = new Exercise($course_data['id']);
  1897. $exercise_obj->read($exercise_data['id']);
  1898. if ($exercise_obj->is_visible()) {
  1899. $best_average = intval(get_best_average_score_by_exercise($exercise_data['id'], $course_data['code'], $my_session_id, $user_count));
  1900. $exercise_graph_list[] = $best_average;
  1901. $all_exercise_graph_list[] = $best_average;
  1902. $user_result_data = get_best_attempt_by_user(api_get_user_id(), $exercise_data['id'], $course_data['code'], $my_session_id);
  1903. $score = 0;
  1904. if (!empty($user_result_data['exe_weighting']) && intval($user_result_data['exe_weighting']) != 0) {
  1905. $score = intval($user_result_data['exe_result']/$user_result_data['exe_weighting'] * 100);
  1906. }
  1907. //$user_results[] = 100;
  1908. $user_results[] = $score;
  1909. $time = api_strtotime($exercise_data['start_time']) ? api_strtotime($exercise_data['start_time']) : 0;
  1910. $all_exercise_start_time[] = $time ;
  1911. $all_user_results[] = $score;
  1912. if (count($exercise_list)<=10) {
  1913. $title = cut($course_data['title'], 30)." \n ".cut($exercise_data['title'], 30);
  1914. $exercise_graph_name_list[]= $title;
  1915. $all_exercise_graph_name_list[] = $title;
  1916. } else {
  1917. // if there are more than 10 results, space becomes difficult to find, so only show the title of the exercise, not the tool
  1918. $title = cut($exercise_data['title'], 30);
  1919. $exercise_graph_name_list[]= $title;
  1920. $all_exercise_graph_name_list[]= $title;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. //Graph per session
  1926. if (!empty($user_results) && !empty($exercise_graph_list)) {
  1927. //$session_graph[$my_session_id] = self::generate_session_exercise_graph($exercise_graph_name_list, $user_results, $exercise_graph_list);
  1928. }
  1929. }
  1930. //Complete graph
  1931. if (!empty($all_user_results) && !empty($all_exercise_graph_list)) {
  1932. asort($all_exercise_start_time);
  1933. //Fix exams order
  1934. $final_all_exercise_graph_name_list = array();
  1935. $final_all_user_results = array();
  1936. $final_all_exercise_graph_list = array();
  1937. foreach($all_exercise_start_time as $key => $time) {
  1938. $label_time = '';
  1939. if (!empty($time)) {
  1940. $label_time = date('d-m-y', $time);
  1941. //$label_time = api_format_date($time, DATE_FORMAT_NUMBER);
  1942. }
  1943. $final_all_exercise_graph_name_list[] = $all_exercise_graph_name_list[$key].' '.$label_time;
  1944. $final_all_user_results[] = $all_user_results[$key];
  1945. $final_all_exercise_graph_list[] = $all_exercise_graph_list[$key];
  1946. }
  1947. //var_dump($final_all_exercise_graph_name_list, $final_all_user_results, $final_all_exercise_graph_list);
  1948. $main_session_graph = self::generate_session_exercise_graph($final_all_exercise_graph_name_list, $final_all_user_results, $final_all_exercise_graph_list);
  1949. }
  1950. }
  1951. $html .= Display::tag('h1',Display::return_icon('session.png', get_lang('Sessions'), array(), 22).' '.get_lang('Sessions'));
  1952. $html .= '<table class="data_table" width="100%">';
  1953. //'.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  1954. $html .= '<tr>
  1955. '.Display::tag('th', get_lang('Session'), array('width'=>'300px')).'
  1956. '.Display::tag('th', get_lang('PublishedExercises'), array('width'=>'300px')).'
  1957. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  1958. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  1959. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  1960. </tr>';
  1961. foreach ($course_in_session as $my_session_id => $session_data) {
  1962. $course_list = $session_data['course_list'];
  1963. $session_name = $session_data['name'];
  1964. if (isset($session_id) && !empty($session_id)) {
  1965. if ($session_id != $my_session_id) {
  1966. continue;
  1967. }
  1968. }
  1969. $all_exercises = 0;
  1970. $all_unanswered_exercises_by_user = 0;
  1971. $all_done_exercise = 0;
  1972. $all_average = 0;
  1973. $stats_array = array();
  1974. foreach($course_list as $course_data) {
  1975. //All exercises in the course @todo change for a real count
  1976. $exercises = get_all_exercises($course_data, $my_session_id);
  1977. $count_exercises = 0;
  1978. if (!empty($exercises)) {
  1979. $count_exercises = count($exercises);
  1980. }
  1981. //Count of user results
  1982. //$done_exercises = get_count_exercises_attempted_by_course($course_data['code'], $my_session_id);
  1983. $done_exercises = null;
  1984. $answered_exercises = 0;
  1985. if (!empty($exercises)) {
  1986. foreach($exercises as $exercise_item) {
  1987. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercise_item['id'], $course_data['code'], $my_session_id);
  1988. if ($attempts > 1) {
  1989. $answered_exercises++;
  1990. }
  1991. }
  1992. }
  1993. //Average
  1994. $average = get_average_score_by_course($course_data['code'], $my_session_id);
  1995. $all_exercises += $count_exercises;
  1996. $all_unanswered_exercises_by_user += $count_exercises - $answered_exercises;
  1997. //$all_done_exercise += $done_exercises;
  1998. $all_average += $average;
  1999. //$stats_array[$course_data['code']] = array('exercises'=>$count_exercises, 'unanswered_exercises_by_user'=>$answered_exercises,'done_exercises'=>$done_exercises, 'average'=>$average);
  2000. }
  2001. $all_average = $all_average / count($course_list);
  2002. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  2003. $html .= '<tr style="background-color:#FBF09D">';
  2004. } else {
  2005. $html .= '<tr>';
  2006. }
  2007. $url = api_get_path(WEB_CODE_PATH)."session/?session_id={$my_session_id}";
  2008. $html .= Display::tag('td', Display::url($session_name, $url, array('target'=>SESSION_LINK_TARGET)));
  2009. $html .= Display::tag('td', $all_exercises);
  2010. $html .= Display::tag('td', $all_unanswered_exercises_by_user);
  2011. //$html .= Display::tag('td', $all_done_exercise);
  2012. $html .= Display::tag('td', convert_to_percentage($all_average));
  2013. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  2014. $icon = Display::url(Display::return_icon('2rightarrow_na.gif', get_lang('Details')), '?session_id='.$my_session_id);
  2015. } else {
  2016. $icon = Display::url(Display::return_icon('2rightarrow.gif', get_lang('Details')), '?session_id='.$my_session_id);
  2017. }
  2018. $html .= Display::tag('td', $icon);
  2019. $html .= '</tr>';
  2020. }
  2021. $html .= '</table><br />';
  2022. $html .= Display::div($main_session_graph, array('id'=>'session_graph','class'=>'chart-session', 'style'=>'position:relative; text-align: center;') );
  2023. //Checking selected session
  2024. if (isset($_GET['session_id'])) {
  2025. $session_id_from_get = intval($_GET['session_id']);
  2026. $session_data = $course_in_session[$session_id_from_get];
  2027. $course_list = $session_data['course_list'];
  2028. $html .= Display::tag('h2',$session_data['name'].' - '.get_lang('CourseList'));
  2029. $html .= '<table class="data_table" width="100%">';
  2030. //'.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  2031. $html .= '
  2032. <tr>
  2033. <th width="300px">'.get_lang('Course').'</th>
  2034. '.Display::tag('th', get_lang('PublishedExercises'), array('class'=>'head')).'
  2035. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  2036. '.Display::tag('th', get_lang('MyAverage'), array('class'=>'head')).'
  2037. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  2038. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  2039. '.Display::tag('th', get_lang('LPProgress') , array('class'=>'head')).'
  2040. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array ('align' => 'absmiddle', 'hspace' => '3px')), array('class'=>'head')).'
  2041. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  2042. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  2043. </tr>';
  2044. foreach ($course_list as $course_data) {
  2045. $course_code = $course_data['code'];
  2046. $course_title = $course_data['title'];
  2047. //All exercises in the course @todo change for a real count
  2048. $exercises = get_all_exercises($course_data, $session_id_from_get);
  2049. $count_exercises = 0;
  2050. if (!empty($exercises)) {
  2051. $count_exercises = count($exercises);
  2052. }
  2053. //Count of user results
  2054. //$done_exercises = get_best_exercise_results_by_course($course_code, $session_id_from_get);
  2055. //From course exercises NOT from LP exercises!!!
  2056. //$done_exercises = get_count_exercises_attempted_by_course($course_code, $session_id_from_get);
  2057. $answered_exercises = 0;
  2058. foreach($exercises as $exercise_item) {
  2059. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercise_item['id'], $course_code, $session_id_from_get);
  2060. if ($attempts > 1) {
  2061. $answered_exercises++;
  2062. }
  2063. }
  2064. $unanswered_exercises = $count_exercises - $answered_exercises;
  2065. //Average
  2066. $average = get_average_score_by_course($course_code, $session_id_from_get);
  2067. $my_average = get_average_score_by_course_by_user(api_get_user_id(), $course_code, $session_id_from_get);
  2068. $stats_array[$course_code] = array( 'exercises' => $count_exercises,
  2069. 'unanswered_exercises_by_user' => $unanswered_exercises,
  2070. 'done_exercises' => $done_exercises,
  2071. 'average' => $average,
  2072. 'my_average' => $my_average);
  2073. $weighting = 0;
  2074. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $course_code, $session_id_from_get);
  2075. $progress = Tracking :: get_avg_student_progress($user_id, $course_code,array(), $session_id_from_get);
  2076. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $course_code, $session_id_from_get);
  2077. $time = api_time_to_hms($total_time_login);
  2078. $percentage_score = Tracking :: get_avg_student_score($user_id, $course_code, array(), $session_id_from_get);
  2079. if ($course_code == $_GET['course'] && $_GET['session_id'] == $session_id_from_get) {
  2080. $html .= '<tr class="row_odd" style="background-color:#FBF09D" >';
  2081. } else {
  2082. $html .= '<tr class="row_even">';
  2083. }
  2084. $url = api_get_course_url($course_code, $session_id_from_get);
  2085. $course_url = Display::url($course_title, $url, array('target'=>SESSION_LINK_TARGET));
  2086. $html .= Display::tag('td', $course_url);
  2087. $html .= Display::tag('td', $stats_array[$course_code]['exercises']);
  2088. $html .= Display::tag('td', $stats_array[$course_code]['unanswered_exercises_by_user']);
  2089. //$html .= Display::tag('td', $stats_array[$course_code]['done_exercises']);
  2090. $html .= Display::tag('td', convert_to_percentage($stats_array[$course_code]['my_average']));
  2091. $html .= Display::tag('td', $stats_array[$course_code]['average'] == 0 ? '-' : '('.convert_to_percentage($stats_array[$course_code]['average']).')');
  2092. $html .= Display::tag('td', $time, array('align'=>'center'));
  2093. if (is_numeric($progress)) {
  2094. $progress = $progress.'%';
  2095. } else {
  2096. $progress = '0%';
  2097. }
  2098. //Progress
  2099. $html .= Display::tag('td', $progress, array('align'=>'center'));
  2100. if (is_numeric($percentage_score)) {
  2101. $percentage_score = $percentage_score.'%';
  2102. } else {
  2103. $percentage_score = '0%';
  2104. }
  2105. //Score
  2106. $html .= Display::tag('td', $percentage_score, array('align'=>'center'));
  2107. $html .= Display::tag('td', $last_connection, array('align'=>'center'));
  2108. if ($course_code == $_GET['course'] && $_GET['session_id'] == $session_id_from_get) {
  2109. $details = '<a href="#">';
  2110. $details .=Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  2111. } else {
  2112. $details = '<a href="'.api_get_self().'?course='.$course_code.'&session_id='.$session_id_from_get.$extra_params.'">';
  2113. $details .=Display::return_icon('2rightarrow.gif', get_lang('Details'));
  2114. }
  2115. $details .= '</a>';
  2116. $html .= Display::tag('td', $details, array('align'=>'center'));
  2117. $html .= '</tr>';
  2118. }
  2119. $html .= '</table>';
  2120. if (!empty($session_graph[$session_id_from_get])) {
  2121. //$html .= Display::div($session_graph[$session_id_from_get], array('id'=>'session_graph','class'=>'chart-session', 'style'=>'position:relative; text-align: center;') );
  2122. }
  2123. }
  2124. }
  2125. if (!empty($html)) {
  2126. $html = Display::div($html, array('class'=>'rounded_div', 'style'=>'position:relative; float:none; width:95%'));
  2127. }
  2128. return $html;
  2129. }
  2130. /**
  2131. * Shows the user detail progress (when clicking in the details link)
  2132. * @param int user id
  2133. * @param string course code
  2134. * @param int session id
  2135. * @return string html code
  2136. */
  2137. function show_course_detail($user_id, $course_code, $session_id) {
  2138. $html = '';
  2139. if (isset($course_code)) {
  2140. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  2141. $user_id = intval($user_id);
  2142. $session_id = intval($session_id);
  2143. $course = Database::escape_string($course_code);
  2144. $course_info = CourseManager::get_course_information($course);
  2145. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  2146. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2147. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  2148. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2149. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $course_info['db_name']);
  2150. $tbl_course_lp_view_item = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $course_info['db_name']);
  2151. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $course_info['db_name']);
  2152. $tbl_course_lp_item = Database :: get_course_table(TABLE_LP_ITEM, $course_info['db_name']);
  2153. $tbl_course_quiz = Database :: get_course_table(TABLE_QUIZ_TEST, $course_info['db_name']);
  2154. $session_name = api_get_session_name($session_id);
  2155. $html .= Display::tag('h2', $course_info['title']);
  2156. $html .= '<table class="data_table" width="100%">';
  2157. // This code was commented on purpose see BT#924
  2158. /*$sql = 'SELECT visibility FROM '.$course_info['db_name'].'.'.TABLE_TOOL_LIST.' WHERE name="quiz"';
  2159. $result_visibility_tests = Database::query($sql);
  2160. if (Database::result($result_visibility_tests, 0, 'visibility') == 1) {*/
  2161. //Course details
  2162. $html .= '<tr>
  2163. <th class="head" style="color:#000">'.get_lang('Exercices').'</th>
  2164. <th class="head" style="color:#000">'.get_lang('Attempts').'</th>
  2165. <th class="head" style="color:#000">'.get_lang('BestAttempt').'</th>
  2166. <th class="head" style="color:#000">'.get_lang('Ranking').'</th>
  2167. <th class="head" style="color:#000">'.get_lang('BestResultInCourse').'</th>
  2168. <th class="head" style="color:#000">'.get_lang('Statistics').' '.Display :: return_icon('info3.gif', get_lang('OnlyBestResultsPerStudent'), array('align' => 'absmiddle', 'hspace' => '3px')).'</th>
  2169. </tr>';
  2170. if (empty($session_id)) {
  2171. $sql_exercices = "SELECT quiz.title,id, results_disabled FROM ".$tbl_course_quiz." AS quiz WHERE active='1' AND session_id = 0";
  2172. } else {
  2173. $sql_exercices = "SELECT quiz.title,id, results_disabled FROM ".$tbl_course_quiz." AS quiz WHERE active='1'";
  2174. }
  2175. $result_exercices = Database::query($sql_exercices);
  2176. $to_graph_exercise_result = array();
  2177. if (empty($session_id)) {
  2178. $user_list = UserManager::get_user_list();
  2179. } else {
  2180. $user_list = SessionManager::get_users_by_session($session_id);
  2181. }
  2182. if (Database::num_rows($result_exercices) > 0) {
  2183. $score = $weighting = $exe_id = 0;
  2184. while ($exercices = Database::fetch_array($result_exercices)) {
  2185. //if ($exercices['id'] != 3) continue;
  2186. $score = $weighting = $attempts = 0;
  2187. //Getting count of attempts by user
  2188. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);
  2189. //For graphics
  2190. $best_exercise_stats = get_best_exercise_results_by_user($exercices['id'], $course_info['code'], $session_id);
  2191. $to_graph_exercise_result[$exercices['id']] = array('title'=>$exercices['title'], 'data'=>$best_exercise_stats);
  2192. $html .= '<tr class="row_even">';
  2193. $url = api_get_path(WEB_CODE_PATH)."exercice/overview.php?cidReq={$course_info['code']}&id_session=$session_id&exerciseId={$exercices['id']}";
  2194. $exercices['title'] = Display::url($exercices['title'], $url, array('target'=>SESSION_LINK_TARGET));
  2195. $html .= Display::tag('td', $exercices['title']);
  2196. //Exercise configuration show results show results or show only score
  2197. if ($exercices['results_disabled'] == 0 || $exercices['results_disabled'] == 2) {
  2198. $latest_attempt_url = '';
  2199. $best_score = $position = $percentage_score_result = '-';
  2200. $graph = $normal_graph = null;
  2201. //Getting best results
  2202. $best_score_data = get_best_attempt_in_course($exercices['id'], $course_info['code'], $session_id);
  2203. $best_score = show_score($best_score_data['exe_result'], $best_score_data['exe_weighting']);
  2204. if ($attempts > 0) {
  2205. $exercise_stat = get_best_attempt_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);
  2206. if (!empty($exercise_stat)) {
  2207. //Always getting the BEST attempt
  2208. $score = $exercise_stat['exe_result'];
  2209. $weighting = $exercise_stat['exe_weighting'];
  2210. $exe_id = $exercise_stat['exe_id'];
  2211. //$latest_attempt_url .= '<a href="../exercice/exercise_show.php?origin=myprogress&id='.$exe_id.'&cidReq='.$course_info['code'].'&id_session='.$session_id.'"> '.Display::return_icon('quiz.gif', get_lang('Quiz')).' </a>';
  2212. $latest_attempt_url .= '../exercice/exercise_show.php?origin=myprogress&id='.$exe_id.'&cidReq='.$course_info['code'].'&id_session='.$session_id;
  2213. $percentage_score_result = Display::url(show_score($score, $weighting), $latest_attempt_url);
  2214. $my_score = 0;
  2215. if (!empty($weighting) && intval($weighting) != 0) {
  2216. $my_score = $score/$weighting;
  2217. }
  2218. $position = get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id, $user_list);
  2219. $graph = self::generate_exercise_result_thumbnail_graph($to_graph_exercise_result[$exercices['id']]);
  2220. $normal_graph = self::generate_exercise_result_graph($to_graph_exercise_result[$exercices['id']]);
  2221. }
  2222. }
  2223. echo Display::div($normal_graph, array('id'=>'main_graph_'.$exercices['id'],'class'=>'dialog', 'style'=>'display:none') );
  2224. if (empty($graph)) {
  2225. $graph = '-';
  2226. } else {
  2227. $graph = Display::url($graph, '#', array('id'=>$exercices['id'], 'class'=>'opener'));
  2228. }
  2229. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  2230. $html .= Display::tag('td', $percentage_score_result, array('align'=>'center'));
  2231. $html .= Display::tag('td', $position, array('align'=>'center'));
  2232. $html .= Display::tag('td', $best_score, array('align'=>'center'));
  2233. $html .= Display::tag('td', $graph, array('align'=>'center'));
  2234. //$html .= Display::tag('td', $latest_attempt_url, array('align'=>'center', 'width'=>'25'));
  2235. } else {
  2236. // Exercise configuration NO results
  2237. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  2238. $html .= Display::tag('td', '-', array('align'=>'center'));
  2239. $html .= Display::tag('td', '-', array('align'=>'center'));
  2240. $html .= Display::tag('td', '-', array('align'=>'center'));
  2241. $html .= Display::tag('td', '-', array('align'=>'center'));
  2242. }
  2243. $html .= '</tr>';
  2244. }
  2245. } else {
  2246. $html .= '<tr><td colspan="5" align="center">'.get_lang('NoEx').'</td></tr>';
  2247. }
  2248. $html .= '</table>';
  2249. //LP table results
  2250. $html .='<table class="data_table" width="100%">';
  2251. $html .= Display::tag('th', get_lang('Learnpaths'), array('class'=>'head', 'style'=>'color:#000'));
  2252. $html .= Display::tag('th', get_lang('LatencyTimeSpent'), array('class'=>'head', 'style'=>'color:#000'));
  2253. $html .= Display::tag('th', get_lang('Progress'), array('class'=>'head', 'style'=>'color:#000'));
  2254. $html .= Display::tag('th', get_lang('Score'), array('class'=>'head', 'style'=>'color:#000'));
  2255. $html .= Display::tag('th', get_lang('LastConnexion'), array('class'=>'head', 'style'=>'color:#000'));
  2256. $html .= '</tr>';
  2257. if (empty($session_id)) {
  2258. $sql_learnpath = "SELECT lp.name,lp.id FROM ".$tbl_course_lp." AS lp WHERE session_id = 0 ORDER BY lp.display_order";
  2259. } else {
  2260. $sql_learnpath = "SELECT lp.name,lp.id FROM ".$tbl_course_lp." AS lp ORDER BY lp.display_order";
  2261. }
  2262. $result_learnpath = Database::query($sql_learnpath);
  2263. if (Database::num_rows($result_learnpath) > 0) {
  2264. while($learnpath = Database::fetch_array($result_learnpath)) {
  2265. $progress = Tracking::get_avg_student_progress($user_id, $course, array($learnpath['id']), $session_id);
  2266. $last_connection_in_lp = Tracking::get_last_connection_time_in_lp($user_id, $course, $learnpath['id'], $session_id);
  2267. $time_spent_in_lp = Tracking::get_time_spent_in_lp($user_id, $course, array($learnpath['id']), $session_id);
  2268. $percentage_score = Tracking::get_avg_student_score($user_id, $course, array($learnpath['id']), $session_id);
  2269. if (is_numeric($percentage_score)) {
  2270. $percentage_score = $percentage_score.'%';
  2271. } else {
  2272. $percentage_score = '0%';
  2273. }
  2274. $time_spent_in_lp = api_time_to_hms($time_spent_in_lp);
  2275. $html .= '<tr class="row_even">';
  2276. $url = api_get_path(WEB_CODE_PATH)."newscorm/lp_controller.php?cidReq={$course_code}&id_session=$session_id&lp_id={$learnpath['id']}&action=view";
  2277. $html .= Display::tag('td', Display::url($learnpath['name'], $url, array('target'=>SESSION_LINK_TARGET)));
  2278. $html .= Display::tag('td', $time_spent_in_lp, array('align'=>'center'));
  2279. if (is_numeric($progress)) {
  2280. $progress = $progress.'%';
  2281. }
  2282. $html .= Display::tag('td', $progress, array('align'=>'center'));
  2283. $html .= Display::tag('td', $percentage_score);
  2284. $last_connection = '-';
  2285. if (!empty($last_connection_in_lp)) {
  2286. $last_connection = api_convert_and_format_date($last_connection_in_lp, DATE_TIME_FORMAT_LONG);
  2287. }
  2288. $html .= Display::tag('td', $last_connection, array('align'=>'center','width'=>'180px'));
  2289. $html .= "</tr>";
  2290. }
  2291. } else {
  2292. $html .= '<tr>
  2293. <td colspan="4" align="center">
  2294. '.get_lang('NoLearnpath').'
  2295. </td>
  2296. </tr>';
  2297. }
  2298. $html .='</table>';
  2299. }
  2300. if (!empty($html)) {
  2301. $html = Display::div($html, array('class'=>'rounded_div', 'style'=>'position:relative; float:none; width:95%'));
  2302. }
  2303. return $html;
  2304. }
  2305. /**
  2306. * Generates an histogram
  2307. *
  2308. * @param array list of exercise names
  2309. * @param array my results 0 to 100
  2310. * @param array average scores 0-100
  2311. */
  2312. function generate_session_exercise_graph($names, $my_results, $average) {
  2313. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2314. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2315. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2316. $cache = new pCache();
  2317. // Dataset definition
  2318. $data_set = new pData();
  2319. // Dataset definition
  2320. $data_set->AddPoint($average, "Serie1");
  2321. $data_set->AddPoint($my_results, "Serie2");
  2322. $data_set->AddPoint($names, "Serie3");
  2323. $data_set->AddAllSeries();
  2324. $data_set->SetAbsciseLabelSerie('Serie3');
  2325. $data_set->SetSerieName(get_lang('AverageScore'),"Serie1");
  2326. $data_set->SetSerieName(get_lang('MyResults'), "Serie2");
  2327. //$data_set->SetYAxisName(get_lang("Percentage"));
  2328. $data_set->SetYAxisUnit("%");
  2329. // Initialise the graph
  2330. $main_width = 860;
  2331. $main_height = 500;
  2332. $y_label_angle = 50;
  2333. $data_set->RemoveSerie("Serie3");
  2334. $graph = new pChart($main_width, $main_height);
  2335. //See 3.2 BT#2797
  2336. $graph->setFixedScale(0,100);
  2337. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2338. $graph->setGraphArea(65,50,$main_width-20, $main_height-140);
  2339. $graph->drawFilledRoundedRectangle(7,7,$main_width-7,$main_height-7,5,240,240,240);
  2340. $graph->drawRoundedRectangle(5,5,$main_width-5,$main_height -5,5,230,230,230);
  2341. $graph->drawGraphArea(255,255,255,TRUE);
  2342. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0, SCALE_ADDALL
  2343. $graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL ,150,150,150,TRUE,$y_label_angle,1, TRUE);
  2344. $graph->drawGrid(4,TRUE,230,230,230,70);
  2345. // Draw the 0 line
  2346. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  2347. $graph->drawTreshold(0,143,55,72,TRUE,TRUE);
  2348. // Draw the cubic curve graph
  2349. $graph->drawLineGraph($data_set->GetData(),$data_set->GetDataDescription());
  2350. $graph->drawPlotGraph($data_set->GetData(),$data_set->GetDataDescription(),1,1,230,255,255);
  2351. // Finish the graph
  2352. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  2353. $graph->drawLegend($main_width - 150,70,$data_set->GetDataDescription(),255,255,255);
  2354. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',11);
  2355. $graph->drawTitle(50, 30, get_lang('ExercisesInTimeProgressChart'), 50,50,50,$main_width-110, true);
  2356. // $main_graph = new pChart($main_width,$main_height);
  2357. $graph_id = 'generate_session_exercise_graph'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2358. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2359. //if (0) {
  2360. //if we already created the img
  2361. //echo 'in cache';
  2362. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2363. } else {
  2364. $cache->WriteToCache($graph_id, $data_set->GetData(), $graph);
  2365. ob_start();
  2366. $graph->Stroke();
  2367. ob_end_clean();
  2368. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2369. }
  2370. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2371. return $html;
  2372. }
  2373. /**
  2374. *
  2375. * Returns a thumbnail of the function generate_exercise_result_graph
  2376. * @param array attempts
  2377. */
  2378. function generate_exercise_result_thumbnail_graph($attempts) {
  2379. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2380. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2381. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2382. $exercise_title = $attempts['title'];
  2383. $attempts = $attempts['data'];
  2384. $my_exercise_result_array = $exercise_result = array();
  2385. if (empty($attempts)) {
  2386. return null;
  2387. }
  2388. foreach ($attempts as $attempt) {
  2389. if (api_get_user_id() == $attempt['exe_user_id']) {
  2390. if ($attempt['exe_weighting'] != 0 ) {
  2391. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2392. }
  2393. } else {
  2394. if ($attempt['exe_weighting'] != 0 ) {
  2395. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2396. }
  2397. }
  2398. }
  2399. //Getting best result
  2400. rsort($my_exercise_result_array);
  2401. $my_exercise_result = 0;
  2402. if (isset($my_exercise_result_array[0])) {
  2403. $my_exercise_result = $my_exercise_result_array[0] *100;
  2404. }
  2405. //var_dump($exercise_result, $my_exercise_result);
  2406. $max = 100;
  2407. $pieces = 5 ;
  2408. $part = round($max / $pieces);
  2409. $x_axis = array();
  2410. $final_array = array();
  2411. $my_final_array = array();
  2412. for ($i=1; $i <=$pieces; $i++) {
  2413. $sum = 1;
  2414. if ($i == 1) {
  2415. $sum = 0;
  2416. }
  2417. $min = ($i-1)*$part + $sum;
  2418. $max = ($i)*$part;
  2419. $x_axis[]= $min." - ".$max;
  2420. $count = 0;
  2421. foreach($exercise_result as $result) {
  2422. $percentage = $result*100;
  2423. //echo $percentage.' - '.$min.' - '.$max."<br />";
  2424. if ($percentage >= $min && $percentage <= $max) {
  2425. //echo ' is > ';
  2426. $count++;
  2427. }
  2428. }
  2429. //echo '<br />';
  2430. $final_array[]= $count;
  2431. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  2432. $my_final_array[] = 1;
  2433. } else {
  2434. $my_final_array[] = 0;
  2435. }
  2436. }
  2437. //var_dump($my_final_array, $final_array); exit;
  2438. //Fix to remove the data of the user with my data
  2439. for($i = 0; $i<=count($my_final_array); $i++) {
  2440. if (!empty($my_final_array[$i])) {
  2441. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  2442. $final_array[$i] = 0;
  2443. }
  2444. }
  2445. //var_dump($my_final_array, $final_array); echo '<br />';
  2446. //echo '<pre>'; var_dump($my_exercise_result, $exercise_result,$x_axis);
  2447. $cache = new pCache();
  2448. // Dataset definition
  2449. $data_set = new pData();
  2450. $data_set->AddPoint($final_array,"Serie1");
  2451. $data_set->AddPoint($my_final_array,"Serie2");
  2452. //$data_set->AddPoint($x_axis,"Serie3");
  2453. $data_set->AddAllSeries();
  2454. // Initialise the graph
  2455. $main_width = 80;
  2456. $main_height = 35;
  2457. $thumbnail_graph = new pChart($main_width, $main_height);
  2458. $thumbnail_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2459. //$thumbnail_graph->setGraphArea(50,30,680,200);
  2460. $thumbnail_graph->drawFilledRoundedRectangle(2,2,$main_width-2,$main_height-2,2,230,230,230);
  2461. $thumbnail_graph->setGraphArea(5,5,$main_width-5,$main_height-5);
  2462. $thumbnail_graph->drawGraphArea(255,255,255);
  2463. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0
  2464. $thumbnail_graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALLSTART0, 150,150,150,FALSE,0,1,TRUE);
  2465. $thumbnail_graph->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  2466. // Finish the graph
  2467. $graph_id = 'thumbnail_exercise_result_graph_'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2468. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2469. //if (0) {
  2470. //if we already created the img
  2471. //echo 'in cache';
  2472. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2473. } else {
  2474. $cache->WriteToCache($graph_id, $data_set->GetData(), $thumbnail_graph);
  2475. ob_start();
  2476. $thumbnail_graph->Stroke();
  2477. ob_end_clean();
  2478. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2479. }
  2480. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2481. return $html;
  2482. }
  2483. /**
  2484. * Generates a big graph with the number of best results
  2485. * @param array
  2486. */
  2487. function generate_exercise_result_graph($attempts) {
  2488. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2489. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2490. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2491. $exercise_title = $attempts['title'];
  2492. $attempts = $attempts['data'];
  2493. $my_exercise_result_array = $exercise_result = array();
  2494. if (empty($attempts)) {
  2495. return null;
  2496. }
  2497. foreach ($attempts as $attempt) {
  2498. if (api_get_user_id() == $attempt['exe_user_id']) {
  2499. if ($attempt['exe_weighting'] != 0 ) {
  2500. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2501. }
  2502. } else {
  2503. if ($attempt['exe_weighting'] != 0 ) {
  2504. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2505. }
  2506. }
  2507. }
  2508. //Getting best result
  2509. rsort($my_exercise_result_array);
  2510. $my_exercise_result = 0;
  2511. if (isset($my_exercise_result_array[0])) {
  2512. $my_exercise_result = $my_exercise_result_array[0] *100;
  2513. }
  2514. //var_dump($exercise_result, $my_exercise_result);
  2515. $max = 100;
  2516. $pieces = 5 ;
  2517. $part = round($max / $pieces);
  2518. $x_axis = array();
  2519. $final_array = array();
  2520. $my_final_array = array();
  2521. for ($i=1; $i <=$pieces; $i++) {
  2522. $sum = 1;
  2523. if ($i == 1) {
  2524. $sum = 0;
  2525. }
  2526. $min = ($i-1)*$part + $sum;
  2527. $max = ($i)*$part;
  2528. $x_axis[]= $min." - ".$max;
  2529. $count = 0;
  2530. foreach($exercise_result as $result) {
  2531. $percentage = $result*100;
  2532. //echo $percentage.' - '.$min.' - '.$max."<br />";
  2533. if ($percentage >= $min && $percentage <= $max) {
  2534. //echo ' is > ';
  2535. $count++;
  2536. }
  2537. }
  2538. //echo '<br />';
  2539. $final_array[]= $count;
  2540. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  2541. $my_final_array[] = 1;
  2542. } else {
  2543. $my_final_array[] = 0;
  2544. }
  2545. }
  2546. //var_dump($my_final_array, $final_array); exit;
  2547. //Fix to remove the data of the user with my data
  2548. for($i = 0; $i<=count($my_final_array); $i++) {
  2549. if (!empty($my_final_array[$i])) {
  2550. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  2551. $final_array[$i] = 0;
  2552. }
  2553. }
  2554. //var_dump($my_final_array, $final_array); echo '<br />';
  2555. //echo '<pre>'; var_dump($my_exercise_result, $exercise_result,$x_axis);
  2556. $cache = new pCache();
  2557. // Dataset definition
  2558. $data_set = new pData();
  2559. $data_set->AddPoint($final_array,"Serie1");
  2560. $data_set->AddPoint($my_final_array,"Serie2");
  2561. $data_set->AddPoint($x_axis,"Serie3");
  2562. $data_set->AddAllSeries();
  2563. $data_set->SetAbsciseLabelSerie('Serie3');
  2564. $data_set->SetSerieName(get_lang('Score'),"Serie1");
  2565. $data_set->SetSerieName(get_lang('MyResults'),"Serie2");
  2566. $data_set->SetXAxisName(get_lang("Score"));
  2567. // Initialise the graph
  2568. $main_width = 500;
  2569. $main_height = 250;
  2570. $main_graph = new pChart($main_width,$main_height);
  2571. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2572. $main_graph->setGraphArea(50,30, $main_width -20,$main_height -50);
  2573. $main_graph->drawFilledRoundedRectangle(10,10, $main_width- 10,$main_height -10,5,240,240,240);
  2574. $main_graph->drawRoundedRectangle(7,7,$main_width - 7,$main_height - 7,5,230,230,230);
  2575. $main_graph->drawGraphArea(255,255,255,TRUE);
  2576. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0
  2577. $main_graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALLSTART0, 150,150,150,TRUE,0,1,TRUE);
  2578. $main_graph->drawGrid(4,TRUE,230,230,230,50);
  2579. // Draw the 0 line
  2580. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  2581. // $main_graph->drawTreshold(0,143,55,72,TRUE,TRUE);
  2582. // Draw the bar graph
  2583. $data_set->RemoveSerie("Serie3");
  2584. //$main_graph->drawBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  2585. //$main_graph->drawStackedBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  2586. $main_graph->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  2587. // Finish the graph
  2588. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2589. $main_graph->drawLegend($main_width - 120,$main_height -100,$data_set->GetDataDescription(),255,255,255);
  2590. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2591. $main_graph->drawTitle(180,22,$exercise_title,50,50,50);
  2592. $graph_id = 'exercise_result_graph'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2593. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2594. //if (0) {
  2595. //if we already created the img
  2596. //echo 'in cache';
  2597. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2598. } else {
  2599. $cache->WriteToCache($graph_id, $data_set->GetData(), $main_graph);
  2600. ob_start();
  2601. $main_graph->Stroke();
  2602. ob_end_clean();
  2603. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2604. }
  2605. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2606. return $html;
  2607. }
  2608. }
  2609. /**
  2610. * @package chamilo.tracking
  2611. */
  2612. class TrackingCourseLog {
  2613. function count_item_resources() {
  2614. global $session_id;
  2615. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2616. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2617. $sql = "SELECT count(tool) AS total_number_of_items FROM $table_item_property track_resource, $table_user user" .
  2618. " WHERE track_resource.insert_user_id = user.user_id AND id_session = $session_id ";
  2619. if (isset($_GET['keyword'])) {
  2620. $keyword = Database::escape_string(trim($_GET['keyword']));
  2621. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%')";
  2622. }
  2623. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2624. $res = Database::query($sql);
  2625. $obj = Database::fetch_object($res);
  2626. return $obj->total_number_of_items;
  2627. }
  2628. function get_item_resources_data($from, $number_of_items, $column, $direction) {
  2629. global $dateTimeFormatLong, $session_id;
  2630. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2631. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2632. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2633. $sql = "SELECT
  2634. tool as col0,
  2635. lastedit_type as col1,
  2636. ref as ref,
  2637. user.username as col3,
  2638. insert_date as col5,
  2639. visibility as col6
  2640. FROM $table_item_property track_resource, $table_user user
  2641. WHERE track_resource.insert_user_id = user.user_id AND id_session = $session_id ";
  2642. if (isset($_GET['keyword'])) {
  2643. $keyword = Database::escape_string(trim($_GET['keyword']));
  2644. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%') ";
  2645. }
  2646. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2647. if ($column == 0) {
  2648. $column = '0';
  2649. }
  2650. if ($column != '' && $direction != '') {
  2651. if ($column != 2 && $column != 4) {
  2652. $sql .= " ORDER BY col$column $direction";
  2653. }
  2654. } else {
  2655. $sql .= " ORDER BY col5 DESC ";
  2656. }
  2657. $sql .= " LIMIT $from, $number_of_items ";
  2658. $res = Database::query($sql);
  2659. $resources = array ();
  2660. $thematic_tools = array('thematic', 'thematic_advance', 'thematic_plan');
  2661. while ($row = Database::fetch_array($res)) {
  2662. $ref = $row['ref'];
  2663. $table_name = TrackingCourseLog::get_tool_name_table($row['col0']);
  2664. $table_tool = Database :: get_course_table($table_name['table_name']);
  2665. $id = $table_name['id_tool'];
  2666. $recorset = false;
  2667. if (in_array($row['col0'], array('thematic_plan', 'thematic_advance'))) {
  2668. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  2669. $sql = "SELECT thematic_id FROM $table_tool WHERE id = $ref";
  2670. $rs_thematic = Database::query($sql);
  2671. if (Database::num_rows($rs_thematic)) {
  2672. $row_thematic = Database::fetch_array($rs_thematic);
  2673. $thematic_id = $row_thematic['thematic_id'];
  2674. $query = "SELECT session.id, session.name, user.username FROM $tbl_thematic t, $table_session session, $table_user user" .
  2675. " WHERE t.session_id = session.id AND session.id_coach = user.user_id AND t.id = $thematic_id";
  2676. $recorset = Database::query($query);
  2677. }
  2678. } else {
  2679. $query = "SELECT session.id, session.name, user.username FROM $table_tool tool, $table_session session, $table_user user" .
  2680. " WHERE tool.session_id = session.id AND session.id_coach = user.user_id AND tool.$id = $ref";
  2681. $recorset = Database::query($query);
  2682. }
  2683. if (!empty($recorset)) {
  2684. $obj = Database::fetch_object($recorset);
  2685. $name_session = '';
  2686. $coach_name = '';
  2687. if (!empty($obj)) {
  2688. $name_session = $obj->name;
  2689. $coach_name = $obj->username;
  2690. }
  2691. $url_tool = api_get_path(WEB_CODE_PATH).$table_name['link_tool'];
  2692. $row[0] = '';
  2693. if ($row['col6'] != 2) {
  2694. if (in_array($row['col0'], $thematic_tools)) {
  2695. $exp_thematic_tool = explode('_', $row['col0']);
  2696. $thematic_tool_title = '';
  2697. if (is_array($exp_thematic_tool)) {
  2698. foreach ($exp_thematic_tool as $exp) {
  2699. $thematic_tool_title .= api_ucfirst($exp);
  2700. }
  2701. } else {
  2702. $thematic_tool_title = api_ucfirst($row['col0']);
  2703. }
  2704. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'&action=thematic_details">'.get_lang($thematic_tool_title).'</a>';
  2705. } else {
  2706. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'">'.get_lang('Tool'.api_ucfirst($row['col0'])).'</a>';
  2707. }
  2708. } else {
  2709. $row[0] = api_ucfirst($row['col0']);
  2710. }
  2711. $row[1] = get_lang($row[1]);
  2712. $row[5] = api_convert_and_format_date($row['col5'], null, date_default_timezone_get());
  2713. $row[4] = '';
  2714. //@todo Improve this code please
  2715. switch ($table_name['table_name']) {
  2716. case 'document' :
  2717. $query_document = "SELECT tool.title as title FROM $table_tool tool WHERE id = $ref";
  2718. $rs_document = Database::query($query_document);
  2719. $obj_document = Database::fetch_object($rs_document);
  2720. $row[4] = $obj_document->title;
  2721. break;
  2722. case 'announcement':
  2723. $query_document = "SELECT title FROM $table_tool " .
  2724. " WHERE id = $ref";
  2725. $rs_document = Database::query($query_document);
  2726. $obj_document = Database::fetch_object($rs_document);
  2727. $row[4] = $obj_document->title;
  2728. break;
  2729. case 'glossary':
  2730. $query_document = "SELECT name FROM $table_tool " .
  2731. " WHERE glossary_id = $ref";
  2732. $rs_document = Database::query($query_document);
  2733. $obj_document = Database::fetch_object($rs_document);
  2734. $row[4] = $obj_document->name;
  2735. break;
  2736. case 'lp':
  2737. $query_document = "SELECT name FROM $table_tool " .
  2738. " WHERE id = $ref";
  2739. $rs_document = Database::query($query_document);
  2740. $obj_document = Database::fetch_object($rs_document);
  2741. $row[4] = $obj_document->name;
  2742. break;
  2743. case 'quiz':
  2744. $query_document = "SELECT title FROM $table_tool " .
  2745. " WHERE id = $ref";
  2746. $rs_document = Database::query($query_document);
  2747. $obj_document = Database::fetch_object($rs_document);
  2748. $row[4] = $obj_document->title;
  2749. break;
  2750. case 'course_description':
  2751. $query_document = "SELECT title FROM $table_tool " .
  2752. " WHERE id = $ref";
  2753. $rs_document = Database::query($query_document);
  2754. $obj_document = Database::fetch_object($rs_document);
  2755. $row[4] = $obj_document->title;
  2756. break;
  2757. case 'thematic':
  2758. $rs = Database::query("SELECT title FROM $table_tool WHERE id = $ref");
  2759. if (Database::num_rows($rs) > 0) {
  2760. $obj = Database::fetch_object($rs);
  2761. $row[4] = $obj->title;
  2762. }
  2763. break;
  2764. case 'thematic_advance':
  2765. $rs = Database::query("SELECT content FROM $table_tool WHERE id = $ref");
  2766. if (Database::num_rows($rs) > 0) {
  2767. $obj = Database::fetch_object($rs);
  2768. $row[4] = $obj->content;
  2769. }
  2770. break;
  2771. case 'thematic_plan':
  2772. $rs = Database::query("SELECT title FROM $table_tool WHERE id = $ref");
  2773. if (Database::num_rows($rs) > 0) {
  2774. $obj = Database::fetch_object($rs);
  2775. $row[4] = $obj->title;
  2776. }
  2777. break;
  2778. default:
  2779. break;
  2780. }
  2781. $row2 = $name_session;
  2782. if (!empty($coach_name)) {
  2783. $row2 .= '<br />'.get_lang('Coach').': '.$coach_name;
  2784. }
  2785. $row[2] = $row2;
  2786. $resources[] = $row;
  2787. }
  2788. }
  2789. return $resources;
  2790. }
  2791. function get_tool_name_table($tool) {
  2792. switch ($tool) {
  2793. case 'document':
  2794. $table_name = TABLE_DOCUMENT;
  2795. $link_tool = 'document/document.php';
  2796. $id_tool = 'id';
  2797. break;
  2798. case 'learnpath':
  2799. $table_name = TABLE_LP_MAIN;
  2800. $link_tool = 'newscorm/lp_controller.php';
  2801. $id_tool = 'id';
  2802. break;
  2803. case 'quiz':
  2804. $table_name = TABLE_QUIZ_TEST;
  2805. $link_tool = 'exercice/exercice.php';
  2806. $id_tool = 'id';
  2807. break;
  2808. case 'glossary':
  2809. $table_name = TABLE_GLOSSARY;
  2810. $link_tool = 'glossary/index.php';
  2811. $id_tool = 'glossary_id';
  2812. break;
  2813. case 'link':
  2814. $table_name = TABLE_LINK;
  2815. $link_tool = 'link/link.php';
  2816. $id_tool = 'id';
  2817. break;
  2818. case 'course_description':
  2819. $table_name = TABLE_COURSE_DESCRIPTION;
  2820. $link_tool = 'course_description/';
  2821. $id_tool = 'id';
  2822. break;
  2823. case 'announcement':
  2824. $table_name = TABLE_ANNOUNCEMENT;
  2825. $link_tool = 'announcements/announcements.php';
  2826. $id_tool = 'id';
  2827. break;
  2828. case 'thematic':
  2829. $table_name = TABLE_THEMATIC;
  2830. $link_tool = 'course_progress/index.php';
  2831. $id_tool = 'id';
  2832. break;
  2833. case 'thematic_advance':
  2834. $table_name = TABLE_THEMATIC_ADVANCE;
  2835. $link_tool = 'course_progress/index.php';
  2836. $id_tool = 'id';
  2837. break;
  2838. case 'thematic_plan':
  2839. $table_name = TABLE_THEMATIC_PLAN;
  2840. $link_tool = 'course_progress/index.php';
  2841. $id_tool = 'id';
  2842. break;
  2843. default:
  2844. $table_name = $tool;
  2845. break;
  2846. }
  2847. return array('table_name' => $table_name,'link_tool' => $link_tool,'id_tool' => $id_tool);
  2848. }
  2849. function display_additional_profile_fields() {
  2850. // getting all the extra profile fields that are defined by the platform administrator
  2851. $extra_fields = UserManager :: get_extra_fields(0,50,5,'ASC');
  2852. // creating the form
  2853. $return = '<form action="courseLog.php" method="get" name="additional_profile_field_form" id="additional_profile_field_form">';
  2854. // the select field with the additional user profile fields (= this is where we select the field of which we want to see
  2855. // the information the users have entered or selected.
  2856. $return .= '<select name="additional_profile_field">';
  2857. $return .= '<option value="-">'.get_lang('SelectFieldToAdd').'</option>';
  2858. $extra_fields_to_show = 0;
  2859. foreach ($extra_fields as $key=>$field) {
  2860. // show only extra fields that are visible + and can be filtered, added by J.Montoya
  2861. if ($field[6]==1 && $field[8] == 1) {
  2862. if (isset($_GET['additional_profile_field']) && $field[0] == $_GET['additional_profile_field'] ) {
  2863. $selected = 'selected="selected"';
  2864. } else {
  2865. $selected = '';
  2866. }
  2867. $extra_fields_to_show++;
  2868. $return .= '<option value="'.$field[0].'" '.$selected.'>'.$field[3].'</option>';
  2869. }
  2870. }
  2871. $return .= '</select>';
  2872. // the form elements for the $_GET parameters (because the form is passed through GET
  2873. foreach ($_GET as $key=>$value){
  2874. if ($key <> 'additional_profile_field') {
  2875. $return .= '<input type="hidden" name="'.Security::remove_XSS($key).'" value="'.Security::remove_XSS($value).'" />';
  2876. }
  2877. }
  2878. // the submit button
  2879. $return .= '<button class="save" type="submit">'.get_lang('AddAdditionalProfileField').'</button>';
  2880. $return .= '</form>';
  2881. if ($extra_fields_to_show > 0) {
  2882. return $return;
  2883. } else {
  2884. return '';
  2885. }
  2886. }
  2887. /**
  2888. * This function gets all the information of a certrain ($field_id) additional profile field.
  2889. * 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
  2890. *
  2891. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2892. * @since October 2009
  2893. * @version 1.8.7
  2894. */
  2895. function get_addtional_profile_information_of_field($field_id){
  2896. // Database table definition
  2897. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2898. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2899. $sql = "SELECT user.user_id, field.field_value FROM $table_user user, $table_user_field_values field
  2900. WHERE user.user_id = field.user_id
  2901. AND field.field_id='".intval($field_id)."'";
  2902. $result = Database::query($sql);
  2903. while($row = Database::fetch_array($result)) {
  2904. $return[$row['user_id']][] = $row['field_value'];
  2905. }
  2906. return $return;
  2907. }
  2908. /**
  2909. * This function gets all the information of a certrain ($field_id) additional profile field for a specific list of users is more efficent than get_addtional_profile_information_of_field() function
  2910. * 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
  2911. *
  2912. * @author Julio Montoya <gugli100@gmail.com>
  2913. * @param int field id
  2914. * @param array list of user ids
  2915. * @return array
  2916. * @since Nov 2009
  2917. * @version 1.8.6.2
  2918. */
  2919. function get_addtional_profile_information_of_field_by_user($field_id, $users) {
  2920. // Database table definition
  2921. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2922. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2923. $result_extra_field = UserManager::get_extra_field_information($field_id);
  2924. if (!empty($users)) {
  2925. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_TAG ) {
  2926. foreach($users as $user_id) {
  2927. $user_result = UserManager::get_user_tags($user_id, $field_id);
  2928. $tag_list = array();
  2929. foreach($user_result as $item) {
  2930. $tag_list[] = $item['tag'];
  2931. }
  2932. $return[$user_id][] = implode(', ',$tag_list);
  2933. }
  2934. } else {
  2935. $new_user_array = array();
  2936. foreach($users as $user_id) {
  2937. $new_user_array[]= "'".$user_id."'";
  2938. }
  2939. $users = implode(',',$new_user_array);
  2940. //selecting only the necessary information NOT ALL the user list
  2941. $sql = "SELECT user.user_id, field.field_value FROM $table_user user INNER JOIN $table_user_field_values field
  2942. ON (user.user_id = field.user_id)
  2943. WHERE field.field_id=".intval($field_id)." AND user.user_id IN ($users)";
  2944. $result = Database::query($sql);
  2945. while($row = Database::fetch_array($result)) {
  2946. // get option value for field type double select by id
  2947. if (!empty($row['field_value'])) {
  2948. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_DOUBLE_SELECT) {
  2949. $id_double_select = explode(';',$row['field_value']);
  2950. if (is_array($id_double_select)) {
  2951. $value1 = $result_extra_field['options'][$id_double_select[0]]['option_value'];
  2952. $value2 = $result_extra_field['options'][$id_double_select[1]]['option_value'];
  2953. $row['field_value'] = ($value1.';'.$value2);
  2954. }
  2955. }
  2956. }
  2957. // get other value from extra field
  2958. $return[$row['user_id']][] = $row['field_value'];
  2959. }
  2960. }
  2961. }
  2962. return $return;
  2963. }
  2964. /**
  2965. * count the number of students in this course (used for SortableTable)
  2966. * Deprecated
  2967. */
  2968. function count_student_in_course() {
  2969. global $nbStudents;
  2970. return $nbStudents;
  2971. }
  2972. function sort_users($a, $b) {
  2973. return strcmp(trim(api_strtolower($a[$_SESSION['tracking_column']])), trim(api_strtolower($b[$_SESSION['tracking_column']])));
  2974. }
  2975. function sort_users_desc($a, $b) {
  2976. return strcmp( trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
  2977. }
  2978. /**
  2979. * Get number of users for sortable with pagination
  2980. * @return int
  2981. */
  2982. function get_number_of_users() {
  2983. global $user_ids;
  2984. return count($user_ids);
  2985. }
  2986. /**
  2987. * Get data for users list in sortable with pagination
  2988. * @return array
  2989. */
  2990. function get_user_data($from, $number_of_items, $column, $direction) {
  2991. global $user_ids, $course_code, $additional_user_profile_info, $export_csv, $is_western_name_order, $csv_content, $session_id, $_configuration;
  2992. $course_code = Database::escape_string($course_code);
  2993. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2994. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2995. $course_info = CourseManager::get_course_information($course_code);
  2996. $access_url_id = api_get_current_access_url_id();
  2997. // get all users data from a course for sortable with limit
  2998. $condition_user = "";
  2999. if (is_array($user_ids)) {
  3000. $user_ids = array_map('intval', $user_ids);
  3001. $condition_user = " WHERE user.user_id IN (".implode(',',$user_ids).") ";
  3002. } else {
  3003. $user_ids = intval($user_ids);
  3004. $condition_user = " WHERE user.user_id = $user_ids ";
  3005. }
  3006. if (!empty($_GET['user_keyword'])) {
  3007. $keyword = trim(Database::escape_string($_GET['user_keyword']));
  3008. $condition_user .= " AND (user.firstname LIKE '%".$keyword."%' OR user.lastname LIKE '%".$keyword."%' OR user.username LIKE '%".$keyword."%' OR user.email LIKE '%".$keyword."%' ) ";
  3009. }
  3010. if ($_configuration['multiple_access_urls']) {
  3011. $url_table = ", ".$tbl_url_rel_user."as url_users";
  3012. $url_condition = " AND user.user_id = url_users.user_id AND access_url_id='$access_url_id'";
  3013. }
  3014. $sql = "SELECT user.user_id as user_id,
  3015. user.official_code as col0,
  3016. user.lastname as col1,
  3017. user.firstname as col2
  3018. FROM $tbl_user as user $url_table
  3019. $condition_user $url_condition";
  3020. if (!in_array($direction, array('ASC','DESC'))) {
  3021. $direction = 'ASC';
  3022. }
  3023. $column = intval($column);
  3024. if ($is_western_name_order) {
  3025. $original_column = $column;
  3026. if ($original_column == 1) {
  3027. $column = 2;
  3028. }
  3029. if ($original_column == 2) {
  3030. $column = 1;
  3031. }
  3032. }
  3033. $from = intval($from);
  3034. $number_of_items = intval($number_of_items);
  3035. $sql .= " ORDER BY col$column $direction ";
  3036. $sql .= " LIMIT $from,$number_of_items";
  3037. $res = Database::query($sql);
  3038. $users = array ();
  3039. $t = time();
  3040. $row = array();
  3041. $course_info = api_get_course_info($course_code);
  3042. $total_surveys = 0;
  3043. if (empty($session_id)) {
  3044. $survey_user_list = array();
  3045. $survey_list = survey_manager::get_surveys($course_code, $session_id);
  3046. $total_surveys = count($survey_list);
  3047. $survey_data = array();
  3048. foreach ($survey_list as $survey) {
  3049. $user_list = survey_manager::get_people_who_filled_survey($survey['survey_id'], false, $course_info['real_id']);
  3050. foreach ($user_list as $user_id) {
  3051. isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id]++ : $survey_user_list[$user_id] = 1;
  3052. }
  3053. }
  3054. }
  3055. while ($user = Database::fetch_array($res, 'ASSOC')) {
  3056. //$user['user_id'] = $user['user_id']
  3057. $user['official_code'] = $user['col0'];
  3058. if ($is_western_name_order) {
  3059. $user['lastname'] = $user['col2'];
  3060. $user['firstname'] = $user['col1'];
  3061. } else {
  3062. $user['lastname'] = $user['col1'];
  3063. $user['firstname'] = $user['col2'];
  3064. }
  3065. $user['time'] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user['user_id'], $course_code, $session_id));
  3066. $avg_student_score = Tracking::get_avg_student_score($user['user_id'], $course_code, array(), $session_id);
  3067. $avg_student_progress = Tracking::get_avg_student_progress($user['user_id'], $course_code, array(), $session_id);
  3068. if (empty($avg_student_progress)) {
  3069. $avg_student_progress=0;
  3070. }
  3071. $user['average_progress'] = $avg_student_progress.'%';
  3072. if (is_numeric($avg_student_score)) {
  3073. $user['student_score'] = $avg_student_score.'%';
  3074. } else {
  3075. $user['student_score'] = $avg_student_score;
  3076. }
  3077. $user['count_assignments'] = Tracking::count_student_assignments($user['user_id'], $course_code, $session_id);
  3078. $user['count_messages'] = Tracking::count_student_messages($user['user_id'], $course_code, $session_id);
  3079. $user['first_connection'] = Tracking::get_first_connection_date_on_the_course($user['user_id'], $course_code, $session_id);
  3080. $user['last_connection'] = Tracking::get_last_connection_date_on_the_course($user['user_id'], $course_code, $session_id);
  3081. // we need to display an additional profile field
  3082. $user['additional'] = '';
  3083. if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
  3084. if (isset($additional_user_profile_info[$user['user_id']]) && is_array($additional_user_profile_info[$user['user_id']])) {
  3085. $user['additional'] = implode(', ', $additional_user_profile_info[$user['user_id']]);
  3086. }
  3087. }
  3088. if (empty($session_id)) {
  3089. $user['survey'] = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0) .' / '.$total_surveys;
  3090. }
  3091. $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>';
  3092. // store columns in array $users
  3093. $is_western_name_order = api_is_western_name_order();
  3094. $user_row = array();
  3095. $user_row[]= $user['official_code'];
  3096. if ($is_western_name_order) {
  3097. $user_row[]= $user['lastname'];
  3098. $user_row[]= $user['firstname'];
  3099. } else {
  3100. $user_row[]= $user['firstname'];
  3101. $user_row[]= $user['lastname'];
  3102. }
  3103. $user_row[]= $user['time'];
  3104. $user_row[]= $user['average_progress'];
  3105. $user_row[]= $user['student_score'];
  3106. $user_row[]= $user['count_assignments'];
  3107. $user_row[]= $user['count_messages'];
  3108. if (empty($session_id)) {
  3109. $user_row[]= $user['survey'];
  3110. }
  3111. $user_row[]= $user['first_connection'];
  3112. $user_row[]= $user['last_connection'];
  3113. $user_row[]= $user['additional'];
  3114. $user_row[]= $user['link'];
  3115. $users[] = $user_row;
  3116. if ($export_csv) {
  3117. if (empty($session_id)) {
  3118. unset($user_row[12]);
  3119. unset($user_row[13]);
  3120. } else {
  3121. unset($user_row[11]);
  3122. unset($user_row[12]);
  3123. }
  3124. $csv_content[] = $user_row;
  3125. }
  3126. }
  3127. return $users;
  3128. }
  3129. }
  3130. /**
  3131. * @package chamilo.tracking
  3132. */
  3133. class TrackingUserLog {
  3134. /**
  3135. * Displays the number of logins every month for a specific user in a specific course.
  3136. */
  3137. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  3138. {
  3139. $MonthsLong = $GLOBALS['MonthsLong'];
  3140. // protected data
  3141. $user_id = intval($user_id);
  3142. $session_id = intval($session_id);
  3143. $course_id = Database::escape_string($course_id);
  3144. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3145. $tempView = $view;
  3146. if(substr($view,0,1) == '1') {
  3147. $new_view = substr_replace($view,'0',0,1);
  3148. echo "
  3149. <tr>
  3150. <td valign='top'>
  3151. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font>" .
  3152. "<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>]
  3153. </td>
  3154. </tr>
  3155. ";
  3156. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LoginsDetails')."<br>";
  3157. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  3158. FROM $track_access_table
  3159. WHERE access_user_id = '$user_id'
  3160. AND access_cours_code = '$course_id'
  3161. AND access_session_id = '$session_id'
  3162. GROUP BY YEAR(access_date),MONTH(access_date)
  3163. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  3164. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3165. //$results = getManyResults2Col($sql);
  3166. $results = getManyResults3Col($sql);
  3167. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3168. echo "<tr>
  3169. <td class='secLine'>
  3170. ".get_lang('LoginsTitleMonthColumn')."
  3171. </td>
  3172. <td class='secLine'>
  3173. ".get_lang('LoginsTitleCountColumn')."
  3174. </td>
  3175. </tr>";
  3176. $total = 0;
  3177. if (is_array($results)) {
  3178. for($j = 0 ; $j < count($results) ; $j++) {
  3179. echo "<tr>";
  3180. 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>";
  3181. echo "<td valign='top' align='right' class='content'>".$results[$j][1]."</td>";
  3182. echo"</tr>";
  3183. $total = $total + $results[$j][1];
  3184. }
  3185. echo "<tr>";
  3186. echo "<td>".get_lang('Total')."</td>";
  3187. echo "<td align='right' class='content'>".$total."</td>";
  3188. echo"</tr>";
  3189. } else {
  3190. echo "<tr>";
  3191. echo "<td colspan='2'><center>".get_lang('NoResult')."</center></td>";
  3192. echo"</tr>";
  3193. }
  3194. echo "</table>";
  3195. echo "</td></tr>";
  3196. } else {
  3197. $new_view = substr_replace($view,'1',0,1);
  3198. echo "
  3199. <tr>
  3200. <td valign='top'>
  3201. +<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>
  3202. </td>
  3203. </tr>
  3204. ";
  3205. }
  3206. }
  3207. /**
  3208. * Displays the exercise results for a specific user in a specific course.
  3209. * @todo remove globals
  3210. */
  3211. function display_exercise_tracking_info($view, $user_id, $course_id)
  3212. {
  3213. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $dateTimeFormatLong;
  3214. if(substr($view,1,1) == '1')
  3215. {
  3216. $new_view = substr_replace($view,'0',1,1);
  3217. echo "<tr>
  3218. <td valign='top'>
  3219. <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>]
  3220. </td>
  3221. </tr>";
  3222. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('ExercicesDetails')."<br />";
  3223. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3224. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  3225. WHERE te.exe_cours_id = '".Database::escape_string($course_id)."'
  3226. AND te.exe_user_id = '".Database::escape_string($user_id)."'
  3227. AND te.exe_exo_id = ce.id
  3228. ORDER BY ce.title ASC, te.exe_date ASC";
  3229. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3230. FROM $TBL_TRACK_HOTPOTATOES AS te
  3231. WHERE te.exe_user_id = '".Database::escape_string($user_id)."' AND te.exe_cours_id = '".Database::escape_string($course_id)."'
  3232. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  3233. $hpresults = getManyResultsXCol($hpsql, 4);
  3234. $NoTestRes = 0;
  3235. $NoHPTestRes = 0;
  3236. echo "<tr>\n<td style='padding-left : 40px;padding-right : 40px;'>\n";
  3237. $results = getManyResultsXCol($sql, 4);
  3238. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>\n";
  3239. echo "
  3240. <tr bgcolor='#E6E6E6'>
  3241. <td>
  3242. ".get_lang('ExercicesTitleExerciceColumn')."
  3243. </td>
  3244. <td>
  3245. ".get_lang('Date')."
  3246. </td>
  3247. <td>
  3248. ".get_lang('ExercicesTitleScoreColumn')."
  3249. </td>
  3250. </tr>";
  3251. if (is_array($results)) {
  3252. for($i = 0; $i < sizeof($results); $i++) {
  3253. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  3254. echo "<tr>\n";
  3255. echo "<td class='content'>".$results[$i][0]."</td>\n";
  3256. echo "<td class='content'>".$display_date."</td>\n";
  3257. echo "<td valign='top' align='right' class='content'>".$results[$i][1]." / ".$results[$i][2]."</td>\n";
  3258. echo "</tr>\n";
  3259. }
  3260. } else {
  3261. // istvan begin
  3262. $NoTestRes = 1;
  3263. }
  3264. // The Result of Tests
  3265. if(is_array($hpresults)) {
  3266. for($i = 0; $i < sizeof($hpresults); $i++) {
  3267. $title = GetQuizName($hpresults[$i][0],'');
  3268. if ($title == '')
  3269. $title = basename($hpresults[$i][0]);
  3270. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  3271. ?>
  3272. <tr>
  3273. <td class="content"><?php echo $title; ?></td>
  3274. <td class="content" align="center"><?php echo $display_date; ?></td>
  3275. <td class="content" align="center"><?php echo $hpresults[$i][1]; ?> / <?php echo $hpresults[$i][2]; ?>
  3276. </td>
  3277. </tr>
  3278. <?php }
  3279. } else {
  3280. $NoHPTestRes = 1;
  3281. }
  3282. if ($NoTestRes == 1 && $NoHPTestRes == 1) {
  3283. echo "<tr>\n";
  3284. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>\n";
  3285. echo "</tr>\n";
  3286. }
  3287. echo "</table>";
  3288. echo "</td>\n</tr>\n";
  3289. } else {
  3290. $new_view = substr_replace($view,'1',1,1);
  3291. echo "
  3292. <tr>
  3293. <td valign='top'>
  3294. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=$user_id&view=".$new_view."' class='specialLink'>".get_lang('ExercicesResults')."</a>
  3295. </td>
  3296. </tr>";
  3297. }
  3298. }
  3299. /**
  3300. * Displays the student publications for a specific user in a specific course.
  3301. * @todo remove globals
  3302. */
  3303. function display_student_publications_tracking_info($view, $user_id, $course_id)
  3304. {
  3305. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  3306. if(substr($view,2,1) == '1') {
  3307. $new_view = substr_replace($view,'0',2,1);
  3308. echo "<tr>
  3309. <td valign='top'>
  3310. <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>]
  3311. </td>
  3312. </tr>";
  3313. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('WorksDetails')."<br>";
  3314. $sql = "SELECT u.upload_date, w.title, w.author,w.url
  3315. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  3316. WHERE u.upload_work_id = w.id
  3317. AND u.upload_user_id = '".Database::escape_string($user_id)."'
  3318. AND u.upload_cours_id = '".Database::escape_string($course_id)."'
  3319. ORDER BY u.upload_date DESC";
  3320. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3321. $results = getManyResultsXCol($sql,4);
  3322. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3323. echo "<tr>
  3324. <td class='secLine' width='40%'>
  3325. ".get_lang('WorkTitle')."
  3326. </td>
  3327. <td class='secLine' width='30%'>
  3328. ".get_lang('WorkAuthors')."
  3329. </td>
  3330. <td class='secLine' width='30%'>
  3331. ".get_lang('Date')."
  3332. </td>
  3333. </tr>";
  3334. if (is_array($results)) {
  3335. for($j = 0 ; $j < count($results) ; $j++) {
  3336. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  3337. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  3338. echo "<tr>";
  3339. echo "<td class='content'>"
  3340. ."<a href ='".$pathToFile."'>".$results[$j][1]."</a>"
  3341. ."</td>";
  3342. echo "<td class='content'>".$results[$j][2]."</td>";
  3343. echo "<td class='content'>".$beautifulDate."</td>";
  3344. echo"</tr>";
  3345. }
  3346. } else {
  3347. echo "<tr>";
  3348. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>";
  3349. echo"</tr>";
  3350. }
  3351. echo "</table>";
  3352. echo "</td></tr>";
  3353. } else {
  3354. $new_view = substr_replace($view,'1',2,1);
  3355. echo "
  3356. <tr>
  3357. <td valign='top'>
  3358. +<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>
  3359. </td>
  3360. </tr>
  3361. ";
  3362. }
  3363. }
  3364. /**
  3365. * Displays the links followed for a specific user in a specific course.
  3366. * @todo remove globals
  3367. */
  3368. function display_links_tracking_info($view, $user_id, $course_id)
  3369. {
  3370. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  3371. if(substr($view,3,1) == '1') {
  3372. $new_view = substr_replace($view,'0',3,1);
  3373. echo "
  3374. <tr>
  3375. <td valign='top'>
  3376. <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>]
  3377. </td>
  3378. </tr>
  3379. ";
  3380. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LinksDetails')."<br>";
  3381. $sql = "SELECT cl.title, cl.url
  3382. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  3383. WHERE sl.links_link_id = cl.id
  3384. AND sl.links_cours_id = '".Database::escape_string($course_id)."'
  3385. AND sl.links_user_id = '".Database::escape_string($user_id)."'
  3386. GROUP BY cl.title, cl.url";
  3387. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3388. $results = getManyResults2Col($sql);
  3389. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3390. echo "<tr>
  3391. <td class='secLine'>
  3392. ".get_lang('LinksTitleLinkColumn')."
  3393. </td>
  3394. </tr>";
  3395. if (is_array($results)) {
  3396. for($j = 0 ; $j < count($results) ; $j++) {
  3397. echo "<tr>";
  3398. echo "<td class='content'><a href='".$results[$j][1]."'>".$results[$j][0]."</a></td>";
  3399. echo"</tr>";
  3400. }
  3401. } else {
  3402. echo "<tr>";
  3403. echo "<td ><center>".get_lang('NoResult')."</center></td>";
  3404. echo"</tr>";
  3405. }
  3406. echo "</table>";
  3407. echo "</td></tr>";
  3408. } else {
  3409. $new_view = substr_replace($view,'1',3,1);
  3410. echo "
  3411. <tr>
  3412. <td valign='top'>
  3413. +<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>
  3414. </td>
  3415. </tr>
  3416. ";
  3417. }
  3418. }
  3419. /**
  3420. * Displays the documents downloaded for a specific user in a specific course.
  3421. * @param string kind of view inside tracking info
  3422. * @param int User id
  3423. * @param string Course code
  3424. * @param int Session id (optional, default = 0)
  3425. * @return void
  3426. */
  3427. function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  3428. // protect data
  3429. $user_id = intval($user_id);
  3430. $course_id = Database::escape_string($course_id);
  3431. $session_id = intval($session_id);
  3432. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3433. if(substr($view,4,1) == '1')
  3434. {
  3435. $new_view = substr_replace($view,'0',4,1);
  3436. echo "
  3437. <tr>
  3438. <td valign='top'>
  3439. <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>]
  3440. </td>
  3441. </tr>
  3442. ";
  3443. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('DocumentsDetails')."<br>";
  3444. $sql = "SELECT down_doc_path
  3445. FROM $downloads_table
  3446. WHERE down_cours_id = '".$course_id."'
  3447. AND down_user_id = '$user_id'
  3448. AND down_session_id = '$session_id'
  3449. GROUP BY down_doc_path";
  3450. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3451. $results = getManyResults1Col($sql);
  3452. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>";
  3453. echo "<tr>
  3454. <td class='secLine'>
  3455. ".get_lang('DocumentsTitleDocumentColumn')."
  3456. </td>
  3457. </tr>";
  3458. if (is_array($results)) {
  3459. for($j = 0 ; $j < count($results) ; $j++) {
  3460. echo "<tr>";
  3461. echo "<td class='content'>".$results[$j]."</td>";
  3462. echo"</tr>";
  3463. }
  3464. } else {
  3465. echo "<tr>";
  3466. echo "<td><center>".get_lang('NoResult')."</center></td>";
  3467. echo"</tr>";
  3468. }
  3469. echo "</table>";
  3470. echo "</td></tr>";
  3471. } else {
  3472. $new_view = substr_replace($view,'1',4,1);
  3473. echo "
  3474. <tr>
  3475. <td valign='top'>
  3476. +<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>
  3477. </td>
  3478. </tr>
  3479. ";
  3480. }
  3481. }
  3482. }
  3483. /**
  3484. * @package chamilo.tracking
  3485. */
  3486. class TrackingUserLogCSV {
  3487. /**
  3488. * Displays the number of logins every month for a specific user in a specific course.
  3489. */
  3490. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  3491. {
  3492. $MonthsLong = $GLOBALS['MonthsLong'];
  3493. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3494. // protected data
  3495. $user_id = intval($user_id);
  3496. $session_id = intval($session_id);
  3497. $course_id = Database::escape_string($course_id);
  3498. $tempView = $view;
  3499. if(substr($view,0,1) == '1')
  3500. {
  3501. $new_view = substr_replace($view,'0',0,1);
  3502. $title[1]= get_lang('LoginsAndAccessTools').get_lang('LoginsDetails');
  3503. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  3504. FROM $track_access_table
  3505. WHERE access_user_id = '$user_id'
  3506. AND access_cours_code = '".$course_id."'
  3507. AND access_session_id = '$session_id'
  3508. GROUP BY YEAR(access_date),MONTH(access_date)
  3509. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  3510. //$results = getManyResults2Col($sql);
  3511. $results = getManyResults3Col($sql);
  3512. $title_line= get_lang('LoginsTitleMonthColumn').';'.get_lang('LoginsTitleCountColumn')."\n";
  3513. $line='';
  3514. $total = 0;
  3515. if (is_array($results))
  3516. {
  3517. for($j = 0 ; $j < count($results) ; $j++)
  3518. {
  3519. $line .= $results[$j][0].';'.$results[$j][1]."\n";
  3520. $total = $total + $results[$j][1];
  3521. }
  3522. $line .= get_lang('Total').";".$total."\n";
  3523. }
  3524. else
  3525. {
  3526. $line= get_lang('NoResult')."</center></td>";
  3527. }
  3528. }
  3529. else
  3530. {
  3531. $new_view = substr_replace($view,'1',0,1);
  3532. }
  3533. return array($title_line, $line);
  3534. }
  3535. /**
  3536. * Displays the exercise results for a specific user in a specific course.
  3537. * @todo remove globals
  3538. */
  3539. function display_exercise_tracking_info($view, $user_id, $course_id) {
  3540. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $TABLETRACK_HOTPOTATOES, $dateTimeFormatLong;
  3541. if (substr($view,1,1) == '1') {
  3542. $new_view = substr_replace($view,'0',1,1);
  3543. $title[1]= get_lang('ExercicesDetails');
  3544. $line='';
  3545. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3546. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  3547. WHERE te.exe_cours_id = '$course_id'
  3548. AND te.exe_user_id = '$user_id'
  3549. AND te.exe_exo_id = ce.id
  3550. ORDER BY ce.title ASC, te.exe_date ASC";
  3551. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3552. FROM $TABLETRACK_HOTPOTATOES AS te
  3553. WHERE te.exe_user_id = '$user_id' AND te.exe_cours_id = '$course_id'
  3554. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  3555. $hpresults = getManyResultsXCol($hpsql, 4);
  3556. $NoTestRes = 0;
  3557. $NoHPTestRes = 0;
  3558. $results = getManyResultsXCol($sql, 4);
  3559. $title_line=get_lang('ExercicesTitleExerciceColumn').";".get_lang('Date').';'.get_lang('ExercicesTitleScoreColumn')."\n";
  3560. if (is_array($results))
  3561. {
  3562. for($i = 0; $i < sizeof($results); $i++)
  3563. {
  3564. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  3565. $line .= $results[$i][0].";".$display_date.";".$results[$i][1]." / ".$results[$i][2]."\n";
  3566. }
  3567. }
  3568. else // istvan begin
  3569. {
  3570. $NoTestRes = 1;
  3571. }
  3572. // The Result of Tests
  3573. if(is_array($hpresults))
  3574. {
  3575. for($i = 0; $i < sizeof($hpresults); $i++)
  3576. {
  3577. $title = GetQuizName($hpresults[$i][0],'');
  3578. if ($title == '')
  3579. $title = basename($hpresults[$i][0]);
  3580. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  3581. $line .= $title.';'.$display_date.';'.$hpresults[$i][1].'/'.$hpresults[$i][2]."\n";
  3582. }
  3583. }
  3584. else
  3585. {
  3586. $NoHPTestRes = 1;
  3587. }
  3588. if ($NoTestRes == 1 && $NoHPTestRes == 1)
  3589. {
  3590. $line=get_lang('NoResult');
  3591. }
  3592. }
  3593. else
  3594. {
  3595. $new_view = substr_replace($view,'1',1,1);
  3596. }
  3597. return array($title_line, $line);
  3598. }
  3599. /**
  3600. * Displays the student publications for a specific user in a specific course.
  3601. * @todo remove globals
  3602. */
  3603. function display_student_publications_tracking_info($view, $user_id, $course_id) {
  3604. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  3605. if (substr($view,2,1) == '1') {
  3606. $new_view = substr_replace($view,'0',2,1);
  3607. $sql = "SELECT u.upload_date, w.title, w.author, w.url
  3608. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  3609. WHERE u.upload_work_id = w.id
  3610. AND u.upload_user_id = '$user_id'
  3611. AND u.upload_cours_id = '$course_id'
  3612. ORDER BY u.upload_date DESC";
  3613. $results = getManyResultsXCol($sql,4);
  3614. $title[1]=get_lang('WorksDetails');
  3615. $line='';
  3616. $title_line=get_lang('WorkTitle').";".get_lang('WorkAuthors').";".get_lang('Date')."\n";
  3617. if (is_array($results)) {
  3618. for($j = 0 ; $j < count($results) ; $j++) {
  3619. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  3620. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  3621. $line .= $results[$j][1].";".$results[$j][2].";".$beautifulDate."\n";
  3622. }
  3623. } else {
  3624. $line= get_lang('NoResult');
  3625. }
  3626. } else {
  3627. $new_view = substr_replace($view,'1',2,1);
  3628. }
  3629. return array($title_line, $line);
  3630. }
  3631. /**
  3632. * Displays the links followed for a specific user in a specific course.
  3633. * @todo remove globals
  3634. */
  3635. function display_links_tracking_info($view, $user_id, $course_id) {
  3636. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  3637. if (substr($view,3,1) == '1') {
  3638. $new_view = substr_replace($view,'0',3,1);
  3639. $title[1]=get_lang('LinksDetails');
  3640. $sql = "SELECT cl.title, cl.url
  3641. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  3642. WHERE sl.links_link_id = cl.id
  3643. AND sl.links_cours_id = '$course_id'
  3644. AND sl.links_user_id = '$user_id'
  3645. GROUP BY cl.title, cl.url";
  3646. $results = getManyResults2Col($sql);
  3647. $title_line= get_lang('LinksTitleLinkColumn')."\n";
  3648. if (is_array($results)) {
  3649. for ($j = 0 ; $j < count($results) ; $j++) {
  3650. $line .= $results[$j][0]."\n";
  3651. }
  3652. } else {
  3653. $line=get_lang('NoResult');
  3654. }
  3655. } else {
  3656. $new_view = substr_replace($view,'1',3,1);
  3657. }
  3658. return array($title_line, $line);
  3659. }
  3660. /**
  3661. * Displays the documents downloaded for a specific user in a specific course.
  3662. * @param string kind of view inside tracking info
  3663. * @param int User id
  3664. * @param string Course code
  3665. * @param int Session id (optional, default = 0)
  3666. * @return void
  3667. */
  3668. function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  3669. // protect data
  3670. $user_id = intval($user_id);
  3671. $course_id = Database::escape_string($course_id);
  3672. $session_id = intval($session_id);
  3673. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3674. if (substr($view,4,1) == '1') {
  3675. $new_view = substr_replace($view,'0',4,1);
  3676. $title[1]= get_lang('DocumentsDetails');
  3677. $sql = "SELECT down_doc_path
  3678. FROM $downloads_table
  3679. WHERE down_cours_id = '$course_id'
  3680. AND down_user_id = '$user_id'
  3681. AND down_session_id = '$session_id'
  3682. GROUP BY down_doc_path";
  3683. $results = getManyResults1Col($sql);
  3684. $title_line = get_lang('DocumentsTitleDocumentColumn')."\n";
  3685. if (is_array($results)) {
  3686. for ($j = 0 ; $j < count($results) ; $j++) {
  3687. $line .= $results[$j]."\n";
  3688. }
  3689. } else {
  3690. $line = get_lang('NoResult');
  3691. }
  3692. } else {
  3693. $new_view = substr_replace($view,'1',4,1);
  3694. }
  3695. return array($title_line, $line);
  3696. }
  3697. }