attendance_controller.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like controller, it should be included inside a dispatcher file (e.g: index.php)
  5. *
  6. * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC ! DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com>
  9. * @author Julio Montoya <gugli100@gmail.com> lot of bugfixes + improvements
  10. * @package chamilo.attendance
  11. */
  12. /**
  13. * Controller script. Prepares the common background variables to give to the scripts corresponding to
  14. * the requested action
  15. * @package chamilo.attendance
  16. */
  17. class AttendanceController
  18. {
  19. /**
  20. * Constructor
  21. */
  22. public function __construct() {
  23. $this->toolname = 'attendance';
  24. $this->view = new View($this->toolname);
  25. }
  26. /**
  27. * It's used for listing attendace,
  28. * render to attendance_list view
  29. * @param boolean true for listing history (optional)
  30. * @param array message for showing by action['edit','add','delete'] (optional)
  31. */
  32. public function attendance_list($history = false, $messages = array()) {
  33. // render to the view
  34. $this->view->set_data(array());
  35. $this->view->set_layout('layout');
  36. $this->view->set_template('attendance_list');
  37. $this->view->render();
  38. }
  39. /**
  40. * It's used for adding attendace,
  41. * render to attendance_add or attendance_list view
  42. */
  43. public function attendance_add() {
  44. $attendance = new Attendance();
  45. $data = array();
  46. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  47. if (!empty($_POST['title'])) {
  48. $check = Security::check_token();
  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']) && $_POST['attendance_qualify_gradebook'] == 1 ) {
  56. $link_to_gradebook = true;
  57. }
  58. $attendance->category_id = $_POST['category_id'];
  59. $last_id = $attendance->attendance_add($link_to_gradebook);
  60. Security::clear_token();
  61. }
  62. $param_gradebook = '';
  63. if (isset($_SESSION['gradebook'])) {
  64. $param_gradebook = '&gradebook='.Security::remove_XSS($_SESSION['gradebook']);
  65. }
  66. //header('location:index.php?action=attendance_sheet_list&attendance_id='.$last_id.'&'.api_get_cidreq().$param_gradebook);
  67. header('location:index.php?action=calendar_add&attendance_id='.$last_id.'&'.api_get_cidreq().$param_gradebook);
  68. exit;
  69. } else {
  70. $data['error'] = true;
  71. $this->view->set_data($data);
  72. $this->view->set_layout('layout');
  73. $this->view->set_template('attendance_add');
  74. $this->view->render();
  75. }
  76. } else {
  77. $this->view->set_data($data);
  78. $this->view->set_layout('layout');
  79. $this->view->set_template('attendance_add');
  80. $this->view->render();
  81. }
  82. }
  83. /**
  84. * It's used for editing attendace,
  85. * render to attendance_edit or attendance_list view
  86. * @param int attendance id
  87. */
  88. public function attendance_edit($attendance_id) {
  89. $attendance = new Attendance();
  90. $data = array();
  91. $attendance_id = intval($attendance_id);
  92. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  93. if (!empty($_POST['title'])) {
  94. $check = Security::check_token();
  95. if ($check) {
  96. $attendance->set_name($_POST['title']);
  97. $attendance->set_description($_POST['description']);
  98. $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
  99. $attendance->set_attendance_weight($_POST['attendance_weight']);
  100. $attendance->category_id = $_POST['category_id'];
  101. $link_to_gradebook = false;
  102. if ( isset($_POST['attendance_qualify_gradebook']) && $_POST['attendance_qualify_gradebook'] == 1 ) {
  103. $link_to_gradebook = true;
  104. }
  105. $last_id = $attendance->attendance_edit($attendance_id,$link_to_gradebook);
  106. Security::clear_token();
  107. $param_gradebook = '';
  108. if (isset($_SESSION['gradebook'])) {
  109. $param_gradebook = '&gradebook='.Security::remove_XSS($_SESSION['gradebook']);
  110. }
  111. header('location:index.php?action=attendance_list&'.api_get_cidreq().$param_gradebook);
  112. exit;
  113. }
  114. } else {
  115. $data['attendance_id'] = $_POST['attendance_id'];
  116. $data['error'] = true;
  117. $this->view->set_data($data);
  118. $this->view->set_layout('layout');
  119. $this->view->set_template('attendance_edit');
  120. $this->view->render();
  121. }
  122. } else {
  123. // default values
  124. $attendance_data = $attendance->get_attendance_by_id($attendance_id);
  125. $data['attendance_id'] = $attendance_data['id'];
  126. $data['title'] = $attendance_data['name'];
  127. $data['description'] = $attendance_data['description'];
  128. $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title'];
  129. $data['attendance_weight'] = $attendance_data['attendance_weight'];
  130. $this->view->set_data($data);
  131. $this->view->set_layout('layout');
  132. $this->view->set_template('attendance_edit');
  133. $this->view->render();
  134. }
  135. }
  136. /**
  137. * It's used for delete attendaces
  138. * render to attendance_list view
  139. * @param int attendance id
  140. */
  141. public function attendance_delete($attendance_id) {
  142. $attendance = new Attendance();
  143. if (!empty($attendance_id)) {
  144. $affected_rows = $attendance->attendance_delete($attendance_id);
  145. }
  146. if ($affected_rows) {
  147. $message['message_attendance_delete'] = true;
  148. }
  149. $this->attendance_list();
  150. }
  151. /**
  152. * Restores an attendance entry and fallback to attendances rendering
  153. * @param int attendance id
  154. */
  155. public function attendance_restore($attendance_id) {
  156. $attendance = new Attendance();
  157. if (!empty($attendance_id)) {
  158. $affected_rows = $attendance->attendance_restore($attendance_id);
  159. }
  160. if ($affected_rows) {
  161. $message['message_attendance_restore'] = true;
  162. }
  163. $this->attendance_list();
  164. }
  165. /**
  166. * Lock or unlock an attendance
  167. * render to attendance_list view
  168. * @param string action (lock_attendance or unlock_attendance)
  169. * @param int attendance id
  170. * render to attendance_list view
  171. */
  172. public function lock_attendance($action, $attendance_id) {
  173. $attendance = new Attendance();
  174. $attendance_id = intval($attendance_id);
  175. if ($action == 'lock_attendance') {
  176. $result = $attendance->lock_attendance($attendance_id);
  177. } else {
  178. $result = $attendance->lock_attendance($attendance_id, false);
  179. }
  180. if ($result) {
  181. $message['message_locked_attendance'] = true;
  182. }
  183. $this->attendance_list();
  184. }
  185. public function export($id, $type = 'pdf') {
  186. $attendance = new Attendance();
  187. $attendance_id = intval($attendance_id);
  188. }
  189. /**
  190. * It's used for controlling attendace sheet (list, add),
  191. * render to attendance_sheet view
  192. * @param string action
  193. * @param int attendance id
  194. */
  195. public function attendance_sheet($action, $attendance_id, $user_id = 0, $edit = true) {
  196. $attendance = new Attendance();
  197. $data = array();
  198. $data['attendance_id'] = $attendance_id;
  199. $data['attendance_obj'] = $attendance;
  200. $data['attendance_states'] = $attendance->get_attendance_states();
  201. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  202. $filter_type = 'today';
  203. if (!empty($user_id)) {
  204. $user_id = intval($user_id);
  205. } else {
  206. $user_id = api_get_user_id();
  207. }
  208. if (!empty($_REQUEST['filter'])) {
  209. $filter_type = $_REQUEST['filter'];
  210. }
  211. if ($edit == true) {
  212. if (api_is_allowed_to_edit(null, true)) {
  213. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  214. }
  215. } else {
  216. if (api_is_allowed_to_edit(null, true) || api_is_coach(api_get_session_id(), api_get_course_int_id())) {
  217. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  218. } else {
  219. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id);
  220. }
  221. $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id);
  222. $data['user_id'] = $user_id;
  223. }
  224. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  225. $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
  226. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  227. if (isset($_POST['hidden_input'])) {
  228. $columns_to_update = $_POST['hidden_input'];
  229. $columns_to_update = array_unique(array_filter($columns_to_update));
  230. foreach ($columns_to_update as $cal_id) {
  231. $users_result = array();
  232. if (isset($_POST['check_presence'][$cal_id])) {
  233. $users_result = $_POST['check_presence'][$cal_id];
  234. }
  235. $user_final_results = array();
  236. if (!empty($users_result)) {
  237. foreach ($users_result as $result) {
  238. //Example: state_1_link_2_12_3 ==> state_[stateid]_YY_[userid]_calid
  239. $user_status = explode('_', $result);
  240. if (isset($user_status[0]) && $user_status[0] == 'state' && isset($user_status[4]) && isset($user_status[1])) {
  241. $user_final_results[$user_status[4]] = $user_status[1];
  242. }
  243. }
  244. }
  245. if (!empty($user_final_results)) {
  246. $attendance->attendance_sheet_add($cal_id, $user_final_results, $attendance_id, false, true);
  247. }
  248. }
  249. }
  250. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  251. $my_calendar_id = null;
  252. if (is_numeric($filter_type)) {
  253. $my_calendar_id = $filter_type;
  254. $filter_type = 'calendar_id';
  255. }
  256. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type, $my_calendar_id);
  257. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id);
  258. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  259. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  260. $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
  261. } else {
  262. $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id);
  263. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type);
  264. }
  265. $data['edit_table'] = intval($edit);
  266. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  267. $this->view->set_data($data);
  268. $this->view->set_layout('layout');
  269. $this->view->set_template('attendance_sheet');
  270. $this->view->render();
  271. }
  272. /**
  273. * It's used for controlling attendace calendar (list, add, edit, delete),
  274. * render to attendance_calendar view
  275. * @param string action (optional, by default 'calendar_list')
  276. * @param int attendance id (optional)
  277. * @param int calendar id (optional)
  278. */
  279. public function attendance_calendar($action = 'calendar_list',$attendance_id = 0, $calendar_id = 0) {
  280. $attendance = new Attendance();
  281. $calendar_id = intval($calendar_id);
  282. $data = array();
  283. $data['attendance_id'] = $attendance_id;
  284. $attendance_id = intval($attendance_id);
  285. if ($action == 'calendar_add') {
  286. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  287. if (!isset($_POST['cancel'])) {
  288. if (isset($_POST['repeat'])) {
  289. //@todo check this error_logs
  290. $start_datetime = api_strtotime(
  291. api_get_utc_datetime($_POST['date_time']), 'UTC'
  292. );
  293. $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC');
  294. $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'));
  295. $repeat_type = $_POST['repeat_type'];
  296. if (($end_datetime > $start_datetime) && $checkdate) {
  297. $affected_rows = $attendance->attendance_repeat_calendar_add(
  298. $attendance_id,
  299. $start_datetime,
  300. $end_datetime,
  301. $repeat_type
  302. );
  303. $action = 'calendar_list';
  304. } else {
  305. if (!$checkdate) {
  306. $data['error_checkdate'] = true;
  307. } else {
  308. $data['error_repeat_date'] = true;
  309. }
  310. $data['repeat'] = true;
  311. $action = 'calendar_add';
  312. }
  313. } else {
  314. $datetime = $_POST['date_time'];
  315. $datetimezone = api_get_utc_datetime($datetime);
  316. if (!empty($datetime)) {
  317. $attendance->set_date_time($datetimezone);
  318. $affected_rows = $attendance->attendance_calendar_add($attendance_id);
  319. $action = 'calendar_list';
  320. } else {
  321. $data['error_date'] = true;
  322. $action = 'calendar_add';
  323. }
  324. }
  325. } else {
  326. $action = 'calendar_list';
  327. }
  328. }
  329. } else if ($action == 'calendar_edit') {
  330. $data['calendar_id'] = $calendar_id;
  331. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  332. if (!isset($_POST['cancel'])) {
  333. $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
  334. $datetimezone = api_get_utc_datetime($datetime);
  335. $attendance->set_date_time($datetimezone);
  336. $affected_rows = $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
  337. $data['calendar_id'] = 0;
  338. $action = 'calendar_list';
  339. } else {
  340. $action = 'calendar_list';
  341. }
  342. }
  343. } else if ($action == 'calendar_delete') {
  344. $affected_rows = $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
  345. $action = 'calendar_list';
  346. } else if ($action == 'calendar_all_delete') {
  347. $affected_rows = $attendance->attendance_calendar_delete(0, $attendance_id, true);
  348. $action = 'calendar_list';
  349. }
  350. $data['action'] = $action;
  351. $data['attendance_calendar'] = $attendance->get_attendance_calendar($attendance_id);
  352. $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
  353. // render to the view
  354. $this->view->set_data($data);
  355. $this->view->set_layout('layout');
  356. $this->view->set_template('attendance_calendar');
  357. $this->view->render();
  358. }
  359. /**
  360. * It's used to print attendance sheet
  361. * @param string action
  362. * @param int attendance id
  363. */
  364. public function attendance_sheet_export_to_pdf($action, $attendance_id, $student_id = 0, $course_id = '') {
  365. $attendance = new Attendance();
  366. $courseInfo = CourseManager::get_course_information($course_id);
  367. $attendance->set_course_id($courseInfo['code']);
  368. $data_array = array();
  369. $data_array['attendance_id'] = $attendance_id;
  370. $data_array['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  371. $filter_type = 'today';
  372. if (!empty($_REQUEST['filter'])) {
  373. $filter_type = $_REQUEST['filter'];
  374. }
  375. $my_calendar_id = null;
  376. if (is_numeric($filter_type)) {
  377. $my_calendar_id = $filter_type;
  378. $filter_type = 'calendar_id';
  379. }
  380. $data_array['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type, $my_calendar_id);
  381. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  382. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  383. } else {
  384. if (!empty($student_id)) {
  385. $user_id = intval($student_id);
  386. } else {
  387. $user_id = api_get_user_id();
  388. }
  389. $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id);
  390. $data_array['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id);
  391. $data_array['user_id'] = $user_id;
  392. }
  393. $data_array['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  394. //Set headers pdf
  395. $courseCategory = CourseManager::get_course_category($courseInfo['category_code']);
  396. $teacherInfo = CourseManager::get_teacher_list_from_course_code($courseInfo['real_id']);
  397. $teacherName = null;
  398. foreach ($teacherInfo as $dados) {
  399. if ($teacherName != null)
  400. $teacherName = $teacherName . " / ";
  401. $teacherName.= $dados['firstname']." ".$dados['lastname'];
  402. }
  403. // Get data table - Marco - ordenacao fixa - just fullname
  404. $data_table = array();
  405. $head_table = array('#', get_lang('Name'));
  406. foreach ($data_array['attendant_calendar'] as $class_day) {
  407. //$head_table[] = api_format_date($class_day['date_time'], DATE_FORMAT_SHORT).' <br />'.api_format_date($class_day['date_time'], TIME_NO_SEC_FORMAT);
  408. $head_table[] = api_format_date($class_day['date_time'], DATE_FORMAT_NUMBER_NO_YEAR);
  409. }
  410. $data_table[] = $head_table;
  411. $dataClass = array();
  412. $max_dates_per_page = 10;
  413. $data_attendant_calendar = $data_array['attendant_calendar'];
  414. $data_users_presence = $data_array['users_presence'];
  415. $count = 1;
  416. if (!empty($data_array['users_in_course'])) {
  417. foreach ($data_array['users_in_course'] as $user) {
  418. $cols = 1;
  419. $result = array();
  420. $result['count'] = $count;
  421. $result['full_name'] = api_get_person_name($user['firstname'], $user['lastname']);
  422. foreach ($data_array['attendant_calendar'] as $class_day) {
  423. if ($class_day['done_attendance'] == 1) {
  424. if ($data_users_presence[$user['user_id']][$class_day['id']]['presence'] == 1)
  425. $result[$class_day['id']] = get_lang('UserAttendedSymbol');
  426. else
  427. $result[$class_day['id']] = get_lang('UserNotAttendedSymbol');
  428. } else {
  429. $result[$class_day['id']] = " ";
  430. }
  431. $cols++;
  432. }
  433. $count++;
  434. $data_table[] = $result;
  435. }
  436. }
  437. $max_cols_per_page = 12; //10 dates + 2 name and number
  438. $max_dates_per_page = $max_dates_per_page_original = $max_cols_per_page - 2;//10
  439. $rows = count($data_table);
  440. if ($cols > $max_cols_per_page) {
  441. $number_tables = round(($cols-2)/$max_dates_per_page);
  442. $headers = $data_table[0];
  443. $all = array();
  444. $tables = array();
  445. $changed = 1;
  446. for ($i= 0; $i <= $rows; $i++) {
  447. $row = $data_table[$i];
  448. $key = 1;
  449. $max_dates_per_page = 10;
  450. $item = $data_table[$i];
  451. $count_j = 0;
  452. if (!empty($item)) {
  453. foreach ($item as $value) {
  454. if ($count_j >= $max_dates_per_page) {
  455. $key++;
  456. $max_dates_per_page = $max_dates_per_page_original*$key;
  457. //magic hack
  458. $tables[$key][$i][] = $tables[1][$i][0];
  459. $tables[$key][$i][] = $tables[1][$i][1];
  460. }
  461. $tables[$key][$i][] = $value;
  462. $count_j++;
  463. }
  464. }
  465. }
  466. $content = null;
  467. if (!empty($tables)) {
  468. foreach ($tables as $sub_table) {
  469. $content .= Export::convert_array_to_html($sub_table).'<br /><br />';
  470. }
  471. }
  472. } else {
  473. $content .= Export::convert_array_to_html($data_table, array('header_attributes' => array('align' => 'center')));
  474. }
  475. $params = array(
  476. 'filename' => get_lang('Attendance').'-'.api_get_local_time(),
  477. 'pdf_title' => $courseInfo['title'],
  478. 'course_code' => $courseInfo['code'],
  479. 'add_signatures' => true,
  480. 'orientation' => 'landscape',
  481. 'pdf_teachers' => $teacherName,
  482. 'pdf_course_category' => $courseCategory['name'],
  483. 'format' => 'A4-L',
  484. 'orientation' => 'L'
  485. );
  486. Export::export_html_to_pdf($content, $params);
  487. exit;
  488. }
  489. }