access_details.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. // $Id:
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2009 Dokeos SPRL
  7. Copyright (c) 2008 Furio Petrossi
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * This is the tracking library for Dokeos.
  21. * Include/require it in your code to use its functionality.
  22. *
  23. * @package dokeos.library
  24. ==============================================================================
  25. * Calculates the time spent on the course
  26. * @param integer $user_id the user id
  27. * @param string $course_code the course code
  28. * Funzione scritta da Mario per testare cose
  29. */
  30. $language_file = array ('registration', 'index', 'tracking');
  31. require_once('../inc/global.inc.php');
  32. /**
  33. * Gets the connections to a course as an array of login and logout time
  34. */
  35. function get_connections_to_course($user_id, $course_code) {
  36. $course_code = Database::escape_string($course_code);
  37. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  38. $tbl_main=Database::get_main_table(TABLE_MAIN_COURSE);
  39. $sql_query='SELECT visual_code as course_code FROM '.$tbl_main.' c WHERE code="'.$course_code.'";';
  40. $result=api_sql_query($sql_query,__FILE__,__LINE__);
  41. $row_query=Database::fetch_array($result,'ASSOC');
  42. $course_true=isset($row_query['course_code']) ? $row_query['course_code']: $course_code;
  43. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  44. WHERE user_id = ' . intval($user_id) . '
  45. AND course_code="' . $course_true . '" ORDER BY login_course_date ASC';
  46. $rs = api_sql_query($sql);
  47. $connections = array();
  48. while ($a_connections = Database::fetch_array($rs)) {
  49. $s_login_date = $a_connections['login_course_date'];
  50. $s_logout_date = $a_connections['logout_course_date'];
  51. $i_timestamp_login_date = strtotime($s_login_date);
  52. $i_timestamp_logout_date = strtotime($s_logout_date);
  53. $connections[] = array('login'=>$i_timestamp_login_date, 'logout'=>$i_timestamp_logout_date);
  54. }
  55. return $connections;
  56. }
  57. function get_connections_to_course_by_time($user_id, $course_code, $year='', $month='', $day='') {
  58. $course_code = Database::escape_string($course_code);
  59. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  60. $tbl_main=Database::get_main_table(TABLE_MAIN_COURSE);
  61. $sql_query='SELECT visual_code as course_code FROM '.$tbl_main.' c WHERE code="'.$course_code.'";';
  62. $result=api_sql_query($sql_query,__FILE__,__LINE__);
  63. $row_query=Database::fetch_array($result,'ASSOC');
  64. $course_true=isset($row_query['course_code']) ? $row_query['course_code']: $course_code;
  65. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  66. WHERE user_id = ' . intval($user_id) . '
  67. AND course_code="' . $course_true . '"
  68. ORDER BY login_course_date DESC';
  69. $rs = api_sql_query($sql);
  70. $connections = array();
  71. while ($a_connections = Database::fetch_array($rs)) {
  72. $s_login_date = $a_connections['login_course_date'];
  73. $s_logout_date = $a_connections['logout_course_date'];
  74. $i_timestamp_login_date = strtotime($s_login_date);
  75. $i_timestamp_logout_date = strtotime($s_logout_date);
  76. $connections[] = array('login'=>$i_timestamp_login_date, 'logout'=>$i_timestamp_logout_date);
  77. }
  78. return $connections;
  79. }
  80. /**
  81. * Transforms seconds into a time format
  82. */
  83. function calculHours($seconds)
  84. {
  85. //How many hours ?
  86. $hours = floor($seconds / 3600);
  87. //How many minutes ?
  88. $min = floor(($seconds - ($hours * 3600)) / 60);
  89. if ($min < 10)
  90. $min = '0'.$min;
  91. //How many seconds
  92. $sec = $seconds - ($hours * 3600) - ($min * 60);
  93. if ($sec < 10)
  94. $sec = '0'.$sec;
  95. return $hours.get_lang('HourShort').' '.$min.':'.$sec;
  96. }
  97. /* MAIN */
  98. $user_id = Database::escape_string($_REQUEST['student']);
  99. $course_code=Database::escape_string($_REQUEST['course']);
  100. include_once(api_get_path(LIBRARY_PATH).'pchart/pData.class.php');
  101. include_once(api_get_path(LIBRARY_PATH).'pchart/pChart.class.php');
  102. include_once(api_get_path(LIBRARY_PATH).'pchart/pCache.class.php');
  103. $connections = get_connections_to_course($user_id, $course_code);
  104. if (api_is_xml_http_request()) {
  105. $type = Security::remove_XSS($_GET['type']);
  106. $main_year = $main_month_year = $main_day = array();
  107. // get last 8 days/months
  108. $last_days = 8;
  109. $last_months = 5;
  110. for ($i=$last_days; $i>=0; $i--) {
  111. $main_day[date ('d-m-Y', mktime () - $i * 3600 * 24)]=0;
  112. }
  113. for ($i=$last_months; $i>=0; $i--) {
  114. $main_month_year[date ('m-Y', mktime () - $i*30 * 3600 * 24)]=0;
  115. }
  116. $i = 0;
  117. if (is_array($connections) && count($connections)>0 ){
  118. foreach ($connections as $key=>$data) {
  119. //creating the main array
  120. //$main_year[date('Y',$data['login'])]+=calculHours($data['logout']-$data['login'])*60;
  121. //$main_month_year[date('m-Y',$data['login'])]+=calculHours($data['logout']-$data['login'])*60;
  122. $main_month_year[date('m-Y',$data['login'])]+=float_format(($data['logout']-$data['login'])/60, 0);
  123. //$main_day[date('d-m-Y',$data['login'])]+=calculHours($data['logout']-$data['login'])*60;
  124. $main_day[date('d-m-Y',$data['login'])]+=float_format(($data['logout']-$data['login'])/60, 0);
  125. if ($i > 500) {
  126. break;
  127. }
  128. $i++;
  129. }
  130. //var_dump($main_month_year);
  131. switch ($type) {
  132. case 'day':
  133. $main_date = $main_day;
  134. break;
  135. case 'month':
  136. $main_date = $main_month_year ;
  137. break;
  138. case 'year':
  139. $main_date = $main_year;
  140. break;
  141. }
  142. //echo '<pre>'; print_r($main_date);
  143. // the nice graphics :D
  144. $labels = array_keys($main_date);
  145. if (count($main_date)==1) {
  146. $labels = $labels[0];
  147. $main_date = $main_date[$labels];
  148. }
  149. $DataSet = new pData;
  150. $DataSet->AddPoint($main_date,'Q');
  151. if (count($main_date)!=1) {
  152. $DataSet->AddPoint($labels,'Date');
  153. }
  154. $DataSet->AddAllSeries();
  155. $DataSet->RemoveSerie('Date');
  156. $DataSet->SetAbsciseLabelSerie('Date');
  157. $DataSet->SetYAxisName(ucfirst(get_lang('MinMinutes')));
  158. $graph_id = api_get_user_id().'AccessDetails'.api_get_course_id();
  159. $DataSet->AddAllSeries();
  160. $Cache = new pCache();
  161. // the graph id
  162. $data = $DataSet->GetData();
  163. if ($Cache->IsInCache($graph_id, $DataSet->GetData())) {
  164. //if (0) {
  165. //if we already created the img
  166. // echo 'in cache';
  167. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  168. } else {
  169. // if the image does not exist in the main/garbage/ folder
  170. // Initialise the graph
  171. $Test = new pChart(760,230);
  172. //which schema of color will be used
  173. $quant_resources = count($data[0])-1;
  174. // Adding the color schemma
  175. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/default.txt");
  176. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  177. $Test->setGraphArea(70,30,680,200);
  178. $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
  179. $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
  180. $Test->drawGraphArea(255,255,255,TRUE);
  181. $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_START0,150,150,150,TRUE,0,0);
  182. $Test->drawGrid(4,TRUE,230,230,230,50);
  183. $Test->setLineStyle(2);
  184. // Draw the 0 line
  185. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",6);
  186. $Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  187. if (count($main_date)==1) {
  188. //Draw a graph
  189. echo '<strong>'.$labels.'</strong><br/>';
  190. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  191. } else {
  192. //Draw the line graph
  193. $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
  194. $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);
  195. }
  196. // Finish the graph
  197. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  198. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",10);
  199. $Test->drawTitle(60,22,get_lang('AccessDetails'),50,50,50,585);
  200. //------------------
  201. //echo 'not in cache';
  202. $Cache->WriteToCache($graph_id,$DataSet->GetData(),$Test);
  203. ob_start();
  204. $Test->Stroke();
  205. ob_end_clean();
  206. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  207. }
  208. echo '<img src="'.api_get_path(WEB_CODE_PATH).'garbage/'.$img_file.'">';
  209. } else {
  210. Display::display_warning_message (get_lang('GraphicNotAvailable'));
  211. }
  212. exit;
  213. }
  214. $nameTools= get_lang('AccessDetails');
  215. //StudentDetails
  216. if (isset($_GET['origin']) && strcmp($_GET['origin'],'tracking_course')===0) {
  217. $interbreadcrumb[] = array ("url" => "../tracking/courseLog.php?cidReq=".Security::remove_XSS($_GET['course'])."&amp;studentlist=true&id_session=".api_get_session_id(), "name" => get_lang("Tracking"));
  218. $interbreadcrumb[] = array ("url" => "myStudents.php?student=".Security::remove_XSS($_GET['student'])."&details=true&origin=".Security::remove_XSS($_GET['origin'])."&amp;course=".Security::remove_XSS($_GET['course']).'&amp;cidReq='.Security::remove_XSS($_GET['course']), "name" => get_lang('DetailsStudentInCourse'));
  219. $interbreadcrumb[] = array ("url" => "#", "name" => get_lang("Details"));
  220. } elseif (isset($_GET['origin']) && strcmp($_GET['origin'],'user_course')===0) {
  221. $interbreadcrumb[] = array ("url" => "../user/user.php?cidReq=".Security::remove_XSS($_GET['course']), "name" => get_lang("Users"));
  222. $interbreadcrumb[] = array ("url" => "myStudents.php?student=".Security::remove_XSS($_GET['student'])."&details=true&origin=".Security::remove_XSS($_GET['origin'])."&amp;course=".Security::remove_XSS($_GET['course']).'&amp;cidReq='.Security::remove_XSS($_GET['course']), "name" => get_lang('DetailsStudentInCourse'));
  223. $interbreadcrumb[] = array ("url" => "#", "name" => get_lang("Details"));
  224. }
  225. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  226. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery-1.1.3.1.pack.js" type="text/javascript"></script>';
  227. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.history_remote.pack.js" type="text/javascript"></script>';
  228. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.tabs.pack.js" type="text/javascript"></script>';
  229. $htmlHeadXtra[] = '<link rel="stylesheet" href="../inc/lib/javascript/jquery.tabs.css" type="text/css" media="print, projection, screen">';
  230. $htmlHeadXtra[] = '<script type="text/javascript">
  231. $(function() {
  232. $("#container-9").tabs({ remote: true});
  233. });
  234. </script>' ;
  235. Display :: display_header('');
  236. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  237. $mainUserInfo = api_get_user_info($user_id, $course_code);
  238. $result_to_print = '';
  239. $main_date_array = array();
  240. foreach ($connections as $key=>$data) {
  241. $result_to_print .= '&nbsp;&nbsp;'.date('d-m-Y (H:i:s)',$data['login']).' - '.calculHours($data['logout']-$data['login']).'<br />'."\n";
  242. }
  243. api_display_tool_title(get_lang('DetailsStudentInCourse'));
  244. echo '<div class="actions">';
  245. echo '<strong>'.get_lang('User').': '.$mainUserInfo['firstName'].' '.$mainUserInfo['lastName'].'</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>'.get_lang('Course').': '.$course_code.'</strong></div>';
  246. ?>
  247. <div id="container-9">
  248. <ul>
  249. <li><a href="access_details.php?type=day&course=<?php echo $course_code?>&student=<?php echo $user_id?>"><span> <?php echo get_lang('Day'); ?></span></a></li>
  250. <li><a href="access_details.php?type=month&course=<?php echo $course_code?>&student=<?php echo $user_id?>"><span><?php echo ucfirst(get_lang('MinMonth')); ?></span></a></li>
  251. </ul>
  252. <?php echo '<div id="show"></div>';?>
  253. </div>
  254. <?php
  255. echo '<div id="graph"></div><br />';
  256. echo '<div class="actions"><strong>',get_lang('DateAndTimeOfAccess'),' - ',get_lang('Duration'),'</strong></div><br />';
  257. echo $result_to_print;
  258. /* Login time against logout time
  259. foreach ($connections as $key=>$data)
  260. {
  261. echo ("<tr><td>".date("d-m-Y (H:i:s)",$data['login'])."</td><td>".date("d-m-Y (H:i:s)",$data['logout'])."</td></tr>");
  262. }
  263. */
  264. /*
  265. foreach ($connections as $key=>$data)
  266. {
  267. echo ("<tr><td>".date("d-m-Y (H:i:s)",$data['login'])."</td><td>".calculHours($data['logout']-$data['login'])."</td></tr>");
  268. }
  269. echo ("</table>");
  270. */
  271. Display:: display_footer();
  272. ?>