access_details.php 12 KB

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