attendance_controller.php 20 KB

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