tracking.lib.php 173 KB

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