access_details.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // name of the language file that needs to be included
  31. $language_file = array ('registration', 'index', 'tracking');
  32. // including the global Dokeos file
  33. require_once '../inc/global.inc.php';
  34. // including additional libraries
  35. include_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  36. include_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  37. include_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  38. // the section (for the tabs)
  39. $this_section = "session_my_space";
  40. /* MAIN */
  41. $user_id = Security::remove_XSS($_REQUEST['student']);
  42. $course_code=Security::remove_XSS($_REQUEST['course']);
  43. $connections = get_connections_to_course($user_id, $course_code);
  44. if (api_is_xml_http_request()) {
  45. $type = Security::remove_XSS($_GET['type']);
  46. $main_year = $main_month_year = $main_day = array();
  47. // get last 8 days/months
  48. $last_days = 8;
  49. $last_months = 5;
  50. for ($i = $last_days; $i >= 0; $i--) {
  51. $main_day[date ('d-m-Y', mktime () - $i * 3600 * 24)] = 0;
  52. }
  53. for ($i = $last_months; $i >= 0; $i--) {
  54. $main_month_year[date ('m-Y', mktime () - $i*30 * 3600 * 24)] = 0;
  55. }
  56. $i = 0;
  57. if (is_array($connections) && count($connections) > 0) {
  58. foreach ($connections as $key => $data) {
  59. //creating the main array
  60. //$main_year[date('Y', $data['login'])] += calculHours($data['logout'] - $data['login']) * 60;
  61. //$main_month_year[date('m-Y', $data['login'])] += calculHours($data['logout'] - $data['login']) * 60;
  62. $main_month_year[date('m-Y', $data['login'])] += float_format(($data['logout'] - $data['login'])/60, 0);
  63. //$main_day[date('d-m-Y', $data['login'])] += calculHours($data['logout'] - $data['login']) * 60;
  64. $main_day[date('d-m-Y', $data['login'])] += float_format(($data['logout'] - $data['login']) / 60, 0);
  65. if ($i > 500) {
  66. break;
  67. }
  68. $i++;
  69. }
  70. //var_dump($main_month_year);
  71. switch ($type) {
  72. case 'day':
  73. $main_date = $main_day;
  74. break;
  75. case 'month':
  76. $main_date = $main_month_year ;
  77. break;
  78. case 'year':
  79. $main_date = $main_year;
  80. break;
  81. }
  82. //echo '<pre>'; print_r($main_date);
  83. // the nice graphics :D
  84. $labels = array_keys($main_date);
  85. if (count($main_date) == 1) {
  86. $labels = $labels[0];
  87. $main_date = $main_date[$labels];
  88. }
  89. $DataSet = new pData;
  90. $DataSet->AddPoint($main_date, 'Q');
  91. if (count($main_date)!= 1) {
  92. $DataSet->AddPoint($labels, 'Date');
  93. }
  94. $DataSet->AddAllSeries();
  95. $DataSet->RemoveSerie('Date');
  96. $DataSet->SetAbsciseLabelSerie('Date');
  97. $DataSet->SetYAxisName(get_lang('Minutes'));
  98. $graph_id = api_get_user_id().'AccessDetails'.api_get_course_id();
  99. $DataSet->AddAllSeries();
  100. $Cache = new pCache();
  101. // the graph id
  102. $data = $DataSet->GetData();
  103. if ($Cache->IsInCache($graph_id, $DataSet->GetData())) {
  104. //if (0) {
  105. //if we already created the img
  106. // echo 'in cache';
  107. $img_file = $Cache->GetHash($graph_id, $DataSet->GetData());
  108. } else {
  109. // if the image does not exist in the archive/ folder
  110. // Initialise the graph
  111. $Test = new pChart(760, 230);
  112. //which schema of color will be used
  113. $quant_resources = count($data[0]) - 1;
  114. // Adding the color schemma
  115. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/default.txt");
  116. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf", 8);
  117. $Test->setGraphArea(70, 30, 680, 200);
  118. $Test->drawFilledRoundedRectangle(7, 7, 693, 223, 5, 240, 240, 240);
  119. $Test->drawRoundedRectangle(5, 5, 695, 225, 5, 230, 230, 230);
  120. $Test->drawGraphArea(255, 255, 255, TRUE);
  121. $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_START0, 150, 150, 150, TRUE, 0, 0);
  122. $Test->drawGrid(4, TRUE, 230, 230, 230, 50);
  123. $Test->setLineStyle(2);
  124. // Draw the 0 line
  125. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf", 6);
  126. $Test->drawTreshold(0, 143, 55, 72, TRUE, TRUE);
  127. if (count($main_date) == 1) {
  128. //Draw a graph
  129. echo '<strong>'.$labels.'</strong><br/>';
  130. $Test->drawBarGraph($DataSet->GetData(), $DataSet->GetDataDescription(), TRUE);
  131. } else {
  132. //Draw the line graph
  133. $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription());
  134. $Test->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 3, 2, 255, 255, 255);
  135. }
  136. // Finish the graph
  137. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf", 8);
  138. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf", 10);
  139. $Test->drawTitle(60, 22, get_lang('AccessDetails'), 50, 50, 50, 585);
  140. //------------------
  141. //echo 'not in cache';
  142. $Cache->WriteToCache($graph_id,$DataSet->GetData(),$Test);
  143. ob_start();
  144. $Test->Stroke();
  145. ob_end_clean();
  146. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  147. }
  148. echo '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  149. } else {
  150. Display::display_warning_message (get_lang('GraphicNotAvailable'));
  151. }
  152. exit;
  153. }
  154. $nameTools= get_lang('AccessDetails');
  155. //StudentDetails
  156. if (isset($_GET['origin']) && strcmp($_GET['origin'], 'tracking_course') === 0) {
  157. $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"));
  158. $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'));
  159. $interbreadcrumb[] = array ("url" => "javascript: void(0);", "name" => get_lang("Details"));
  160. } elseif (isset($_GET['origin']) && strcmp($_GET['origin'], 'user_course') === 0) {
  161. $interbreadcrumb[] = array ("url" => "../user/user.php?cidReq=".Security::remove_XSS($_GET['course']), "name" => get_lang("Users"));
  162. $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'));
  163. $interbreadcrumb[] = array ("url" => "javascript: void(0);", "name" => get_lang("Details"));
  164. }
  165. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  166. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery-1.1.3.1.pack.js" type="text/javascript"></script>';
  167. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.history_remote.pack.js" type="text/javascript"></script>';
  168. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.tabs.pack.js" type="text/javascript"></script>';
  169. $htmlHeadXtra[] = '<link rel="stylesheet" href="../inc/lib/javascript/jquery.tabs.css" type="text/css" media="print, projection, screen">';
  170. $htmlHeadXtra[] = '<script type="text/javascript">
  171. $(function() {
  172. $("#container-9").tabs({ remote: true});
  173. });
  174. </script>' ;
  175. Display :: display_header('');
  176. $TBL_USERINFO_DEF = Database :: get_course_table(TABLE_USER_INFO);
  177. $mainUserInfo = api_get_user_info($user_id);
  178. $result_to_print = '';
  179. $main_date_array = array();
  180. foreach ($connections as $key => $data) {
  181. $result_to_print .= '&nbsp;&nbsp;'.date('d-m-Y (H:i:s)', $data['login']).' - '.calculHours($data['logout'] - $data['login']).'<br />'."\n";
  182. }
  183. api_display_tool_title(get_lang('DetailsStudentInCourse'));
  184. echo '<div class="actions">';
  185. echo '<strong>'.get_lang('User').': '.api_get_person_name($mainUserInfo['firstName'], $mainUserInfo['lastName']).'</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>'.get_lang('Course').': '.$course_code.'</strong></div>';
  186. ?>
  187. <div id="container-9">
  188. <ul>
  189. <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>
  190. <li><a href="access_details.php?type=month&course=<?php echo $course_code?>&student=<?php echo $user_id?>"><span><?php echo api_ucfirst(get_lang('MinMonth')); ?></span></a></li>
  191. </ul>
  192. <?php echo '<div id="show"></div>';?>
  193. </div>
  194. <?php
  195. echo '<div id="graph"></div><br />';
  196. echo '<div class="actions"><strong>', get_lang('DateAndTimeOfAccess'), ' - ', get_lang('Duration'), '</strong></div><br />';
  197. echo $result_to_print;
  198. /* Login time against logout time
  199. foreach ($connections as $key => $data) {
  200. 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>");
  201. }
  202. */
  203. /*
  204. foreach ($connections as $key => $data) {
  205. echo ("<tr><td>".date("d-m-Y (H:i:s)",$data['login'])."</td><td>".calculHours($data['logout']-$data['login'])."</td></tr>");
  206. }
  207. echo ("</table>");
  208. */
  209. Display:: display_footer();
  210. /*
  211. -----------------------------------------------------------
  212. Functions
  213. -----------------------------------------------------------
  214. */
  215. /**
  216. * Gets the connections to a course as an array of login and logout time
  217. *
  218. * @param unknown_type $user_id
  219. * @param unknown_type $course_code
  220. * @return unknown
  221. */
  222. function get_connections_to_course($user_id, $course_code) {
  223. // Database table definitions
  224. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  225. $tbl_main = Database :: get_main_table(TABLE_MAIN_COURSE);
  226. $sql_query = 'SELECT visual_code as course_code FROM '.$tbl_main.' c WHERE code="'.Database::escape_string($course_code).'";';
  227. $result = Database::query($sql_query, __FILE__, __LINE__);
  228. $row_query = Database::fetch_array($result,'ASSOC');
  229. $course_true = isset($row_query['course_code']) ? $row_query['course_code']: $course_code;
  230. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  231. WHERE user_id = ' . intval($user_id) . '
  232. AND course_code="' . Database::escape_string($course_true) . '" ORDER BY login_course_date ASC';
  233. $rs = Database::query($sql, __FILE__, __LINE__);
  234. $connections = array();
  235. while ($a_connections = Database::fetch_array($rs)) {
  236. $s_login_date = $a_connections['login_course_date'];
  237. $s_logout_date = $a_connections['logout_course_date'];
  238. $i_timestamp_login_date = strtotime($s_login_date);
  239. $i_timestamp_logout_date = strtotime($s_logout_date);
  240. $connections[] = array('login' => $i_timestamp_login_date, 'logout' => $i_timestamp_logout_date);
  241. }
  242. return $connections;
  243. }
  244. /**
  245. * Enter description here...
  246. *
  247. * @param unknown_type $user_id
  248. * @param unknown_type $course_code
  249. * @param unknown_type $year
  250. * @param unknown_type $month
  251. * @param unknown_type $day
  252. * @return unknown
  253. */
  254. function get_connections_to_course_by_time($user_id, $course_code, $year = '', $month = '', $day = '') {
  255. // Database table definitions
  256. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  257. $tbl_main = Database :: get_main_table(TABLE_MAIN_COURSE);
  258. $sql_query = 'SELECT visual_code as course_code FROM '.$tbl_main.' c WHERE code="'.Database :: escape_string($course_code).'";';
  259. $result = Database::query($sql_query, __FILE__, __LINE__);
  260. $row_query = Database::fetch_array($result,'ASSOC');
  261. $course_true = isset($row_query['course_code']) ? $row_query['course_code']: $course_code;
  262. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  263. WHERE user_id = ' . intval($user_id) . '
  264. AND course_code="' . Database::escape_string($course_true) . '"
  265. ORDER BY login_course_date DESC';
  266. $rs = Database::query($sql, __FILE__, __LINE__);
  267. $connections = array();
  268. while ($a_connections = Database::fetch_array($rs)) {
  269. $s_login_date = $a_connections['login_course_date'];
  270. $s_logout_date = $a_connections['logout_course_date'];
  271. $i_timestamp_login_date = strtotime($s_login_date);
  272. $i_timestamp_logout_date = strtotime($s_logout_date);
  273. $connections[] = array('login' => $i_timestamp_login_date, 'logout' => $i_timestamp_logout_date);
  274. }
  275. return $connections;
  276. }
  277. /**
  278. * Transforms seconds into a time format
  279. *
  280. * @param unknown_type $seconds
  281. * @return unknown
  282. */
  283. function calculHours($seconds) {
  284. //How many hours ?
  285. $hours = floor($seconds / 3600);
  286. //How many minutes ?
  287. $min = floor(($seconds - ($hours * 3600)) / 60);
  288. if ($min < 10) {
  289. $min = '0'.$min;
  290. }
  291. //How many seconds
  292. $sec = $seconds - ($hours * 3600) - ($min * 60);
  293. if ($sec < 10) {
  294. $sec = '0'.$sec;
  295. }
  296. return $hours.get_lang('HourShort').' '.$min.':'.$sec;
  297. }