access_details_session.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. $partialMinLogin = 0;
  114. $partialMaxLogin = 0;
  115. $partialDuration = 0;
  116. foreach ($result as $item) {
  117. $record = [
  118. customDate($item['login'], true),
  119. customDate($item['logout'], true),
  120. api_format_time($item['duration'], 'js'),
  121. ];
  122. $totalDuration += $item['duration'];
  123. if (empty($minLogin)) {
  124. $minLogin = api_strtotime($item['login'], 'UTC');
  125. }
  126. if ($minLogin > api_strtotime($item['login'], 'UTC')) {
  127. $minLogin = api_strtotime($item['login'], 'UTC');
  128. }
  129. if (api_strtotime($item['logout']) > $maxLogin) {
  130. $maxLogin = api_strtotime($item['logout'], 'UTC');
  131. }
  132. // Partials
  133. $partialDuration += $item['duration'];
  134. if (empty($partialMinLogin)) {
  135. $partialMinLogin = api_strtotime($item['login'], 'UTC');
  136. }
  137. if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) {
  138. $partialMinLogin = api_strtotime($item['login'], 'UTC');
  139. }
  140. if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) {
  141. $partialMaxLogin = api_strtotime($item['logout'], 'UTC');
  142. }
  143. $report[$sessionId]['courses'][$course['real_id']][] = $record;
  144. $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
  145. }
  146. if (!empty($result)) {
  147. $record = [
  148. customDate($partialMinLogin, true),
  149. customDate($partialMaxLogin, true),
  150. api_format_time($partialDuration, 'js'),
  151. ];
  152. $report[$sessionId]['courses'][$course['real_id']][] = $record;
  153. $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
  154. }
  155. }
  156. }
  157. }
  158. $courses = CourseManager::returnCourses($userId);
  159. $courses = array_merge($courses['in_category'], $courses['not_category']);
  160. foreach ($courses as $course) {
  161. $result = MySpace::get_connections_to_course_by_date(
  162. $userId,
  163. $course,
  164. 0,
  165. $from,
  166. $to
  167. );
  168. $partialMinLogin = 0;
  169. $partialMaxLogin = 0;
  170. $partialDuration = 0;
  171. foreach ($result as $item) {
  172. $record = [
  173. customDate($item['login'], true),
  174. customDate($item['logout'], true),
  175. api_format_time($item['duration'], 'js'),
  176. ];
  177. $report[0]['courses'][$course['course_id']][] = $record;
  178. $report[0]['name'][$course['course_id']] = $course['title'];
  179. $totalDuration += $item['duration'];
  180. if (empty($minLogin)) {
  181. $minLogin = api_strtotime($item['login'], 'UTC');
  182. }
  183. if ($minLogin > api_strtotime($item['login'], 'UTC')) {
  184. $minLogin = api_strtotime($item['login'], 'UTC');
  185. }
  186. if (api_strtotime($item['logout'], 'UTC') > $maxLogin) {
  187. $maxLogin = api_strtotime($item['logout'], 'UTC');
  188. }
  189. // Partials
  190. $partialDuration += $item['duration'];
  191. if (empty($partialMinLogin)) {
  192. $partialMinLogin = api_strtotime($item['login'], 'UTC');
  193. }
  194. if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) {
  195. $partialMinLogin = api_strtotime($item['login'], 'UTC');
  196. }
  197. if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) {
  198. $partialMaxLogin = api_strtotime($item['logout'], 'UTC');
  199. }
  200. }
  201. if (!empty($result)) {
  202. $record = [
  203. customDate($partialMinLogin, true),
  204. customDate($partialMaxLogin, true),
  205. api_format_time($partialDuration, 'js'),
  206. ];
  207. $report[0]['courses'][$course['course_id']][] = $record;
  208. $report[0]['name'][$course['course_id']] = $course['title'];
  209. }
  210. }
  211. $table = new HTML_Table(['class' => 'data_table_pdf']);
  212. $headers = [
  213. get_lang('MinStartDate'),
  214. get_lang('MaxEndDate'),
  215. get_lang('TotalDuration'),
  216. ];
  217. $row = 0;
  218. $column = 0;
  219. foreach ($headers as $header) {
  220. $table->setHeaderContents($row, $column, $header);
  221. $column++;
  222. }
  223. $row++;
  224. $column = 0;
  225. $table->setCellContents($row, $column++, customDate($minLogin));
  226. $table->setCellContents($row, $column++, customDate($maxLogin));
  227. $table->setCellContents($row, $column++, api_format_time($totalDuration, 'js'));
  228. $content = Display::page_subheader3(get_lang('Total')).$table->toHtml();
  229. foreach ($report as $sessionId => $data) {
  230. foreach ($data['courses'] as $courseId => $courseData) {
  231. $content .= Display::page_subheader3($data['name'][$courseId]);
  232. $table = new HTML_Table(['class' => 'data_table']);
  233. $headers = [
  234. get_lang('StartDate'),
  235. get_lang('EndDate'),
  236. get_lang('Duration'),
  237. ];
  238. $row = 0;
  239. $column = 0;
  240. foreach ($headers as $header) {
  241. $table->setHeaderContents($row, $column, $header);
  242. $column++;
  243. }
  244. $row++;
  245. $countData = count($courseData);
  246. foreach ($courseData as $record) {
  247. $column = 0;
  248. foreach ($record as $item) {
  249. $table->setCellContents($row, $column++, $item);
  250. if ($row == $countData) {
  251. $table->setRowAttributes($row, ['style' => 'font-weight:bold']);
  252. }
  253. }
  254. $row++;
  255. }
  256. $content .= $table->toHtml();
  257. }
  258. }
  259. $tpl = new Template('', false, false, false, true, false, false);
  260. $tpl->assign('title', get_lang('AttestationOfAttendance'));
  261. $tpl->assign('student', $userInfo['complete_name']);
  262. $tpl->assign('table_progress', $content);
  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();