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