| Total Complexity | 53 |
| Total Lines | 602 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CoursesAndSessionsCatalog 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 CoursesAndSessionsCatalog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CoursesAndSessionsCatalog |
||
| 13 | { |
||
| 14 | const PAGE_LENGTH = 12; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Check the configuration for the courses and sessions catalog. |
||
| 18 | * |
||
| 19 | * @global array $_configuration Configuration |
||
| 20 | * |
||
| 21 | * @param int $value The value to check |
||
| 22 | * |
||
| 23 | * @return bool Whether the configuration is $value |
||
| 24 | */ |
||
| 25 | public static function is($value = CATALOG_COURSES) |
||
| 26 | { |
||
| 27 | $showCoursesSessions = intval(api_get_setting('catalog_show_courses_sessions')); |
||
| 28 | if ($showCoursesSessions == $value) { |
||
| 29 | return true; |
||
| 30 | } |
||
| 31 | |||
| 32 | return false; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Check whether to display the sessions list. |
||
| 37 | * |
||
| 38 | * @global array $_configuration Configuration |
||
| 39 | * |
||
| 40 | * @return bool whether to display |
||
| 41 | */ |
||
| 42 | public static function showSessions() |
||
| 43 | { |
||
| 44 | $catalogShow = intval(api_get_setting('catalog_show_courses_sessions')); |
||
| 45 | |||
| 46 | if ($catalogShow == CATALOG_SESSIONS || $catalogShow == CATALOG_COURSES_SESSIONS) { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | |||
| 50 | return false; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check whether to display the courses list. |
||
| 55 | * |
||
| 56 | * @global array $_configuration Configuration |
||
| 57 | * |
||
| 58 | * @return bool whether to display |
||
| 59 | */ |
||
| 60 | public static function showCourses() |
||
| 61 | { |
||
| 62 | $catalogShow = intval(api_get_setting('catalog_show_courses_sessions')); |
||
| 63 | |||
| 64 | if ($catalogShow == CATALOG_COURSES || $catalogShow == CATALOG_COURSES_SESSIONS) { |
||
| 65 | return true; |
||
| 66 | } |
||
| 67 | |||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public static function getCoursesToAvoid() |
||
| 75 | { |
||
| 76 | $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 77 | $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES); |
||
| 78 | |||
| 79 | // Check special courses |
||
| 80 | $courseListToAvoid = CourseManager::get_special_course_list(); |
||
| 81 | |||
| 82 | // Checks "hide_from_catalog" extra field |
||
| 83 | $extraFieldType = ExtraField::COURSE_FIELD_TYPE; |
||
| 84 | |||
| 85 | $sql = "SELECT item_id FROM $TABLE_COURSE_FIELD_VALUE tcfv |
||
| 86 | INNER JOIN $TABLE_COURSE_FIELD tcf |
||
| 87 | ON tcfv.field_id = tcf.id |
||
| 88 | WHERE |
||
| 89 | tcf.extra_field_type = $extraFieldType AND |
||
| 90 | tcf.variable = 'hide_from_catalog' AND |
||
| 91 | tcfv.value = 1 |
||
| 92 | "; |
||
| 93 | |||
| 94 | $result = Database::query($sql); |
||
| 95 | if (Database::num_rows($result) > 0) { |
||
| 96 | while ($row = Database::fetch_array($result)) { |
||
| 97 | $courseListToAvoid[] = $row['item_id']; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return $courseListToAvoid; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public static function getAvoidCourseCondition() |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get available le courses count. |
||
| 124 | * |
||
| 125 | * @param int $accessUrlId (optional) |
||
| 126 | * |
||
| 127 | * @return int Number of courses |
||
| 128 | */ |
||
| 129 | public static function countAvailableCoursesToShowInCatalog($accessUrlId = 1) |
||
| 130 | { |
||
| 131 | $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 132 | $tableCourseRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 133 | $courseToAvoidCondition = self::getAvoidCourseCondition(); |
||
| 134 | |||
| 135 | $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true); |
||
| 136 | |||
| 137 | $accessUrlId = (int) $accessUrlId; |
||
| 138 | if (empty($accessUrlId)) { |
||
| 139 | $accessUrlId = 1; |
||
| 140 | } |
||
| 141 | |||
| 142 | $sql = "SELECT count(course.id) |
||
| 143 | FROM $tableCourse course |
||
| 144 | INNER JOIN $tableCourseRelAccessUrl u |
||
| 145 | ON (course.id = u.c_id) |
||
| 146 | WHERE |
||
| 147 | u.access_url_id = $accessUrlId AND |
||
| 148 | course.visibility != 0 AND |
||
| 149 | course.visibility != 4 |
||
| 150 | $courseToAvoidCondition |
||
| 151 | $visibilityCondition |
||
| 152 | "; |
||
| 153 | |||
| 154 | $res = Database::query($sql); |
||
| 155 | $row = Database::fetch_row($res); |
||
| 156 | |||
| 157 | return $row[0]; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public static function getCourseCategories() |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Return LIMIT to filter SQL query. |
||
| 210 | * |
||
| 211 | * @param array $limit |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public static function getLimitFilterFromArray($limit) |
||
| 216 | { |
||
| 217 | $limitFilter = ''; |
||
| 218 | if (!empty($limit) && is_array($limit)) { |
||
| 219 | $limitStart = isset($limit['start']) ? $limit['start'] : 0; |
||
| 220 | $limitLength = isset($limit['length']) ? $limit['length'] : 12; |
||
| 221 | $limitFilter = 'LIMIT '.$limitStart.', '.$limitLength; |
||
| 222 | } |
||
| 223 | |||
| 224 | return $limitFilter; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $category_code |
||
| 229 | * @param int $random_value |
||
| 230 | * @param array $limit will be used if $random_value is not set. |
||
| 231 | * This array should contains 'start' and 'length' keys |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public static function getCoursesInCategory($category_code, $random_value = null, $limit = []) |
||
| 236 | { |
||
| 237 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 238 | $avoidCoursesCondition = self::getAvoidCourseCondition(); |
||
| 239 | $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition( |
||
| 240 | 'course', |
||
| 241 | true |
||
| 242 | ); |
||
| 243 | |||
| 244 | if (!empty($random_value)) { |
||
| 245 | $random_value = intval($random_value); |
||
| 246 | |||
| 247 | $sql = "SELECT COUNT(*) FROM $tbl_course"; |
||
| 248 | $result = Database::query($sql); |
||
| 249 | list($num_records) = Database::fetch_row($result); |
||
| 250 | |||
| 251 | if (api_is_multiple_url_enabled()) { |
||
| 252 | $url_access_id = api_get_current_access_url_id(); |
||
| 253 | $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 254 | |||
| 255 | $sql = "SELECT COUNT(*) FROM $tbl_course course |
||
| 256 | INNER JOIN $tbl_url_rel_course as url_rel_course |
||
| 257 | ON (url_rel_course.c_id = course.id) |
||
| 258 | WHERE access_url_id = $url_access_id "; |
||
| 259 | $result = Database::query($sql); |
||
| 260 | list($num_records) = Database::fetch_row($result); |
||
| 261 | |||
| 262 | $sql = "SELECT course.id, course.id as real_id |
||
| 263 | FROM $tbl_course course |
||
| 264 | INNER JOIN $tbl_url_rel_course as url_rel_course |
||
| 265 | ON (url_rel_course.c_id = course.id) |
||
| 266 | WHERE |
||
| 267 | access_url_id = $url_access_id AND |
||
| 268 | RAND()*$num_records< $random_value |
||
| 269 | $avoidCoursesCondition |
||
| 270 | $visibilityCondition |
||
| 271 | ORDER BY RAND() |
||
| 272 | LIMIT 0, $random_value"; |
||
| 273 | } else { |
||
| 274 | $sql = "SELECT id, id as real_id FROM $tbl_course course |
||
| 275 | WHERE |
||
| 276 | RAND()*$num_records< $random_value |
||
| 277 | $avoidCoursesCondition |
||
| 278 | $visibilityCondition |
||
| 279 | ORDER BY RAND() |
||
| 280 | LIMIT 0, $random_value"; |
||
| 281 | } |
||
| 282 | |||
| 283 | $result = Database::query($sql); |
||
| 284 | $id_in = null; |
||
| 285 | while (list($id) = Database::fetch_row($result)) { |
||
| 286 | if ($id_in) { |
||
| 287 | $id_in .= ",$id"; |
||
| 288 | } else { |
||
| 289 | $id_in = "$id"; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | if ($id_in === null) { |
||
| 293 | return []; |
||
| 294 | } |
||
| 295 | $sql = "SELECT *, id as real_id FROM $tbl_course WHERE id IN($id_in)"; |
||
| 296 | } else { |
||
| 297 | $limitFilter = self::getLimitFilterFromArray($limit); |
||
| 298 | $category_code = Database::escape_string($category_code); |
||
| 299 | if (empty($category_code) || $category_code == "ALL") { |
||
| 300 | $sql = "SELECT *, id as real_id |
||
| 301 | FROM $tbl_course course |
||
| 302 | WHERE |
||
| 303 | 1=1 |
||
| 304 | $avoidCoursesCondition |
||
| 305 | $visibilityCondition |
||
| 306 | ORDER BY title $limitFilter "; |
||
| 307 | } else { |
||
| 308 | if ($category_code == 'NONE') { |
||
| 309 | $category_code = ''; |
||
| 310 | } |
||
| 311 | $sql = "SELECT *, id as real_id FROM $tbl_course course |
||
| 312 | WHERE |
||
| 313 | category_code='$category_code' |
||
| 314 | $avoidCoursesCondition |
||
| 315 | $visibilityCondition |
||
| 316 | ORDER BY title $limitFilter "; |
||
| 317 | } |
||
| 318 | |||
| 319 | // Showing only the courses of the current Chamilo access_url_id |
||
| 320 | if (api_is_multiple_url_enabled()) { |
||
| 321 | $url_access_id = api_get_current_access_url_id(); |
||
| 322 | $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 323 | if ($category_code != "ALL") { |
||
| 324 | $sql = "SELECT *, course.id real_id FROM $tbl_course as course |
||
| 325 | INNER JOIN $tbl_url_rel_course as url_rel_course |
||
| 326 | ON (url_rel_course.c_id = course.id) |
||
| 327 | WHERE |
||
| 328 | access_url_id = $url_access_id AND |
||
| 329 | category_code='$category_code' |
||
| 330 | $avoidCoursesCondition |
||
| 331 | $visibilityCondition |
||
| 332 | ORDER BY title $limitFilter"; |
||
| 333 | } else { |
||
| 334 | $sql = "SELECT *, course.id real_id FROM $tbl_course as course |
||
| 335 | INNER JOIN $tbl_url_rel_course as url_rel_course |
||
| 336 | ON (url_rel_course.c_id = course.id) |
||
| 337 | WHERE |
||
| 338 | access_url_id = $url_access_id |
||
| 339 | $avoidCoursesCondition |
||
| 340 | $visibilityCondition |
||
| 341 | ORDER BY title $limitFilter"; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $result = Database::query($sql); |
||
| 347 | $courses = []; |
||
| 348 | while ($row = Database::fetch_array($result)) { |
||
| 349 | $row['registration_code'] = !empty($row['registration_code']); |
||
| 350 | $count_users = CourseManager::get_users_count_in_course($row['code']); |
||
| 351 | $count_connections_last_month = Tracking::get_course_connections_count( |
||
| 352 | $row['id'], |
||
| 353 | 0, |
||
| 354 | api_get_utc_datetime(time() - (30 * 86400)) |
||
| 355 | ); |
||
| 356 | |||
| 357 | if ($row['tutor_name'] == '0') { |
||
| 358 | $row['tutor_name'] = get_lang('NoManager'); |
||
| 359 | } |
||
| 360 | $point_info = CourseManager::get_course_ranking($row['id'], 0); |
||
| 361 | $courses[] = [ |
||
| 362 | 'real_id' => $row['real_id'], |
||
| 363 | 'point_info' => $point_info, |
||
| 364 | 'code' => $row['code'], |
||
| 365 | 'directory' => $row['directory'], |
||
| 366 | 'visual_code' => $row['visual_code'], |
||
| 367 | 'title' => $row['title'], |
||
| 368 | 'tutor' => $row['tutor_name'], |
||
| 369 | 'subscribe' => $row['subscribe'], |
||
| 370 | 'unsubscribe' => $row['unsubscribe'], |
||
| 371 | 'registration_code' => $row['registration_code'], |
||
| 372 | 'creation_date' => $row['creation_date'], |
||
| 373 | 'visibility' => $row['visibility'], |
||
| 374 | 'category' => $row['category_code'], |
||
| 375 | 'count_users' => $count_users, |
||
| 376 | 'count_connections' => $count_connections_last_month, |
||
| 377 | ]; |
||
| 378 | } |
||
| 379 | |||
| 380 | return $courses; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Search the courses database for a course that matches the search term. |
||
| 385 | * The search is done on the code, title and tutor field of the course table. |
||
| 386 | * |
||
| 387 | * @param string $search_term The string that the user submitted, what we are looking for |
||
| 388 | * @param array $limit |
||
| 389 | * @param bool $justVisible search only on visible courses in the catalogue |
||
| 390 | * |
||
| 391 | * @return array an array containing a list of all the courses matching the the search term |
||
| 392 | */ |
||
| 393 | public static function search_courses($search_term, $limit, $justVisible = false) |
||
| 394 | { |
||
| 395 | $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 396 | $limitFilter = self::getLimitFilterFromArray($limit); |
||
| 397 | $avoidCoursesCondition = self::getAvoidCourseCondition(); |
||
| 398 | $visibilityCondition = $justVisible ? CourseManager::getCourseVisibilitySQLCondition('course', true) : ''; |
||
| 399 | |||
| 400 | $search_term_safe = Database::escape_string($search_term); |
||
| 401 | $sql_find = "SELECT * FROM $courseTable course |
||
| 402 | WHERE ( |
||
| 403 | course.code LIKE '%".$search_term_safe."%' OR |
||
| 404 | course.title LIKE '%".$search_term_safe."%' OR |
||
| 405 | course.tutor_name LIKE '%".$search_term_safe."%' |
||
| 406 | ) |
||
| 407 | $avoidCoursesCondition |
||
| 408 | $visibilityCondition |
||
| 409 | ORDER BY title, visual_code ASC |
||
| 410 | $limitFilter |
||
| 411 | "; |
||
| 412 | |||
| 413 | if (api_is_multiple_url_enabled()) { |
||
| 414 | $url_access_id = api_get_current_access_url_id(); |
||
| 415 | if ($url_access_id != -1) { |
||
| 416 | $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 417 | $sql_find = "SELECT * |
||
| 418 | FROM $courseTable as course |
||
| 419 | INNER JOIN $tbl_url_rel_course as url_rel_course |
||
| 420 | ON (url_rel_course.c_id = course.id) |
||
| 421 | WHERE |
||
| 422 | access_url_id = $url_access_id AND ( |
||
| 423 | code LIKE '%".$search_term_safe."%' OR |
||
| 424 | title LIKE '%".$search_term_safe."%' OR |
||
| 425 | tutor_name LIKE '%".$search_term_safe."%' |
||
| 426 | ) |
||
| 427 | $avoidCoursesCondition |
||
| 428 | $visibilityCondition |
||
| 429 | ORDER BY title, visual_code ASC |
||
| 430 | $limitFilter |
||
| 431 | "; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | $result_find = Database::query($sql_find); |
||
| 435 | $courses = []; |
||
| 436 | while ($row = Database::fetch_array($result_find)) { |
||
| 437 | $row['registration_code'] = !empty($row['registration_code']); |
||
| 438 | $count_users = count(CourseManager::get_user_list_from_course_code($row['code'])); |
||
| 439 | $count_connections_last_month = Tracking::get_course_connections_count( |
||
| 440 | $row['id'], |
||
| 441 | 0, |
||
| 442 | api_get_utc_datetime(time() - (30 * 86400)) |
||
| 443 | ); |
||
| 444 | |||
| 445 | $point_info = CourseManager::get_course_ranking($row['id'], 0); |
||
| 446 | |||
| 447 | $courses[] = [ |
||
| 448 | 'real_id' => $row['id'], |
||
| 449 | 'point_info' => $point_info, |
||
| 450 | 'code' => $row['code'], |
||
| 451 | 'directory' => $row['directory'], |
||
| 452 | 'visual_code' => $row['visual_code'], |
||
| 453 | 'title' => $row['title'], |
||
| 454 | 'tutor' => $row['tutor_name'], |
||
| 455 | 'subscribe' => $row['subscribe'], |
||
| 456 | 'unsubscribe' => $row['unsubscribe'], |
||
| 457 | 'registration_code' => $row['registration_code'], |
||
| 458 | 'creation_date' => $row['creation_date'], |
||
| 459 | 'visibility' => $row['visibility'], |
||
| 460 | 'count_users' => $count_users, |
||
| 461 | 'count_connections' => $count_connections_last_month, |
||
| 462 | ]; |
||
| 463 | } |
||
| 464 | |||
| 465 | return $courses; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * List the sessions. |
||
| 470 | * |
||
| 471 | * @param string $date (optional) The date of sessions |
||
| 472 | * @param array $limit |
||
| 473 | * |
||
| 474 | * @return array The session list |
||
| 475 | */ |
||
| 476 | public static function browseSessions($date = null, $limit = []) |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Search sessions by searched term by session name. |
||
| 521 | * |
||
| 522 | * @param string $queryTerm Term for search |
||
| 523 | * @param array $limit Limit info |
||
| 524 | * |
||
| 525 | * @return array The sessions |
||
| 526 | */ |
||
| 527 | public static function browseSessionsBySearch($queryTerm, array $limit) |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Search sessions by the tags in their courses. |
||
| 555 | * |
||
| 556 | * @param string $termTag Term for search in tags |
||
| 557 | * @param array $limit Limit info |
||
| 558 | * |
||
| 559 | * @return array The sessions |
||
| 560 | */ |
||
| 561 | public static function browseSessionsByTags($termTag, array $limit) |
||
| 614 | } |
||
| 615 | } |
||
| 616 |