access_details_session.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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('ThisFieldIsRequired'), 'required');
  70. $form->addRule('from', get_lang('ThisFieldIsRequired').' dd/mm/yyyy', 'callback', 'validateDate');
  71. $form->addRule('to', get_lang('ThisFieldIsRequired'), 'required');
  72. $form->addRule('to', get_lang('ThisFieldIsRequired').' dd/mm/yyyy', 'callback', 'validateDate');
  73. $form->addButtonSearch(get_lang('GenerateReport'));
  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('MinStartDate'),
  192. get_lang('MaxEndDate'),
  193. get_lang('TotalDuration'),
  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->setCellContents($row, $column++, api_format_time($totalDuration, 'js'));
  206. $totalTable = Display::page_subheader3(get_lang('Total')).$table->toHtml();
  207. $courseSessionTable = '';
  208. $courseSessionTableData = [];
  209. foreach ($report as $sessionId => $data) {
  210. foreach ($data['courses'] as $courseId => $courseData) {
  211. $courseSessionTable .= Display::page_subheader3($data['name'][$courseId]);
  212. $table = new HTML_Table(['class' => 'data_table']);
  213. $headers = [
  214. get_lang('StartDate'),
  215. get_lang('EndDate'),
  216. get_lang('Duration'),
  217. ];
  218. $row = 0;
  219. $column = 0;
  220. foreach ($headers as $header) {
  221. $table->setHeaderContents($row, $column, $header);
  222. $column++;
  223. }
  224. $row++;
  225. $countData = count($courseData);
  226. foreach ($courseData as $record) {
  227. $column = 0;
  228. foreach ($record as $item) {
  229. $table->setCellContents($row, $column++, $item);
  230. if ($row == $countData) {
  231. $courseSessionTableData[$data['name'][$courseId]] = $item;
  232. $table->setRowAttributes($row, ['style' => 'font-weight:bold']);
  233. }
  234. }
  235. $row++;
  236. }
  237. $courseSessionTable .= $table->toHtml();
  238. }
  239. }
  240. $table = new HTML_Table(['class' => 'data_table']);
  241. $headers = [
  242. get_lang('Course'),
  243. get_lang('TotalDuration'),
  244. ];
  245. $row = 0;
  246. $column = 0;
  247. foreach ($headers as $header) {
  248. $table->setHeaderContents($row, $column, $header);
  249. $column++;
  250. }
  251. $row++;
  252. foreach ($courseSessionTableData as $name => $duration) {
  253. $column = 0;
  254. $table->setCellContents($row, $column++, $name);
  255. $table->setCellContents($row, $column++, $duration);
  256. $row++;
  257. }
  258. $totalCourseSessionTable = $table->toHtml();
  259. $tpl = new Template('', false, false, false, true, false, false);
  260. $tpl->assign('title', get_lang('RealisationCertificate'));
  261. $tpl->assign('student', $userInfo['complete_name']);
  262. $tpl->assign('table_progress', $totalCourseSessionTable.$totalTable.$courseSessionTable);
  263. $content = $tpl->fetch($tpl->get_template('my_space/pdf_export_student.tpl'));
  264. $params = [
  265. 'pdf_title' => get_lang('Resume'),
  266. //'session_info' => $sessionInfo,
  267. 'course_info' => '',
  268. 'pdf_date' => '',
  269. 'student_info' => $userInfo,
  270. 'show_grade_generated_date' => true,
  271. 'show_real_course_teachers' => false,
  272. 'show_teacher_as_myself' => false,
  273. 'orientation' => 'P',
  274. ];
  275. @$pdf = new PDF('A4', $params['orientation'], $params);
  276. $pdf->setBackground($tpl->theme);
  277. @$pdf->content_to_pdf(
  278. $content,
  279. '',
  280. '',
  281. null,
  282. 'D',
  283. false,
  284. null,
  285. false,
  286. true,
  287. false
  288. );
  289. exit;
  290. }
  291. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('AccessDetails')];
  292. Display::display_header('');
  293. $userInfo = api_get_user_info($userId);
  294. $result_to_print = '';
  295. echo Display::page_header(get_lang('DetailsStudentInCourse'));
  296. echo Display::page_subheader(
  297. get_lang('User').': '.$userInfo['complete_name']
  298. );
  299. $form->setDefaults(['from' => $startDate, 'to' => $endDate]);
  300. $form->display();
  301. Display::display_footer();