attendance_controller.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * @package chamilo.attendance
  10. */
  11. /**
  12. * Controller script. Prepares the common background variables to give to the scripts corresponding to
  13. * the requested action
  14. * @package chamilo.attendance
  15. */
  16. class AttendanceController
  17. {
  18. /**
  19. * Constructor
  20. */
  21. public function __construct() {
  22. $this->toolname = 'attendance';
  23. $this->view = new View($this->toolname);
  24. }
  25. /**
  26. * It's used for listing attendace,
  27. * render to attendance_list view
  28. * @param boolean true for listing history (optional)
  29. * @param array message for showing by action['edit','add','delete'] (optional)
  30. */
  31. public function attendance_list($history=false,$messages=array()) {
  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. $attendance = new Attendance();
  46. $data = array();
  47. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  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. $last_id = $attendance->attendance_add($link_to_gradebook);
  59. Security::clear_token();
  60. }
  61. if ($last_id) {
  62. $this->attendance_sheet('calendar_list',$last_id);
  63. } else {
  64. $data['error_attendance_add'] = true;
  65. $this->view->set_data($data);
  66. $this->view->set_layout('layout');
  67. $this->view->set_template('attendance_add');
  68. $this->view->render();
  69. }
  70. } else {
  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. }
  77. /**
  78. * It's used for editing attendace,
  79. * render to attendance_edit or attendance_list view
  80. * @param int attendance id
  81. */
  82. public function attendance_edit($attendance_id) {
  83. $attendance = new Attendance();
  84. $data = array();
  85. $attendance_id = intval($attendance_id);
  86. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  87. $check = Security::check_token();
  88. if ($check) {
  89. $attendance->set_name($_POST['title']);
  90. $attendance->set_description($_POST['description']);
  91. $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
  92. $attendance->set_attendance_weight($_POST['attendance_weight']);
  93. $link_to_gradebook = false;
  94. if ( isset($_POST['attendance_qualify_gradebook']) && $_POST['attendance_qualify_gradebook'] == 1 ) {
  95. $link_to_gradebook = true;
  96. }
  97. $affected_rows = $attendance->attendance_edit($attendance_id,$link_to_gradebook);
  98. Security::clear_token();
  99. }
  100. if ($affected_rows) {
  101. $data['message_edit'] = true;
  102. }
  103. $this->attendance_list();
  104. } else {
  105. // default values
  106. $attendance_data = $attendance->get_attendance_by_id($attendance_id);
  107. $data['attendance_id'] = $attendance_data['id'];
  108. $data['title'] = $attendance_data['name'];
  109. $data['description'] = $attendance_data['description'];
  110. $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title'];
  111. $data['attendance_weight'] = $attendance_data['attendance_weight'];
  112. $this->view->set_data($data);
  113. $this->view->set_layout('layout');
  114. $this->view->set_template('attendance_edit');
  115. $this->view->render();
  116. }
  117. }
  118. /**
  119. * It's used for delete attendace,
  120. * render to attendance_list view
  121. * @param int attendance id
  122. */
  123. public function attendance_delete($attendance_id) {
  124. $attendance = new Attendance();
  125. //$attendance_id = intval($attendance_id);
  126. if (!empty($attendance_id)) {
  127. $affected_rows = $attendance->attendance_delete($attendance_id);
  128. }
  129. if ($affected_rows) {
  130. $message['message_attendance_delete'] = true;
  131. }
  132. $this->attendance_list();
  133. }
  134. /**
  135. * It's used for controlling attendace sheet (list, add),
  136. * render to attendance_sheet view
  137. * @param string action
  138. * @param int attendance id
  139. */
  140. public function attendance_sheet($action, $attendance_id, $student_id = 0) {
  141. $attendance = new Attendance();
  142. $data = array();
  143. $data['attendance_id'] = $attendance_id;
  144. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  145. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id);
  146. if (api_is_allowed_to_edit(null, true)) {
  147. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  148. } else {
  149. if (!empty($student_id)) {
  150. $user_id = intval($student_id);
  151. } else {
  152. $user_id = api_get_user_id();
  153. }
  154. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id);
  155. $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id);
  156. $data['user_id'] = $user_id;
  157. }
  158. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  159. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  160. if (isset($_POST['hidden_input'])) {
  161. foreach ($_POST['hidden_input'] as $cal_id) {
  162. $users_present = array();
  163. if (isset($_POST['check_presence'][$cal_id])) {
  164. $users_present = $_POST['check_presence'][$cal_id];
  165. }
  166. $affected_rows = $attendance->attendance_sheet_add($cal_id,$users_present,$attendance_id);
  167. }
  168. }
  169. $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id);
  170. $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id);
  171. $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id);
  172. $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
  173. }
  174. $this->view->set_data($data);
  175. $this->view->set_layout('layout');
  176. $this->view->set_template('attendance_sheet');
  177. $this->view->render();
  178. }
  179. /**
  180. * It's used for controlling attendace calendar (list, add, edit, delete),
  181. * render to attendance_calendar view
  182. * @param string action (optional, by default 'calendar_list')
  183. * @param int attendance id (optional)
  184. * @param int calendar id (optional)
  185. */
  186. public function attendance_calendar($action = 'calendar_list',$attendance_id = 0, $calendar_id = 0) {
  187. $attendance = new Attendance();
  188. $calendar_id = intval($calendar_id);
  189. $data = array();
  190. $data['attendance_id'] = $attendance_id;
  191. $attendance_id = intval($attendance_id);
  192. if ($action == 'calendar_add') {
  193. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  194. if (!isset($_POST['cancel'])) {
  195. $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
  196. $datetimezone = api_get_utc_datetime($datetime);
  197. if (!empty($datetime)) {
  198. $attendance->set_date_time($datetimezone);
  199. $affected_rows = $attendance->attendant_calendar_add($attendance_id);
  200. } else {
  201. $data['error_date'] = true;
  202. }
  203. $action = 'calendar_list';
  204. } else {
  205. $action = 'calendar_list';
  206. }
  207. }
  208. } else if ($action == 'calendar_edit') {
  209. $data['calendar_id'] = $calendar_id;
  210. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  211. if (!isset($_POST['cancel'])) {
  212. $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
  213. $datetimezone = api_get_utc_datetime($datetime);
  214. $attendance->set_date_time($datetimezone);
  215. $affected_rows = $attendance->attendant_calendar_edit($calendar_id, $attendance_id);
  216. $data['calendar_id'] = 0;
  217. $action = 'calendar_list';
  218. } else {
  219. $action = 'calendar_list';
  220. }
  221. }
  222. } else if ($action == 'calendar_delete') {
  223. $affected_rows = $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
  224. $action = 'calendar_list';
  225. } else if ($action == 'calendar_all_delete') {
  226. $affected_rows = $attendance->attendance_calendar_delete(0, $attendance_id, true);
  227. $action = 'calendar_list';
  228. }
  229. $data['action'] = $action;
  230. $data['attendance_calendar'] = $attendance->get_attendance_calendar($attendance_id);
  231. // render to the view
  232. $this->view->set_data($data);
  233. $this->view->set_layout('layout');
  234. $this->view->set_template('attendance_calendar');
  235. $this->view->render();
  236. }
  237. }
  238. ?>