access_details_session.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. api_block_anonymous_users();
  5. $is_allowedToTrack = api_is_platform_admin(true, true) ||
  6. api_is_teacher() || api_is_course_tutor();
  7. if (!$is_allowedToTrack) {
  8. api_not_allowed(true);
  9. exit;
  10. }
  11. // the section (for the tabs)
  12. $this_section = SECTION_TRACKING;
  13. $quote_simple = "'";
  14. $userId = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0;
  15. $userInfo = api_get_user_info($userId);
  16. if (empty($userInfo)) {
  17. api_not_allowed(true);
  18. }
  19. /**
  20. * @param string $dateTime
  21. * @param bool $showTime
  22. *
  23. * @return string
  24. */
  25. function customDate($dateTime, $showTime = false)
  26. {
  27. $format = 'd/m/Y';
  28. if ($showTime) {
  29. $format = 'd/m/Y H:i:s';
  30. }
  31. $dateTime = api_get_local_time(
  32. $dateTime,
  33. null,
  34. null,
  35. true,
  36. false,
  37. true,
  38. $format
  39. );
  40. return $dateTime;
  41. }
  42. $sessions = SessionManager::getSessionsFollowedByUser($userId,
  43. null,
  44. null,
  45. null,
  46. false,
  47. false,
  48. false,
  49. 'ORDER BY s.access_end_date'
  50. );
  51. $startDate = '';
  52. $endDate = '';
  53. if (!empty($sessions)) {
  54. foreach ($sessions as $session) {
  55. $startDate = customDate($session['access_start_date']);
  56. $endDate = customDate($session['access_end_date']);
  57. }
  58. }
  59. $form = new FormValidator(
  60. 'myform',
  61. 'get',
  62. api_get_self().'?user_id='.$userId,
  63. null,
  64. ['id' => 'myform']
  65. );
  66. $form->addElement('text', 'from', get_lang('From'));
  67. $form->addElement('text', 'to', get_lang('Until'));
  68. $form->addElement('hidden', 'user_id', $userId);
  69. $form->addRule('from', get_lang('Required field'), 'required');
  70. $form->addRule('from', get_lang('Required field').' dd/mm/yyyy', 'callback', 'validateDate');
  71. $form->addRule('to', get_lang('Required field'), 'required');
  72. $form->addRule('to', get_lang('Required field').' dd/mm/yyyy', 'callback', 'validateDate');
  73. $form->addButtonSearch(get_lang('Generate report'));
  74. /**
  75. * @param string $value
  76. *
  77. * @return bool
  78. */
  79. function validateDate($value)
  80. {
  81. $value = DateTime::createFromFormat('d/m/Y', $value);
  82. if ($value === false) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. if ($form->validate()) {
  88. $values = $form->getSubmitValues();
  89. $from = $values['from'];
  90. $to = $values['to'];
  91. $from = DateTime::createFromFormat('d/m/Y', $from);
  92. $to = DateTime::createFromFormat('d/m/Y', $to);
  93. $from = api_get_utc_datetime($from->format('Y-m-d'));
  94. $to = api_get_utc_datetime($to->format('Y-m-d'));
  95. $sessionCategories = UserManager::get_sessions_by_category($userId, false);
  96. $report = [];
  97. $minLogin = 0;
  98. $maxLogin = 0;
  99. $totalDuration = 0;
  100. foreach ($sessionCategories as $category) {
  101. foreach ($category['sessions'] as $session) {
  102. $sessionId = $session['session_id'];
  103. $courseList = $session['courses'];
  104. foreach ($courseList as $course) {
  105. $courseInfo = api_get_course_info_by_id($course['real_id']);
  106. $result = MySpace::get_connections_to_course_by_date(
  107. $userId,
  108. $courseInfo,
  109. $sessionId,
  110. $from,
  111. $to
  112. );
  113. $partialDuration = 0;
  114. foreach ($result as $item) {
  115. $record = [
  116. customDate($item['login'], true),
  117. customDate($item['logout'], true),
  118. api_format_time($item['duration'], 'js'),
  119. ];
  120. $totalDuration += $item['duration'];
  121. if (empty($minLogin)) {
  122. $minLogin = api_strtotime($item['login'], 'UTC');
  123. }
  124. if ($minLogin > api_strtotime($item['login'], 'UTC')) {
  125. $minLogin = api_strtotime($item['login'], 'UTC');
  126. }
  127. if (api_strtotime($item['logout']) > $maxLogin) {
  128. $maxLogin = api_strtotime($item['logout'], 'UTC');
  129. }
  130. // Partials
  131. $partialDuration += $item['duration'];
  132. $report[$sessionId]['courses'][$course['real_id']][] = $record;
  133. $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
  134. }
  135. if (!empty($result)) {
  136. $record = [
  137. '',
  138. '',
  139. api_format_time($partialDuration, 'js'),
  140. ];
  141. $report[$sessionId]['courses'][$course['real_id']][] = $record;
  142. $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
  143. }
  144. }
  145. }
  146. }
  147. $courses = CourseManager::returnCourses($userId);
  148. $courses = array_merge($courses['in_category'], $courses['not_category']);
  149. foreach ($courses as $course) {
  150. $result = MySpace::get_connections_to_course_by_date(
  151. $userId,
  152. $course,
  153. 0,
  154. $from,
  155. $to
  156. );
  157. $partialDuration = 0;
  158. foreach ($result as $item) {
  159. $record = [
  160. customDate($item['login'], true),
  161. customDate($item['logout'], true),
  162. api_format_time($item['duration'], 'js'),
  163. ];
  164. $report[0]['courses'][$course['course_id']][] = $record;
  165. $report[0]['name'][$course['course_id']] = $course['title'];
  166. $totalDuration += $item['duration'];
  167. if (empty($minLogin)) {
  168. $minLogin = api_strtotime($item['login'], 'UTC');
  169. }
  170. if ($minLogin > api_strtotime($item['login'], 'UTC')) {
  171. $minLogin = api_strtotime($item['login'], 'UTC');
  172. }
  173. if (api_strtotime($item['logout'], 'UTC') > $maxLogin) {
  174. $maxLogin = api_strtotime($item['logout'], 'UTC');
  175. }
  176. // Partials
  177. $partialDuration += $item['duration'];
  178. }
  179. if (!empty($result)) {
  180. $record = [
  181. '',
  182. '',
  183. api_format_time($partialDuration, 'js'),
  184. ];
  185. $report[0]['courses'][$course['course_id']][] = $record;
  186. $report[0]['name'][$course['course_id']] = $course['title'];
  187. }
  188. }
  189. $table = new HTML_Table(['class' => 'data_table']);
  190. $headers = [
  191. get_lang('First connection'),
  192. get_lang('Last connection'),
  193. get_lang('Total time spent'),
  194. ];
  195. $row = 0;
  196. $column = 0;
  197. foreach ($headers as $header) {
  198. $table->setHeaderContents($row, $column, $header);
  199. $column++;
  200. }
  201. $row++;
  202. $column = 0;
  203. $table->setCellContents($row, $column++, customDate($minLogin));
  204. $table->setCellContents($row, $column++, customDate($maxLogin));
  205. $table->setRowAttributes($row, ['style' => 'font-weight:bold']);
  206. $table->setCellContents($row, $column++, api_format_time($totalDuration, 'js'));
  207. $totalTable = Display::page_subheader3(sprintf(get_lang('Extraction from %s'), api_get_local_time()));
  208. $totalTable .= $table->toHtml();
  209. $courseSessionTable = '';
  210. $courseSessionTableData = [];
  211. foreach ($report as $sessionId => $data) {
  212. foreach ($data['courses'] as $courseId => $courseData) {
  213. $courseSessionTable .= Display::page_subheader3($data['name'][$courseId]);
  214. $table = new HTML_Table(['class' => 'data_table']);
  215. $headers = [
  216. get_lang('Start Date'),
  217. get_lang('End Date'),
  218. get_lang('Duration'),
  219. ];
  220. $row = 0;
  221. $column = 0;
  222. foreach ($headers as $header) {
  223. $table->setHeaderContents($row, $column, $header);
  224. $column++;
  225. }
  226. $row++;
  227. $countData = count($courseData);
  228. foreach ($courseData as $record) {
  229. $column = 0;
  230. foreach ($record as $item) {
  231. $table->setCellContents($row, $column++, $item);
  232. if ($row == $countData) {
  233. $courseSessionTableData[$data['name'][$courseId]] = $item;
  234. $table->setRowAttributes($row, ['style' => 'font-weight:bold']);
  235. }
  236. }
  237. $row++;
  238. }
  239. $courseSessionTable .= $table->toHtml();
  240. }
  241. }
  242. $table = new HTML_Table(['class' => 'data_table']);
  243. $headers = [
  244. get_lang('Course'),
  245. get_lang('Total time spent'),
  246. ];
  247. $row = 0;
  248. $column = 0;
  249. foreach ($headers as $header) {
  250. $table->setHeaderContents($row, $column, $header);
  251. $column++;
  252. }
  253. $row++;
  254. foreach ($courseSessionTableData as $name => $duration) {
  255. $column = 0;
  256. $table->setCellContents($row, $column++, $name);
  257. $table->setCellContents($row, $column++, $duration);
  258. $row++;
  259. }
  260. $totalCourseSessionTable = $table->toHtml();
  261. $tpl = new Template('', false, false, false, true, false, false);
  262. $tpl->assign('title', get_lang('Certificate of achievement'));
  263. $tpl->assign('student', $userInfo['complete_name']);
  264. $tpl->assign('table_progress', $totalTable.$totalCourseSessionTable.'<pagebreak>'.$courseSessionTable);
  265. $content = $tpl->fetch($tpl->get_template('my_space/pdf_export_student.tpl'));
  266. $params = [
  267. 'pdf_title' => get_lang('Resume'),
  268. //'session_info' => $sessionInfo,
  269. 'course_info' => '',
  270. 'pdf_date' => '',
  271. 'student_info' => $userInfo,
  272. 'show_grade_generated_date' => true,
  273. 'show_real_course_teachers' => false,
  274. 'show_teacher_as_myself' => false,
  275. 'orientation' => 'P',
  276. ];
  277. @$pdf = new PDF('A4', $params['orientation'], $params);
  278. $pdf->setBackground($tpl->theme);
  279. @$pdf->content_to_pdf(
  280. $content,
  281. '',
  282. '',
  283. null,
  284. 'D',
  285. false,
  286. null,
  287. false,
  288. true,
  289. false
  290. );
  291. exit;
  292. }
  293. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Access details')];
  294. Display::display_header('');
  295. $userInfo = api_get_user_info($userId);
  296. echo Display::page_header(get_lang('Learner details in course'));
  297. echo Display::page_subheader(
  298. get_lang('User').': '.$userInfo['complete_name']
  299. );
  300. $form->setDefaults(['from' => $startDate, 'to' => $endDate]);
  301. $form->display();
  302. Display::display_footer();