1711c1711 < * @return navigation_node|void returns a navigation node if a category has been loaded. --- > * @return void 1713c1713 < protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) { --- > protected function load_all_categories($categoryid = null, $showbasecategories = false) { 1717,1718c1717,1718 < if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) { < return true; --- > if ($categoryid !== null && array_key_exists($categoryid, $this->addedcategories) && $this->addedcategories[$categoryid]->children->count() > 0) { > return $this->addedcategories[$categoryid]; 1721,1733c1721,1722 < $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); < $sqlselect = "SELECT cc.*, $catcontextsql < FROM {course_categories} cc < JOIN {context} ctx ON cc.id = ctx.instanceid"; < $sqlwhere = "WHERE ctx.contextlevel = ".CONTEXT_COURSECAT; < $sqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; < $params = array(); < < $categoriestoload = array(); < if ($categoryid == self::LOAD_ALL_CATEGORIES) { < // We are going to load all categories regardless... prepare to fire < // on the database server! < } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0 --- > $coursestoload = array(); > if (empty($categoryid)) { // can be 0 1735c1724 < $sqlwhere .= " AND cc.parent = 0"; --- > $categories = $DB->get_records('course_categories', array('parent'=>'0'), 'sortorder ASC, id ASC'); 1742,1745c1731,1741 < $sqlwhere .= " AND (cc.parent = :categoryid OR cc.parent = 0) AND cc.parent {$sql}"; < } else { < // All we need is categories that match the parent < $sqlwhere .= " AND cc.parent = :categoryid AND cc.parent {$sql}"; --- > $sql = "SELECT * > FROM {course_categories} cc > WHERE (parent = :categoryid OR parent = 0) AND > parent {$sql} > ORDER BY depth DESC, sortorder ASC, id ASC"; > } else { > $sql = "SELECT * > FROM {course_categories} cc > WHERE parent = :categoryid AND > parent {$sql} > ORDER BY depth DESC, sortorder ASC, id ASC"; 1747a1744,1748 > $categories = $DB->get_records_sql($sql, $params); > if (count($categories) == 0) { > // There are no further categories that require loading. > return; > } 1752,1775c1753,1757 < $categoriestoload = explode('/', trim($category->path, '/')); < list($select, $params) = $DB->get_in_or_equal($categoriestoload); < // We are going to use select twice so double the params < $params = array_merge($params, $params); < $basecategorysql = ($showbasecategories)?' OR cc.depth = 1':''; < $sqlwhere .= " AND (cc.id {$select} OR cc.parent {$select}{$basecategorysql})"; < } < < $categoriesrs = $DB->get_recordset_sql("$sqlselect $sqlwhere $sqlorder", $params); < $categories = array(); < foreach ($categoriesrs as $category) { < // Preload the context.. we'll need it when adding the category in order < // to format the category name. < context_helper::preload_from_record($category); < if (array_key_exists($category->id, $this->addedcategories)) { < // Do nothing, its already been added. < } else if ($category->parent == '0') { < // This is a root category lets add it immediately < $this->add_category($category, $this->rootnodes['courses']); < } else if (array_key_exists($category->parent, $this->addedcategories)) { < // This categories parent has already been added we can add this immediately < $this->add_category($category, $this->addedcategories[$category->parent]); < } else { < $categories[] = $category; --- > $coursestoload = explode('/', trim($category->path, '/')); > list($select, $params) = $DB->get_in_or_equal($coursestoload); > $select = 'id '.$select.' OR parent '.$select; > if ($showbasecategories) { > $select .= ' OR parent = 0'; 1776a1759,1760 > $params = array_merge($params, $params); > $categories = $DB->get_records_select('course_categories', $select, $params, 'sortorder'); 1778d1761 < $categoriesrs->close(); 1817,1819d1799 < if ($categoryid === self::LOAD_ALL_CATEGORIES) { < $this->allcategoriesloaded = true; < } 1821,1830c1801,1802 < if (count($categoriestoload) > 0) { < $readytoloadcourses = array(); < foreach ($categoriestoload as $category) { < if ($this->can_add_more_courses_to_category($category)) { < $readytoloadcourses[] = $category; < } < } < if (count($readytoloadcourses)) { < $this->load_all_courses($readytoloadcourses); < } --- > if (count($coursestoload) > 0) { > $this->load_all_courses($coursestoload); 3576c3548 < if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) { --- > if ($usemodchooser = get_user_preferences('usemodchooser', 1)) {