tracking.lib.php 172 KB

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