attendance_controller.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like controller,
  5. * it should be included inside a dispatcher file (e.g: index.php).
  6. *
  7. * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC !
  8. * DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
  9. *
  10. * @author Christian Fasanando <christian1827@gmail.com>
  11. * @author Julio Montoya <gugli100@gmail.com> lot of bugfixes + improvements
  12. */
  13. class AttendanceController
  14. {
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct()
  19. {
  20. $this->toolname = 'attendance';
  21. $this->view = new View($this->toolname);
  22. }
  23. /**
  24. * It's used for listing attendance,
  25. * render to attendance_list view.
  26. */
  27. public function attendance_list()
  28. {
  29. // render to the view
  30. $this->view->set_data([]);
  31. $this->view->set_layout('layout');
  32. $this->view->set_template('attendance_list');
  33. $this->view->render();
  34. }
  35. /**
  36. * It's used for adding attendace,
  37. * render to attendance_add or attendance_list view.
  38. */
  39. public function attendance_add()
  40. {
  41. $attendance = new Attendance();
  42. $data = [];
  43. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
  44. if (!empty($_POST['title'])) {
  45. $check = Security::check_token();
  46. $attendanceId = 0;
  47. if ($check) {
  48. $attendance->set_name($_POST['title']);
  49. $attendance->set_description($_POST['description']);
  50. $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
  51. $attendance->set_attendance_weight($_POST['attendance_weight']);
  52. $link_to_gradebook = false;
  53. if (isset($_POST['attendance_qualify_gradebook']) &&
  54. $_POST['attendance_qualify_gradebook'] == 1
  55. ) {
  56. $link_to_gradebook = true;
  57. }
  58. $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : 0;
  59. $attendanceId = $attendance->attendance_add($link_to_gradebook);
  60. if ($attendanceId) {
  61. $form = new FormValidator('attendance_add');
  62. Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendanceId);
  63. }
  64. Security::clear_token();
  65. }
  66. header('Location: index.php?action=calendar_add&attendance_id='.$attendanceId.'&'.api_get_cidreq());
  67. exit;
  68. } else {
  69. $data['error'] = true;
  70. $this->view->set_data($data);
  71. $this->view->set_layout('layout');
  72. $this->view->set_template('attendance_add');
  73. $this->view->render();
  74. }
  75. } else {
  76. $this->view->set_data($data);
  77. $this->view->set_layout('layout');
  78. $this->view->set_template('attendance_add');
  79. $this->view->render();
  80. }
  81. }
  82. /**
  83. * It's used for editing attendance,
  84. * render to attendance_edit or attendance_list view.
  85. *
  86. * @param int $attendance_id
  87. */
  88. public function attendance_edit($attendance_id)
  89. {
  90. $attendance = new Attendance();
  91. $data = [];
  92. $attendance_id = intval($attendance_id);
  93. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
  94. if (!empty($_POST['title'])) {
  95. $check = Security::check_token();
  96. if ($check) {
  97. $attendance->set_name($_POST['title']);
  98. $attendance->set_description($_POST['description']);
  99. if (isset($_POST['attendance_qualify_title'])) {
  100. $attendance->set_attendance_qualify_title(
  101. $_POST['attendance_qualify_title']
  102. );
  103. }
  104. if (isset($_POST['attendance_weight'])) {
  105. $attendance->set_attendance_weight(
  106. $_POST['attendance_weight']
  107. );
  108. }
  109. $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : '';
  110. $link_to_gradebook = false;
  111. if (isset($_POST['attendance_qualify_gradebook']) &&
  112. $_POST['attendance_qualify_gradebook'] == 1
  113. ) {
  114. $link_to_gradebook = true;
  115. }
  116. $attendance->attendance_edit($attendance_id, $link_to_gradebook);
  117. $form = new FormValidator('attendance_edit');
  118. Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendance_id);
  119. Display::addFlash(Display::return_message(get_lang('Update successful')));
  120. Security::clear_token();
  121. header('Location:index.php?action=attendance_list&'.api_get_cidreq());
  122. exit;
  123. }
  124. } else {
  125. $data['attendance_id'] = $_POST['attendance_id'];
  126. $data['error'] = true;
  127. $this->view->set_data($data);
  128. $this->view->set_layout('layout');
  129. $this->view->set_template('attendance_edit');
  130. $this->view->render();
  131. }
  132. } else {
  133. // default values
  134. $attendance_data = $attendance->get_attendance_by_id(
  135. $attendance_id
  136. );
  137. $data['attendance_id'] = $attendance_data['id'];
  138. $data['title'] = $attendance_data['name'];
  139. $data['description'] = $attendance_data['description'];
  140. $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title'];
  141. $data['attendance_weight'] = $attendance_data['attendance_weight'];
  142. $this->view->set_data($data);
  143. $this->view->set_layout('layout');
  144. $this->view->set_template('attendance_edit');
  145. $this->view->render();
  146. }
  147. }
  148. /**
  149. * It's used for delete attendaces
  150. * render to attendance_list view.
  151. *
  152. * @param int $attendance_id
  153. *
  154. * @return bool
  155. */
  156. public function attendance_delete($attendance_id)
  157. {
  158. $allowDeleteAttendance = api_get_setting('allow_delete_attendance');
  159. if ($allowDeleteAttendance !== 'true') {
  160. $this->attendance_list();
  161. return false;
  162. }
  163. $attendance = new Attendance();
  164. if (!empty($attendance_id)) {
  165. $affected_rows = $attendance->attendance_delete($attendance_id);
  166. Skill::deleteSkillsFromItem($attendance_id, ITEM_TYPE_ATTENDANCE);
  167. }
  168. if ($affected_rows) {
  169. $message['message_attendance_delete'] = true;
  170. }
  171. $this->attendance_list();
  172. return true;
  173. }
  174. /**
  175. * It's used for make attendance visible
  176. * render to attendance_list view.
  177. *
  178. * @param int $attendanceId
  179. */
  180. public function attendanceSetVisible($attendanceId)
  181. {
  182. $attendance = new Attendance();
  183. $affectedRows = null;
  184. if (!empty($attendanceId)) {
  185. $affectedRows = $attendance->changeVisibility($attendanceId, 1);
  186. }
  187. if ($affectedRows) {
  188. $message['message_attendance_delete'] = true;
  189. }
  190. $this->attendance_list();
  191. }
  192. /**
  193. * It's used for make attendance invisible
  194. * render to attendance_list view.
  195. *
  196. * @param int $attendanceId
  197. */
  198. public function attendanceSetInvisible($attendanceId)
  199. {
  200. $attendance = new Attendance();
  201. if (!empty($attendanceId)) {
  202. $affectedRows = $attendance->changeVisibility($attendanceId, 0);
  203. }
  204. if ($affectedRows) {
  205. $message['message_attendance_delete'] = true;
  206. }
  207. $this->attendance_list();
  208. }
  209. /**
  210. * Restores an attendance entry and fallback to attendances rendering.
  211. *
  212. * @param int $attendance_id
  213. */
  214. public function attendance_restore($attendance_id)
  215. {
  216. $attendance = new Attendance();
  217. $affected_rows = false;
  218. if (!empty($attendance_id)) {
  219. $affected_rows = $attendance->attendance_restore($attendance_id);
  220. }
  221. if ($affected_rows) {
  222. $message['message_attendance_restore'] = true;
  223. }
  224. $this->attendance_list();
  225. }
  226. /**
  227. * Lock or unlock an attendance
  228. * render to attendance_list view.
  229. *
  230. * @param string $action (lock_attendance or unlock_attendance)
  231. * @param int $attendance_id
  232. * render to attendance_list view
  233. */
  234. public function lock_attendance($action, $attendance_id)
  235. {
  236. $attendance = new Attendance();
  237. $attendance_id = intval($attendance_id);
  238. if ($action == 'lock_attendance') {
  239. $result = $attendance->lock_attendance($attendance_id);
  240. } else {
  241. $result = $attendance->lock_attendance($attendance_id, false);
  242. }
  243. if ($result) {
  244. $message['message_locked_attendance'] = true;
  245. }
  246. $this->attendance_list();
  247. }
  248. public function export($id, $type = 'pdf')
  249. {
  250. $attendance = new Attendance();
  251. }
  252. /**
  253. * It's used for controlling attendance sheet (list, add),
  254. * render to attendance_sheet view.
  255. *
  256. * @param string $action
  257. * @param int $attendance_id
  258. * @param int $student_id
  259. * @param bool $edit
  260. */
  261. public function attendance_sheet(
  262. $action,
  263. $attendance_id,
  264. $student_id = 0,
  265. $edit = true
  266. ) {
  267. $attendance = new Attendance();
  268. $data = [];
  269. $data['attendance_id'] = $attendance_id;
  270. $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
  271. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
  272. $filter_type = 'today';
  273. if (!empty($_REQUEST['filter'])) {
  274. $filter_type = $_REQUEST['filter'];
  275. }
  276. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  277. api_get_user_id(),
  278. api_get_course_info()
  279. ) || api_is_drh();
  280. if ($edit == true) {
  281. if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
  282. $data['users_presence'] = $attendance->get_users_attendance_sheet(
  283. $attendance_id,
  284. 0,
  285. $groupId
  286. );
  287. }
  288. } else {
  289. if (!empty($student_id)) {
  290. $user_id = intval($student_id);
  291. } else {
  292. $user_id = api_get_user_id();
  293. }
  294. if (api_is_allowed_to_edit(null, true) ||
  295. api_is_coach(api_get_session_id(), api_get_course_int_id()) ||
  296. $isDrhOfCourse
  297. ) {
  298. $data['users_presence'] = $attendance->get_users_attendance_sheet(
  299. $attendance_id,
  300. 0,
  301. $groupId
  302. );
  303. } else {
  304. $data['users_presence'] = $attendance->get_users_attendance_sheet(
  305. $attendance_id,
  306. $user_id,
  307. $groupId
  308. );
  309. }
  310. $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
  311. $data['user_id'] = $user_id;
  312. }
  313. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id(
  314. $attendance_id
  315. );
  316. $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime(
  317. $attendance_id
  318. );
  319. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
  320. if (isset($_POST['hidden_input'])) {
  321. foreach ($_POST['hidden_input'] as $cal_id) {
  322. $users_present = [];
  323. if (isset($_POST['check_presence'][$cal_id])) {
  324. $users_present = $_POST['check_presence'][$cal_id];
  325. }
  326. $attendance->attendance_sheet_add(
  327. $cal_id,
  328. $users_present,
  329. $attendance_id
  330. );
  331. }
  332. }
  333. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
  334. $my_calendar_id = null;
  335. if (is_numeric($filter_type)) {
  336. $my_calendar_id = $filter_type;
  337. $filter_type = 'calendar_id';
  338. }
  339. $data['attendant_calendar'] = $attendance->get_attendance_calendar(
  340. $attendance_id,
  341. $filter_type,
  342. $my_calendar_id,
  343. $groupId
  344. );
  345. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar(
  346. $attendance_id,
  347. 'all',
  348. null,
  349. $groupId
  350. );
  351. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
  352. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  353. $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime($attendance_id);
  354. } else {
  355. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar(
  356. $attendance_id,
  357. 'all',
  358. null,
  359. $groupId
  360. );
  361. $data['attendant_calendar'] = $attendance->get_attendance_calendar(
  362. $attendance_id,
  363. $filter_type,
  364. null,
  365. $groupId
  366. );
  367. }
  368. $data['edit_table'] = intval($edit);
  369. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  370. $this->view->set_data($data);
  371. $this->view->set_layout('layout');
  372. $this->view->set_template('attendance_sheet');
  373. $this->view->render();
  374. }
  375. /**
  376. * It's used for controlling attendance calendar (list, add, edit, delete),
  377. * render to attendance_calendar view.
  378. *
  379. * @param string $action (optional, by default 'calendar_list')
  380. * @param int $attendance_id (optional)
  381. * @param int $calendar_id (optional)
  382. */
  383. public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
  384. {
  385. $attendance = new Attendance();
  386. $calendar_id = intval($calendar_id);
  387. $data = [];
  388. $data['attendance_id'] = $attendance_id;
  389. $attendance_id = intval($attendance_id);
  390. $groupList = isset($_POST['groups']) ? [$_POST['groups']] : [];
  391. if ($action == 'calendar_add') {
  392. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  393. if (!isset($_POST['cancel'])) {
  394. if (isset($_POST['repeat'])) {
  395. //@todo check this error_logs
  396. $start_datetime = api_strtotime(
  397. api_get_utc_datetime($_POST['date_time']),
  398. 'UTC'
  399. );
  400. $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC');
  401. $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'));
  402. $repeat_type = $_POST['repeat_type'];
  403. if (($end_datetime > $start_datetime) && $checkdate) {
  404. $attendance->attendance_repeat_calendar_add(
  405. $attendance_id,
  406. $start_datetime,
  407. $end_datetime,
  408. $repeat_type,
  409. $groupList
  410. );
  411. $action = 'calendar_list';
  412. } else {
  413. if (!$checkdate) {
  414. $data['error_checkdate'] = true;
  415. } else {
  416. $data['error_repeat_date'] = true;
  417. }
  418. $data['repeat'] = true;
  419. $action = 'calendar_add';
  420. }
  421. } else {
  422. $datetime = $_POST['date_time'];
  423. $datetimezone = api_get_utc_datetime($datetime);
  424. if (!empty($datetime)) {
  425. $attendance->set_date_time($datetimezone);
  426. $attendance->attendance_calendar_add($attendance_id, $groupList);
  427. $action = 'calendar_list';
  428. } else {
  429. $data['error_date'] = true;
  430. $action = 'calendar_add';
  431. }
  432. }
  433. } else {
  434. $action = 'calendar_list';
  435. }
  436. }
  437. } elseif ($action === 'calendar_edit') {
  438. $data['calendar_id'] = $calendar_id;
  439. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  440. if (!isset($_POST['cancel'])) {
  441. $datetime = $_POST['date_time'];
  442. $datetimezone = api_get_utc_datetime($datetime);
  443. $attendance->set_date_time($datetimezone);
  444. $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
  445. $data['calendar_id'] = 0;
  446. $action = 'calendar_list';
  447. } else {
  448. $action = 'calendar_list';
  449. }
  450. }
  451. } elseif ($action == 'calendar_delete') {
  452. $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
  453. $action = 'calendar_list';
  454. } elseif ($action == 'calendar_all_delete') {
  455. $attendance->attendance_calendar_delete(0, $attendance_id, true);
  456. $action = 'calendar_list';
  457. }
  458. $data['action'] = $action;
  459. $data['attendance_calendar'] = $attendance->get_attendance_calendar(
  460. $attendance_id,
  461. 'all',
  462. null,
  463. null,
  464. true
  465. );
  466. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  467. // render to the view
  468. $this->view->set_data($data);
  469. $this->view->set_layout('layout');
  470. $this->view->set_template('attendance_calendar');
  471. $this->view->render();
  472. }
  473. /**
  474. * It's used to print attendance sheet.
  475. *
  476. * @param string $action
  477. * @param int $attendance_id
  478. */
  479. public function attendance_sheet_export_to_pdf(
  480. $action,
  481. $attendance_id,
  482. $student_id = 0,
  483. $course_id = ''
  484. ) {
  485. $attendance = new Attendance();
  486. $courseInfo = api_get_course_info($course_id);
  487. $attendance->set_course_id($courseInfo['code']);
  488. $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
  489. $data_array = [];
  490. $data_array['attendance_id'] = $attendance_id;
  491. $data_array['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
  492. $filter_type = 'today';
  493. if (!empty($_REQUEST['filter'])) {
  494. $filter_type = $_REQUEST['filter'];
  495. }
  496. $my_calendar_id = null;
  497. if (is_numeric($filter_type)) {
  498. $my_calendar_id = $filter_type;
  499. $filter_type = 'calendar_id';
  500. }
  501. $data_array['attendant_calendar'] = $attendance->get_attendance_calendar(
  502. $attendance_id,
  503. $filter_type,
  504. $my_calendar_id,
  505. $groupId
  506. );
  507. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  508. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
  509. } else {
  510. if (!empty($student_id)) {
  511. $user_id = intval($student_id);
  512. } else {
  513. $user_id = api_get_user_id();
  514. }
  515. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id, $groupId);
  516. $data_array['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
  517. $data_array['user_id'] = $user_id;
  518. }
  519. $data_array['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  520. // Set headers pdf.
  521. $courseCategory = CourseManager::get_course_category($courseInfo['categoryCode']);
  522. $teacherInfo = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
  523. $teacherName = null;
  524. foreach ($teacherInfo as $teacherData) {
  525. if ($teacherName != null) {
  526. $teacherName = $teacherName." / ";
  527. }
  528. $teacherName .= api_get_person_name($teacherData['firstname'], $teacherData['lastname']);
  529. }
  530. // Get data table
  531. $data_table = [];
  532. $head_table = ['#', get_lang('Name')];
  533. foreach ($data_array['attendant_calendar'] as $class_day) {
  534. $head_table[] =
  535. api_format_date($class_day['date_time'], DATE_FORMAT_NUMBER_NO_YEAR).' '.
  536. api_format_date($class_day['date_time'], TIME_NO_SEC_FORMAT);
  537. }
  538. $data_table[] = $head_table;
  539. $data_attendant_calendar = $data_array['attendant_calendar'];
  540. $data_users_presence = $data_array['users_presence'];
  541. $count = 1;
  542. if (!empty($data_array['users_in_course'])) {
  543. foreach ($data_array['users_in_course'] as $user) {
  544. $cols = 1;
  545. $result = [];
  546. $result['count'] = $count;
  547. $result['full_name'] = api_get_person_name($user['firstname'], $user['lastname']);
  548. foreach ($data_array['attendant_calendar'] as $class_day) {
  549. if ($class_day['done_attendance'] == 1) {
  550. if ($data_users_presence[$user['user_id']][$class_day['id']]['presence'] == 1) {
  551. $result[$class_day['id']] = get_lang('P');
  552. } else {
  553. $result[$class_day['id']] = '<span style="color:red">'.get_lang('NP').'</span>';
  554. }
  555. } else {
  556. $result[$class_day['id']] = ' ';
  557. }
  558. $cols++;
  559. }
  560. $count++;
  561. $data_table[] = $result;
  562. }
  563. }
  564. $max_cols_per_page = 12; //10 dates + 2 name and number
  565. $max_dates_per_page = $max_dates_per_page_original = $max_cols_per_page - 2; //10
  566. $rows = count($data_table);
  567. if ($cols > $max_cols_per_page) {
  568. $number_tables = round(($cols - 2) / $max_dates_per_page);
  569. $headers = $data_table[0];
  570. $all = [];
  571. $tables = [];
  572. $changed = 1;
  573. for ($i = 0; $i <= $rows; $i++) {
  574. $row = isset($data_table[$i]) ? $data_table[$i] : null;
  575. $key = 1;
  576. $max_dates_per_page = 10;
  577. $item = isset($data_table[$i]) ? $data_table[$i] : null;
  578. $count_j = 0;
  579. if (!empty($item)) {
  580. foreach ($item as $value) {
  581. if ($count_j >= $max_dates_per_page) {
  582. $key++;
  583. $max_dates_per_page = $max_dates_per_page_original * $key;
  584. //magic hack
  585. $tables[$key][$i][] = $tables[1][$i][0];
  586. $tables[$key][$i][] = $tables[1][$i][1];
  587. }
  588. $tables[$key][$i][] = $value;
  589. $count_j++;
  590. }
  591. }
  592. }
  593. $content = null;
  594. if (!empty($tables)) {
  595. foreach ($tables as $sub_table) {
  596. $content .= Export::convert_array_to_html($sub_table).'<br /><br />';
  597. }
  598. }
  599. } else {
  600. $content = Export::convert_array_to_html(
  601. $data_table,
  602. ['header_attributes' => ['align' => 'center']]
  603. );
  604. }
  605. $params = [
  606. 'filename' => get_lang('Attendance').'-'.api_get_local_time(),
  607. 'pdf_title' => $courseInfo['title'],
  608. 'course_code' => $courseInfo['code'],
  609. 'add_signatures' => ['Drh', 'Teacher', 'Date'],
  610. 'orientation' => 'landscape',
  611. 'pdf_teachers' => $teacherName,
  612. 'pdf_course_category' => $courseCategory['name'],
  613. 'format' => 'A4-L',
  614. 'orientation' => 'L',
  615. ];
  616. Export::export_html_to_pdf($content, $params);
  617. exit;
  618. }
  619. /**
  620. * Gets attendance base in the table:
  621. * TABLE_STATISTIC_TRACK_E_COURSE_ACCESS.
  622. *
  623. * @param bool $showForm
  624. * @param bool $exportToPdf
  625. */
  626. public function getAttendanceBaseInLogin($showForm = false, $exportToPdf = true)
  627. {
  628. $table = null;
  629. $formToDisplay = null;
  630. $startDate = null;
  631. $endDate = null;
  632. $sessionId = api_get_session_id();
  633. if ($showForm) {
  634. $form = new FormValidator(
  635. 'search',
  636. 'post',
  637. api_get_self().'?'.api_get_cidreq().'&action=calendar_logins'
  638. );
  639. $form->addDateRangePicker('range', get_lang('Date range'));
  640. $form->addButton('submit', get_lang('Submit'));
  641. if ($form->validate()) {
  642. $values = $form->getSubmitValues();
  643. $startDate = api_get_utc_datetime($values['range_start']);
  644. $endDate = api_get_utc_datetime($values['range_end']);
  645. }
  646. $formToDisplay = $form->returnForm();
  647. } else {
  648. if (!empty($sessionId)) {
  649. $sessionInfo = api_get_session_info($sessionId);
  650. $startDate = $sessionInfo['access_start_date'];
  651. $endDate = $sessionInfo['access_end_date'];
  652. }
  653. }
  654. $attendance = new Attendance();
  655. if ($exportToPdf) {
  656. $result = $attendance->exportAttendanceLogin($startDate, $endDate);
  657. if (empty($result)) {
  658. api_not_allowed(true, get_lang('No data available'));
  659. }
  660. }
  661. $table = $attendance->getAttendanceLoginTable($startDate, $endDate);
  662. $data = [
  663. 'form' => $formToDisplay,
  664. 'table' => $table,
  665. ];
  666. $this->view->set_data($data);
  667. $this->view->set_layout('layout');
  668. $this->view->set_template('calendar_logins');
  669. $this->view->render();
  670. }
  671. }