| Total Complexity | 63 |
| Total Lines | 634 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CoursesController 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 CoursesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class CoursesController |
||
| 23 | { |
||
| 24 | private $toolname; |
||
| 25 | private $view; |
||
| 26 | private $model; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor. |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 32 | { |
||
| 33 | $this->toolname = 'auth'; |
||
| 34 | $this->view = new View($this->toolname); |
||
| 35 | $this->model = new Auth(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * It's used for listing courses with categories, |
||
| 40 | * render to courses_categories view. |
||
| 41 | * |
||
| 42 | * @param string $action |
||
| 43 | * @param string $category_code |
||
| 44 | * @param string $message |
||
| 45 | * @param string $error |
||
| 46 | * @param string $content |
||
| 47 | * @param array $limit will be used if $random_value is not set. |
||
| 48 | * This array should contains 'start' and 'length' keys |
||
| 49 | * |
||
| 50 | * @internal param \action $string |
||
| 51 | * @internal param \Category $string code (optional) |
||
| 52 | */ |
||
| 53 | public function courses_categories( |
||
| 54 | $action, |
||
| 55 | $category_code = null, |
||
| 56 | $message = '', |
||
| 57 | $error = '', |
||
| 58 | $content = null, |
||
| 59 | $limit = [] |
||
| 60 | ) { |
||
| 61 | $data = []; |
||
| 62 | $listCategories = CoursesAndSessionsCatalog::getCourseCategoriesTree(); |
||
| 63 | |||
| 64 | $data['countCoursesInCategory'] = CourseCategory::countCoursesInCategory($category_code); |
||
| 65 | if ($action === 'display_random_courses') { |
||
| 66 | // Random value is used instead limit filter |
||
| 67 | $data['browse_courses_in_category'] = CoursesAndSessionsCatalog::getCoursesInCategory(null, 12); |
||
| 68 | $data['countCoursesInCategory'] = count($data['browse_courses_in_category']); |
||
| 69 | } else { |
||
| 70 | if (!isset($category_code)) { |
||
| 71 | $category_code = $listCategories['ALL']['code']; // by default first category |
||
| 72 | } |
||
| 73 | $limit = isset($limit) ? $limit : self::getLimitArray(); |
||
| 74 | $listCourses = CoursesAndSessionsCatalog::getCoursesInCategory($category_code, null, $limit); |
||
| 75 | |||
| 76 | $data['browse_courses_in_category'] = $listCourses; |
||
| 77 | } |
||
| 78 | |||
| 79 | $data['list_categories'] = $listCategories; |
||
| 80 | $data['code'] = Security::remove_XSS($category_code); |
||
| 81 | |||
| 82 | // getting all the courses to which the user is subscribed to |
||
| 83 | $curr_user_id = api_get_user_id(); |
||
| 84 | $user_courses = $this->model->get_courses_of_user($curr_user_id); |
||
| 85 | $user_coursecodes = []; |
||
| 86 | |||
| 87 | // we need only the course codes as these will be used to match against the courses of the category |
||
| 88 | if ($user_courses != '') { |
||
| 89 | foreach ($user_courses as $key => $value) { |
||
| 90 | $user_coursecodes[] = $value['code']; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | if (api_is_drh()) { |
||
| 95 | $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id()); |
||
| 96 | foreach ($courses as $course) { |
||
| 97 | $user_coursecodes[] = $course['code']; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $data['user_coursecodes'] = $user_coursecodes; |
||
| 102 | $data['action'] = $action; |
||
| 103 | $data['message'] = $message; |
||
| 104 | $data['content'] = $content; |
||
| 105 | $data['error'] = $error; |
||
| 106 | $data['catalogShowCoursesSessions'] = 0; |
||
| 107 | $showCoursesSessions = (int) api_get_setting('catalog_show_courses_sessions'); |
||
| 108 | if ($showCoursesSessions > 0) { |
||
| 109 | $data['catalogShowCoursesSessions'] = $showCoursesSessions; |
||
| 110 | } |
||
| 111 | |||
| 112 | // render to the view |
||
| 113 | $this->view->set_data($data); |
||
| 114 | $this->view->set_layout('layout'); |
||
| 115 | $this->view->set_template('courses_categories'); |
||
| 116 | $this->view->render(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $search_term |
||
| 121 | * @param string $message |
||
| 122 | * @param string $error |
||
| 123 | * @param string $content |
||
| 124 | * @param array $limit |
||
| 125 | * @param bool $justVisible Whether to search only in courses visibles in the catalogue |
||
| 126 | */ |
||
| 127 | public function search_courses( |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Unsubscribe user from a course |
||
| 174 | * render to listing view. |
||
| 175 | * |
||
| 176 | * @param string $course_code |
||
| 177 | * @param string $search_term |
||
| 178 | * @param string $category_code |
||
| 179 | */ |
||
| 180 | public function unsubscribe_user_from_course( |
||
| 181 | $course_code, |
||
| 182 | $search_term = null, |
||
| 183 | $category_code = null |
||
| 184 | ) { |
||
| 185 | $result = $this->model->remove_user_from_course($course_code); |
||
| 186 | $message = ''; |
||
| 187 | $error = ''; |
||
| 188 | |||
| 189 | if ($result) { |
||
| 190 | Display::addFlash( |
||
| 191 | Display::return_message(get_lang('You have been unsubscribed from this course')) |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | if (!empty($search_term)) { |
||
| 196 | CoursesAndSessionsCatalog::search_courses($search_term, $message, $error); |
||
| 197 | } else { |
||
| 198 | $this->courses_categories( |
||
| 199 | 'subcribe', |
||
| 200 | $category_code, |
||
| 201 | $message, |
||
| 202 | $error |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get a HTML button for subscribe to session. |
||
| 209 | * |
||
| 210 | * @param int $sessionId The session ID |
||
| 211 | * @param string $sessionName The session name |
||
| 212 | * @param bool $checkRequirements Optional. |
||
| 213 | * Whether the session has requirement. Default is false |
||
| 214 | * @param bool $includeText Optional. Whether show the text in button |
||
| 215 | * @param bool $btnBing |
||
| 216 | * |
||
| 217 | * @return string The button HTML |
||
| 218 | */ |
||
| 219 | public function getRegisteredInSessionButton( |
||
| 220 | $sessionId, |
||
| 221 | $sessionName, |
||
| 222 | $checkRequirements = false, |
||
| 223 | $includeText = false, |
||
| 224 | $btnBing = false |
||
| 225 | ) { |
||
| 226 | $sessionId = (int) $sessionId; |
||
| 227 | if ($btnBing) { |
||
| 228 | $btnBing = 'btn-lg btn-block'; |
||
| 229 | } else { |
||
| 230 | $btnBing = 'btn-sm'; |
||
| 231 | } |
||
| 232 | if ($checkRequirements) { |
||
| 233 | $url = api_get_path(WEB_AJAX_PATH); |
||
| 234 | $url .= 'sequence.ajax.php?'; |
||
| 235 | $url .= http_build_query([ |
||
| 236 | 'a' => 'get_requirements', |
||
| 237 | 'id' => $sessionId, |
||
| 238 | 'type' => SequenceResource::SESSION_TYPE, |
||
| 239 | ]); |
||
| 240 | |||
| 241 | return Display::toolbarButton( |
||
| 242 | get_lang('Check requirements'), |
||
| 243 | $url, |
||
| 244 | 'shield', |
||
| 245 | 'info', |
||
| 246 | [ |
||
| 247 | 'class' => $btnBing.' ajax', |
||
| 248 | 'data-title' => get_lang('Check requirements'), |
||
| 249 | 'data-size' => 'md', |
||
| 250 | 'title' => get_lang('Check requirements'), |
||
| 251 | ], |
||
| 252 | $includeText |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | $catalogSessionAutoSubscriptionAllowed = false; |
||
| 257 | if (api_get_setting('catalog_allow_session_auto_subscription') === 'true') { |
||
| 258 | $catalogSessionAutoSubscriptionAllowed = true; |
||
| 259 | } |
||
| 260 | |||
| 261 | $url = api_get_path(WEB_CODE_PATH); |
||
| 262 | |||
| 263 | if ($catalogSessionAutoSubscriptionAllowed) { |
||
| 264 | $url .= 'auth/courses.php?'; |
||
| 265 | $url .= http_build_query([ |
||
| 266 | 'action' => 'subscribe_to_session', |
||
| 267 | 'session_id' => $sessionId, |
||
| 268 | ]); |
||
| 269 | |||
| 270 | $result = Display::toolbarButton( |
||
| 271 | get_lang('Subscribe'), |
||
| 272 | $url, |
||
| 273 | 'pencil', |
||
| 274 | 'primary', |
||
| 275 | [ |
||
| 276 | 'class' => $btnBing.' ajax', |
||
| 277 | 'data-title' => get_lang('Are you sure to subscribe?'), |
||
| 278 | 'data-size' => 'md', |
||
| 279 | 'title' => get_lang('Subscribe'), |
||
| 280 | ], |
||
| 281 | $includeText |
||
| 282 | ); |
||
| 283 | } else { |
||
| 284 | $url .= 'inc/email_editor.php?'; |
||
| 285 | $url .= http_build_query([ |
||
| 286 | 'action' => 'subscribe_me_to_session', |
||
| 287 | 'session' => Security::remove_XSS($sessionName), |
||
| 288 | ]); |
||
| 289 | |||
| 290 | $result = Display::toolbarButton( |
||
| 291 | get_lang('Request subscription'), |
||
| 292 | $url, |
||
| 293 | 'pencil', |
||
| 294 | 'primary', |
||
| 295 | ['class' => $btnBing], |
||
| 296 | $includeText |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | |||
| 300 | $hook = Container::instantiateHook(HookResubscribe::class); |
||
| 301 | if (!empty($hook)) { |
||
| 302 | $hook->setEventData([ |
||
| 303 | 'session_id' => $sessionId, |
||
| 304 | ]); |
||
| 305 | try { |
||
| 306 | $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE); |
||
| 307 | } catch (Exception $exception) { |
||
| 308 | $result = $exception->getMessage(); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | return $result; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Generate a label if the user has been registered in session. |
||
| 317 | * |
||
| 318 | * @return string The label |
||
| 319 | */ |
||
| 320 | public function getAlreadyRegisteredInSessionLabel() |
||
| 329 | ] |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get a icon for a session. |
||
| 335 | * |
||
| 336 | * @param string $sessionName The session name |
||
| 337 | * |
||
| 338 | * @return string The icon |
||
| 339 | */ |
||
| 340 | public function getSessionIcon($sessionName) |
||
| 347 | ); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return Session catalog rendered view. |
||
| 352 | * |
||
| 353 | * @param string $action |
||
| 354 | * @param string $nameTools |
||
| 355 | * @param array $limit |
||
| 356 | */ |
||
| 357 | public function sessionList($action, $nameTools, $limit = []) |
||
| 358 | { |
||
| 359 | $date = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d'); |
||
| 360 | $hiddenLinks = isset($_GET['hidden_links']) ? $_GET['hidden_links'] == 1 : false; |
||
| 361 | $limit = isset($limit) ? $limit : self::getLimitArray(); |
||
| 362 | |||
| 363 | $countSessions = CoursesAndSessionsCatalog::browseSessions($date, [], false, true); |
||
| 364 | $sessions = CoursesAndSessionsCatalog::browseSessions($date, $limit); |
||
| 365 | |||
| 366 | $pageTotal = ceil($countSessions / $limit['length']); |
||
| 367 | // Do NOT show pagination if only one page or less |
||
| 368 | $pagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($limit['current'], $limit['length'], $pageTotal) : ''; |
||
| 369 | $sessionsBlocks = $this->getFormattedSessionsBlock($sessions); |
||
| 370 | |||
| 371 | // Get session search catalogue URL |
||
| 372 | $courseUrl = CourseCategory::getCourseCategoryUrl( |
||
| 373 | 1, |
||
| 374 | $limit['length'], |
||
| 375 | null, |
||
| 376 | 0, |
||
| 377 | 'subscribe' |
||
| 378 | ); |
||
| 379 | |||
| 380 | $tpl = new Template(); |
||
| 381 | $tpl->assign('actions', self::getTabList(2)); |
||
| 382 | $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses()); |
||
| 383 | $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions()); |
||
| 384 | $tpl->assign('show_tutor', api_get_setting('show_session_coach') === 'true'); |
||
| 385 | $tpl->assign('course_url', $courseUrl); |
||
| 386 | $tpl->assign('catalog_pagination', $pagination); |
||
| 387 | $tpl->assign('hidden_links', $hiddenLinks); |
||
| 388 | $tpl->assign('search_token', Security::get_token()); |
||
| 389 | $tpl->assign('search_date', $date); |
||
| 390 | $tpl->assign('web_session_courses_ajax_url', api_get_path(WEB_AJAX_PATH).'course.ajax.php'); |
||
| 391 | $tpl->assign('sessions', $sessionsBlocks); |
||
| 392 | $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel()); |
||
| 393 | |||
| 394 | $layout = $tpl->get_template('auth/session_catalog.html.twig'); |
||
| 395 | $content = $tpl->fetch($layout); |
||
| 396 | $tpl->assign('content', $content); |
||
| 397 | $tpl->display_one_col_template(); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Show the Session Catalogue with filtered session by course tags. |
||
| 402 | * |
||
| 403 | * @param array $limit Limit info |
||
| 404 | */ |
||
| 405 | public function sessionsListByName(array $limit) |
||
| 406 | { |
||
| 407 | $keyword = isset($_POST['keyword']) ? $_POST['keyword'] : null; |
||
| 408 | $hiddenLinks = isset($_GET['hidden_links']) ? (int) $_GET['hidden_links'] == 1 : false; |
||
| 409 | $courseUrl = CourseCategory::getCourseCategoryUrl( |
||
| 410 | 1, |
||
| 411 | $limit['length'], |
||
| 412 | null, |
||
| 413 | 0, |
||
| 414 | 'subscribe' |
||
| 415 | ); |
||
| 416 | |||
| 417 | $sessions = CoursesAndSessionsCatalog::getSessionsByName($keyword, $limit); |
||
| 418 | $sessionsBlocks = $this->getFormattedSessionsBlock($sessions); |
||
| 419 | |||
| 420 | $tpl = new Template(); |
||
| 421 | $tpl->assign('actions', self::getTabList(2)); |
||
| 422 | $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses()); |
||
| 423 | $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions()); |
||
| 424 | $tpl->assign('show_tutor', api_get_setting('show_session_coach') === 'true' ? true : false); |
||
| 425 | $tpl->assign('course_url', $courseUrl); |
||
| 426 | $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel()); |
||
| 427 | $tpl->assign('hidden_links', $hiddenLinks); |
||
| 428 | $tpl->assign('search_token', Security::get_token()); |
||
| 429 | $tpl->assign('keyword', Security::remove_XSS($keyword)); |
||
| 430 | $tpl->assign('sessions', $sessionsBlocks); |
||
| 431 | |||
| 432 | $layout = $tpl->get_template('auth/session_catalog.html.twig'); |
||
| 433 | $content = $tpl->fetch($layout); |
||
| 434 | $tpl->assign('content', $content); |
||
| 435 | $tpl->display_one_col_template(); |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $active |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public static function getTabList($active = 1) |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Show the Session Catalogue with filtered session by course tags. |
||
| 468 | * |
||
| 469 | * @param array $limit Limit info |
||
| 470 | */ |
||
| 471 | public function sessionsListByCoursesTag(array $limit) |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | public static function getLimitArray() |
||
| 508 | { |
||
| 509 | $pageCurrent = isset($_REQUEST['pageCurrent']) ? (int) $_GET['pageCurrent'] : 1; |
||
| 510 | $pageLength = isset($_REQUEST['pageLength']) ? (int) $_GET['pageLength'] : CoursesAndSessionsCatalog::PAGE_LENGTH; |
||
| 511 | |||
| 512 | return [ |
||
| 513 | 'start' => ($pageCurrent - 1) * $pageLength, |
||
| 514 | 'current' => $pageCurrent, |
||
| 515 | 'length' => $pageLength, |
||
| 516 | ]; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get the formatted data for sessions block to be displayed on Session Catalog page. |
||
| 521 | * |
||
| 522 | * @param array $sessions The session list |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | private function getFormattedSessionsBlock(array $sessions) |
||
| 656 | } |
||
| 657 | } |
||
| 658 |