| Total Complexity | 66 |
| Total Lines | 664 |
| 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 |
||
| 21 | class CoursesController |
||
| 22 | { |
||
| 23 | private $toolname; |
||
| 24 | private $view; |
||
| 25 | private $model; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor. |
||
| 29 | */ |
||
| 30 | public function __construct() |
||
| 31 | { |
||
| 32 | $this->toolname = 'auth'; |
||
| 33 | $this->view = new View($this->toolname); |
||
| 34 | $this->model = new Auth(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * It's used for listing courses with categories, |
||
| 39 | * render to courses_categories view. |
||
| 40 | * |
||
| 41 | * @param string $action |
||
| 42 | * @param string $category_code |
||
| 43 | * @param string $message |
||
| 44 | * @param string $error |
||
| 45 | * @param string $content |
||
| 46 | * @param array $limit will be used if $random_value is not set. |
||
| 47 | * This array should contains 'start' and 'length' keys |
||
| 48 | * |
||
| 49 | * @internal param \action $string |
||
| 50 | * @internal param \Category $string code (optional) |
||
| 51 | */ |
||
| 52 | public function courses_categories( |
||
| 53 | $action, |
||
| 54 | $category_code = null, |
||
| 55 | $message = '', |
||
| 56 | $error = '', |
||
| 57 | $content = null, |
||
| 58 | $limit = [] |
||
| 59 | ) { |
||
| 60 | $data = []; |
||
| 61 | $listCategories = CoursesAndSessionsCatalog::getCourseCategoriesTree(); |
||
| 62 | |||
| 63 | $data['countCoursesInCategory'] = CourseCategory::countCoursesInCategory($category_code); |
||
| 64 | if ('display_random_courses' === $action) { |
||
| 65 | // Random value is used instead limit filter |
||
| 66 | $data['browse_courses_in_category'] = CoursesAndSessionsCatalog::getCoursesInCategory(null, 12); |
||
| 67 | $data['countCoursesInCategory'] = count($data['browse_courses_in_category']); |
||
| 68 | } else { |
||
| 69 | if (!isset($category_code)) { |
||
| 70 | $category_code = $listCategories['ALL']['code']; // by default first category |
||
| 71 | } |
||
| 72 | $limit = isset($limit) ? $limit : self::getLimitArray(); |
||
| 73 | $listCourses = CoursesAndSessionsCatalog::getCoursesInCategory($category_code, null, $limit); |
||
| 74 | |||
| 75 | $data['browse_courses_in_category'] = $listCourses; |
||
| 76 | } |
||
| 77 | |||
| 78 | $data['list_categories'] = $listCategories; |
||
| 79 | $data['code'] = Security::remove_XSS($category_code); |
||
| 80 | |||
| 81 | // getting all the courses to which the user is subscribed to |
||
| 82 | $curr_user_id = api_get_user_id(); |
||
| 83 | $user_courses = $this->model->get_courses_of_user($curr_user_id); |
||
| 84 | $user_coursecodes = []; |
||
| 85 | |||
| 86 | // we need only the course codes as these will be used to match against the courses of the category |
||
| 87 | if ('' != $user_courses) { |
||
| 88 | foreach ($user_courses as $key => $value) { |
||
| 89 | $user_coursecodes[] = $value['code']; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | if (api_is_drh()) { |
||
| 94 | $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id()); |
||
| 95 | foreach ($courses as $course) { |
||
| 96 | $user_coursecodes[] = $course['code']; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $data['user_coursecodes'] = $user_coursecodes; |
||
| 101 | $data['action'] = $action; |
||
| 102 | $data['message'] = $message; |
||
| 103 | $data['content'] = $content; |
||
| 104 | $data['error'] = $error; |
||
| 105 | $data['catalogShowCoursesSessions'] = 0; |
||
| 106 | $showCoursesSessions = (int) api_get_setting('catalog_show_courses_sessions'); |
||
| 107 | if ($showCoursesSessions > 0) { |
||
| 108 | $data['catalogShowCoursesSessions'] = $showCoursesSessions; |
||
| 109 | } |
||
| 110 | |||
| 111 | // render to the view |
||
| 112 | $this->view->set_data($data); |
||
| 113 | $this->view->set_layout('layout'); |
||
| 114 | $this->view->set_template('courses_categories'); |
||
| 115 | $this->view->render(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $search_term |
||
| 120 | * @param string $message |
||
| 121 | * @param string $error |
||
| 122 | * @param string $content |
||
| 123 | * @param array $limit |
||
| 124 | * @param bool $justVisible Whether to search only in courses visibles in the catalogue |
||
| 125 | */ |
||
| 126 | public function search_courses( |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Unsubscribe user from a course |
||
| 173 | * render to listing view. |
||
| 174 | * |
||
| 175 | * @param string $course_code |
||
| 176 | * @param string $search_term |
||
| 177 | * @param string $category_code |
||
| 178 | */ |
||
| 179 | public function unsubscribe_user_from_course( |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get a HTML button for subscribe to session. |
||
| 208 | * |
||
| 209 | * @param int $sessionId The session ID |
||
| 210 | * @param string $sessionName The session name |
||
| 211 | * @param bool $checkRequirements Optional. |
||
| 212 | * Whether the session has requirement. Default is false |
||
| 213 | * @param bool $includeText Optional. Whether show the text in button |
||
| 214 | * @param bool $btnBing |
||
| 215 | * |
||
| 216 | * @return string The button HTML |
||
| 217 | */ |
||
| 218 | public function getRegisteredInSessionButton( |
||
| 219 | $sessionId, |
||
| 220 | $sessionName, |
||
| 221 | $checkRequirements = false, |
||
| 222 | $includeText = false, |
||
| 223 | $btnBing = false |
||
| 224 | ) { |
||
| 225 | $sessionId = (int) $sessionId; |
||
| 226 | |||
| 227 | if ($btnBing) { |
||
| 228 | $btnBing = 'btn-lg btn-block'; |
||
| 229 | } else { |
||
| 230 | $btnBing = 'btn-sm'; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($checkRequirements) { |
||
| 234 | return $this->getRequirements($sessionId, SequenceResource::SESSION_TYPE, $includeText, $btnBing); |
||
| 235 | } |
||
| 236 | |||
| 237 | $catalogSessionAutoSubscriptionAllowed = false; |
||
| 238 | if ('true' === api_get_setting('catalog_allow_session_auto_subscription')) { |
||
| 239 | $catalogSessionAutoSubscriptionAllowed = true; |
||
| 240 | } |
||
| 241 | |||
| 242 | $url = api_get_path(WEB_CODE_PATH); |
||
| 243 | |||
| 244 | if ($catalogSessionAutoSubscriptionAllowed) { |
||
| 245 | $url .= 'auth/courses.php?'; |
||
| 246 | $url .= http_build_query([ |
||
| 247 | 'action' => 'subscribe_to_session', |
||
| 248 | 'session_id' => $sessionId, |
||
| 249 | ]); |
||
| 250 | |||
| 251 | $result = Display::toolbarButton( |
||
| 252 | get_lang('Subscribe'), |
||
| 253 | $url, |
||
| 254 | 'pencil', |
||
| 255 | 'primary', |
||
| 256 | [ |
||
| 257 | 'class' => $btnBing.' ajax', |
||
| 258 | 'data-title' => get_lang('Are you sure to subscribe?'), |
||
| 259 | 'data-size' => 'md', |
||
| 260 | 'title' => get_lang('Subscribe'), |
||
| 261 | ], |
||
| 262 | $includeText |
||
| 263 | ); |
||
| 264 | } else { |
||
| 265 | $url .= 'inc/email_editor.php?'; |
||
| 266 | $url .= http_build_query([ |
||
| 267 | 'action' => 'subscribe_me_to_session', |
||
| 268 | 'session' => Security::remove_XSS($sessionName), |
||
| 269 | ]); |
||
| 270 | |||
| 271 | $result = Display::toolbarButton( |
||
| 272 | get_lang('Request subscription'), |
||
| 273 | $url, |
||
| 274 | 'pencil', |
||
| 275 | 'primary', |
||
| 276 | ['class' => $btnBing], |
||
| 277 | $includeText |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | $hook = Container::instantiateHook(HookResubscribe::class); |
||
| 282 | if (!empty($hook)) { |
||
| 283 | $hook->setEventData([ |
||
| 284 | 'session_id' => $sessionId, |
||
| 285 | ]); |
||
| 286 | try { |
||
| 287 | $hook->notifyResubscribe(HOOK_EVENT_TYPE_PRE); |
||
| 288 | } catch (Exception $exception) { |
||
| 289 | $result = $exception->getMessage(); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | return $result; |
||
| 294 | } |
||
| 295 | |||
| 296 | public function getRequirements($id, $type, $includeText, $btnBing) |
||
| 297 | { |
||
| 298 | $id = (int) $id; |
||
| 299 | $type = (int) $type; |
||
| 300 | |||
| 301 | $url = api_get_path(WEB_AJAX_PATH); |
||
| 302 | $url .= 'sequence.ajax.php?'; |
||
| 303 | $url .= http_build_query( |
||
| 304 | [ |
||
| 305 | 'a' => 'get_requirements', |
||
| 306 | 'id' => $id, |
||
| 307 | 'type' => $type, |
||
| 308 | ] |
||
| 309 | ); |
||
| 310 | |||
| 311 | return Display::toolbarButton( |
||
| 312 | get_lang('CheckRequirements'), |
||
| 313 | $url, |
||
| 314 | 'shield', |
||
| 315 | 'info', |
||
| 316 | [ |
||
| 317 | 'class' => $btnBing.' ajax', |
||
| 318 | 'data-title' => get_lang('CheckRequirements'), |
||
| 319 | 'data-size' => 'md', |
||
| 320 | 'title' => get_lang('CheckRequirements'), |
||
| 321 | ], |
||
| 322 | $includeText |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Generate a label if the user has been registered in session. |
||
| 328 | * |
||
| 329 | * @return string The label |
||
| 330 | */ |
||
| 331 | public function getAlreadyRegisteredInSessionLabel() |
||
| 332 | { |
||
| 333 | $icon = '<em class="fa fa-graduation-cap"></em>'; |
||
| 334 | |||
| 335 | return Display::div( |
||
| 336 | $icon, |
||
| 337 | [ |
||
| 338 | 'class' => 'btn btn-default btn-sm registered', |
||
| 339 | 'title' => get_lang("Already registered to session"), |
||
| 340 | ] |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get a icon for a session. |
||
| 346 | * |
||
| 347 | * @param string $sessionName The session name |
||
| 348 | * |
||
| 349 | * @return string The icon |
||
| 350 | */ |
||
| 351 | public function getSessionIcon($sessionName) |
||
| 352 | { |
||
| 353 | return Display::return_icon( |
||
| 354 | 'window_list.png', |
||
| 355 | $sessionName, |
||
| 356 | null, |
||
| 357 | ICON_SIZE_MEDIUM |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Return Session catalog rendered view. |
||
| 363 | * |
||
| 364 | * @param array $limit |
||
| 365 | */ |
||
| 366 | public function sessionList($limit = []) |
||
| 367 | { |
||
| 368 | $date = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d'); |
||
| 369 | $hiddenLinks = isset($_GET['hidden_links']) ? 1 == $_GET['hidden_links'] : false; |
||
| 370 | $limit = isset($limit) ? $limit : self::getLimitArray(); |
||
| 371 | |||
| 372 | $countSessions = CoursesAndSessionsCatalog::browseSessions($date, [], false, true); |
||
| 373 | $sessions = CoursesAndSessionsCatalog::browseSessions($date, $limit); |
||
| 374 | |||
| 375 | $pageTotal = ceil($countSessions / $limit['length']); |
||
| 376 | // Do NOT show pagination if only one page or less |
||
| 377 | $pagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($limit['current'], $limit['length'], $pageTotal) : ''; |
||
| 378 | $sessionsBlocks = $this->getFormattedSessionsBlock($sessions); |
||
| 379 | |||
| 380 | // Get session search catalogue URL |
||
| 381 | $courseUrl = CourseCategory::getCourseCategoryUrl( |
||
| 382 | 1, |
||
| 383 | $limit['length'], |
||
| 384 | null, |
||
| 385 | 0, |
||
| 386 | 'subscribe' |
||
| 387 | ); |
||
| 388 | |||
| 389 | $tpl = new Template(); |
||
| 390 | $tpl->assign('actions', self::getTabList(2)); |
||
| 391 | $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses()); |
||
| 392 | $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions()); |
||
| 393 | $tpl->assign('show_tutor', 'true' === api_get_setting('show_session_coach')); |
||
| 394 | $tpl->assign('course_url', $courseUrl); |
||
| 395 | $tpl->assign('catalog_pagination', $pagination); |
||
| 396 | $tpl->assign('hidden_links', $hiddenLinks); |
||
| 397 | $tpl->assign('search_token', Security::get_token()); |
||
| 398 | $tpl->assign('search_date', $date); |
||
| 399 | $tpl->assign('web_session_courses_ajax_url', api_get_path(WEB_AJAX_PATH).'course.ajax.php'); |
||
| 400 | $tpl->assign('sessions', $sessionsBlocks); |
||
| 401 | $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel()); |
||
| 402 | $tpl->assign('catalog_settings', self::getCatalogSearchSettings()); |
||
| 403 | |||
| 404 | $layout = $tpl->get_template('auth/session_catalog.html.twig'); |
||
| 405 | $content = $tpl->fetch($layout); |
||
| 406 | $tpl->assign('content', $content); |
||
| 407 | $tpl->display_one_col_template(); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Show the Session Catalogue with filtered session by course tags. |
||
| 412 | * |
||
| 413 | * @param array $limit Limit info |
||
| 414 | */ |
||
| 415 | public function sessionsListByName(array $limit) |
||
| 416 | { |
||
| 417 | $keyword = isset($_POST['keyword']) ? $_POST['keyword'] : null; |
||
| 418 | $hiddenLinks = isset($_GET['hidden_links']) ? 1 == (int) $_GET['hidden_links'] : false; |
||
| 419 | $courseUrl = CourseCategory::getCourseCategoryUrl( |
||
| 420 | 1, |
||
| 421 | $limit['length'], |
||
| 422 | null, |
||
| 423 | 0, |
||
| 424 | 'subscribe' |
||
| 425 | ); |
||
| 426 | |||
| 427 | $sessions = CoursesAndSessionsCatalog::getSessionsByName($keyword, $limit); |
||
| 428 | $sessionsBlocks = $this->getFormattedSessionsBlock($sessions); |
||
| 429 | |||
| 430 | $tpl = new Template(); |
||
| 431 | $tpl->assign('actions', self::getTabList(2)); |
||
| 432 | $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses()); |
||
| 433 | $tpl->assign('show_sessions', CoursesAndSessionsCatalog::showSessions()); |
||
| 434 | $tpl->assign('show_tutor', 'true' === api_get_setting('show_session_coach') ? true : false); |
||
| 435 | $tpl->assign('course_url', $courseUrl); |
||
| 436 | $tpl->assign('already_subscribed_label', $this->getAlreadyRegisteredInSessionLabel()); |
||
| 437 | $tpl->assign('hidden_links', $hiddenLinks); |
||
| 438 | $tpl->assign('search_token', Security::get_token()); |
||
| 439 | $tpl->assign('keyword', Security::remove_XSS($keyword)); |
||
| 440 | $tpl->assign('sessions', $sessionsBlocks); |
||
| 441 | $tpl->assign('catalog_settings', self::getCatalogSearchSettings()); |
||
| 442 | |||
| 443 | $layout = $tpl->get_template('auth/session_catalog.html.twig'); |
||
| 444 | $content = $tpl->fetch($layout); |
||
| 445 | $tpl->assign('content', $content); |
||
| 446 | $tpl->display_one_col_template(); |
||
| 447 | } |
||
| 448 | |||
| 449 | public static function getCatalogSearchSettings() |
||
| 450 | { |
||
| 451 | $settings = api_get_configuration_value('catalog_settings'); |
||
| 452 | if (empty($settings)) { |
||
| 453 | // Default everything is visible |
||
| 454 | $settings = [ |
||
| 455 | 'sessions' => [ |
||
| 456 | 'by_title' => true, |
||
| 457 | 'by_date' => true, |
||
| 458 | 'by_tag' => true, |
||
| 459 | 'show_session_info' => true, |
||
| 460 | 'show_session_date' => true, |
||
| 461 | ], |
||
| 462 | ]; |
||
| 463 | } |
||
| 464 | |||
| 465 | return $settings; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param int $active |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public static function getTabList($active = 1) |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Show the Session Catalogue with filtered session by course tags. |
||
| 498 | * |
||
| 499 | * @param array $limit Limit info |
||
| 500 | */ |
||
| 501 | public function sessionsListByCoursesTag(array $limit) |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | public static function getLimitArray() |
||
| 538 | { |
||
| 539 | $pageCurrent = isset($_REQUEST['pageCurrent']) ? (int) $_GET['pageCurrent'] : 1; |
||
| 540 | $pageLength = isset($_REQUEST['pageLength']) ? (int) $_GET['pageLength'] : CoursesAndSessionsCatalog::PAGE_LENGTH; |
||
| 541 | |||
| 542 | return [ |
||
| 543 | 'start' => ($pageCurrent - 1) * $pageLength, |
||
| 544 | 'current' => $pageCurrent, |
||
| 545 | 'length' => $pageLength, |
||
| 546 | ]; |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get the formatted data for sessions block to be displayed on Session Catalog page. |
||
| 551 | * |
||
| 552 | * @param array $sessions The session list |
||
| 553 | * |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | private function getFormattedSessionsBlock(array $sessions) |
||
| 685 | } |
||
| 686 | } |
||
| 687 |