tracking.lib.php 190 KB

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