| Total Complexity | 72 |
| Total Lines | 751 |
| 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 = (int) 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 = (int) 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 = (int) 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) |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function getCourseCategoriesTree() |
||
| 160 | { |
||
| 161 | $urlId = 1; |
||
| 162 | if (api_is_multiple_url_enabled()) { |
||
| 163 | $urlId = api_get_current_access_url_id(); |
||
| 164 | } |
||
| 165 | |||
| 166 | $countCourses = self::countAvailableCoursesToShowInCatalog($urlId); |
||
| 167 | $categories = []; |
||
| 168 | $list = []; |
||
| 169 | |||
| 170 | $categories['ALL'] = [ |
||
| 171 | 'id' => 0, |
||
| 172 | 'name' => get_lang('DisplayAll'), |
||
| 173 | 'code' => 'ALL', |
||
| 174 | 'parent_id' => null, |
||
| 175 | 'tree_pos' => 0, |
||
| 176 | 'number_courses' => $countCourses, |
||
| 177 | 'level' => 0, |
||
| 178 | ]; |
||
| 179 | |||
| 180 | $allCategories = CourseCategory::getAllCategories(); |
||
| 181 | |||
| 182 | foreach ($allCategories as $category) { |
||
| 183 | if (empty($category['parent_id'])) { |
||
| 184 | $list[$category['code']] = $category; |
||
| 185 | $list[$category['code']]['level'] = 0; |
||
| 186 | list($subList, $childrenCount) = self::buildCourseCategoryTree($allCategories, $category['code'], 0); |
||
| 187 | foreach ($subList as $item) { |
||
| 188 | $list[$item['code']] = $item; |
||
| 189 | } |
||
| 190 | // Real course count |
||
| 191 | $countCourses = CourseCategory::countCoursesInCategory($category['code']); |
||
| 192 | $list[$category['code']]['number_courses'] = $childrenCount + $countCourses; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | // count courses that are in no category |
||
| 197 | $countCourses = CourseCategory::countCoursesInCategory(); |
||
| 198 | $categories['NONE'] = [ |
||
| 199 | 'id' => 0, |
||
| 200 | 'name' => get_lang('WithoutCategory'), |
||
| 201 | 'code' => 'NONE', |
||
| 202 | 'parent_id' => null, |
||
| 203 | 'tree_pos' => 0, |
||
| 204 | 'children_count' => 0, |
||
| 205 | 'auth_course_child' => true, |
||
| 206 | 'auth_cat_child' => true, |
||
| 207 | 'number_courses' => $countCourses, |
||
| 208 | 'level' => 0, |
||
| 209 | ]; |
||
| 210 | |||
| 211 | $result = array_merge($list, $categories); |
||
| 212 | |||
| 213 | return $result; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public static function getCourseCategories() |
||
| 220 | { |
||
| 221 | $urlId = 1; |
||
| 222 | if (api_is_multiple_url_enabled()) { |
||
| 223 | $urlId = api_get_current_access_url_id(); |
||
| 224 | } |
||
| 225 | |||
| 226 | $countCourses = self::countAvailableCoursesToShowInCatalog($urlId); |
||
| 227 | |||
| 228 | $categories = []; |
||
| 229 | $categories[0][0] = [ |
||
| 230 | 'id' => 0, |
||
| 231 | 'name' => get_lang('DisplayAll'), |
||
| 232 | 'code' => 'ALL', |
||
| 233 | 'parent_id' => null, |
||
| 234 | 'tree_pos' => 0, |
||
| 235 | 'count_courses' => $countCourses, |
||
| 236 | ]; |
||
| 237 | |||
| 238 | $categoriesFromDatabase = CourseCategory::getCategories(); |
||
| 239 | |||
| 240 | foreach ($categoriesFromDatabase as $row) { |
||
| 241 | $countCourses = CourseCategory::countCoursesInCategory($row['code']); |
||
| 242 | $row['count_courses'] = $countCourses; |
||
| 243 | if (empty($row['parent_id'])) { |
||
| 244 | $categories[0][$row['tree_pos']] = $row; |
||
| 245 | } else { |
||
| 246 | $categories[$row['parent_id']][$row['tree_pos']] = $row; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | // count courses that are in no category |
||
| 251 | $countCourses = CourseCategory::countCoursesInCategory(); |
||
| 252 | $categories[0][count($categories[0]) + 1] = [ |
||
| 253 | 'id' => 0, |
||
| 254 | 'name' => get_lang('None'), |
||
| 255 | 'code' => 'NONE', |
||
| 256 | 'parent_id' => null, |
||
| 257 | 'tree_pos' => $row['tree_pos'] + 1, |
||
| 258 | 'children_count' => 0, |
||
| 259 | 'auth_course_child' => true, |
||
| 260 | 'auth_cat_child' => true, |
||
| 261 | 'count_courses' => $countCourses, |
||
| 262 | ]; |
||
| 263 | |||
| 264 | return $categories; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return LIMIT to filter SQL query. |
||
| 269 | * |
||
| 270 | * @param array $limit |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public static function getLimitFilterFromArray($limit) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $category_code |
||
| 288 | * @param int $random_value |
||
| 289 | * @param array $limit will be used if $random_value is not set. |
||
| 290 | * This array should contains 'start' and 'length' keys |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public static function getCoursesInCategory($category_code, $random_value = null, $limit = []) |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Search the courses database for a course that matches the search term. |
||
| 462 | * The search is done on the code, title and tutor field of the course table. |
||
| 463 | * |
||
| 464 | * @param string $search_term The string that the user submitted, what we are looking for |
||
| 465 | * @param array $limit |
||
| 466 | * @param bool $justVisible search only on visible courses in the catalogue |
||
| 467 | * |
||
| 468 | * @return array an array containing a list of all the courses matching the the search term |
||
| 469 | */ |
||
| 470 | public static function search_courses($search_term, $limit, $justVisible = false) |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * List the sessions. |
||
| 561 | * |
||
| 562 | * @param string $date |
||
| 563 | * @param array $limit |
||
| 564 | * @param bool $returnQueryBuilder |
||
| 565 | * @param bool $getCount |
||
| 566 | * |
||
| 567 | * @return array|\Doctrine\ORM\Query The session list |
||
| 568 | */ |
||
| 569 | public static function browseSessions($date = null, $limit = [], $returnQueryBuilder = false, $getCount = false) |
||
| 570 | { |
||
| 571 | $urlId = api_get_current_access_url_id(); |
||
| 572 | |||
| 573 | $select = 's'; |
||
| 574 | if ($getCount) { |
||
| 575 | $select = 'count(s) '; |
||
| 576 | } |
||
| 577 | |||
| 578 | $dql = "SELECT $select |
||
| 579 | FROM ChamiloCoreBundle:Session s |
||
| 580 | WHERE EXISTS |
||
| 581 | ( |
||
| 582 | SELECT url.sessionId FROM ChamiloCoreBundle:AccessUrlRelSession url |
||
| 583 | WHERE url.sessionId = s.id AND url.accessUrlId = $urlId |
||
| 584 | ) AND |
||
| 585 | s.nbrCourses > 0 |
||
| 586 | "; |
||
| 587 | if (!is_null($date)) { |
||
| 588 | $date = Database::escape_string($date); |
||
| 589 | $dql .= " |
||
| 590 | AND ( |
||
| 591 | (s.accessEndDate IS NULL) |
||
| 592 | OR |
||
| 593 | ( |
||
| 594 | s.accessStartDate IS NOT NULL AND |
||
| 595 | s.accessEndDate IS NOT NULL AND |
||
| 596 | s.accessStartDate >= '$date' AND s.accessEndDate <= '$date') |
||
| 597 | OR |
||
| 598 | ( |
||
| 599 | s.accessStartDate IS NULL AND |
||
| 600 | s.accessEndDate IS NOT NULL AND |
||
| 601 | s.accessStartDate >= '$date' |
||
| 602 | ) |
||
| 603 | ) |
||
| 604 | "; |
||
| 605 | } |
||
| 606 | |||
| 607 | $qb = Database::getManager()->createQuery($dql); |
||
| 608 | |||
| 609 | if (!empty($limit)) { |
||
| 610 | $qb |
||
| 611 | ->setFirstResult($limit['start']) |
||
| 612 | ->setMaxResults($limit['length']) |
||
| 613 | ; |
||
| 614 | } |
||
| 615 | |||
| 616 | if ($returnQueryBuilder) { |
||
| 617 | return $qb; |
||
| 618 | } |
||
| 619 | |||
| 620 | if ($getCount) { |
||
| 621 | return $qb->getSingleScalarResult(); |
||
| 622 | } |
||
| 623 | |||
| 624 | return $qb->getResult(); |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Search sessions by the tags in their courses. |
||
| 629 | * |
||
| 630 | * @param string $termTag Term for search in tags |
||
| 631 | * @param array $limit Limit info |
||
| 632 | * |
||
| 633 | * @return array The sessions |
||
| 634 | */ |
||
| 635 | public static function browseSessionsByTags($termTag, array $limit) |
||
| 636 | { |
||
| 637 | $em = Database::getManager(); |
||
| 638 | $qb = $em->createQueryBuilder(); |
||
| 639 | |||
| 640 | $urlId = api_get_current_access_url_id(); |
||
| 641 | |||
| 642 | $sessions = $qb->select('s') |
||
| 643 | ->distinct() |
||
| 644 | ->from('ChamiloCoreBundle:Session', 's') |
||
| 645 | ->innerJoin( |
||
| 646 | 'ChamiloCoreBundle:SessionRelCourse', |
||
| 647 | 'src', |
||
| 648 | Join::WITH, |
||
| 649 | 's.id = src.session' |
||
| 650 | ) |
||
| 651 | ->innerJoin( |
||
| 652 | 'ChamiloCoreBundle:AccessUrlRelSession', |
||
| 653 | 'url', |
||
| 654 | Join::WITH, |
||
| 655 | 'url.sessionId = s.id' |
||
| 656 | ) |
||
| 657 | ->innerJoin( |
||
| 658 | 'ChamiloCoreBundle:ExtraFieldRelTag', |
||
| 659 | 'frt', |
||
| 660 | Join::WITH, |
||
| 661 | 'src.course = frt.itemId' |
||
| 662 | ) |
||
| 663 | ->innerJoin( |
||
| 664 | 'ChamiloCoreBundle:Tag', |
||
| 665 | 't', |
||
| 666 | Join::WITH, |
||
| 667 | 'frt.tagId = t.id' |
||
| 668 | ) |
||
| 669 | ->innerJoin( |
||
| 670 | 'ChamiloCoreBundle:ExtraField', |
||
| 671 | 'f', |
||
| 672 | Join::WITH, |
||
| 673 | 'frt.fieldId = f.id' |
||
| 674 | ) |
||
| 675 | ->where( |
||
| 676 | $qb->expr()->like('t.tag', ':tag') |
||
| 677 | ) |
||
| 678 | ->andWhere( |
||
| 679 | $qb->expr()->eq('f.extraFieldType', ExtraField::COURSE_FIELD_TYPE) |
||
| 680 | )->andWhere( |
||
| 681 | $qb->expr()->eq('url.accessUrlId', $urlId) |
||
| 682 | ) |
||
| 683 | ->setFirstResult($limit['start']) |
||
| 684 | ->setMaxResults($limit['length']) |
||
| 685 | ->setParameter('tag', "$termTag%") |
||
| 686 | ->getQuery() |
||
| 687 | ->getResult(); |
||
| 688 | |||
| 689 | $sessionsToBrowse = []; |
||
| 690 | foreach ($sessions as $session) { |
||
| 691 | if ($session->getNbrCourses() === 0) { |
||
| 692 | continue; |
||
| 693 | } |
||
| 694 | $sessionsToBrowse[] = $session; |
||
| 695 | } |
||
| 696 | |||
| 697 | return $sessionsToBrowse; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Build a recursive tree of course categories. |
||
| 702 | * |
||
| 703 | * @param array $categories |
||
| 704 | * @param int $parentId |
||
| 705 | * @param int $level |
||
| 706 | * |
||
| 707 | * @return array |
||
| 708 | */ |
||
| 709 | public static function buildCourseCategoryTree($categories, $parentId = 0, $level = 0) |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * List Code Search Category. |
||
| 740 | * |
||
| 741 | * @param string $code |
||
| 742 | * |
||
| 743 | * @return array |
||
| 744 | */ |
||
| 745 | public static function childrenCategories($code) |
||
| 763 | } |
||
| 764 | } |
||
| 765 |