attendance_controller.php 26 KB

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