attendance_controller.php 26 KB

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