tracking.lib.php 187 KB

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