| Total Complexity | 97 |
| Total Lines | 714 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AttendanceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AttendanceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AttendanceController |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | */ |
||
| 21 | public function __construct() |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * It's used for listing attendance, |
||
| 29 | * render to attendance_list view. |
||
| 30 | */ |
||
| 31 | public function attendance_list() |
||
| 32 | { |
||
| 33 | // render to the view |
||
| 34 | $this->view->set_data([]); |
||
| 35 | $this->view->set_layout('layout'); |
||
| 36 | $this->view->set_template('attendance_list'); |
||
| 37 | $this->view->render(); |
||
| 38 | } |
||
| 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 = []; |
||
| 48 | if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { |
||
| 49 | if (!empty($_POST['title'])) { |
||
| 50 | $check = Security::check_token(); |
||
| 51 | $attendanceId = 0; |
||
| 52 | if ($check) { |
||
| 53 | $attendance->set_name($_POST['title']); |
||
| 54 | $attendance->set_description($_POST['description']); |
||
| 55 | $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']); |
||
| 56 | $attendance->set_attendance_weight($_POST['attendance_weight']); |
||
| 57 | $link_to_gradebook = false; |
||
| 58 | if (isset($_POST['attendance_qualify_gradebook']) && |
||
| 59 | $_POST['attendance_qualify_gradebook'] == 1 |
||
| 60 | ) { |
||
| 61 | $link_to_gradebook = true; |
||
| 62 | } |
||
| 63 | $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : 0; |
||
| 64 | $attendanceId = $attendance->attendance_add($link_to_gradebook); |
||
| 65 | |||
| 66 | if ($attendanceId) { |
||
| 67 | $form = new FormValidator('attendance_add'); |
||
| 68 | Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendanceId); |
||
| 69 | } |
||
| 70 | Security::clear_token(); |
||
| 71 | } |
||
| 72 | header('Location: index.php?action=calendar_add&attendance_id='.$attendanceId.'&'.api_get_cidreq()); |
||
| 73 | exit; |
||
| 74 | } else { |
||
| 75 | $data['error'] = true; |
||
| 76 | $this->view->set_data($data); |
||
| 77 | $this->view->set_layout('layout'); |
||
| 78 | $this->view->set_template('attendance_add'); |
||
| 79 | $this->view->render(); |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | $this->view->set_data($data); |
||
| 83 | $this->view->set_layout('layout'); |
||
| 84 | $this->view->set_template('attendance_add'); |
||
| 85 | $this->view->render(); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * It's used for editing attendance, |
||
| 91 | * render to attendance_edit or attendance_list view. |
||
| 92 | * |
||
| 93 | * @param int $attendance_id |
||
| 94 | */ |
||
| 95 | public function attendance_edit($attendance_id) |
||
| 96 | { |
||
| 97 | $attendance = new Attendance(); |
||
| 98 | $data = []; |
||
| 99 | $attendance_id = intval($attendance_id); |
||
| 100 | |||
| 101 | if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { |
||
| 102 | if (!empty($_POST['title'])) { |
||
| 103 | $check = Security::check_token(); |
||
| 104 | if ($check) { |
||
| 105 | $attendance->set_name($_POST['title']); |
||
| 106 | $attendance->set_description($_POST['description']); |
||
| 107 | if (isset($_POST['attendance_qualify_title'])) { |
||
| 108 | $attendance->set_attendance_qualify_title( |
||
| 109 | $_POST['attendance_qualify_title'] |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (isset($_POST['attendance_weight'])) { |
||
| 114 | $attendance->set_attendance_weight( |
||
| 115 | $_POST['attendance_weight'] |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : ''; |
||
| 120 | $link_to_gradebook = false; |
||
| 121 | if (isset($_POST['attendance_qualify_gradebook']) && |
||
| 122 | $_POST['attendance_qualify_gradebook'] == 1 |
||
| 123 | ) { |
||
| 124 | $link_to_gradebook = true; |
||
| 125 | } |
||
| 126 | $attendance->attendance_edit($attendance_id, $link_to_gradebook); |
||
| 127 | |||
| 128 | $form = new FormValidator('attendance_edit'); |
||
| 129 | Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendance_id); |
||
| 130 | Display::addFlash(Display::return_message(get_lang('Updated'))); |
||
| 131 | |||
| 132 | Security::clear_token(); |
||
| 133 | header('Location:index.php?action=attendance_list&'.api_get_cidreq()); |
||
| 134 | exit; |
||
| 135 | } |
||
| 136 | } else { |
||
| 137 | $data['attendance_id'] = $_POST['attendance_id']; |
||
| 138 | $data['error'] = true; |
||
| 139 | $this->view->set_data($data); |
||
| 140 | $this->view->set_layout('layout'); |
||
| 141 | $this->view->set_template('attendance_edit'); |
||
| 142 | $this->view->render(); |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | // default values |
||
| 146 | $attendance_data = $attendance->get_attendance_by_id( |
||
| 147 | $attendance_id |
||
| 148 | ); |
||
| 149 | $data['attendance_id'] = $attendance_data['id']; |
||
| 150 | $data['title'] = $attendance_data['name']; |
||
| 151 | $data['description'] = $attendance_data['description']; |
||
| 152 | $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title']; |
||
| 153 | $data['attendance_weight'] = $attendance_data['attendance_weight']; |
||
| 154 | |||
| 155 | $this->view->set_data($data); |
||
| 156 | $this->view->set_layout('layout'); |
||
| 157 | $this->view->set_template('attendance_edit'); |
||
| 158 | $this->view->render(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * It's used for delete attendaces |
||
| 164 | * render to attendance_list view. |
||
| 165 | * |
||
| 166 | * @param int $attendance_id |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function attendance_delete($attendance_id) |
||
| 171 | { |
||
| 172 | $allowDeleteAttendance = api_get_setting('allow_delete_attendance'); |
||
| 173 | if ($allowDeleteAttendance !== 'true') { |
||
| 174 | $this->attendance_list(); |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | $attendance = new Attendance(); |
||
| 180 | if (!empty($attendance_id)) { |
||
| 181 | $affected_rows = $attendance->attendance_delete($attendance_id); |
||
| 182 | Skill::deleteSkillsFromItem($attendance_id, ITEM_TYPE_ATTENDANCE); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ($affected_rows) { |
||
| 186 | $message['message_attendance_delete'] = true; |
||
| 187 | } |
||
| 188 | $this->attendance_list(); |
||
| 189 | |||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * It's used for make attendance visible |
||
| 195 | * render to attendance_list view. |
||
| 196 | * |
||
| 197 | * @param int $attendanceId |
||
| 198 | */ |
||
| 199 | public function attendanceSetVisible($attendanceId) |
||
| 200 | { |
||
| 201 | $attendance = new Attendance(); |
||
| 202 | $affectedRows = null; |
||
| 203 | if (!empty($attendanceId)) { |
||
| 204 | $affectedRows = $attendance->changeVisibility($attendanceId, 1); |
||
| 205 | } |
||
| 206 | if ($affectedRows) { |
||
| 207 | $message['message_attendance_delete'] = true; |
||
| 208 | } |
||
| 209 | $this->attendance_list(); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * It's used for make attendance invisible |
||
| 214 | * render to attendance_list view. |
||
| 215 | * |
||
| 216 | * @param int $attendanceId |
||
| 217 | */ |
||
| 218 | public function attendanceSetInvisible($attendanceId) |
||
| 219 | { |
||
| 220 | $attendance = new Attendance(); |
||
| 221 | if (!empty($attendanceId)) { |
||
| 222 | $affectedRows = $attendance->changeVisibility($attendanceId, 0); |
||
| 223 | } |
||
| 224 | if ($affectedRows) { |
||
| 225 | $message['message_attendance_delete'] = true; |
||
| 226 | } |
||
| 227 | $this->attendance_list(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Restores an attendance entry and fallback to attendances rendering. |
||
| 232 | * |
||
| 233 | * @param int $attendance_id |
||
| 234 | */ |
||
| 235 | public function attendance_restore($attendance_id) |
||
| 236 | { |
||
| 237 | $attendance = new Attendance(); |
||
| 238 | $affected_rows = false; |
||
| 239 | if (!empty($attendance_id)) { |
||
| 240 | $affected_rows = $attendance->attendance_restore($attendance_id); |
||
| 241 | } |
||
| 242 | if ($affected_rows) { |
||
| 243 | $message['message_attendance_restore'] = true; |
||
| 244 | } |
||
| 245 | $this->attendance_list(); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Lock or unlock an attendance |
||
| 250 | * render to attendance_list view. |
||
| 251 | * |
||
| 252 | * @param string $action (lock_attendance or unlock_attendance) |
||
| 253 | * @param int $attendance_id |
||
| 254 | * render to attendance_list view |
||
| 255 | */ |
||
| 256 | public function lock_attendance($action, $attendance_id) |
||
| 257 | { |
||
| 258 | $attendance = new Attendance(); |
||
| 259 | $attendance_id = intval($attendance_id); |
||
| 260 | |||
| 261 | if ($action == 'lock_attendance') { |
||
| 262 | $result = $attendance->lock_attendance($attendance_id); |
||
| 263 | } else { |
||
| 264 | $result = $attendance->lock_attendance($attendance_id, false); |
||
| 265 | } |
||
| 266 | if ($result) { |
||
| 267 | $message['message_locked_attendance'] = true; |
||
| 268 | } |
||
| 269 | $this->attendance_list(); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function export($id, $type = 'pdf') |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * It's used for controlling attendance sheet (list, add), |
||
| 279 | * render to attendance_sheet view. |
||
| 280 | * |
||
| 281 | * @param string $action |
||
| 282 | * @param int $attendance_id |
||
| 283 | * @param int $student_id |
||
| 284 | * @param bool $edit |
||
| 285 | */ |
||
| 286 | public function attendance_sheet( |
||
| 287 | $action, |
||
| 288 | $attendance_id, |
||
| 289 | $student_id = 0, |
||
| 290 | $edit = true |
||
| 291 | ) { |
||
| 292 | $attendance = new Attendance(); |
||
| 293 | $data = []; |
||
| 294 | $data['attendance_id'] = $attendance_id; |
||
| 295 | $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null; |
||
| 296 | $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId); |
||
| 297 | |||
| 298 | $filter_type = 'today'; |
||
| 299 | if (!empty($_REQUEST['filter'])) { |
||
| 300 | $filter_type = $_REQUEST['filter']; |
||
| 301 | } |
||
| 302 | |||
| 303 | $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh( |
||
| 304 | api_get_user_id(), |
||
| 305 | api_get_course_info() |
||
| 306 | ) || api_is_drh(); |
||
| 307 | |||
| 308 | if ($edit == true) { |
||
| 309 | if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) { |
||
| 310 | $data['users_presence'] = $attendance->get_users_attendance_sheet( |
||
| 311 | $attendance_id, |
||
| 312 | 0, |
||
| 313 | $groupId |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | } else { |
||
| 317 | if (!empty($student_id)) { |
||
| 318 | $user_id = intval($student_id); |
||
| 319 | } else { |
||
| 320 | $user_id = api_get_user_id(); |
||
| 321 | } |
||
| 322 | |||
| 323 | if (api_is_allowed_to_edit(null, true) || |
||
| 324 | api_is_coach(api_get_session_id(), api_get_course_int_id()) || |
||
| 325 | $isDrhOfCourse |
||
| 326 | ) { |
||
| 327 | $data['users_presence'] = $attendance->get_users_attendance_sheet( |
||
| 328 | $attendance_id, |
||
| 329 | 0, |
||
| 330 | $groupId |
||
| 331 | ); |
||
| 332 | } else { |
||
| 333 | $data['users_presence'] = $attendance->get_users_attendance_sheet( |
||
| 334 | $attendance_id, |
||
| 335 | $user_id, |
||
| 336 | $groupId |
||
| 337 | ); |
||
| 338 | } |
||
| 339 | |||
| 340 | $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId); |
||
| 341 | $data['user_id'] = $user_id; |
||
| 342 | } |
||
| 343 | |||
| 344 | $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id( |
||
| 345 | $attendance_id |
||
| 346 | ); |
||
| 347 | $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime( |
||
| 348 | $attendance_id |
||
| 349 | ); |
||
| 350 | |||
| 351 | if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { |
||
| 352 | if (isset($_POST['hidden_input'])) { |
||
| 353 | foreach ($_POST['hidden_input'] as $cal_id) { |
||
| 354 | $users_present = []; |
||
| 355 | if (isset($_POST['check_presence'][$cal_id])) { |
||
| 356 | $users_present = $_POST['check_presence'][$cal_id]; |
||
| 357 | } |
||
| 358 | $attendance->attendance_sheet_add( |
||
| 359 | $cal_id, |
||
| 360 | $users_present, |
||
| 361 | $attendance_id |
||
| 362 | ); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId); |
||
| 367 | $my_calendar_id = null; |
||
| 368 | if (is_numeric($filter_type)) { |
||
| 369 | $my_calendar_id = $filter_type; |
||
| 370 | $filter_type = 'calendar_id'; |
||
| 371 | } |
||
| 372 | $data['attendant_calendar'] = $attendance->get_attendance_calendar( |
||
| 373 | $attendance_id, |
||
| 374 | $filter_type, |
||
| 375 | $my_calendar_id, |
||
| 376 | $groupId |
||
| 377 | ); |
||
| 378 | $data['attendant_calendar_all'] = $attendance->get_attendance_calendar( |
||
| 379 | $attendance_id, |
||
| 380 | 'all', |
||
| 381 | null, |
||
| 382 | $groupId |
||
| 383 | ); |
||
| 384 | $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId); |
||
| 385 | $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id); |
||
| 386 | $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime($attendance_id); |
||
| 387 | } else { |
||
| 388 | $data['attendant_calendar_all'] = $attendance->get_attendance_calendar( |
||
| 389 | $attendance_id, |
||
| 390 | 'all', |
||
| 391 | null, |
||
| 392 | $groupId |
||
| 393 | ); |
||
| 394 | $data['attendant_calendar'] = $attendance->get_attendance_calendar( |
||
| 395 | $attendance_id, |
||
| 396 | $filter_type, |
||
| 397 | null, |
||
| 398 | $groupId |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | |||
| 402 | $data['edit_table'] = intval($edit); |
||
| 403 | $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id); |
||
| 404 | $this->view->set_data($data); |
||
| 405 | $this->view->set_layout('layout'); |
||
| 406 | $this->view->set_template('attendance_sheet'); |
||
| 407 | $this->view->render(); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * It's used for controlling attendance calendar (list, add, edit, delete), |
||
| 412 | * render to attendance_calendar view. |
||
| 413 | * |
||
| 414 | * @param string $action (optional, by default 'calendar_list') |
||
| 415 | * @param int $attendance_id (optional) |
||
| 416 | * @param int $calendar_id (optional) |
||
| 417 | */ |
||
| 418 | public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0) |
||
| 419 | { |
||
| 420 | $attendance = new Attendance(); |
||
| 421 | $calendar_id = intval($calendar_id); |
||
| 422 | $data = []; |
||
| 423 | $data['attendance_id'] = $attendance_id; |
||
| 424 | $attendance_id = intval($attendance_id); |
||
| 425 | $groupList = isset($_POST['groups']) ? [$_POST['groups']] : []; |
||
| 426 | |||
| 427 | if ($action == 'calendar_add') { |
||
| 428 | if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") { |
||
| 429 | if (!isset($_POST['cancel'])) { |
||
| 430 | if (isset($_POST['repeat'])) { |
||
| 431 | //@todo check this error_logs |
||
| 432 | $start_datetime = api_strtotime( |
||
| 433 | api_get_utc_datetime($_POST['date_time']), |
||
| 434 | 'UTC' |
||
| 435 | ); |
||
| 436 | |||
| 437 | $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC'); |
||
| 438 | $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59')); |
||
| 439 | |||
| 440 | $repeat_type = $_POST['repeat_type']; |
||
| 441 | if (($end_datetime > $start_datetime) && $checkdate) { |
||
| 442 | $attendance->attendance_repeat_calendar_add( |
||
| 443 | $attendance_id, |
||
| 444 | $start_datetime, |
||
| 445 | $end_datetime, |
||
| 446 | $repeat_type, |
||
| 447 | $groupList |
||
| 448 | ); |
||
| 449 | $action = 'calendar_list'; |
||
| 450 | } else { |
||
| 451 | if (!$checkdate) { |
||
| 452 | $data['error_checkdate'] = true; |
||
| 453 | } else { |
||
| 454 | $data['error_repeat_date'] = true; |
||
| 455 | } |
||
| 456 | $data['repeat'] = true; |
||
| 457 | $action = 'calendar_add'; |
||
| 458 | } |
||
| 459 | } else { |
||
| 460 | $datetime = $_POST['date_time']; |
||
| 461 | $datetimezone = api_get_utc_datetime($datetime); |
||
| 462 | if (!empty($datetime)) { |
||
| 463 | $attendance->set_date_time($datetimezone); |
||
| 464 | $attendance->attendance_calendar_add($attendance_id, $groupList); |
||
| 465 | $action = 'calendar_list'; |
||
| 466 | } else { |
||
| 467 | $data['error_date'] = true; |
||
| 468 | $action = 'calendar_add'; |
||
| 469 | } |
||
| 470 | } |
||
| 471 | } else { |
||
| 472 | $action = 'calendar_list'; |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } elseif ($action === 'calendar_edit') { |
||
| 476 | $data['calendar_id'] = $calendar_id; |
||
| 477 | if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") { |
||
| 478 | if (!isset($_POST['cancel'])) { |
||
| 479 | $datetime = $_POST['date_time']; |
||
| 480 | $datetimezone = api_get_utc_datetime($datetime); |
||
| 481 | $attendance->set_date_time($datetimezone); |
||
| 482 | $attendance->attendance_calendar_edit($calendar_id, $attendance_id); |
||
| 483 | $data['calendar_id'] = 0; |
||
| 484 | $action = 'calendar_list'; |
||
| 485 | } else { |
||
| 486 | $action = 'calendar_list'; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } elseif ($action == 'calendar_delete') { |
||
| 490 | $attendance->attendance_calendar_delete($calendar_id, $attendance_id); |
||
| 491 | $action = 'calendar_list'; |
||
| 492 | } elseif ($action == 'calendar_all_delete') { |
||
| 493 | $attendance->attendance_calendar_delete(0, $attendance_id, true); |
||
| 494 | $action = 'calendar_list'; |
||
| 495 | } |
||
| 496 | |||
| 497 | $data['action'] = $action; |
||
| 498 | $data['attendance_calendar'] = $attendance->get_attendance_calendar( |
||
| 499 | $attendance_id, |
||
| 500 | 'all', |
||
| 501 | null, |
||
| 502 | null, |
||
| 503 | true |
||
| 504 | ); |
||
| 505 | $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id); |
||
| 506 | // render to the view |
||
| 507 | $this->view->set_data($data); |
||
| 508 | $this->view->set_layout('layout'); |
||
| 509 | $this->view->set_template('attendance_calendar'); |
||
| 510 | $this->view->render(); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * It's used to print attendance sheet. |
||
| 515 | * |
||
| 516 | * @param string $action |
||
| 517 | * @param int $attendance_id |
||
| 518 | */ |
||
| 519 | public function attendance_sheet_export_to_pdf( |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Gets attendance base in the table: |
||
| 677 | * TABLE_STATISTIC_TRACK_E_COURSE_ACCESS. |
||
| 678 | * |
||
| 679 | * @param bool $showForm |
||
| 680 | * @param bool $exportToPdf |
||
| 681 | */ |
||
| 682 | public function getAttendanceBaseInLogin($showForm = false, $exportToPdf = true) |
||
| 730 | } |
||
| 731 | } |
||
| 732 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.