attendance_controller.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. $attendance = new Attendance();
  147. if (!empty($attendance_id)) {
  148. $affected_rows = $attendance->attendance_delete($attendance_id);
  149. }
  150. if ($affected_rows) {
  151. $message['message_attendance_delete'] = true;
  152. }
  153. $this->attendance_list();
  154. }
  155. /**
  156. * Restores an attendance entry and fallback to attendances rendering
  157. * @param int attendance id
  158. */
  159. public function attendance_restore($attendance_id)
  160. {
  161. $attendance = new Attendance();
  162. if (!empty($attendance_id)) {
  163. $affected_rows = $attendance->attendance_restore($attendance_id);
  164. }
  165. if ($affected_rows) {
  166. $message['message_attendance_restore'] = true;
  167. }
  168. $this->attendance_list();
  169. }
  170. /**
  171. * Lock or unlock an attendance
  172. * render to attendance_list view
  173. * @param string action (lock_attendance or unlock_attendance)
  174. * @param int attendance id
  175. * render to attendance_list view
  176. */
  177. public function lock_attendance($action, $attendance_id)
  178. {
  179. $attendance = new Attendance();
  180. $attendance_id = intval($attendance_id);
  181. if ($action == 'lock_attendance') {
  182. $result = $attendance->lock_attendance($attendance_id);
  183. } else {
  184. $result = $attendance->lock_attendance($attendance_id, false);
  185. }
  186. if ($result) {
  187. $message['message_locked_attendance'] = true;
  188. }
  189. $this->attendance_list();
  190. }
  191. public function export($id, $type = 'pdf')
  192. {
  193. $attendance = new Attendance();
  194. $attendance_id = intval($attendance_id);
  195. }
  196. /**
  197. * It's used for controlling attendance sheet (list, add),
  198. * render to attendance_sheet view
  199. * @param string $action
  200. * @param int $attendance_id
  201. * @param int $student_id
  202. * @param bool $edit
  203. */
  204. public function attendance_sheet($action, $attendance_id, $student_id = 0, $edit = true)
  205. {
  206. $attendance = new Attendance();
  207. $data = array();
  208. $data['attendance_id'] = $attendance_id;
  209. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  210. $filter_type = 'today';
  211. if (!empty($_REQUEST['filter'])) {
  212. $filter_type = $_REQUEST['filter'];
  213. }
  214. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  215. api_get_user_id(),
  216. api_get_course_info()
  217. );
  218. if ($edit == true) {
  219. if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
  220. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  221. }
  222. } else {
  223. if (!empty($student_id)) {
  224. $user_id = intval($student_id);
  225. } else {
  226. $user_id = api_get_user_id();
  227. }
  228. if (api_is_allowed_to_edit(null, true) ||
  229. api_is_coach(api_get_session_id(), api_get_course_id()) ||
  230. $isDrhOfCourse
  231. ) {
  232. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  233. } else {
  234. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id);
  235. }
  236. $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id);
  237. $data['user_id'] = $user_id;
  238. }
  239. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  240. $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
  241. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  242. if (isset($_POST['hidden_input'])) {
  243. foreach ($_POST['hidden_input'] as $cal_id) {
  244. $users_present = array();
  245. if (isset($_POST['check_presence'][$cal_id])) {
  246. $users_present = $_POST['check_presence'][$cal_id];
  247. }
  248. $attendance->attendance_sheet_add($cal_id,$users_present,$attendance_id);
  249. }
  250. }
  251. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  252. $my_calendar_id = null;
  253. if (is_numeric($filter_type)) {
  254. $my_calendar_id = $filter_type;
  255. $filter_type = 'calendar_id';
  256. }
  257. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type, $my_calendar_id);
  258. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id);
  259. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  260. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  261. $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
  262. } else {
  263. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id);
  264. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type);
  265. }
  266. $data['edit_table'] = intval($edit);
  267. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  268. $this->view->set_data($data);
  269. $this->view->set_layout('layout');
  270. $this->view->set_template('attendance_sheet');
  271. $this->view->render();
  272. }
  273. /**
  274. * It's used for controlling attendance calendar (list, add, edit, delete),
  275. * render to attendance_calendar view
  276. * @param string $action (optional, by default 'calendar_list')
  277. * @param int $attendance_id (optional)
  278. * @param int $calendar_id (optional)
  279. */
  280. public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
  281. {
  282. $attendance = new Attendance();
  283. $calendar_id = intval($calendar_id);
  284. $data = array();
  285. $data['attendance_id'] = $attendance_id;
  286. $attendance_id = intval($attendance_id);
  287. if ($action == 'calendar_add') {
  288. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  289. if (!isset($_POST['cancel'])) {
  290. if (isset($_POST['repeat'])) {
  291. //@todo check this error_logs
  292. $start_datetime = api_strtotime(
  293. api_get_utc_datetime($_POST['date_time']), 'UTC'
  294. );
  295. $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC');
  296. $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'));
  297. $repeat_type = $_POST['repeat_type'];
  298. if (($end_datetime > $start_datetime) && $checkdate) {
  299. $affected_rows = $attendance->attendance_repeat_calendar_add(
  300. $attendance_id,
  301. $start_datetime,
  302. $end_datetime,
  303. $repeat_type
  304. );
  305. $action = 'calendar_list';
  306. } else {
  307. if (!$checkdate) {
  308. $data['error_checkdate'] = true;
  309. } else {
  310. $data['error_repeat_date'] = true;
  311. }
  312. $data['repeat'] = true;
  313. $action = 'calendar_add';
  314. }
  315. } else {
  316. $datetime = $_POST['date_time'];
  317. $datetimezone = api_get_utc_datetime($datetime);
  318. if (!empty($datetime)) {
  319. $attendance->set_date_time($datetimezone);
  320. $affected_rows = $attendance->attendance_calendar_add($attendance_id);
  321. $action = 'calendar_list';
  322. } else {
  323. $data['error_date'] = true;
  324. $action = 'calendar_add';
  325. }
  326. }
  327. } else {
  328. $action = 'calendar_list';
  329. }
  330. }
  331. } else if ($action == 'calendar_edit') {
  332. $data['calendar_id'] = $calendar_id;
  333. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  334. if (!isset($_POST['cancel'])) {
  335. $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
  336. $datetimezone = api_get_utc_datetime($datetime);
  337. $attendance->set_date_time($datetimezone);
  338. $affected_rows = $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
  339. $data['calendar_id'] = 0;
  340. $action = 'calendar_list';
  341. } else {
  342. $action = 'calendar_list';
  343. }
  344. }
  345. } else if ($action == 'calendar_delete') {
  346. $affected_rows = $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
  347. $action = 'calendar_list';
  348. } else if ($action == 'calendar_all_delete') {
  349. $affected_rows = $attendance->attendance_calendar_delete(0, $attendance_id, true);
  350. $action = 'calendar_list';
  351. }
  352. $data['action'] = $action;
  353. $data['attendance_calendar'] = $attendance->get_attendance_calendar($attendance_id);
  354. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  355. // render to the view
  356. $this->view->set_data($data);
  357. $this->view->set_layout('layout');
  358. $this->view->set_template('attendance_calendar');
  359. $this->view->render();
  360. }
  361. /**
  362. * It's used to print attendance sheet
  363. * @param string $action
  364. * @param int $attendance_id
  365. */
  366. public function attendance_sheet_export_to_pdf($action, $attendance_id, $student_id = 0, $course_id = '')
  367. {
  368. $attendance = new Attendance();
  369. $courseInfo = CourseManager::get_course_information($course_id);
  370. $attendance->set_course_id($courseInfo['code']);
  371. $data_array = array();
  372. $data_array['attendance_id'] = $attendance_id;
  373. $data_array['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  374. $filter_type = 'today';
  375. if (!empty($_REQUEST['filter'])) {
  376. $filter_type = $_REQUEST['filter'];
  377. }
  378. $my_calendar_id = null;
  379. if (is_numeric($filter_type)) {
  380. $my_calendar_id = $filter_type;
  381. $filter_type = 'calendar_id';
  382. }
  383. $data_array['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type, $my_calendar_id);
  384. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  385. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  386. } else {
  387. if (!empty($student_id)) {
  388. $user_id = intval($student_id);
  389. } else {
  390. $user_id = api_get_user_id();
  391. }
  392. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id);
  393. $data_array['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id);
  394. $data_array['user_id'] = $user_id;
  395. }
  396. $data_array['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  397. // Set headers pdf.
  398. $courseCategory = CourseManager::get_course_category($courseInfo['category_code']);
  399. $teacherInfo = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
  400. $teacherName = null;
  401. foreach ($teacherInfo as $dados) {
  402. if ($teacherName != null)
  403. $teacherName = $teacherName . " / ";
  404. $teacherName.= $dados['firstname']." ".$dados['lastname'];
  405. }
  406. // Get data table
  407. $data_table = array();
  408. $head_table = array('#', get_lang('Name'));
  409. foreach ($data_array['attendant_calendar'] as $class_day) {
  410. $head_table[] = api_format_date($class_day['date_time'], DATE_FORMAT_NUMBER_NO_YEAR);
  411. }
  412. $data_table[] = $head_table;
  413. $dataClass = array();
  414. $max_dates_per_page = 10;
  415. $data_attendant_calendar = $data_array['attendant_calendar'];
  416. $data_users_presence = $data_array['users_presence'];
  417. $count = 1;
  418. if (!empty($data_array['users_in_course'])) {
  419. foreach ($data_array['users_in_course'] as $user) {
  420. $cols = 1;
  421. $result = array();
  422. $result['count'] = $count;
  423. $result['full_name'] = api_get_person_name($user['firstname'], $user['lastname']);
  424. foreach ($data_array['attendant_calendar'] as $class_day) {
  425. if ($class_day['done_attendance'] == 1) {
  426. if ($data_users_presence[$user['user_id']][$class_day['id']]['presence'] == 1)
  427. $result[$class_day['id']] = get_lang('UserAttendedSymbol');
  428. else
  429. $result[$class_day['id']] = get_lang('UserNotAttendedSymbol');
  430. } else {
  431. $result[$class_day['id']] = " ";
  432. }
  433. $cols++;
  434. }
  435. $count++;
  436. $data_table[] = $result;
  437. }
  438. }
  439. $max_cols_per_page = 12; //10 dates + 2 name and number
  440. $max_dates_per_page = $max_dates_per_page_original = $max_cols_per_page - 2;//10
  441. $rows = count($data_table);
  442. if ($cols > $max_cols_per_page) {
  443. $number_tables = round(($cols-2)/$max_dates_per_page);
  444. $headers = $data_table[0];
  445. $all = array();
  446. $tables = array();
  447. $changed = 1;
  448. for ($i= 0; $i <= $rows; $i++) {
  449. $row = $data_table[$i];
  450. $key = 1;
  451. $max_dates_per_page = 10;
  452. $item = $data_table[$i];
  453. $count_j = 0;
  454. if (!empty($item)) {
  455. foreach ($item as $value) {
  456. if ($count_j >= $max_dates_per_page) {
  457. $key++;
  458. $max_dates_per_page = $max_dates_per_page_original*$key;
  459. //magic hack
  460. $tables[$key][$i][] = $tables[1][$i][0];
  461. $tables[$key][$i][] = $tables[1][$i][1];
  462. }
  463. $tables[$key][$i][] = $value;
  464. $count_j++;
  465. }
  466. }
  467. }
  468. $content = null;
  469. if (!empty($tables)) {
  470. foreach ($tables as $sub_table) {
  471. $content .= Export::convert_array_to_html($sub_table).'<br /><br />';
  472. }
  473. }
  474. } else {
  475. $content .= Export::convert_array_to_html($data_table, array('header_attributes' => array('align' => 'center')));
  476. }
  477. $params = array(
  478. 'filename' => get_lang('Attendance').'-'.api_get_local_time(),
  479. 'pdf_title' => $courseInfo['title'],
  480. 'course_code' => $courseInfo['code'],
  481. 'add_signatures' => true,
  482. 'orientation' => 'landscape',
  483. 'pdf_teachers' => $teacherName,
  484. 'pdf_course_category' => $courseCategory['name'],
  485. 'format' => 'A4-L',
  486. 'orientation' => 'L'
  487. );
  488. Export::export_html_to_pdf($content, $params);
  489. exit;
  490. }
  491. }